$(document).ready(function(){

	// Turn off click action for the current page in the menu
   	$("nav li.current a").click(function(event){
		return false;
	});
	
	$("ul.pagination li.pagination_current a").click(function(){
		return false;
	});
   
   	// Form submit since submit button is anchor / image 
   	$("#file_upload form .file_upload_submit a").click(function(){
   		$(this).parents('form').submit();
   	});
   	// Close the error message
   	$("#file_upload .file_upload_error").click(function(){
   		$(this).hide();
   	});
   	// Break the error message list into multiple columns
   	$("#file_upload .file_upload_error li:nth-child(2n)").addClass('file_upload_error_list_odd');
   	// Auto show the file upload box if the error message is there 
   	$(".file_upload_error").parent().css('marginTop', '0');
   	
   	// Link the logo to the homepage
   	$("h1.logo_imageworks").click(function(){
   		window.location = '/default.aspx';
   	});
   	   
   	  
   	// On contact page, display just the first letter of the contact types
   	$(".vcard .type").each(function() { 	
		var fl = $(this).text().substr(0, 1);
		$(this).text(fl);   	
   	});
   	  
   	// Open / Close of file upload box.  1st case is if the box is open
	$("#file_upload_link").click(function(){
		var fub = $("#file_upload");
		
		// Hide the labels if there's already text in them when the box opens
		fub.find('input').each(function() {
			if ($(this).val() != '') {
				$(this).parent().find('label').css('opacity', 0);
			}	
		});
		
		if (fub.css('marginTop') == '0px') {
			var moveUp = ($("#file_upload").outerHeight() * -1);
			$("#file_upload").animate({
	   			marginTop: moveUp
	   		}, 700);
		} else {
			$("#file_upload").animate({
	   			marginTop: "0"   		
	   		}, 700);
		}		
	
	});
	
	// Close the file upload box, same as above's first statement
	$("a.file_upload_close").click(function(){
		var moveUp = ($("#file_upload").outerHeight() * -1);
   		$("#file_upload").animate({
   			marginTop: moveUp
   		}, 700);	
	});
		
	// Animate the label to fade back if there's no text input already
	$('#file_upload input').focus(function(){
		if ($(this).val() == '') {
			$(this).parent().find('label').animate({opacity: 0.5}, "fast");
		} else {
			$(this).parent().find('label').css('opacity', 0);
		}
	});

	// Animate the label to fade up if the box is still empty	
	$('#file_upload input').blur(function(){
		if ($(this).val() == '') {
			$(this).parent().find('label').animate({opacity: 1}, "fast");
		} else {
			$(this).parent().find('label').css('opacity', 0);
		}
	});
	
	// Hide the label if text is put into the box
	$('#file_upload input').keypress(function(){
		$(this).parent().find('label').css('opacity', 0);
	});
	
	$('#file_upload input:file')
		.css('opacity', 0)
		.wrap('<div class="fancy_file_container"></div>')
		.after('<div class="fancy_file_redraw"><a href="#" title="Browse">Browse</a><span></span></div>')
		.change(function(){
			$(this).parent().find('.fancy_file_redraw span').text($(this).val())
		})
	;
	$('#file_upload .fancy_file_redraw').click(function(){
		$(this).parent().find('input').trigger('mousedown');
	});
		
		
	// Close the portfolio piece by sliding up.  slideUp() turns display to none, making the top border-disappear
	$("a.portfolio_close").click(function(){
		$("#portfolio_item").animate(
			{
				height: 0
				,paddingTop: 0
				,paddingBottom: 0
			}
			, "fast"
			, function(){
				$(this).removeClass('open');
				$('ul.portfolio_list li').css('opacity', 1);
			}
		)		
		return false;
	});
	
	
	// Previous & Next buttons
	$("a.portfolio_next, a.portfolio_previous").click(function(){
		var selected_parent = $('ul.portfolio_list a.portfolio_selected').parent();
		var direction = $(this).hasClass('portfolio_next') ? 'next':'prev';

		// Find the next/prev li item, also determining if we're at the beginning or end of the list
		switch(direction){
			case 'next':				
				anchor = (selected_parent.next().find('a').length == 0) ?  $('ul.portfolio_list li:first a') : selected_parent.next().find("a");
				break;				
			case 'prev':
				anchor = (selected_parent.prev().find('a').length == 0) ? $('ul.portfolio_list li:last a') : selected_parent.prev().find("a");
				break;		
		}	
		
		// Remove any currently selected, then add class to the new current selection
		$('a.portfolio_selected').removeClass('portfolio_selected');
		anchor.addClass('portfolio_selected');	
				
		// Do the image swap
		loadResize(anchor.attr('href'));
	
		return false;
	});
	
	// Remove margin off left-most portfolio pieces, end of each row	
	$("ul.portfolio_list li:nth-child(3n)").addClass('end_of_row');
	
	// Load up the portfolio piece in the display	
	$("ul.portfolio_list li a").click(function(){
		$('ul.portfolio_list li').css('opacity', 0.5);
	
		$('#portfolio_item').height($('#portfolio_item').height()).addClass('open');

		$('a.portfolio_selected').removeClass('portfolio_selected');
		$(this).addClass('portfolio_selected');
					
		loadResize($(this).attr('href'));
				
		return false;
	});
	
	$('div.portfolio_description').css('opacity', 0);
	
	$('div.portfolio_content').hover(
		function() {
			var selected_item = $("ul.portfolio_list li a.portfolio_selected");
			var selected_title = selected_item.attr('title');
			var selected_description = selected_item.find('img').attr('alt');
			var description_container = $(this).find('.portfolio_description');	
			
			description_container.width(($('#portfolio_item img').width() - 40));
			description_container.find('h2').text(selected_title);
			description_container.find('p').text(selected_description);
			
			description_container.animate({opacity: 0.6}, "fast");
		},
		function() {
			$(this).parent().find('.portfolio_description').animate({opacity: 0}, "fast");	
		}		
	)
	
	function loadResize(new_src) {
		var img = new Image();
  
		// wrap our new image in jQuery, then:
		$(img)
			// once the image has loaded, execute this code
			.load(function () {
				$('#portfolio_item img').replaceWith($(this));

				var new_height = $(this).outerHeight(true) + 7;
				var new_width = $(this).outerWidth(true);
											
				$("#portfolio_item").animate(
					{
						height: new_height
						, paddingTop: 10
						, paddingBottom: 20
					}, "fast"					
				)
				$("#portfolio_item .portfolio_image").animate(
					{
						width: new_width
					}, "fast"
				)
			})
		
		// if there was an error loading the image, react accordingly
		.error(function () {
		  // notify the user that the image could not be loaded
		})
		// *finally*, set the src attribute of the new image to our image
		.attr('src', new_src);
			
	}
	
});

