//Define Sliding Function
function slide (navigation_id, pad_out, pad_in, time, multiplier, orientation, totalPad, pad) {
	// creates the target paths
	var list_elements = navigation_id + " .sliding-element";
	var link_elements = list_elements + " a";
	// initiates the timer used for the sliding animation
	var timer = 0;
	$("#fastGalNav-sliding").show("fast");
	//changes the orientation of the slide
	if (orientation === "ori") {
		// creates the slide animation for all list elements 
		$(list_elements).each(function(i)
		{
			// margin left = - ([width of element] + [total vertical padding of element])
			$(this).css("margin-left", totalPad);
			// updates timer
			timer = (timer*multiplier + time);
			$(this).animate({ marginLeft: "15px" }, timer);
			$(this).animate({ marginLeft: pad }, timer);
			$(this).animate({ marginLeft: "0" }, timer);
		});
		// creates the hover-slide effect for all link elements 		
		$(link_elements).each(function(i)
		{
			$(this).hover(
			function()
			{	if($(this).parent().hasClass('Active') === false)
					$(this).animate({ paddingLeft: pad_out }, 150);
			},		
			function()
			{
				if ($(this).parent().hasClass('Active') === false)
					$(this).animate({ paddingLeft: pad_in }, 150);
			});
		});
	}
	else if (orientation === "ver") { 
		// creates the slide animation for all list elements 
		$(list_elements).each(function(i)
		{
			// margin left = - ([width of element] + [total vertical padding of element])
			$(this).css("margin-top", totalPad);
			// updates timer
			timer = (timer*multiplier + time);
			$(this).animate({ marginTop: "0" }, timer);
			$(this).animate({ marginTop: pad }, timer);
			$(this).animate({ marginTop: "0" }, timer);
		});
		// creates the hover-slide effect for all link elements 		
		$(link_elements).each(function(i)
		{
			$(this).hover(
			function()
			{
				if ($(this).parent().hasClass('Active') === false){
					$(this).animate({ marginTop: pad_out }, 150);
				}
			},		
			function()
			{	
				if ($(this).parent().hasClass('Active') === false){
					$(this).animate({ marginTop: pad_in }, 150);
				}
			});
		});	
	}
};
//Activate FancyBox
var gallery = {
	makeGallery: function() {
		$("div#gallery a").fancybox({
			'overlayShow':	true
		});
	},
	init: function(){
		this.makeGallery();
	}
};
//Activate Fast gallery Navigation
var fastGalleryNavigation = {
	makeFastNav: function() {
			$('#fastGalNav-sliding a').click(function(){
				var self = $('.sliding-element').index($(this).parent()); 
				var hrefer = $(this).attr('href');
				$('.sliding-element').each(function () {
					$(this).removeClass('Active');
					$(this).children().css("padding-left", 0);
				});
				$(this).parent().addClass('Active');
				$(this).css("padding-left", 15);
				window.location.hash = hrefer.substr(0,hrefer.length-5);
				var toLoad = hrefer+' #gallery';
				var toLoadNav = hrefer+' #galleryNav';
				$('#content').livequery(function(){
					$(this).load(toLoad);
					$(this).queue(function () {
						$('#insertNav').livequery(function(){
							$(this).load(toLoadNav);
							
						});
					});
					inGallery = true;
					gallery.init();
				});
				$('#insertNav').livequery(function(){
					$(this).load(toLoadNav);
				});
				return false;
				
		});
		
	},
	init: function(){
		this.makeFastNav();
	}
};	
function updateFastSlide(slideNumber){
	slideNumber = slideNumber.substr(7,slideNumber.length)-1;
	$('.sliding-element').each(function () {
		$(this).removeClass('Active');
		$(this).children().css("padding-left", 0);
	});
	$('.sliding-element:eq(' + slideNumber + ')').addClass('Active');
	$('.sliding-element:eq(' + slideNumber + ')').children().animate({ paddingLeft: 15 }, 150);
}
function siteNav(){
	var inGallery = false;
	var hash = window.location.hash.substr(1);
	var href = $('#logo a').each(function(){
		var href = $(this).attr('href');
		if(hash == href.substr(0,href.length-5)){
			var toLoad = hash+'.html #gallery';
			var toLoadNav = hash+'.html #galleryNav';
			fastGalleryNavigation.init();
		        slide("#fastGalNav-sliding", 15, 0, 150, .8, "ori", "-35", "0"); 
			$('#content').livequery(function(){
				$(this).load(toLoad);
				$(this).queue(function () {
					$('#insertNav').livequery(function(){
						$(this).load(toLoadNav);
						updateFastSlide(hash);
					});
				});
				inGallery = true;
				gallery.init();
			});
			$('#insertNav').livequery(function(){
				$(this).load(toLoadNav);
			});
		}											
	});
	if(!inGallery) {
		$('a.enterButton').livequery(function(){
			gallery.init();
			
			$(this).click(function(){
				fastGalleryNavigation.init();
		        slide("#fastGalNav-sliding", 15, 0, 150, .8, "ori", "-35", "0"); 
				var toLoad = $(this).attr('href')+' #gallery';
				var toLoadNav = $(this).attr('href')+' #galleryNav';
				$('#content').fadeOut('normal',loadContent);
				$('#load').remove();
				$('#warper').append('<span id="load">LOADING...</span>');
				$('#load').fadeIn('normal');
				var whash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
				window.location.hash = whash;
				function loadContent() {
					$('#content').load(toLoad,'',showNewContent());
				}
				function showNewContent() {
					$('#content').fadeIn('normal',hideLoader());
					$("#content").queue(function () {
						$('#insertNav').load(toLoadNav,'',showNav());
						updateFastSlide(whash);
					});
				}
				function showNav() {
					$('#insertNav').fadeIn('normal');
				}
				function hideLoader() {
					$('#load').fadeOut('normal');
				}
				return false;
			});
		});
		inGallery = true;
		//$('#fastGalNav-sliding').show();
		
	}
	if(inGallery) {
		$('#nextGallery a').livequery(function(){
			gallery.init();
			$(this).click(function(){
				var toLoad = $(this).attr('href')+' #gallery';
				var toLoadNav = $(this).attr('href')+' #galleryNav';
				$('#gallery').fadeOut('normal',loadContent);
				$('#load').remove();
				$('#warper').append('<span id="load">LOADING...</span>');
				$('#load').fadeIn('normal');
				var whash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
				window.location.hash = whash;
				function loadContent() {
					$('#content').load(toLoad,'',showNewContent());
				}
				function showNewContent() {
					$('#content').fadeIn('normal',hideLoader());
					$('#insertNav').load(toLoadNav,'',showNav());
					updateFastSlide(whash);
				}
				function showNav() {
					$('#insertNav').fadeIn('normal');
				}				
				function hideLoader() {
					$('#load').fadeOut('normal');
				}
				return false;
			});
		});
		$('#previousGallery a').livequery(function(){
			gallery.init();
			$(this).click(function(){
				var toLoad = $(this).attr('href')+' #gallery';
				var toLoadNav = $(this).attr('href')+' #galleryNav';
				$('#gallery').fadeOut('normal',loadContent);
				$('#load').remove();
				$('#warper').append('<span id="load">LOADING...</span>');
				$('#load').fadeIn('normal');
				var whash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
				window.location.hash = whash;
				function loadContent() {
					$('#content').load(toLoad,'',showNewContent());
				}
				function showNewContent() {
					$('#content').fadeIn('normal',hideLoader());
					$('#insertNav').load(toLoadNav,'',showNav());
					updateFastSlide(whash);
				}
				function showNav() {
					$('#insertNav').fadeIn('normal');
				}				
				function hideLoader() {
					$('#load').fadeOut('normal');
				}
				return false;
			});
		});		
	}
}

$(document).ready(function() {
	$("div#contactButton a").fancybox({
		'overlayShow':	true
	});
	$(".teamButton").fancybox({
		'overlayShow':	true
	});
	if(jQuery.browser.version <= 6.0){
		$('#ie').css("display", "block");	
	}
	siteNav();	
});
