var scrollLocked = false;
var scrollPositions = new Array();
$(document).ready(function() {
	
	// jQuery SmoothScroll | Version 11-06-11
	$('a[href*=#]').click(function() {

		// skip SmoothScroll on links inside sliders or scroll boxes also using anchors or if there is a javascript call
		//if($(this).parent().attr('class')=='scrollable_navigation' || $(this).attr('href').indexOf('javascript')>-1) return;

		// duration in ms
		var duration=750;

		// easing values: swing | linear
		var easing='swing';

		// get / set parameters
		var newHash=this.hash;
		var oldLocation=window.location.href.replace(window.location.hash, '');
		var newLocation=this;
		scrollLocked = true;		
		// make sure it's the same location		
		if(oldLocation+newHash==newLocation)
		{
			// get target
			var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;

			// adjust target for anchors near the bottom of the page
			if(target > $(document).height()-$(window).height()) target=$(document).height()-$(window).height();			
			
			// set selector
			if($.browser.safari) var animationSelector='body:not(:animated)';
			else var animationSelector='html:not(:animated)';
			
			// animate to target and set the hash to the window.location after the animation
			$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {

				// add new hash to the browser location
				window.location.href=newLocation;
				scrollLocked = false;
				$('a.main-menu-item').removeClass("main-menu-item-active");
				if($(newLocation).hasClass("main-menu-item")) $(newLocation).addClass("main-menu-item-active");
				if($(newLocation).parent().attr("id")=="to-the-top") $($('a.main-menu-item')[0]).addClass("main-menu-item-active");				
			});

			// cancel default click action
			return false;
		}
	});
	 	
    jQuery(window).bind('scrollstop',function () { 
    	if(scrollLocked) return;
      	$("#scrollpos").html($(document).scrollTop());
		idnx = 0;
		firstVal = 0;
		secondVal = scrollPositions[1];
      	do{
      		idnx++;
      		if($(document).scrollTop() > firstVal && $(document).scrollTop() < secondVal)
      		{
      			$("#shownContainer").html(idnx);
					$('a.main-menu-item').removeClass("main-menu-item-active");
					$($('a.main-menu-item')[idnx-1]).addClass("main-menu-item-active");	
      			idnx = scrollPositions.length;
      		}else{
      			firstVal = secondVal;
      			if(scrollPositions[idnx+1])
      			{
      				secondVal = scrollPositions[idnx+1];
      			}else{
      				secondVal = scrollPositions[scrollPositions.length];
      			}	
      		}
      	}while(idnx != scrollPositions.length)		
    });		
	$('div.highlightContainer').each(function(index, elem){
	    scrollPositions[index+1] = $(elem).offset().top+$(elem).height()/2;	
	});  
	$(document).ready(function() {
		if(document.location.hash.slice(1) && document.location.hash.slice(1) != "")
		{
			//find selected menu-elem and select it
			var selectedElem = $('a[href='+document.location.hash+']');
			if(selectedElem && selectedElem.hasClass('main-menu-item')){
				selectedElem.addClass("main-menu-item-active");
			}
		}else if($(document).scrollTop() < 120){
			$('a.main-menu-item').first().addClass("main-menu-item-active");
		}
	});
});
