function update_menu(active_panel) {
	var active_class = active_panel.attr('class'); 
	$("ul#portfolio-menu>li").removeClass('active');
	$("ul#portfolio-menu>li."+active_class).addClass('active');
}
function load_home(full_page) {
	if(full_page){
		$('#examples ul').css({backgroundPosition:"188px -80px"});
		//$("div#spotlight.adobe-tree p").show().fadeTo(4000, 0);
		$("div#spotlight.adobe-tree p").css({opacity:"0", display:"block"}).fadeTo(4000, 0);;
	}
	else{ 
		$("div#spotlight.adobe-tree p").css({opacity:"0", display:"block"}).fadeTo(2500, 0);
	}
	$('#menu-arrow').hide();
	$("div#spotlight.adobe-tree h2").css({opacity:"0", display:"block"}).fadeTo(1700, 0);
	$("div#spotlight.adobe-tree h2").css({opacity:"0", display:"block"}).fadeTo(1900, 1);
	//$("div#spotlight.adobe-tree p, #intro p, #examples, #contact").css({opacity:"0", display:"block"}).fadeTo(4500, 1);
	$("div#spotlight.adobe-tree p").css({opacity:"0", display:"block"}).fadeTo(4500, 1);
	$('#examples ul').removeClass('active');//css({backgroundPosition:"-527px -137px"});
}

