//The page where to navigate
var next_url;

//Play effect and go to another page
function goToURL(url){	
	next_url = url;
	effect_pageExit();
}

//Once transition is complete
function onTransitionComplete(){
	window.location.href = next_url;
}

//Effect for loaded page
function effect_pageEnter() {
	
	//Maybe other elements need to be animated too on page enter
	MyEventDispatcher.dispatchEvent('page_enter');
	
	//Default animations
	$('#footer .wrapper').fadeIn(1000);
	$('#content').fadeIn(1000);
}

//Effect for page leaving
function effect_pageExit() {
	
	//Maybe other elements need to be animated too on exit
	MyEventDispatcher.dispatchEvent('page_exit');
	
	//Default animations
	$('#footer .wrapper').fadeOut(300);
	$('#content').fadeOut(1000, onTransitionComplete);
}

//Play effect and set click listeners to the links
$(document).ready(function() {
	
	//Play effect
	effect_pageEnter();
	
	//Find links and set listeners
	
	$("a").not("dt.gallery-icon a, li.item_holder a").click(function(){
			
		//Check if this is internal link
		var href = $(this).attr("href");
		if($(this).attr("target") == "_blank"){
			return truel
		}
		if(href.charAt(0) == '#'){
			return true;
		}
		
		goToURL(href);
		return false;		
	});	
});



//Animate on home page enter
function homeEnter(){
	
	//Prepare slider
	setTimeout(function(){
		$("#slider_large").easySlider({		
			continuous: true,
			controlsShow: false
		});
	}, 500);
	
	//Animate features on by one
	var i = 0;
	var delay = 200;//Delay between animations
	
	//Show features
	$('#section_featured > ul > li').each(function(){		
		var obj = $(this);
		obj.hide();
		setTimeout(function(){
			obj.css({position:'relative', left: -obj.width()});
			obj.show().animate({left:0}, 'slow');
		}, i * delay);
		i++;
	});
}

//Animate on home page exit
function homeExit(){
	var i = 0;	
	var delay = 200;//Delay between animations
	
	//Animate
	$('#section_featured > ul > li').each(function(){
			var obj = $(this);						
			setTimeout(function(){				
				$(obj).animate({left:-obj.width()}, 'slow');
			}, i * delay);
			i++;
		}
	);
}



//Animate on about page enter
function aboutEnter(){
	setTimeout(function(){
		
		//Set the width
		var w = 0;
		$('#project_slider_large > ul > li').each(function(){		
			w += $(this).width();
		});
		$('#project_slider_large > ul').width(w);
		
		//Add listeners
		$('div.gallery-dragger').mousedown(function(e){
											is_dragging = true; 
											var pos = $(this).offset();																					
											click_x = e.pageX - pos.left;
											
											$('html').bind('mousemove', onMouseMove);
											if(scroller_int){
												clearInterval(scroller_int);
											}											
											scroller_int = setInterval(updatePos, 10);
											
											return false;
										});
										
		$('html').mouseup(function(){
							is_dragging = false
							$('html').unbind('mousemove', onMouseMove);
						});
		scroller = $('div.gallery-scroller');		
		dragger = $('div.gallery-dragger');
		
		offset = scroller.offset();
		max_x = scroller.width() - dragger.width();
		images = $('#project_slider_large > ul');		
	}, 500);
}

//Animate on about page exit
function aboutExit(){

}



//Animate on page enter
function portfolioSingleEnter(){
	//Prepare slider
	/*setTimeout(function(){
		$("#project_slider_large").easySlider({		
			continuous: true,
			controlsShow: false
		});
	}, 500);*/
	setTimeout(function(){
		
		//Set the width
		var w = 0;
		$('#project_slider_large > ul > li').each(function(){		
			w += $(this).width();
		});
		$('#project_slider_large > ul').width(w);
		
		//Add listeners
		$('div.gallery-dragger').mousedown(function(e){
											is_dragging = true; 
											var pos = $(this).offset();																					
											click_x = e.pageX - pos.left;
											
											$('html').bind('mousemove', onMouseMove);
											if(scroller_int){
												clearInterval(scroller_int);
											}											
											scroller_int = setInterval(updatePos, 10);
											
											return false;
										});
										
		$('html').mouseup(function(){
							is_dragging = false
							$('html').unbind('mousemove', onMouseMove);
						});
		scroller = $('div.gallery-scroller');		
		dragger = $('div.gallery-dragger');
		
		offset = scroller.offset();
		max_x = scroller.width() - dragger.width();
		images = $('#project_slider_large > ul');		
	}, 500);
}

//Animate on page exit
function portfolioSingleExit(){
	
}



// Box sliders
$(document).ready(function(){
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'105px'},{queue:false,duration:100});
	}, function() {
		$(".cover", this).stop().animate({top:'175px'},{queue:false,duration:400});
	});
	
	$('.panelgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'320px'},{queue:false,duration:100});
	}, function() {
		$(".cover", this).stop().animate({top:'410px'},{queue:false,duration:400});
	});
	
	//Main nav
	$('#nav ul li:not(.current)').hover(
		function() {			
        	$(this).find('a').stop().animate({'top':'-19px'}, 'fast');
        }, 
        function () {
        	$(this).find('a').stop().animate({'top':'0px'}, 'fast')
    	});

});