function ckGalleryDisplayObj() {
	this.galleryID = (arguments.length >=1 ? arguments[0] : ""); 
	this.galleryImageGroupID = (arguments.length >=2 ? arguments[1] : "");
	
	this.oGallery = null;
	this.oGroup = null; //current group
	this.oImage = null; //current image
	
	this.curImageIndex = 0;
	this.curGroupIndex = 0;
	
	this.outerContainerID = "ckGalleryDisplay"
	return this;
}

ckGalleryDisplayObj.prototype.show = function() {
	var oEl;
	if (oEl = document.getElementById(this.outerContainerID)) oEl.style.display="";
}
ckGalleryDisplayObj.prototype.hide = function() {
	var oEl;
	if (oEl = document.getElementById(this.outerContainerID)) oEl.style.display="none";
}

ckGalleryDisplayObj.prototype.getImages = function() {
	if (arguments.length >=1) this.galleryID = arguments[0];
	if (arguments.length >=2) this.galleryImageGroupID=arguments[1];
	
	this.hide(); //show waiting?
	var sData = "actionCode=getImages&galleryID=" + this.galleryID + "&galleryImageGroupID=" + this.galleryImageGroupID;
	(new ckAjaxObj()).postFormData("/ckcommon/gallery/ajax/gallery_action.asp",sData,this.ajaxReturnHandler_getImages,this)
				
}
ckGalleryDisplayObj.prototype.ajaxReturnHandler_getImages=function(oAjax) {
	var oResponse,El;
	if ((oResponse = oAjax.responseObj) && (this.oGallery = oResponse.payload.oGallery)) {
		//alert(objectDebugStr(this.oGallery,6));
		this.displayGroup(0);
		this.displayThumbnails();
	}
}

ckGalleryDisplayObj.prototype.displayThumbnails = function() {
	var oEl;
	//alert(objectDebugStr(this.oGallery,3));
	
	if ((oEl = document.getElementById("gallery_thumbnails")) && (this.oGroup)) {
		oEl.innerHTML = ""; //clear any old images
	
		var numImages = this.oGroup.aImages.length;
		for(var index=0; index < numImages;index++) {
			var oImage=this.oGroup.aImages[index];
			
			var oHref = document.createElement("A");
			oHref.href="javascript:oGalleryDisplay.displayImage(" + index + ")";
			
			
			var oImg = document.createElement("IMG");
			oImg.src="/ckcommon/gallery/getThumbnail.asp?src=" + oImage.imgURL + "&w=" + this.oGallery.thumbWidth + "&h=" + this.oGallery.thumbHeight;
			oImg.style.border="1px solid #FFFFFF";
			oImg.style.margin="0 5px 5px 0";
			oImg.border="0";
			
			oHref.appendChild(oImg);
			oEl.appendChild(oHref);
		}
		
		oEl.style.width=(numImages * (this.oGallery.thumbWidth + 7)); //7=5padding+2border
		
	}
}

ckGalleryDisplayObj.prototype.displayGroup = function(index) {
	var oEl;
	
	if ((index >= 0) && (index < this.oGallery.aGroups.length) && (this.oGroup = this.oGallery.aGroups[index])) {
		this.curGroupIndex = index

		if (oEl = document.getElementById("gallery_image")) oEl.style.display="none"; //hide any previously showing image

		if (this.oGallery.boolGroupBased) {
			if (oEl = document.getElementById("gallery_primaryText_title")) oEl.innerHTML = this.oGroup.grpName;
			if (oEl = document.getElementById("gallery_primaryText_description")) oEl.innerHTML = this.oGroup.grpDesc;
			if (oEl = document.getElementById("gallery_primaryText_subName")) oEl.innerHTML = this.oGroup.grpSubname;
		
			if (oEl = document.getElementById("gallery_nextButton")) oEl.className=(this.curGroupIndex == (this.oGallery.aGroups.length-1) ? "controlButton-disabled" : "");
			if (oEl = document.getElementById("gallery_prevButton")) oEl.className=(this.curGroupIndex > 0 ? "" : "controlButton-disabled");

			
			var numImages = this.oGroup.aImages.length;
			if (numImages==1) numImages=0; //just so button doesn't show until at least 2 images avail
			for(var index=1; index <= this.oGallery.maxNumImagesPerGroup;index++) {
				if (oEl = document.getElementById("images_image" + index + "Button_href")) oEl.style.display = (index<=numImages ? "" : "none")
			}
			if (oEl = document.getElementById("content_gallery_groupImageControls")) oEl.style.display=(numImages==0 ? "none" : ""); //turn back on in case turned off from non-groupBased gallery showing

		} else {
			if (oEl = document.getElementById("content_gallery_groupImageControls")) oEl.style.display="none";
			if (oEl = document.getElementById("content_gallery_groupImageText")) oEl.style.display="none";
			
		}
		
		this.displayGroup_addOnHandler(); //can be overwritten by calling scripts
		
		this.displayImage(0);
		
	}

}
ckGalleryDisplayObj.prototype.displayGroup_addOnHandler = function(index) {
	//dummy function that can be overwritten by individual calling pages (see CK site that uses groupSubName as link to portfolio website)
}

