$(document).ready(function(){
	
	// open/close the articles panel
	$("#articles").hover(
		function(){
			$(this).stop(true,false).animate({"left": "820px"}, "normal");
		},
		function(){
			$(this).stop(true,false).animate({"left": "705px"}, "fast");
		}
	);
	
	// open/close the main nav sections
	$(".navsection").hover(
		function(){
			if ($(this).attr('id') == "nav-portfolio"){
				var newWidth = "250px";
			} else {
				var newWidth = "95px";
			}
			$(this).stop(true,false).animate({"top":"0px", "width":newWidth}, "normal");
		},
		function(){
			var closeMargin = 50;
			if ($(this).hasClass('selected')){
				closeMargin = 35;
			}
			if ($(this).attr('id') == "nav-portfolio"){
				var newWidth = "168px";
			} else {
				var newWidth = "95px";
			}
			$(this).stop(true,false).animate({"top":closeMargin+"px", "width":newWidth}, "fast");
		}
	);
	
	// fade between portfolio home slides
	$("#portfolionav a").hover(
		function(){
			// animate the button
			var childSpan = $(this).children('span');
			if (childSpan.hasClass('single')){
				$(childSpan).stop(true,false).animate({"padding-top":"25px"}, "normal");
			} else {
				$(childSpan).stop(true,false).animate({"padding-top":"14px"}, "normal");
			}
			$(this).stop(true,false).animate({"width":"185px", "height":"70px"}, "normal");
			
			// fade out the welcome text
			$("#welcome").fadeOut(500);
			
			// switch the background image
			var section = $(this).attr('id').replace("pnav-", "");
			var $active = $('#homeslideshow img.active');
			var $next = $('#homeslideshow img#pimg-'+section);
			$active.addClass('last-active');
			$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function(){
				$active.removeClass('active last-active');
			});
		},
		function(){
			var childSpan = $(this).children('span');
			if (childSpan.hasClass('single')){
				$(childSpan).stop(true,false).animate({"padding-top":"12px"}, "fast");
			} else {
				$(childSpan).stop(true,false).animate({"padding-top":"4px"}, "fast");
			}
			$(this).stop(true,false).animate({"width":"120px", "height":"48px"}, "fast");
		}
	);
	
	// rollover portfolio section thumbnails
	$(".portfolio-thumbnav li a.thmb").hover(
		function(){
			$('#thumbname p').html("<img src=\"imgs/thumbname_dot.gif\" height=\"12\" width=\"12\" alt=\"\" /> "+$(this).find('img').attr('alt'));
		},
		function(){
			$('#thumbname p').html('');
		}
	);
	
	// project category thumbnail paging nav
	$(".portfolio-thumbnav li.blank a").click(
		function(){
			var btnTarget = $(this);
			
			if ($(this).hasClass('prev')){
				var direction = 'prev';
			} else if ($(this).hasClass('next')){
				var direction = 'next';
			}
								
			var shellTarget = $("#portfolio-thumbnav-shell");
			var shellWidth = shellTarget.width();
			var shellLeft = 0;

			var listTarget = $("#portfolio-thumbnav-set");
			var listWidth = listTarget.width();
			var listLeft = parseInt(listTarget.css('left'));

			var newListPos = listLeft;
			if (direction == 'next'){
				newListPos = listLeft-(shellWidth);
			} else if (direction == 'prev'){
				newListPos = listLeft+(shellWidth);
			}
				
			listTarget.stop(true,false).animate({"left": newListPos+"px"}, "normal", function(){
				var newListLeft = parseInt(listTarget.css('left'));
			});
			
			return false;
		}
	);
	
	// project detail thumbnail paging nav
	$(".thumb-shell-nav a").click(
		function(){
			var padding = 9;
			var btnTarget = $(this);
			
			if (!($(this).hasClass('disabled')) && !($(this).hasClass('working'))){
				if ($(this).hasClass('prev')){
					var direction = 'prev';
				} else if ($(this).hasClass('next')){
					var direction = 'next';
				}
				
				var shellTarget = $(this).parent().prev();
				var shellWidth = shellTarget.width();
				var shellLeft = 0;

				var listTarget = $(this).parent().prev().find('ul');
				var listWidth = listTarget.width();
				var listLeft = parseInt(listTarget.css('left'));

				var newListPos = listLeft;
				if (direction == 'next'){
					newListPos = listLeft-(shellWidth-padding);
				} else if (direction == 'prev'){
					newListPos = listLeft+(shellWidth-padding);
				}

				btnTarget.siblings('a').addClass('working');
				btnTarget.addClass('working');
				
				listTarget.stop(true,false).animate({"left": newListPos+"px"}, "normal", function(){
					var newListLeft = parseInt(listTarget.css('left'));
					
					if (direction == 'next'){
						if (newListLeft+listWidth <= shellLeft+shellWidth){
							btnTarget.addClass('disabled');
						} else {
							btnTarget.removeClass('disabled');
						}
					} else if (direction == 'prev'){
						if (newListLeft < (shellLeft+(padding-1))){
							btnTarget.removeClass('disabled');
						} else {
							btnTarget.addClass('disabled');
						}
					}
					btnTarget.siblings('a').removeClass('disabled');
					btnTarget.siblings('a').removeClass('working');
					btnTarget.removeClass('working');
				});
				
			}
			
			return false;
		}
	);
	
	// swap main image with selected thumbnail
	$(".column-main ul.thumbphotosnav li a").click(
		function(){
			return false; // prevent click action since hover activates swap
		}
	);
	$(".column-main ul.thumbphotosnav li a").mouseover(
		function(){
			var src = $(this).attr('href');
			var full = src.replace(/.jpg/i, "_f.jpg");
			var imghtml = "<a target=\"_blank\" href=\""+full+"\"><img src=\""+src+"\" alt=\"\" /></a>";
			$('#mainimageswap').html(imghtml);
			return false;
		}
	);
	
	// swap plan image
	$(".plans ul li a").click(
		function(){
			var src = $(this).attr('href');
			var imghtml = "<img src=\""+src+"\" alt=\"\" />";
			$('#planimageswap').html(imghtml);
			return false;
		}
	);
	
	// services list
	$("dl.services dt").click(
		function(){
			var target = $(this).attr('class');
			$("dl.services dd").each(function(index){
				if ($(this).hasClass(target)){
					$(this).children('p').show();
				} else {
					$(this).children('p').hide();
				}
			});
			
			$('.scroll-pane').jScrollPane({scrollbarWidth:7});
			stopSlideShow();
			
			$("#mainimageslideshow img").each(function(index){
				if ($(this).attr('id') == target){
					$(this).show();
				} else {
					$(this).hide();
				}
			});
			
			return false;
		}
	);
	
	// tooltips
	$("a.tooltip").hover(
		function(e){									  
			this.t = this.title;
			this.title = "";					  
			$("body").append('<p id="tooltip">'+ this.t +'</p>');
			$("#tooltip")
				.css("top",(e.pageY - 7) + "px")
				.css("left",(e.pageX + 12) + "px")
				.fadeIn("fast");		
		},
		function(){
			this.title = this.t;		
			$("#tooltip").remove();
		}
	);	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - 7) + "px")
			.css("left",(e.pageX + 12) + "px");
	});
	
	// image slideshow
	var slideShowInterval;
	$('#mainimageslideshow img:gt(0)').hide();
});

function startSlideShow(){
	slideShowInterval = setInterval(function(){$('#mainimageslideshow :first-child').fadeOut(1000).next('img').fadeIn(1000).end().appendTo('#mainimageslideshow');}, 3000);
};
function stopSlideShow(){
	clearInterval(slideShowInterval);
}
