$(document).ready(function(){
	
	// fix PNGs
	$(document).pngFix();
	
	// external URLs in links
	$("a[rel=external]").click(function(){
		window.open(this.href);
		return false;
	}).attr('title', 'Opens in New Window or Tab');
	
	// homepage animations
	$("#homepage-thumbs div").each(function(){	
		var indx = $(this).index();
		var speed_rdm = Math.floor(Math.random()*801);
		var delay_rdm = Math.floor(Math.random()*3001);
		var timeout_rdm = Math.floor(Math.random()*4001);
		$(this).fadeIn(speed_rdm);
		$(this).delay( 2000+delay_rdm ).cycle({
			speed: speed_rdm+800,
			timeout:4000+timeout_rdm
		});
	});
	//$("#homepage-thumbs").append("<div id='homepage-thumb-shadow'></div>");

	// push extra homepage content to secondary <div>
	if ($("#homepage-extra").length>0) {
		$("#homepage-extra").detach().appendTo(".page-container")
			.before('<div id="sidebar" class="home-bottom">&nbsp;</div>');
		$("#homepage-extra").attr("id","page-content").addClass("secondary");
	}
	// push extra homepage sidebar content to secondary <div>
	if ($("#sidebar-extra").length>0) {
		$("#sidebar-extra").detach().appendTo("#sidebar.home-bottom");
	}
	
	// push client logo slideshow content to sidebar <div>
	if ($("#client-logo-slides").length>0) {
		$("#client-logo-slides").detach().appendTo("#sidebar");
	}
	$("#client-logo-slides").cycle({
		speed: 1500,
		timeout: 1500
	});
	
	
	// portfolio nav
	$("#cat-nav").detach().prependTo("#sidebar.portfolio ul li.current-cat.cat-item");
	numSlides = $("#portfolio-items > div").size();
	currSlide = 0;
	$(".type-portfolio:eq(0)").show("fast"); // to show in Safari (:first-child not working)
	changeCatNavAbility();
	$("#cat-nav a#prev").click(function(){
		if(currSlide>0) {
			$(".type-portfolio:eq("+currSlide+")").fadeOut("fast");
			currSlide--;
			$(".type-portfolio:eq("+currSlide+")").fadeIn("fast");
			changeCatNavAbility();
		}
		return false;
	});
	$("#cat-nav a#next").click(function(){
		if(currSlide<numSlides-1) {
			$(".type-portfolio:eq("+currSlide+")").fadeOut("fast");
			currSlide++;
			$(".type-portfolio:eq("+currSlide+")").fadeIn("fast");
			changeCatNavAbility();
		}
		return false;
	});
	
});

// this function controls the "prev" and "next" buttons, by disabling them as necessary
function changeCatNavAbility(){
	if(currSlide==0 || numSlides==1) {
		$("#cat-nav a#prev").addClass("disabled");
	}else{
		$("#cat-nav a#prev").removeClass("disabled");
	}
	if(currSlide==numSlides-1) {
		$("#cat-nav a#next").addClass("disabled");
	}else{
		$("#cat-nav a#next").removeClass("disabled");
	}
}

