//Document Ready
jQuery(function($){ 
	function blurSlideshow() {
		if ($('#slideshow').length > 0) {
			$('#header > *').hide();
		} else {
			return;
		}
		$('#slideshow').hide();
	 	$.preload('#slideshow ul img',{
	 		onRequest: function(){ 
	 			// pass
	 		 },
	 		 onFinish: function(){ 
	 		 	var slideshowFlag = true; 
	 		 	var time = window._slidshow_speed;
	 		 	var time_ms = ( time < 60 )? time * 1000 : time;
				setTimeout(function() {
					$('#slideshow').hide();
					$('#slideshow').fadeIn(time_ms/2, 'linear');
					$('#slideshow .blur:first').fadeOut(time_ms, 'linear', function() {
						fader.auto = true;
						fader.start();
					});
				}, 500);
	 			$('#slideshow li').simpleFade({
					speed    : time_ms*0.45,
					duration : time,
					auto     : false,
					init : function () {
						fader = this;
					},
					onFade : function (index) {
						$('.blur').show();
						$('#slideshow li:eq(' + index + ') .blur').fadeOut(time_ms*0.45);
						if (slideshowFlag) {
							fader.auto = true;
							slideshowFlag = false;
						}
					}
				});
				
	 		 	if ($('#slideshow').length > 0) {
					$('#header > *').fadeIn(time/4);
				}
	 		  }
	 		 
	 	});
	}
	
	function photoThumbs() {
		var gallery_items = $('.gallery-main:not(.videos) li');
		if ( gallery_items.length < 1 ) {
			return;
		};
		var gallery_thumbs = $('.gallery-thumbs li');
		
		var current_item = $('.gallery-thumbs a[href="' + window.location.hash + '"]');
		var initial_index = (current_item.length > 0) ? $('.gallery-thumbs a').index(current_item) : 0;
		
		$.preload('.gallery-thumbs img',{
			onComplete: function(data){ 
				var parentIndex = $(data.original.offsetParent);
				$('.gallery-thumbs li').eq($(parentIndex).index()).find('.blur').fadeOut(1000);
			 },
			 onFinish: function(){ 
			 	var firstRunFlag = false; 
			 	$('.gallery-main ul').fadeIn(2000, function(){ 
			 		if (firstRunFlag) {
						$('.gallery-main ul li .blur').fadeIn(1000,function(){ 
							$(this).fadeOut(1000);
						 });
						 firstRunFlag = false;
					}
			 	 });
			 	var out = gallery_items.simpleFade({
					speed    : 1200,
					duration : 2500,
					auto     : false,
					first    : initial_index,
					init : function () {
						fader = this;
					},
					onFade: function(index){ 
						count = $('.gallery-main ul li').length
						current = index + 1; 
						$('.nav .total').text(count);
						$('.nav .current').text(current);
						$('.nav').fadeIn();
						gallery_items.removeClass('active');
					 },
					onAfterFade: function(index){
						gallery_items.eq(index).addClass('active');
						var hash = gallery_thumbs.eq(index).find('a').attr('href');
						if ( window.location.hash != hash ) {	
							window.location.hash = hash
						};
					}
				});	
				gallery_items.click(function() {
					if ( $(this).is(':not(.active)') ) {
						return;
					};
					fader.next();
				});
				
				$('.gallery-thumbs li a').click(function () { 
					fader.fadeTo($(this).parent().index());
					return false;
				});
				
				$('.nav .prev').click(function () { 
					fader.prev();
					return false;
				});
				
				$('.nav .next').click(function () { 
					fader.next();
					return false;
				});
			 	
			  }
		});
	}
	
	function videoThumbs() {
		var gallery_items = $('.gallery-main.videos li');
		if ( gallery_items.length < 1 ) {
			return;
		};
		var gallery_thumbs = $('.gallery-thumbs li');
		
		var current_item = $('.gallery-thumbs a[href="' + window.location.hash + '"]');
		var initial_index = (current_item.length > 0) ? $('.gallery-thumbs a').index(current_item) : 0;
		
		$.preload('.gallery-thumbs img',{
			reversed: true,
			onComplete: function(data){ 
				var parentIndex = $(data.original.offsetParent);
				parentIndex.parent().find('.blur').fadeOut(1000);
			 },
			 onFinish: function(){ 
			 	var firstRunFlag = false; 
			 	$('.gallery-main ul').show();
			 	 
				$('.nav').fadeIn();
				var count = $('.gallery-thumbs > li').length
				$('.nav .total').text(count);
				$('.nav .current').text(initial_index+1);
				
			 	var current_vid = -1;
			 	function goTo ( index ) {
			 		index = index >= count ? 0 : index < 0 ? count-1 : index ;
					if ( current_vid == index ) {
						return;
					};
					var current = index + 1; 
					current_vid = index;
					$('.nav .total').text(count);
					$('.nav .current').text(current);
					
					$('.gallery-main.videos > ul > li:first').html('<iframe src="http://player.vimeo.com/video/' + gallery_thumbs.eq(index).find('a').attr('data-video-id') + '?title=0&amp;byline=0&amp;portrait=0&amp;color=000000" width="640" height="348" frameborder="0"></iframe>');
					
					var hash = gallery_thumbs.eq(index).find('a').attr('href');
					if ( window.location.hash != hash ) {	
						window.location.hash = hash
					};
					
					gallery_items.removeClass('active');
			 	};
				
				$('.gallery-thumbs li a').click(function () { 
					goTo($(this).parent().index());
					return false;
				});
				
				$('.nav .prev').click(function () { 
					goTo(current_vid - 1);
					return false;
				});
				
				$('.nav .next').click(function () { 
					goTo(current_vid + 1);
					return false;
				});
			 	
				goTo(initial_index);
			  }
		});
	}
	
	function animateClients() {
		$('.clients-list li a').each(function(){ 
			$(this).animate({ pacity: 1, color: '#000'}, 2000);
		});
	}
	
	function animateContact() {
		$('.contactus').each(function(){ 
			$(this).animate({opacity: 1,color: '#fff'}, 2000);		
		 });
	}
	
	// $(' > .menu-item:eq(2)', '#menu-main-menu').css({'margin-right': 64});
	$(' > .menu-item', '#menu-main-menu').eq(-3).css({'margin-right': 64});
	$('.contact-form').append('<div class="cl">&nbsp;</div>');
	$('input[type=submit]').addClass('submit');
	
	if ( $('.gallery-main li img').length > 0 ) {
		var max_height = 0;
		var images = $('.gallery-main li img');
		images.load(function() {
			var parent_hidden = $(this).is(':not(:visible)');
			if ( parent_hidden ) {
				$(this).parents('ul').show();
			};
			var h = $(this).height();
			$(this).css({'margin-top': Math.ceil(322 - h/2)})
			$(this).data('orig_h', h);
			if ( parent_hidden ) {
				$(this).parents('ul').hide();
			};
			if ( h > max_height ) {
				max_height = h;
				$('.gallery-main li').css('min-height', max_height);
			};
			
		});
	};
	
	blurSlideshow();
	photoThumbs();
	animateClients();
	videoThumbs();
	animateContact();
});