ckGalleryDisplayObj.prototype.displayImage = function(index) {
	var oEl;
	
	if ((index >= 0) && (index < this.oGroup.aImages.length) && (this.oGroup.aImages[index])) {
		this.oImage = this.oGroup.aImages[index];
		this.curImageIndex = index;
		
		//alert(objectDebugStr(this.oImage));
		if (oEl = document.getElementById("gallery_image")) {
			//use server side class to check size against gallery allowed size. Image may have been uploaded before a size change on the gallery. Need "new Image()" since browser will not return correct size of dynamically loaded img src
			oEl.src = (this.oImage.imgURL=="" ? "/_ckcommon/images/spacer.gif" : "/ckcommon/gallery/getThumbnail.asp?src=" + this.oImage.imgURL + "&maxW=" + this.oGallery.maxImageWidth + "&maxH=" + this.oGallery.maxImageHeight);
			oEl.style.display="";
		}
		if (oEl = document.getElementById("content_gallery_imageCredits")) {
			oEl.innerHTML= this.oImage.imgCredits;
		}
		
		if (this.oGallery.boolGroupBased) {
			if (oEl = document.getElementById("content_gallery_groupImageText")) oEl.style.display=((this.oImage.imgDesc!="" || this.oImage.imgTitle!="") ? "block" : "none");
			if (oEl = document.getElementById("gallery_groupImageText_title")) oEl.innerHTML=this.oImage.imgTitle;
			if (oEl = document.getElementById("gallery_groupImageText_description")) oEl.innerHTML=this.oImage.imgDesc.replace("/\\n/g","<br>");
			
			//for whatever sites implement a group image next and prev button
			if (oEl = document.getElementById("gallery_groupImage_nextButton")) oEl.className=(this.curImageIndex == (this.oGroup.aImages.length-1) ? "controlButton-disabled" : "");
			if (oEl = document.getElementById("gallery_groupImage_lastButton")) oEl.className=(this.curImageIndex == (this.oGroup.aImages.length-1) ? "controlButton-disabled" : "");
			if (oEl = document.getElementById("gallery_groupImage_prevButton")) oEl.className=(this.curImageIndex > 0 ? "" : "controlButton-disabled");
			if (oEl = document.getElementById("gallery_groupImage_firstButton")) oEl.className=(this.curImageIndex > 0 ? "" : "controlButton-disabled");
		} else {
			if (oEl = document.getElementById("gallery_primaryText_title")) oEl.innerHTML = this.oImage.imgTitle;
			if (oEl = document.getElementById("gallery_primaryText_description")) oEl.innerHTML = this.oImage.imgDesc;
			
			if (oEl = document.getElementById("gallery_nextButton")) oEl.className=(this.curImageIndex == (this.oGroup.aImages.length-1) ? "controlButton-disabled" : "");
			if (oEl = document.getElementById("gallery_prevButton")) oEl.className=(this.curImageIndex > 0 ? "" : "controlButton-disabled");

		}

		this.setLinkOnImage();		
		this.updateImageCounter();	
	
		//Show main container which is always initially hidden so the previous group/gallery/image stuff isn't showing on the initial display of an image
		this.show();		
	}
}

ckGalleryDisplayObj.prototype.updateImageCounter = function() {
	var oEl;
	if (oEl = document.getElementById("gallery_imageCounter")) oEl.innerHTML = (this.curImageIndex+1) + " / " + this.oGroup.aImages.length;
}

ckGalleryDisplayObj.prototype.next = function() {
	if (this.oGallery.boolGroupBased) this.nextImageGroup();
	else this.nextImage();
}
	
ckGalleryDisplayObj.prototype.previous = function() {
	if (this.oGallery.boolGroupBased) this.prevImageGroup();
	else this.prevImage();
}

ckGalleryDisplayObj.prototype.nextImageGroup = function() {this.displayGroup(this.curGroupIndex+1); }
ckGalleryDisplayObj.prototype.prevImageGroup = function() {this.displayGroup(this.curGroupIndex-1); }
	
ckGalleryDisplayObj.prototype.nextImage = function() {this.displayImage(this.curImageIndex+1); }
ckGalleryDisplayObj.prototype.prevImage = function() {this.displayImage(this.curImageIndex-1); }
ckGalleryDisplayObj.prototype.firstImage = function() {this.displayImage(0); }
ckGalleryDisplayObj.prototype.lastImage = function() {this.displayImage(this.oGroup.aImages.length-1); }


ckGalleryDisplayObj.prototype.setLinkOnImage = function() {
	//separated so it can be called by custom handlers as well
	var sHref = (arguments.length >= 1 ? arguments[0] : this.oImage.imgLink);
	var sTitle = (arguments.length >= 2 ? arguments[1] : "click to " + sHref);
	var sTarget= (arguments.length >= 2 ? arguments[2] : this.oImage.imgLinkTarget);
	var oEl;
	
	if (oEl = document.getElementById("gallery_image_href")) {
		if (sHref=="") {
			oEl.removeAttribute("href");
			oEl.title = "";
			oEl.target = "";
		} else {
			oEl.setAttribute("href",sHref);
			oEl.title = sTitle;
			oEl.target = sTarget;
		}
		oEl.alt = oEl.title
	}
}
