jQuery(document).ready(function(){
	var thefeed = window.feedurl;
	jQuery.ajax({
		url: thefeed,
		dataType: 'jsonp',
		cache: true,
		ifModified: true,
		success: function(data) {
			processJSON(data);
		},
		complete: function(data) {
			bindALL();
		}
	});
});		

function processJSON(feed) {
			 	
	var tagline = feed.TagLine;
	var favicon = feed.FavIcon;
	var noticeon = feed.NoticeOn;
	var notice = feed.Notice;
	var mainmenu = feed.MainMenu;
	var secondmenu = feed.SecMenu;
	var splats = feed.Splat;
	var starthere = feed.StartHere;
	var choosefrom = feed.ChooseFrom;
	var connect = feed.Connect;
	var copyright = feed.CopyRight;
	var footermenu = feed.FootMenu;
	 
   	if (favicon.length > 10) {
  		var currenticon = jQuery("link[rel='shortcut icon']").attr("href");
  		if(currenticon != favicon) {
  			jQuery("link[rel='shortcut icon']").attr("href", favicon);
  		}
  	}
  	
  	if (noticeon == '1') {
  		jQuery('#public_replace').replaceWith(notice);
  	}
   			   	
  	if (secondmenu.length > 30) {
  		jQuery('#secondary_nav_menu').replaceWith(secondmenu);
  	}
   			   	
  	if (splats.length > 30) {
  		jQuery('#head_splat').html(splats);
  	}
   			   	
  	if (starthere.length > 30) {
  		if(jQuery('body').hasClass('home'))
  			jQuery('.inner_sub_menu_content').append(starthere);
  	}
   			   	
  	if (choosefrom.length > 30) {
  		if(jQuery('#sub_sub_menu').length)
  			jQuery('.inner_sub_menu_content').append(choosefrom);
  	}
   			   	
  	if (mainmenu.length > 30) {
  		jQuery('#primary_nav_menu').replaceWith(mainmenu);
  	}
  	
  	if (connect.length > 30) {
  		jQuery('#connect_sub_menu_content').html(connect);
  	}
  	
  	if (copyright.length > 5) {
  		jQuery('.footer_copy').html(copyright);
  	}
  	
  	if (secondmenu.length > 5) {
  		jQuery('.footer_bottom').replaceWith(footermenu);
  	}
			  	
}

function bindALL(){
	
		jQuery('.hide-if-no-js').show();
		jQuery('.hide-if-js').hide();
				 		
		jQuery("a[href^='#']").click(function(event) { 
	      	
	      	event.preventDefault();
	      	
	    	var anchornameraw = jQuery(this).attr("href");
	    	var anchorname = anchornameraw.substr(1);
	    	if (jQuery("a[name=" + anchorname + "]").length) {
	    		scrolltopanchor = jQuery("a[name=" + anchorname + "]").offset().top;
	    	} else {
	    		scrolltopanchor = jQuery("#" + anchorname).offset().top;
	    	}
	    	if (anchorname != "pagetop") {
	    		scrolltopanchor = scrolltopanchor - 30;
	    	}
	    	
	    	jQuery("html, body").animate({
							scrollTop: scrolltopanchor
							}, 800);
							    	
	    	return false;
	    
	    });
	    
	    jQuery("#main_menu a").each(function(){
	    	if(jQuery(this).attr("href").search("patterns") != -1) {
	    		jQuery(this).prepend('<span id="free_patterns"></span>');
	    	}
	    });
	    
	    jQuery("#main_menu a").hover(function () {
	    	if(jQuery(this).attr("title").length >= 4) {
	    		var theTitle = jQuery(this).attr("title");
	    		jQuery(this).find('.link_text_inner').css('color', '#fff').parent().append('<span class="hover_text">' + theTitle + '</span>');
	    		var widthOne = jQuery(this).find('.link_text_inner').innerWidth();
	    		var widthTwo = jQuery(this).find('.hover_text').innerWidth();
	    		var marginNew = parseFloat( (widthOne - widthTwo) / 2 );
	    		jQuery(this).find('.hover_text').css('margin-left', marginNew + 'px');
	    	}
		},
		function () {
	    	jQuery(this).find('.hover_text').remove();
			jQuery(this).find('.link_text_inner').removeAttr('style');
		});
		
		jQuery('#sub_menu_nav span.link_text').click(function(){
			var animatingnow = true;
			jQuery('#start_here_bubble').hide();
			var thisID = jQuery(this).attr('id');
			var thisDIV = jQuery('#'+ thisID + '_content');
			if(!thisDIV.hasClass('active-item') && animatingnow) {
				var newHeight = thisDIV.innerHeight();
				jQuery('#sub_menu_content').animate({'height' : newHeight + 'px'}, 400, function(){animatingnow = false; jQuery('#start_here_bubble').show(); });
				jQuery('#sub_menu_content .active-item').fadeOut(200, function() {
					thisDIV.fadeIn(200, function(){
						jQuery('#sub_menu_content .active-item').removeClass('active-item');
						thisDIV.addClass('active-item');						
					});
				});
			}
			jQuery('#sub_menu_nav li').removeClass('active-item');
			jQuery(this).closest('li').addClass('active-item');
		});

}
