function url_gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


$(document).ready(function(){

	/* Scroll back to the top, let's not use anchors...
        $('html, body').each(function(){
        	$(this).attr('scrollTop',0);
        });
	*/

	//Find the active meny and display it's sublevel siblings, also, remove their links...
	$('#services_nav_v2 #active_menu ~ div').each(function(){
		var sublevel_item_text = $(this).children('a').text();
		$(this).text(sublevel_item_text);

	});


	//Get Y of each named anchor
	//var name_jump_value = window.location.href;
	//name_jump_value = name_jump_value.split("#");
	var name_jump_value = url_gup("name");
	var array_of_anchors = new Array();
	$('#services_main_content a').each(function(index){
		if ( $(this).attr('name') != '' ) //if it's a named anchor
		{
			var this_anchor_index = $(this).attr('name');
			//var this_anchor_y = $(this).offset().top;
			var this_anchor_y = $(this).attr('offsetTop') + 20;
			//alert(this_anchor_y);

			array_of_anchors[this_anchor_index] = this_anchor_y;
		}

	});

	if ( array_of_anchors[name_jump_value] > 0 )
        {
		$('#services_main_content').attr('scrollTop',array_of_anchors[name_jump_value]);
		
		/*****
		$('html, body').each(function(){
			$(this).attr('scrollTop',0);
		});
		******/
        }




	/***** CHECK FOR PREVIEW PARAM *****	
	var is_preview = url_gup('preview');
	
	if ( is_preview )
	{
		$('#services_nav_v2').parent().css('display','block');
		$('#services_nav').css('display','none');
	}
	/***** END PREVIEW PARAM CHECK *****/

	/***** SET MAIN MENU (for all pages) *****/
	//Test if there isn't already a set menu
	if ( $('#main_menu #active_menu').length == 0)
	{	
		//find href of first nav in standard left nav
		var href_of_first_lnav = $('#nav_container a:first').attr('href');
		/* not really needed for now
		if ( href_of_first_lnav.indexOf('/') == -1 ) //fix old style navigation
		{
			href_of_first_lnav = "http://gkbrand.com/" + href_of_first_lnav;
		}
		*/

		//test if href of the first nav is the same as any of main nav items
		$('#main_menu a').each(function(){
			if ( $(this).attr('href') == href_of_first_lnav )
			{
				$(this).attr('id','active_menu'); //if so, activate it
				return;
			}
		});
	}

});
