jQuery.preloadImages = function()
{
	preload = arguments[0];
	
	for(var i = 0; i<arguments[0].length; i++)
	{
		$(document.createElement('img')).bind('load', function() {
			if(preload[0]) this.src = preload.shift();
		}).trigger('load');
	}
}

$(document).ready(function() {

  offState = .3;
  overState = .6;
  onState = 1;
  var current = "hello";
  
  // Press Archive Scripts
  $(".expandLink a").click(function() {
  	expandDiv = $(this).attr('href');
  	if($("#" + expandDiv).hasClass("open")) {
  		$("#" + expandDiv).slideUp();
  		$(this).find("span.expandButton").text("[ + ]");
  		$("#" + expandDiv).removeClass("open");
  		$("#" + expandDiv).addClass("closed");
  	} else {
  		$("#" + expandDiv).slideDown();
  		$(this).find("span.expandButton").text("[ - ]");
  		$("#" + expandDiv).removeClass("closed");
  		$("#" + expandDiv).addClass("open");
  	}
  	return false;
  });
  
  $("a#expandAll").click(function() { 
  	if($(this).hasClass("all-expanded")) { 
  		$(".newsListing").slideUp();
  		$("span.expandButton").text("[ + ]");
  		$(this).removeClass("all-expanded");
  		$(this).text("Expand All [ + ]");
  	} else {
		$(this).addClass("all-expanded");
		$(".newsListing").slideDown();
		$("span.expandButton").text("[ - ]");
		$(this).text("Collapse All [ - ]");
	}
  	
  });
  
  $("a[rel^='prettyPhoto']").prettyPhoto();
  
  // Determin on state for sliding navigation dynamically
  $("#nav li.head").each(function() {
	
	var $myCurrent = $(this).find("ul").children(".current");
		
	if(($myCurrent.length + 0) > 0) {

		current = $(this).attr("title");
		current = parseInt(current);
			
	}
		
  });
  
  if(current == "hello") {
  	current = false;
  }
  
	
  // turn on our portfolio
  $(".portfolio").accordion({ active: current, collapsible: true, autoHeight: false });
  
  // Create an array of portfolio images, and preload them
  projectImages = new Array();
	
	$("ul#imageNav a").each(function() {
		projectImages[projectImages.length] = $(this).attr("href");
	});
	
	// set all our images to our offState instantly
	$("ul#imageNav a").fadeTo(0, offState);
	
	// set our first image to on
	$("ul#imageNav a:first").addClass("selected").fadeTo(0, onState);
	
	// when we click an image, load it up
	$("ul#imageNav a").click(function() {
		$("img#portfolioImg").attr("src", $(this).attr("href"));
		$("p#portfolioCaption").text($(this).attr("title"));
		$("ul#imageNav a.selected").removeClass("selected").fadeTo(0, offState);
		$(this).addClass("selected").fadeTo(0, onState);
		//$("img#loader").show();
		return false;
	});
	
	// when we hover, switch to our over state if appropriate
	$("ul#imageNav a").hover(
		function() {
			if($(this).hasClass("selected") == false) {
				$(this).fadeTo(0, overState);
			}
		},
		function() {
			if($(this).hasClass("selected") == false) {
				$(this).fadeTo(0, offState);
			}
		}
	);
	
	// Insert our captions
	$("p#portfolioCaption").text($("ul#imageNav li:first a").attr("title"));
	$("img#portfolioImg").load(function() {
		$("#portfolioImgContainer").css("width", $(this).width() + "px");
	}).error(function() {
		//alert("something's broke.");
	}).attr('src', $("ul#imageNav li:first a").attr("href"));
	
	// determine the width of the image container after it loads, so we can place our curved corners
	$("#portfolioImgContainer").css("width", $("img#portfolioImg").width() + "px");
	
	// deal with hover states for news articles
	$("#coverListing a").hover(
		function() {
			$(this).fadeTo(0, overState);
		},
		function() {
			$(this).fadeTo(0, onState);
		}
	);
	
});

$(window).load(function(){
	$.preloadImages(projectImages);
}); 
