// JavaScript Document
var mainHeight;
var endAnimate;
var linkDelay;
var $link;
var $main;

//Hide body until DOM is ready to avoid flicker & hide #main till Images are loaded
document.write('<style type="text/css">body{display:none} #main{display:none}</style>');

$(document).ready(function(){
	
	//show body once DOM is loaded
	$('body').css('display','block');
	
    // Fix background image caching problem
    if (jQuery.browser.msie) {
        try { 
            document.execCommand("BackgroundImageCache", false, true); 
        } catch(err) {}
    }
	
});

$(window).load(function(){
	
	//Main Content Blind Effect
						
	$main = $('#main');
	$overflow = $('.blind_overflow');
	
	//show #main once Images are loaded
	$main.css('display','block');
	
	//Read #main height once images have loaded, then hide #main and #content
	mainHeight = $main.outerHeight();
	$main.css('height', mainHeight);
	$main.blindClose();
	
	//Slide in content
	setTimeout(slide_content, 600);
	
	function slide_content(){
		$main.blindToggle(1000, 'swing', function(){
			
			//Unset #main and .blind_overflow height to allow stretch
			$main.css('height', '');
			$overflow.css('height', '');
		
			//If link was clicked before load in animation completed, do load out animation and redirect
			//Else endAnimate=1 to register load in animation as complete
			if (linkDelay) {
				$main.blindToggle(1000, 'swing', function(){
					//now get the anchor href and redirect the browser
					document.location = $link.attr('href');
				});
				
				//If Nav Link then run nav_slide function
				$li = linkDelay.parent();
				if ($li.hasClass('page_item')) {
					nav_slide($li.position().left);
				};
				
							}
			else {
				endAnimate = 1;
			}
		});
	};

	
	//Main Content Blind Effect
	
	//Nav Highlight Slide Effect
	
	function nav_slide($leftEnd){
		$current_page = $('.current_page_item', '#navbar');
		
		$navSlide = $('#nav_slide');
		
		$topStart = $current_page.position().top;
		
		$leftStart = $current_page.position().left;
		
		$current_page.css('background-image', 'none');
		
		$navSlide.css({
			top : $topStart,
			left : $leftStart,
			display : 'block'
		});
	
		$navSlide.animate({
			left : $leftEnd
		}, 1000);
	};

	//END Nav Highlight Slide Effect
	
	//Change Links Behaviour
	
	$('a').click( function(ev){
		//prevent the default action of the event, this will stop the href in the anchor being followed
		//before the animation has started, u can also use return false
		ev.preventDefault();
		
		//store a referene to the anchor tag
		$link=$(this);
		
		//get the element and animate
		//If load in animation is complete, do load out animation and redirect
		//Else linkDelay=1 to register the load out animation and redirect to occur after the load in animation completes
		if(endAnimate){
			
			//If link's target=_blank don't animate and open in a new window
			if($link.attr('target')=='_blank'){
				window.open($link.attr('href'));
			} else {
				
					//If Nav Link then run nav_slide function
					$li = $(this).parent();
					if($li.hasClass('page_item')){
						nav_slide($li.position().left);
					};
					
					$main.blindToggle(1000, 'swing', function(){
						//now get the anchor href and redirect the browser
							document.location = $link.attr('href');
					});
			};
		} else {
			linkDelay = $link;
		}
	});
	
	//END Change Links Behaviour
		
	//Hover Fade Effect
	
	//navbar fade area
	$('#navbar', '#navigation').children().hover(function() {
		$(this).siblings().children().stop().fadeTo(500,0.5);
	}, function() {
		$(this).siblings().children().stop().fadeTo(500,1);
	});
	
	//fade area 1 (only works on display:block elements, IE bug)
	$('#fade_area_1', '#content').children().hover(function() {
		$(this).siblings().stop().fadeTo(500,0.5);
	}, function() {
		$(this).siblings().stop().fadeTo(500,1);
	});
	
	//END Hover Fade Effect

});
