var maxClick = 0;
var currentClick = 1;
var holdQty = 1;

/*galImgInfo is an array of gallery information....*/
/*0 - path
1 - caption
2 - description
3 - isPrimary
4 - galleryID
5 - onclick event*/

/*holdQty is how many images widths it should slide on a click*/
function populateGallery(galImgStr,holdQty,galleryID){
	
	/*first remove the images from the gallery....*/
	document.getElementById('galRail').innerHTML = "";
	
	eval(galImgStr);
	
	try{
				
		/*kinda hacky, but we set the name of the galRail div to be our gallery ID. we do this so
		we can access the ID for other gallery functions, for example, functions that require an image path*/
		document.getElementById("galRail").name = galleryID;
		document.getElementById("galRail").style.left = "0px";
		
		var qtyCnt = 0;
		var containCnt = -1;
		
		for(gi=0;gi<galImgInfo.length;gi++){
			
			/*determine how many iamges we put in the screen at once looping through here using holdQty*/
			if(qtyCnt==holdQty){
				qtyCnt = 0;
			}
			
			/*our group of holdQty images go in here*/
			if(qtyCnt==0){
				containCnt++;
				buildHtml.createDivHTMLElement("galImgContainer" + containCnt,"galImgContainer",null,null,document.getElementById("galRail"));
			}
			
			/*div for a specific image*/
			buildHtml.createDivHTMLElement("galImg" + gi,"galImg",null,null,"galImgContainer" + containCnt);
			
			/*the image itself*/
			buildHtml.createImage(galImgInfo[gi][0],null,null,"galImg" + gi,"galleryImage" + gi);
			
			/*div to cover the image, incase it needs a frame, we can put events here too*/
			buildHtml.createDivHTMLElement("galImgCover" + gi,"galImgCover",null,null,"galImg" + gi,galImgInfo[gi][5]);
			
			qtyCnt++;
		}
		
		/*hide arrows to scroll through gallery if there is less than 2 images*/
		if(galImgInfo.length < 2){
			document.getElementById("slideLeft").style.display = "none";
			document.getElementById("slideRight").style.display = "none";
		}
		else{
			document.getElementById("slideLeft").style.display = "block";
			document.getElementById("slideRight").style.display = "block";
		}
		
		/*how far we can scroll along before we run out of images*/
		maxClick = containCnt;
		currentClick = 1;
		
	}
	catch(e){alert(gi);
		alert(e);
	}
	
}

/*imgSize 1-thumb 2-medium 3-large*/
function getGalleryArray(galleryID,imgSize,holdQty){
	var galleryModule = ajaxObj();
	
	var requestParams = "function=getGalleryArrayByID&galleryID=" + galleryID + "&imgSize=" + imgSize;
	
	galleryModule.onreadystatechange=function(){
		if(galleryModule.readyState==4){
			populateGallery(galleryModule.responseText,holdQty,galleryID);
		}
	}
	
	ajaxPost(galleryModule,"/cmsAPI/gallery/gallery.php",requestParams);
}

/*1  +x, 0  -x*/
function gallerySlide(slideDir){
	
	var slide = false;
	
	var slideX = document.getElementById("galImgContainer0").offsetWidth;
	if(slideDir == 1){
		slideX = slideX * -1;
		
		if(currentClick <= maxClick){
			currentClick = currentClick+1;
			slide = true;
		}
	}
	else{
		if(currentClick > 1){
			currentClick = currentClick-1;
			slide = true;
		}
	}
	
	if(slide){
		new Effect.Move('galRail', { x: slideX, y: 0, mode: 'relative',duration:0.5, queue: { position: 'end', scope: 'galSlideQue', limit: 3 } });
	}
}

/*show the large gallery image in a new window*/
function showGalleryImage(divObj){
	
	/*we need to create the path of the large image file*/
	
	/*we store the gallery id as the name of this div...*/
	var galID = document.getElementById("galRail").name;
	
	/*we need the actual file name, we parse the src to get it*/
	var imgNum = divObj.id.substring(11,divObj.id.length);
	var galthumb = document.getElementById("galleryImage" + imgNum);
	var imgFileName = galthumb.src.substring(galthumb.src.lastIndexOf("/"),galthumb.src.length);
	var lrgPath = "/images/galleryImages/" + galID + "/large/" + imgFileName;
	window.open (lrgPath,"GalleryImage"); 
}