var $ = jQuery.noConflict();

$.fn.exists = function(){return $(this).length>0;}

$(document).ready(function(){  
	/* 


	Gallery
	-------------------------------------------------------------- */
	if($('.gallery').exists())
	{
		var windowHeight;
		var windowWidth;
		var headerHeight;
		var footerHeight;
		
		var minHeight = 768;
		var minWidth = 1024;
		
		var current_slide = 0;
		
		function update_sizes()
		{ 
			windowHeight = $(window).height();
			windowWidth = $(window).width();
			headerHeight = $('.header').height();
			footerHeight = $('.footer').height();
			
			/*$('.stats p').html(
				'windowHeight: '+windowHeight+'<br>'+
				'windowWidth: '+windowWidth+'<br>' + 
				'headerHeight: '+headerHeight+'<br>'+
				'footerHeight: '+footerHeight
			);*/
		}
		
		function resize_gallery()
		{
			// resize img
			$('.gallery img').each(function(){
				if(windowWidth > minWidth)
				{
					$(this).css({'width':windowWidth});
				}
				else
				{
					$(this).css({'width':minWidth});
				}
				
			});	
			
			// set gallery images verticaly middle
			$('.gallery img').each(function(){
				//alert('this height = '+$(this).height()+', this data orig = '+$(this).data('originalHeight'));
				var padding = (headerHeight - footerHeight)/2;
				$(this).css({'margin-top':padding-($(this).height()/2)});
			});
			
			if($('.gallery-thumbnails').exists())
			{
				$('.gallery-thumbnails .mask').css({'margin-top':-($('.gallery-thumbnails .mask').height()/2)});
			}
		}
		/* 


		Load Next Image
		-------------------------------------------------------------- */
		function loadNextImage()
		{
			// loads active slide first
		   	var div = $('div.gallery .active .img');
			if(!div.exists())
			{
				// if no active slide (already been loaded), load slide images left to right
				div = $('div.gallery .img:first');	
			}
			if(div.exists())
			{
				
				var parent = div.parent('.slide');
				parent.addClass('loading');
				
				var img = new Image();
				$(img).load(function () {
					$(this).hide();
					parent.removeClass('loading').append(this);
					$(window).trigger('resize');
					$(this).fadeIn(1000, function(){loadNextImage();});
				}).error(function () {
					// do nothing
				}).attr('src', div.attr('src'));
					
				var img = '<img src="'+div.attr('src')+'" alt="'+div.attr('alt')+'">';
				parent.html('');
				/*parent.html(img);
				parent.find('img').load(function()
				{
					alert('image loaded! height = '+parent.find('img').height());
					
					parent.animate({opacity: 1.0}, 1000, function(){loadNextImage();});
			   		
				});
				*/
		   }
			
		}
		loadNextImage();
		/* 


		Fade Gallery
		-------------------------------------------------------------- */
	
		// next
		
		if($('.gallery .next').exists())
		{
			function next_hide_show()
			{
				var active = $('.gallery .active');
				if(!active.next('.slide').exists())
				{
					$('.gallery .next').hide();	
				}
				else
				{
					$('.gallery .next').show();
				}
			}
			next_hide_show();
			
			function gallery_fade_next()
			{
				var active = $('.gallery .active');
				if(active.next('.slide').exists())
				{
					if($('.header h3.hide').exists())
					{
						$('.header h3.hide').animate({opacity: 1.0}, 1000);
					}
					var next = active.next('.slide');
					active.removeClass('active').addClass('last-active');
					next.css({opacity: 0.0})
						.addClass('active')
						.animate({opacity: 1.0}, 0, function() {
							active.removeClass('last-active');
					});
					next_hide_show();
					prev_hide_show();
				}
				
				return false;		
			}
			$('.gallery .next').click(function(){gallery_fade_next();});
		}
		
		
		// prev
		
		if($('.gallery .prev').exists())
		{
			function prev_hide_show()
			{
				var active = $('.gallery .active');
				if(!active.prev('.slide').exists())
				{
					$('.gallery .prev').hide();	
					if($('.header h3.hide').exists())
					{
						$('.header h3.hide').animate({opacity: 1.0}, 1000);
					}
				}
				else
				{
					$('.gallery .prev').show();
				}
			}
			prev_hide_show();
			
			function gallery_fade_prev()
			{
				var active = $('.gallery .active');
				if(active.prev('.slide').exists())
				{
					var prev = active.prev();
					active.removeClass('active').addClass('last-active');
					prev.css({opacity: 0.0})
						.addClass('active')
						.animate({opacity: 1.0}, 0, function() {
							active.removeClass('last-active');
					});
					next_hide_show();
					prev_hide_show();
				}
				return false;	
			}
			$('.gallery .prev').click(function(){gallery_fade_prev();});
		}
		
	
		$(window).resize(function()
		{
			update_sizes();
			resize_gallery();
		});
		
		$(window).trigger('resize');
		
	}
	/* 


	Drop down menu
	-------------------------------------------------------------- */
	if($('.header .menu').exists())
	{
		$('.header .menu li:has(ul)').each(function(){
			var li = $(this);
			li.children('a').attr('href','#').click(function(){return false;})
			
			li.hover(function(){
				li.children('ul').children('li').stop(true, true).slideDown(300);
			},
			function(){
				li.children('ul').children('li').slideUp(300);
			});
			if(li.is('.page-item-5') || li.is('.page-item-7'))
			{
				li.find('ul li a').click(function(){
					// get the page URL
					var url = $(this).attr('href');
					
					// clear .mask
					$('.gallery-thumbnails').html('');
					$('.gallery-background').fadeTo(1000, 0.9);
					//$('.gallery-thumbnails').addClass('loading');
			
					// load in div from page
					$('.gallery-thumbnails').load(url + '?page=thumbnails table', function(){
						$(window).trigger('resize');
						var clicked_a = false;
						$(this).find('td').mousedown(function(){
							if(clicked_a == false)
							{
								close_gallery_thumbnails();
							}
						});
						$(this).find('a').mousedown(function(){
							clicked_a = true;
						});
						$('.gallery-thumbnails').fadeTo(500, 1);
						//$('.gallery-thumbnails .mask').removeClass('loading');
					});
					
					// return false so the link doesnt redirect the browser
					return false;
	
				});
			}
		});
	}
	
	function close_gallery_thumbnails()
	{
		$('.gallery-thumbnails').animate({opacity: 0}, 500, function(){$('.gallery-thumbnails').hide();});
		$('.gallery-background').animate({opacity: 0}, 500, function(){$('.gallery-background').hide();});
	}

	
	/* 


	Team thumbnails
	-------------------------------------------------------------- */
	if($('.team-thumbs').exists())
	{
		$('.team-thumbs').children('a').each(function(){
			$(this).click(function(){
				gallery_goto($(this).index());
			});
		});
		
		function gallery_goto(position)
		{
			var active = $('.gallery .active');
			var next = $('.gallery .slide:nth-child('+(position+1)+')');
			if(next.exists())
			{
				active.removeClass('active').addClass('last-active');
				next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						active.removeClass('last-active');
					});
			}
			
			return false;	
		}
	}// end if team thumbs
	
	
});

$(window).load(function(){ 
	$(window).trigger('resize');
});
