jQuery(document).ready(function() {
	
	/*	function used to calculate the window offset in every browser */
	function getScrollXY() {
		var scrOfY = 0;
	  	if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
		    scrOfY = window.pageYOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    scrOfY = document.body.scrollTop;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		    //IE6 standards compliant mode
		    scrOfY = document.documentElement.scrollTop;
		}
		return [ scrOfY ];
	}
	

jQuery('a[rel*=facebox]').facebox();

	/*
	jQuery("#loginLink").facebox();
	jQuery("#kontoLink").facebox();
	jQuery("#priseksempel").facebox();
	jQuery("#keyfeatures").facebox();
	jQuery("#bookLink").facebox();
	*/
	
	/* Binds the scroll event to each of the link with the class srollingLink */
	var isSliding = false; // this is used to controll the sliding of the sidebar. 
	
	jQuery(".scrollingLink").each(function() {
		var href = jQuery(this).attr("href");
		jQuery(this).bind("click", function() {
			
			isSliding = true;
			var offsetpx = jQuery(href).offset().top;
			jQuery('html,body').animate({scrollTop: offsetpx}, 900);
			offsetpx = offsetpx -310;
			
			// makes sure it doesn't overlab the rest of the content.
			if (offsetpx > 1700) {
				if (jQuery.browser.msie) {
					offsetpx = 1950;
				} else {
					offsetpx = 1800;
				}	
			}
			if (offsetpx < 35) {
				offsetpx = 0;
			}
			
			setTimeout( function() {
				jQuery(".l_box2").animate({top: offsetpx}, 1000, function() {
					isSliding = false;
				});
			}, 900);
    		
    		return false;	
		});	
	});
	
	/* makes sure the sidebar slides when the user scrolls the window	*/	
	jQuery(window).scroll(function () { 
    	
      	if (isSliding == true) {
      		// don't do anything
      	} else {
	      	jQuery(".l_box2").stop();
	      	var windowOffset = getScrollXY() - 400;
	      	if (windowOffset > 1700) {
				if (jQuery.browser.msie) {
					windowOffset = 1950;
				} else {
					windowOffset = 1800;
				}		
			}
			if (windowOffset < 35) {
				windowOffset = 0;
			}
	      	jQuery(".l_box2").animate({top: windowOffset}, 1000);
      	}
    }); 
		
	/* binds the hover event to each og the images with the class hover (uses the alt attr. to find the hover-image	*/
	jQuery(".hover").each( function() {
		var thisImage = jQuery(this);
		var oldUrl = thisImage.attr("src");
		var imgurl = "images/sidebar-menu/" + thisImage.attr("alt");
		var hoverurl = imgurl + "-hover.png";
		jQuery(thisImage).hover(
			function(){
				thisImage.attr("src", hoverurl);
			}
			, 
			function(){
				thisImage.attr("src", oldUrl);
			})
	});
	
	
		
});