var imagePath;
var images = new Array; //global image array
var imageLabels = new Array; //image label array
var displayCopy;

//var inputImages = "%ctgy_hdr%";
//doCopy("%ctgy_ftr%");


function bam(){
	// Force header redraw
	$("#displayHeader").css('display','none').css('display','block');
}

function buildHeader(){
	buildImageArray();
	loadThumbs();
	doCopy();
	if (images.length>1) doPic(1);
}


function buildImageArray(){
	//input images is in the following string format:     working dir~image1~image2.jpg|desc1~desc2     
	//note that images are separated by a ~ and the desc and image lists are separated by a pipe
	var imageList = new Array; //the input image list, where it's split into the labels and the 

	if (inputImages.indexOf('|') == -1) inputImages = inputImages + '|';
	inputImages = inputImages + '~~~~';
	
	imageList = inputImages.split('|');
	imageLabels = imageList[1].split('~');
	imageLabels.length=4;
	images = imageList[0].split(',');
	
	imagePath = images[0];
}


function loadThumbs(){
	var thumbString = '';
	var theImage;

	if (images.length <=2) 
		$("#thumbtext").html('&nbsp;');
	else {
		for(var x=1; x<images.length; x++){
			if (images[x].indexOf('.jpg') == -1) theImage = images[x]+'.jpg'; 
			else theImage = images[x];
			
			thumbString = thumbString + '<span onmousedown="doPic('+x+');" style="cursor: pointer"><img border="0" src="graphics/00000001/'+imagePath+'/thumbnail-images-views/thumb-'+theImage+'" width="40" height="40"></span>&nbsp;&nbsp;';
			}
		$("#thumbcontainer").html(thumbString);
	}
}	

function doCopy(){
	var Space;
	var cutlength;
	var InputString=displayCopy;
	
	if (images.length <= 2)//if there are no thumbs it needs to be longer
		cutlength=380+bigchop;
	else 
		cutlength=300+bigchop;
	
	if (InputString.indexOf('<br>')>(cutlength-100) && InputString.indexOf('<br>')<cutlength)
		Space = InputString.indexOf('<br>',0)-1; 
	else 
		Space = InputString.indexOf(' ',cutlength); //CHANGE THIS BACK TO PERIOD
	
	if (Space==-1 || InputString.length-Space<40) Space=InputString.length;
	
	var Top = InputString.substring(0,Space+1);
	var Lcolumn = InputString.substring(Space+1);
	//Space = SecondHalf.indexOf(' ',SecondHalf.length*.55);
	
	//if (Space==-1 || SecondHalf.length<100) Space=SecondHalf.length;
	
	//var Lcolumn = SecondHalf.substring(0,Space);
	//var Rcolumn = SecondHalf.substring(Space+1);
	
	if (Lcolumn.substring(0,4)=="<br>") Lcolumn = Lcolumn.substring(4);
	
	if (Lcolumn != '') {
		//Lcolumn = Lcolumn + " " + Rcolumn; //drop this if going back to 2 column layout
		
		//Rcolumn = '<p>&nbsp;</p><p style="color:#4d4d4d;">' + Rcolumn + '</p>';
		Lcolumn = '<h3><a name="CONTINUED_FROM_TOP"></a>CONTINUED FROM TOP</h3><p>' + Lcolumn + '</p>';
		
		var loca = window.location.href;
		if(loca.indexOf('#')>0) loca = loca.substring(0,loca.indexOf('#'));
		
		Top = Top+' ... <a href="'+loca+'#CONTINUED_FROM_TOP">READ&nbsp;MORE</a>';
		
		if (Lcolumn!=null) $("#leftcolumn").html(Lcolumn);
		//if (Rcolumn!=null) document.getElementById("rightcolumn").innerHTML = Rcolumn;			
	}
	$("#toptext").html(Top);

}


function doPic(picNum){
	var widtht = 625;
	var heightt = 500;
	
	if(images[picNum].indexOf('.jpg') == -1){
		snippet = '<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
			'CODEBASE="http:/download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" WIDTH="'+
			widtht+'" HEIGHT="'+heightt+'" ID="theMovie"><PARAM NAME="wmode" VALUE="transparent"><PARAM NAME="FlashVars" '+
			'VALUE="zoomifyImagePath=graphics/00000001/'+imagePath+
			'/zoomify-folders/'+images[picNum]+'/&zoomifyX=0.0&zoomifyY=0.0&zoomifyZoom=-1&zoomifyToolbar=1&zoomifyNavWindow=1">'+
			'<PARAM NAME="src" VALUE="zoomifyViewer.swf"><EMBED FlashVars="zoomifyImagePath='+
			'graphics/00000001/'+imagePath+'/zoomify-folders/'+images[picNum]+'/&zoomifyX=0.0&zoomifyY=0.0&zoomifyZoom='+
			'-1&zoomifyToolbar=1&zoomifyNavWindow=1" SRC="zoomifyViewer.swf" PLUGINSPAGE="http:/www.macromedia.com/shockwave'+
			'/download/index.cgi?P1_Prod_Version=ShockwaveFlash" WIDTH="'+widtht+'" HEIGHT="'+heightt+'" NAME="theMovie" wmode="transparent"></EMBED></OBJECT>';
			// This is an ugly work around to a stupid IE7 problem
			setTimeout("bam();",500);
			setTimeout("bam();",1500);
			setTimeout("bam();",10000);
	}else{
		snippet = '<img onload="bam();" src="graphics/00000001/'+imagePath+'/full-size-images/'+images[picNum]+'">';
	}
	
	$('#Image').html(snippet);	
	$('#imagelabel, #imagelabelContainer').html(imageLabels[picNum-1]);
}