//gallery
var iCount = 0;
//place holder for icon set intraval
var iInterval;
//container for icon html 4 col
var childDivLeft ="";
//container for icon html 4 row
var childDivRow ="";
// onestop looking for icon divs
var parentDivLeft = ($(".iconcol"))
var parentDivRow = ($(".iconrow"))
//Gallery length
var gallerySize;
//onestop lookup divs used for swaping in and out gal imgs
var $newDiv;
var $oldDiv;
//place holder for current div gal img and icon
var $curDiv;
var $curIcon;
//onestop lookup 4 legend below image
var $legendDiv;
//dyno txt 4 above
var $legendText;
//dyno span for icon col text
var $galheadText;
var $gallText;
//slideshow controls
//var playColor = "#fff"
var stopVis = "none"
//navigation for gallery

//slide show intraval
var slideIntrval;
//the gallery
var gallery = new Array ()
var galleryText = new Array ()
//Images to be preloaded
var preloadThese = "";

//Gallery
function displayIcons(speed){

gallerySize = (gallery.length-1)
var leftDiv = gallerySize;
//intital max ct 4 icons in lft div
if (iconcolCt != "all")
{
 leftDiv = iconcolCt;
 //adjust upwords if more than 20
 if ((gallerySize -iconcolCt) >= iconRowCt)
	{
	var add2Left = (Math.round((gallerySize-iconcolCt)*.332));	
	leftDiv = (iconcolCt+add2Left)
	}
}

 
 //build the icons put int he correct div
for (j=0; j<gallery.length;j++)
	{
		if (j > leftDiv){
			
			childDivRow += (iconConstruct(j))
			
		}
		else {
		childDivLeft += (iconConstruct(j))
		}
	}
$(childDivLeft).appendTo($(".iconcol"));
if (iconcolCt != "all"){
	$(childDivRow).appendTo($(".iconrow"));
}
iShow (speed)
}

function iconConstruct(n){
	
var icon =	("<div class='icondiv' id='icon_"+j+"'><img src='"+gallery[n].icon+"' alt='"+gallery[n].alt+"' title='"+gallery[n].alt+"'  id='imgicon_"+n+"'  onclick='showImg("+n+",\"stop\")' class='icon'></div>"	)

return icon;

}

function playShow(){
stopVis = "inline";
playColor = "#f00"
$("#slideshow").css({"color":playColor});
$("#stop").css({'display': stopVis});
curImgId++;
showImg (curImgId);
slideIntrval = setInterval(play,4000);
}

function iShow (speed){
	iInterval = setInterval(addIcon, speed);
	
}

function addIcon(){
	if(iCount == gallery.length){
		$(".iconhead").fadeIn(3500);
 		 clearInterval(iInterval);
		}
	else {
			if (iCount ==0){
			$(".iconhead").show(3500);
			}
		var div2show = ("#icon_"+iCount)
		$(div2show).show(500);
		$(div2show).fadeTo(250, 1.0, function (){
			$(div2show).fadeTo(250, .33)			  
			 });
				
		iCount++;
		}
}

//slide show
function play(){
	if (curImgId < gallerySize){
	curImgId++;
	showImg (curImgId);
	}
	else if (curImgId == gallerySize){
	stopAnimi ()
	showImg (0)
	
	}
	
}

function stopAnimi ()
{
	clearInterval(slideIntrval);
	stopVis = "none";
	playColor = "#fff"
	$("#slideshow").css({"color":"#fff"});
	$("#stop").css({"display": "none"});

}
function incrementGT (){
if (currentGT == galleryText.length -1){
	currentGT = 0;
}
else {currentGT++}	
}

//displsy gal image
function showImg (imgID,stopAni){
	if (($("#openingtext"))&&($("#openingtext").css('display') != "none"))
		{
		$("#openingtext").fadeOut(2000);	
		}
if (hasGText){
//if the taget obj is empty put in first increment currentGT
	if (($gallText.html == "")||(imgID == 0)){
	$gallText.html(galleryText[currentGT])
	incrementGT ()
	}
//if the imgID != 0 flip a coin and change text
	else if(imgID != 0 ){ 
		var randomNo =(Math.floor(Math.random()*2)%2)
		if (randomNo != 0){
			$gallText.html(galleryText[currentGT])
			incrementGT ()					   
		}
		
	}
	}

if (stopAni != null){
	stopAnimi ()
	}
var imgNav = ("&nbsp;<a href=\"javascript:javascript:void(0);\" onClick=\"showImg(curImgId+1)\" id='next'>&gt;</a>&nbsp;<a href=\"javascript:playShow();\" id='slideshow' style='color:"+playColor+";'>&gt;&gt;</a>&nbsp;<a href=\"javascript:stopAnimi ()\" style='display:"+stopVis+"; font-weight:100;' id='stop'>X</a>")
$curIcon.css({"border":"1px #000 solid"});
$curIcon.fadeTo("slow", 0.33)
	if (imgID > gallerySize)
		{
		imgID = 0;
		}
	curImgId = imgID;
var bkgImg = (gallery[imgID].fileName);
$curIcon = ($("#imgicon_"+imgID));
$curIcon.css({"border":"1px #f00 solid"});
$curIcon.fadeTo("fast", 1.0)	
$newDiv.html("<a href=\"javascript:javascript:void(0);\" onClick=\"showImg(curImgId+1)\"><img src='"+bkgImg+"' class='galleryimage' alt='"+gallery[imgID].title+"' title='"+gallery[imgID].title+"'><a/>");

$legendDiv.fadeOut(500,function (){
	$legendText.html("<p class='legend'>"+gallery[imgID].title+"&nbsp;&nbsp;&copy;"+gallery[imgID].date+" Guillaume Zuili</p>");						
	$galheadText.html("<span class='bold'>"+gallery[imgID].title+"</span>"+imgNav+"<br/><span class='imgtext'>"+gallery[imgID].desc+"<br/>"+gallery[imgID].location+"&nbsp;&copy;"+gallery[imgID].date+" Guillaume Zuili</span>");						
	});
$oldDiv.css({"zIndex": 100});
$newDiv.css({"zIndex": 200});
$newDiv.fadeIn(1000, function (){
	//hide old div
	
	$oldDiv.fadeOut(500, function () {	
		$oldDiv.css({"zIndex": 2});
		$newDiv.css({"zIndex": 1});
		
	// old div current div (old)
			$curIcon.css({"border":"1px red solid"})
			$curDiv	= $newDiv;			   
			$newDiv	= $oldDiv;		
			$oldDiv = $curDiv;
			});
		});
	$legendDiv.fadeIn(1000);
	
}




//create array gallery item
function picture (file, title, alt, desc, date, location, lp, ct)
{
this.fileName = "images/"+file;
this.title = title;
this.alt = alt;
this.desc = desc;
this.date = date;
this.location = location;
this.lp = lp;
this.ct = ct;
this.icon = "images/i_"+file;
this.frameBkg = "images/iframe_"+file;
$.preload(this.fileName)
$.preload(this.icon)
$.preload(this.frameBkg);
return this;
}