var index, imageArr, imageCount;
if(pgId!="")
	var section = pgId.substring(pgId.indexOf("/")+1);

function loadPortContent(clientName) {
	//re-initialize variables on each load
	index = 0;
	imageArr = new Array();
	
	//get # of image files for this client, via AJAX
	$.get("content/work/countImages.php", { client: clientName, id: section },
    function(data){
    	imageCount = data;
		
		$("#display-copy").load("content/"+pgId+"/"+clientName+"/blurb.php",function(responseText, textStatus){
			if (textStatus=="success") { 
				imageCount--;
				$("#display-copy").css("display", "block").animate({"height": "toggle"},1);
				$("#portfolio-display #controls #show-text").css("display", "block");
				$("#display-copy").prepend("<div id='hide-text' style='float:right;'><img src='images/portfolio/icons/icon_collapse.jpg' /></div>");
				$("#hide-text").click(
					function(){
						$("#display-copy").animate({"height": "toggle"},"slow");
					}
				);
			}
			else{
				if($("#display-copy").css("height")!="auto") $("#display-copy").animate({"height": "toggle"},"slow");
				$("#portfolio-display #controls #show-text").css("display", "none");
			}

			//only display nav arrows if there is more than one image
			if (imageCount>=2) $("#port-nav img").css("visibility", "visible");
			else $("#port-nav img").css("visibility", "hidden");
			
			//build array of client images
			for (i=0; i<imageCount; i++){
				var str = "content/work/"+section+"/"+clientName+"/";
				if (i<10) str += "0";
				str += (i+1) + "_"+clientName+".jpg";
				imageArr[i] = str;
			}
			
			//set first image in array as src of display image
			imageArr.sort();
			$("#display-image").attr("src", imageArr[0]);
		
		});
    });
	
	//show display div
	
	
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").prepend("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
				$("#TB_overlay").click(closeDiv);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").prepend("<div id='TB_overlay'></div>");
				$("#TB_overlay").click(closeDiv);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		
	$("#portfolio-display").insertAfter("#TB_overlay");
	$("#portfolio-display").fadeIn("slow");
}
function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function imageForward(){
	index++;
	$("#display-image").attr("src", imageArr[index%imageCount]);	
}

function imageBack(){
	if(--index < 0) index = imageCount-1;
	$("#display-image").attr("src", imageArr[index%imageCount]);
}

function closeDiv(){
	if($("#display-copy").css("height")!="auto") $("#display-copy").animate({"height": "toggle"},"slow");
	$("#portfolio-display").fadeOut("fast");
	$("#display-image").attr("src", "content/images/portfolio/blank.gif");
	$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
}



$(function(){
	$("table.portfolio_grid tr td").not("#pdf td").each(
				function(){
					var off = $(this).css("background-image");
					if(off != "none"){
						var over = off.replace(/_off.jpg/, "_over.jpg")
						$(this).hover(
									 function(){
										 $(this).css("background-image", over)
									 }, 
									 function(){
										 $(this).css("background-image", off)
									 }
						);
					}
					else $(this).css("border-width", "0px").css("cursor", "auto");
					
					if( $(this).children("a").children("img").attr("src") == "images/portfolio/blank.gif") 
						$(this).children("a").children("img").css("border-width", "0px");
				}
	);
});