$(document).ready(function() {
	// Load external links in a new window
	$("a").live("click", function(){
		if($(this).attr('rel') == 'external'){ window.open($(this).attr('href')); return false;}
	});
	load_home(1);
	$("#examples").hover(function(){
			$('#examples').addClass('hover');
		},
		function(){
			$('#examples').removeClass('hover');
	});
	$("#examples li").hover(function(){
			$(this).stop().fadeTo(200, 1);
			$('#examples').addClass('hover');
			return false;
		},
		function(){
			if($(this).hasClass('selected')) return false;
			$(this).stop().fadeTo(300, .5);
	});

	// Set the examples menu click actions
	$("#examples ul li>a").click(function(){
			// Stop the current animation and place a loading screen up			
			$('#spotlight').stop().html('<div id="loading">Loading</div>');
			$(this).parent().siblings().removeClass('selected').fadeTo(300, .5);
			$(this).parent().addClass('selected').fadeTo(200, 1);
			var thisLi = $(this).parent();
			var selectedItemName = $(this).attr('class');
			var selectedItemPosition = $("#examples ul li").index(thisLi);

			// Animate the arrow at the top. The horizontal background pixel position of the arrow placed on the first item is -882. We get the index of the item selected (1 through 5), and multiply that by the pixel distance between each item, then subtract that from the first item's position at 882
			if(!$('#examples').hasClass('active')) $('#examples ul').css({backgroundPosition:"-527px -137px"});
			$('#menu-arrow').hide();
			$('#examples').addClass('active');
			x_position = 884 - (132*(selectedItemPosition));
			$('#examples ul').stop().animate(
			  {backgroundPosition:"(-" + x_position + "px -107px)"},
			  {duration:500});
			if(selectedItemName == 'borderzine'){ fadeInPause = 3000; fadeInSpeed = 1500; }
			else{ fadeInPause = 500; fadeInSpeed = 200; }
			$('#menu-arrow').removeClass().addClass(selectedItemName).css('left', -x_position + 598).fadeTo(fadeInPause, 1).fadeIn(fadeInSpeed);

			// Load the selected item in the spotlight page then fade in the h2
			$('#spotlight').load("/adobetree_2009/wp-content/themes/adobetree/script/load_spotlight.php?item="+selectedItemName, function(){
						$('#spotlight').removeClass().addClass(selectedItemName); 
						//$("div#spotlight h2").fadeTo(3000, 0).fadeTo(1500,1);
						$("div#spotlight h2").css({opacity:"0", display:"block"}).fadeTo(3000, 0);
						$("div#spotlight h2").css({opacity:"0", display:"block"}).fadeTo(1500, 1);
						$("#spotlight a.sample").fancybox();
																																																						   				});
			
			// Place #menu-arrow's over the current arrow position and then change the background to match the current spotlight
			return false;
	});
	$("#examples a.next").click(function(){
		var nextUl = $('#examples ul.selected').next('ul');
		if(!nextUl.attr('id') || nextUl.attr('id') == 'undefined') return false();
		$("#examples a.previous").removeClass('none');
		$("#examples ul").removeClass('selected');
		nextUl.addClass('selected');
		var nextNextUl = nextUl.next('ul');
		if(!nextNextUl.attr('id') || nextNextUl.attr('id') == 'undefined') $("#examples a.next").addClass('none');
		return false;
	});
	$("#examples a.previous").click(function(){
		var prevUl = $('#examples ul.selected').prev('ul');
		if(!prevUl.attr('id') || prevUl.attr('id') == 'undefined') return false();
		$("#examples a.next").removeClass('none');
		$("#examples ul").removeClass('selected');
		prevUl.addClass('selected');
		var prevPrevUl = prevUl.prev('ul');
		if(!prevPrevUl.attr('id') || prevPrevUl.attr('id') == 'undefined') $("#examples a.previous").addClass('none');
		return false;
	});
	// Set the Home Link click action to load the front page and reset the navigation menu
	$("p#home-link a").live("click", function(){
		$('#spotlight').load("/adobetree_2009/wp-content/themes/adobetree/script/load_spotlight.php?item=home", function(){$('#spotlight').removeClass().addClass('adobe-tree'); load_home(); });
		$('#examples').removeClass('active');
		$('#examples ul li').removeClass('selected').fadeTo(300, .5);
		//remove_examples_li_selected();
		$('#examples ul').stop().animate(
			  {backgroundPosition:"(-884px -137px)"},
			  {duration:500});
		//$('#examples ul').css({backgroundPosition:"272px -80px"});
		return false;
	});
	// Set up specific spotlight actions
	$("#spotlight.apartment-ideas ul.header>li a").live("click", function(){
		$(this).parent().siblings().removeClass('active');
		$(this).parent().addClass('active');
		$(this).closest('ul').next('ul').find('li').removeClass('active');
		$(this).closest('ul').next('ul').find('li.'+$(this).attr('class')).addClass('active');
		$(this).closest('div#spotlight').find('h2').removeClass().addClass($(this).attr('class'));
		return false;
	});
	
	// Set up the contact form actions
	if(!start_height) var start_height = $('#get-in-contact').height();
	$("#get-in-contact a.button").click(function(){
		if($(this).parent().hasClass('open')){
			$('#get-in-contact fieldset').show();
			$("#get-in-contact").animate({ 
				height: start_height+"px"
			  }, 1500 );
			$(this).parent().removeClass('open');
		}
		else{
			$('#message-success').hide();
			$('a#contact_send').animate({ opacity: 1}, 10);
			$(this).parent().addClass('open');
			$("#get-in-contact").animate({ 
				height: "340px"
			  }, 1500 );
		}
		return false;
	});
	
	$('a#contact_send').click(function(){
		$('#contact-form').submit();
	});
	/* Validate the form */
	$('form#contact-form').validate({
		rules: {
			contact_email: {
			  email: true
			}
		},
		messages : {
			contact_email : {
				required : 'Please enter your email',  
				email : 'Oops! That email doesn\'t look valid...'
			},  
			contact_name : 'Please enter your name',  
			contact_message : 'Please write a message'
		},
		submitHandler: function() {
			$('a#contact-send').animate({ opacity: 0.1}, 700);
			$('#contact-form').ajaxSubmit({ 
				// target identifies the element(s) to update with the server response 
				//target: '#htmlExampleTarget', 
		 
				// success identifies the function to invoke when the server response 
				// has been received; here we apply a fade-in effect to the new content 
				success: function(){ 
					$('#contact-form fieldset').hide();
					$('#message-success').fadeIn('slow');
					$('#contact-form fieldset input, #contact-form fieldset textarea').val('');
					//$('#htmlExampleTarget').fadeIn('slow'); 
				} 
			});
			form.submit();
 		}
	});

/*
	$('#examples.active ul').mouseover(function(){
		  $(this).stop().animate(
			  {backgroundPosition:"(-137px -107px)"}, 
			  {duration:500})
		  })
	  .mouseout(function(){
		  $(this).stop().animate(
			  {backgroundPosition:"(-537px -107px)"}, 
			  {duration:500})
		  });

	// Product Loading
	$("#product-list>ul>li a").click(function() {
		// Fade out the current product navigation
		if($(this).attr('class').split(' ').slice(-1) == 'active') return false; 
		$("#product-list>ul>li a").removeClass('active');
		$(this).addClass('active');

// Load the selected product
		var product = $(this).attr('id');
		var product_title = $(this).attr('title');
		$('#portfolio').html('<div class="loading '+product+'"><h2>'+product_title+'</h2>Loading</div>');
		$('#portfolio').load("/wp-content/themes/papercuts-printing/script/load_products.php?product="+product);
		// Scoll to the first panel
		var first_panel = $('#portfolio>ul> li:first');
		$('div#portfolio').stop().scrollTo(first_panel, 700);
		return false;
	});
	// Home link
	$("#home-link a").live("click", function(){
		$("#product-list>ul>li a").removeClass('active');
		$('#portfolio').html('<div class="loading home"><h2>Home</h2>Loading</div>');
		$('#portfolio').load("/wp-content/themes/papercuts-printing/script/load_products.php?product=home");
		// Scoll to the first panel
		var first_panel = $('#portfolio>ul> li:first');
		$('div#portfolio').stop().scrollTo(first_panel, 700);
		return false;
		
	});
	// Navigation Bar
	$('#navigation>ul>li a').live("click", function(){
		$("#navigation>ul li").removeClass('active');
		$(this).parent().addClass('active');
		/// Scroll to the target
		var target_panel = $(this).attr('class');
		$("#portfolio>ul li").removeClass('active');
		$("#portfolio>ul li."+target_panel).addClass('active');
		$('div#portfolio').stop().scrollTo( 'li.'+target_panel, 700 );
		return false;
	});

	// Panel Scrolling
	// Reset all scrollable panes to (0,0)
	$('div#portfolio').scrollTo( 0 );
	// Reset the screen to (0,0)
	$.scrollTo( 0 );
	$('div#next').live("click", function(){
		// Find the class of the next panel
		var active_panel = $('#portfolio>ul> li.active');
		var next_panel = active_panel.next();
		// If this is the last panel, then scroll over to the first
		if(!next_panel.attr('class')){
			next_panel = $('#portfolio>ul> li:first');
		}
		active_panel.removeClass('active');
		update_menu(next_panel);
		next_panel.addClass('active');
		$('div#portfolio').stop().scrollTo(next_panel, 900);
		return false;
	});
	$('div#previous').live("click", function(){
		// Find the class of the next panel
		var active_panel = $('#portfolio>ul> li.active');
		var previous_panel = active_panel.prev();
		// If this is the last panel, then scroll over to the first
		if(!previous_panel.attr('class')){
			previous_panel = $('#portfolio>ul> li:last');
		}
		active_panel.removeClass('active');
		update_menu(previous_panel);
		previous_panel.addClass('active');
		$('div#portfolio').stop().scrollTo(previous_panel, 900);
		return false;
	});
	$('ul#portfolio-menu>li> a').click(function(){
		// Find the class of the next panel
		if($(this).parent().hasClass('active')) return false;
		var active_class = $(this).parent().attr('class');
		var active_panel = $('#portfolio>ul>li.'+active_class);
		$('#portfolio>ul> li').removeClass('active');
		update_menu(active_panel);
		active_panel.addClass('active');
		$('div#portfolio').stop().scrollTo(active_panel, 900);
		return false;
	});
*/
});