//PREVENT IE ERRORS
var featureTimer = 0;
if (window.console === undefined) window.console = {log: function(){}};
	
	
	$(document).ready(function()
	{
		$("#feature_navigation a").click(swapFeature);
		autoRunFeature();
		
		$(".person .more, .person .close").click(togglePerson);
		$(".pagination .page").click(scrollThumbnails);
		$(".project_display .images li a").click(swapBigImage);
		
		$("#project_image").click(nextBigImage);
		$(".next_image").click(nextBigImage);
		
		// SHOP
		$(".cart_add_form").submit(verifyCart);
		
		// LIGHTBOX IT
		$("a.lightbox").smartbox({width:600,height:340});
		
		checkFirstImage();
		checkThumbnails();
	});
		
	
	//Event Handlers
	function verifyCart(e)
	{
		var form = $(this);
		var select = form.find('select');
		if (select.val() == "") {
			e.preventDefault();
			alert("Please select a " + select.find(':selected').text());
		}
	}	
	function swapFeature(e)
	{
		e.preventDefault();
		clearInterval(featureTimer);
		var target = $(this);
		
		if(!target.hasClass("active"))
		{
			var nav = target.parents("#feature_navigation");
			var index = nav.find("li").index(target.parent("li"));
			
			nav.find(".active").removeClass("active").animate({ backgroundColor: '#DDDDDD' }, { queue: false, complete: function() {
				$(this).removeAttr("style");
			} });
			target.addClass("active").animate({ backgroundColor: target.attr("rel") }, { queue: false });
			
			$(".feature_content.active").css({ zIndex: 1 });
			$(".feature_content").eq(index).css({ zIndex: 2 }).animate({ opacity: 1 }, { queue: false, complete: function() {
				$(".feature_content.active").css({ opacity: 0 }).removeClass("active");
				$(this).addClass("active")
			} });
		}
	}
	function nextFeature()
	{
		var current = $("#feature_navigation a.active");
		var current_index = current.parent().index();
		
		var new_index = current_index + 1;
		if(new_index >= 4)
		{
			new_index = 0;
		}
		var target =  $("#feature_navigation li:eq(" + new_index + ")").find("a");
		
		var nav = target.parents("#feature_navigation");
		var index = nav.find("li").index(target.parent("li"));
		
		nav.find(".active").removeClass("active").animate({ backgroundColor: '#DDDDDD' }, { queue: false, complete: function() {
			$(this).removeAttr("style");
		} });
		target.addClass("active").animate({ backgroundColor: target.attr("rel") }, { queue: false });
		
		$(".feature_content.active").css({ zIndex: 1 });
		$(".feature_content").eq(index).css({ zIndex: 2 }).animate({ opacity: 1 }, { queue: false, complete: function() {
			$(".feature_content.active").css({ opacity: 0 }).removeClass("active");
			$(this).addClass("active")
		} });
		
	}
	function autoRunFeature()
	{
		featureTimer = setInterval(function()
		{
			nextFeature();
		}, 10000);
	}
	
	
	function scrollThumbnails(e)
	{
		e.preventDefault();
		var a = $(this);
		if(!a.hasClass("selected"))
		{
			$(".pagination .selected").removeClass("selected");
			a.addClass("selected");
			var index = $(".pagination .page").index(a);
			var target = 406 * index;
			$(".paged_content ul").stop().animate({marginLeft:"-" + target + "px"},{duration:700});
		}
	}
	function checkFirstImage()
	{
		var image_holder = $("#project_image");
		var img = image_holder.find("img");
		
		if($.browser.msie || img[0].complete || img[0].readyState === 4) 
		{
			fixFirstImage.apply(img, []);
		}
		else 
		{
			img.one("load", fixFirstImage);
		}
	}
	function fixFirstImage()
	{
		var image_holder = $("#project_image");
		var img = $(this);
		var oldHeight = img.outerHeight();
		if(oldHeight > 1000)
		{
			$(".project_images").find(".edge").show();
			oldHeight = 1000;
		}
		image_holder.css({height:oldHeight});
	}
	function checkThumbnails()
	{
		var lis = $(".project_display .images li a");
		
		lis.each(function() {
			var img = $(this).find("img");
			if($.browser.msie || img[0].complete || img[0].readyState === 4) 
			{
				fixThumbnail.apply(img, []);
			}
			else 
			{
				img.one("load", fixThumbnail);
			}
		});
	}
	function fixThumbnail()
	{
		var img = $(this);
		var parent = img.parents("a");
		var thumbHeight = img.outerHeight();
		if(thumbHeight > 350)
		{
			parent.find(".edge").show();
			thumbHeight = 350;
		}
		parent.css({height:thumbHeight});
	}
	
	function swapBigImage(e)
	{
		e.preventDefault();
		var a = $(this);
		if(!a.parent().hasClass("selected"))
		{
			$(".project_display .images li.selected").removeClass("selected");
			a.parent().addClass("selected");
			var image_holder = $("#project_image");
			var img = image_holder.find("img");
			var oldHeight = img.outerHeight();
			if(oldHeight > 1000)
			{
				oldHeight = 1000;
			}
			image_holder.css({height:oldHeight});
			img.stop().animate({opacity:0}, {duration:400, complete:function() { 
				img.one("load", function(){ 
					var newHeight = img.outerHeight();
					if(newHeight > 1000)
					{
						newHeight = 1000;
					}
					image_holder.stop().animate({height:newHeight}, {duration:100, complete: function() { 
						if(newHeight >= 1000)
						{
							$(".project_images").find(".edge").show();
							newHeight = 1000;
						}
						else
						{
							$(".project_images").find(".edge").hide();
						}
						img.stop().animate({opacity:1, duration:400});
					} });
				}).attr("src", a.attr("href")); 
			} });
		}
	}
	function nextBigImage(e)
	{
		e.preventDefault();
		var current = $(".project_display .images li.selected");
		var next = current.next();
		if(next.length == 0)
		{
			next =  $(".project_display .images li").eq(0);
		}
		current.removeClass("selected");
		next.addClass("selected");
		
		var image_holder = $("#project_image");
		var img = image_holder.find("img");
		var oldHeight = img.outerHeight();
		if(oldHeight > 1000)
		{
			oldHeight = 1000;
		}
		image_holder.css({height:oldHeight});
		img.stop().animate({opacity:0}, {duration:400, complete:function() { 
			img.one("load", function(){ 
				var newHeight = img.outerHeight();
				if(newHeight > 1000)
				{
					newHeight = 1000;
				}
				image_holder.stop().animate({height:newHeight}, {duration:100, complete: function() { 
					if(newHeight >= 1000)
					{
						$(".project_images").find(".edge").show();
					}
					else
					{
						$(".project_images").find(".edge").hide();
					}
					img.stop().animate({opacity:1, duration:400});
				} });
			}).attr("src", next.find("a").attr("href")); 
		} });
		
		if(next.index() % 2 == 0)
		{
			var page_int = Math.ceil(next.index() / 2);
			var target = 406 * page_int;
			$(".paged_content ul").stop().animate({marginLeft:"-" + target + "px"},{duration:700});
			
			$(".pagination .selected").removeClass("selected");
			$(".pagination .page").eq(page_int).addClass("selected");
		}
		
	}
	function togglePerson(e)
	{
		e.preventDefault();
		
		var target = $(this);
		var person = target.parents('.person');
		
		closeOtherPeople(person);
		
		if(person.hasClass("open"))
		{
			closePerson(person);
		}
		else
		{
			openPerson(person);
		}
	}
	
	function closeOtherPeople(targetPerson)
	{
		$(".person.open").not(targetPerson).each(function() {
			closePerson($(this));
		});
	}
	
	function openPerson(person)
	{
		var description = person.find('.person_description');
		var oldHeight = parseInt(person.css("height"));
		var newHeight = oldHeight + description.outerHeight(true);
		person.stop().animate({ height: newHeight }).attr("originalHeight", oldHeight).addClass("open").find(".more").fadeOut();
		description.animate({ opacity: 1 });
	}
	function closePerson(person)
	{
		var description = person.find('.person_description');
		var newHeight = person.attr("originalHeight");
		person.stop().animate({ height: newHeight }).removeAttr("originalHeight").removeClass("open").find(".more").fadeIn();
		description.animate({ opacity: 0 });
	}

