$(document).ready(function(){

/*						   
	// Remove the border from the last list item
   	$("#footer ul li:last, #subnav li a:last").css({border: 'none'});
	$("#transmissions .col:last").css({margin: '0'});
	
	// no margin on first paragraph of a portfolio text piece
	$("#content .piece .text p:first").css({margin: '0 0 1.5em 0', padding: '0'});
	
	
	
	$('#content .piece .text .button a').addClass('external').attr('target', '_blank');
   
   	// horizontal accordion on the homepage
	$(".haccordion").haccordion();
	
*/	
	/*****************************
	 * Searching the portfolio
	 *****************************/
/*
	var disable_portfolio_click = false;
	
	$("div#portfolio-search-box a").click(function(){
					
		if(disable_portfolio_click)
			return false;
			
		disable_portfolio_click = true;
		
		times_clicked_more = 0;
		
		$(this).parent().toggleClass("off");

		var parent_div = $("div#portfolio-search-box a");
		var a = 0;
		
		parent_div.fadeTo("fast", 0.35, function()
		{
			
			if(++a <= 1)
				$.ajax(
				    {
				        url: "portfolio/more/",
				        data: "selected=" + portfolio_get_selected_list(),
				        success: function(text, ajax) {
				        
				        	parent_div.fadeTo("slow", 0.99);
				        	
							$("div.piece").remove();
							$("p#show-more-work").remove();
				        
				        	$("div#transmissions").before(text);
							
							portfolio_page_reset_clickables();
							disable_portfolio_click = false;
							
				        }
				    }
				);
			
		});
				
		return false;
	
	});
*/		
	
	/*****************************
	 * Portfolio page image change 
	 *****************************/
	 
	 // Load links in new window
	 $("div.portfolio_piece div.portfolio_description .visit a").click(function(){
		window.open(this.href);
		return false;
	});
	 
	var times_clicked_more = 0;
	var link_selected = null;
	var row_selected = null;

	function portfolio_page_reset_clickables()
	{
				
		$("ul.portfolio_pagination li a").unbind("click").click(function(){

			// ---------------------
			// reset	
			// ---------------------
			//$(this).parent().parent().parent().parent().find("div.selected").removeClass("selected").find("li.selected").removeClass("selected");
			
			// ---------------------
			// change link colour
			// ---------------------
			//link_selected = $(this).parent();
			//link_selected.addClass("selected");
			
			// ---------------------
			// change div colour
			// ---------------------
			link_clicked = $(this);
			row_selected = $(this).parent().parent();	
			row_selected.parent().find("p.portfolio_name").html(". . . Loading . . .");
	
					
			// ---------------------
			// change image
			// ---------------------
			image_url = $(this).attr("href");

			// First we fade out the current image
			row_selected.parent().parent().find("div.portfolio_image div.portfolio_image_fade").fadeOut("fast", function(){
	
				// When the image has finished fading out we start loading the new one
				var image = new Image(); 
				image.onload = function(){
	
					// when the new one has finished loading, we fade it in again
					var findme = row_selected.parent().parent().find("div.portfolio_image div.portfolio_image_fade");
	
					findme.html(image);
					findme.fadeIn("fast");
	
					// Transfer the hidden description to the description box
					row_selected.parent().find("p.portfolio_name").html(
						link_clicked.parent().parent().find("li strong").html()
					);
					
					// flash!
					//row_selected.effect("highlight", {}, "slow");				
					
				}
	
				image.src = image_url; 
				 		
			});
			
			return false;
		
		});

		/*****************************
		 * Portfolio page more link 
		 *****************************/
		$("p.show_more_work a").unbind("click").click(function(){
		
			times_clicked_more++;
			
			$.ajax(
			    {
			        url: "portfolio/more/",
			        data: "timesclicked=" + times_clicked_more,
			        success: function(text, ajax) {
			        
			        	$("p.show_more_work").replaceWith(text);
						
						portfolio_page_reset_clickables();
						
						return false;
						
			        }
			    }
			);
			
			return false;
			
		});		

	}

	/*
	
	function portfolio_get_selected_list()
	{
	
		var list_of_selections = new Array();
	
		$("div#portfolio-search-box li:not(.off)").each(function()
		{
			var name = $(this).attr("id").split("_")[1];
			list_of_selections.push(name);
		});


		if(list_of_selections.length == 0)
			return ""; 
		else
			return list_of_selections.join("|");

	}*/
			
	portfolio_page_reset_clickables();
	
});