var mainImage;
var prevButtonHidden=false;
var nextButtonHidden=false;
var activeIndex = 0;

function buttonStateHandler(button, enabled) { 
	if (button == "prev-arrow"){
		$("prev-arrow").src = enabled ? "/stellent/groups/aia/documents/image/btn_arrows_left.gif" : "/stellent/groups/aia/documents/image/btn_arrows_left.gif"; 
	}else{ 
		$("next-arrow").src = enabled ? "/stellent/groups/aia/documents/image/btn_arrows_right.gif" : "/stellent/groups/aia/documents/image/btn_arrows_right.gif"; 
	} 
}


function animHandler(carouselID, status, direction) { 
	var region = $(carouselID).down(".carousel-clip-region"); 
	if (status == "before") { 
		Effect.Fade($("mainProjectPicture"),{duration:.3});
		Effect.Fade(region, {to: 0.2, queue: { position:"end", scope: "carousel" }, duration: 0.1}); 
	} 
	if (status == "after") { 
		Effect.Fade(region, {to: 1, queue: { position:"end", scope: "carousel" }, duration: 0.1, afterFinish: function callUpdateActive(){getNewMainImage();}}); 
		$("PhotoSummary").innerHTML = "Photo " + (carousel.currentIndex + 1) + " of " + (document.getElementsByClassName("thumbImage").length);
        activeIndex = carousel.currentIndex;
	} 
}   


function getNewMainImage(){
	mainImage = new Image();
	mainImage.src =  $("hThumbMedium" + activeIndex).value;
	mainImage.onComplete = updateActive();
}

function updateActive(){
    var dID = $("hThumbdID" + activeIndex).value;
    var dDocTitle = $("hThumbdDocTitle" + activeIndex).value;
    var xPhotographerName = $("hThumbxPhotographerName" + activeIndex).value;
    var xArchName = $("hThumbxArchitectName" + activeIndex).value;
    var url = $("hThumbURL" + activeIndex).value;
    var largeImgUrl = $("hThumbLarge" + activeIndex).value;
    var mediumImgUrl;
	
	if(mainImage){
		mediumImgUrl = mainImage.src;
	}else{
		mediumImgUrl = $("hThumbMedium" + activeIndex).value;;
	}
	clearBorders();
    
	addBlueBorder(activeIndex);
    $("mainProjectPicture").src = mediumImgUrl;
    $("mainProjectPicture").alt = dDocTitle;
    $("downloadButton").href = largeImgUrl;
	$("mainImageUrl").href = url;
    $("mainProjectPhotoCaption").innerHTML = "<h1>" + dDocTitle + "</h1>Photographer:&nbsp;" + xPhotographerName + "<br/>";
    if (xArchName != "") {
    	$("mainProjectPhotoCaption").innerHTML += "Architect:&nbsp;" + xArchName + "<br/>";
    }
    Effect.Appear($("mainProjectPicture"),{duration:.5});
	
	if(activeIndex==0){
		if(prevButtonHidden==false){
			$("prev-arrow").style.visibility = "hidden";
			prevButtonHidden=true;
		}
	}else{
		if(prevButtonHidden){
			$("prev-arrow").style.visibility = "visible";
			prevButtonHidden=false;
		}
	}

	if(activeIndex==totalThumbsCount-1){
		if(nextButtonHidden==false){
			$("next-arrow").style.visibility = "hidden";
			nextButtonHidden=true;
		}
	}else{
		if(nextButtonHidden){
			$("next-arrow").style.visibility = "visible";
			nextButtonHidden=false;
		}
	}

}


function clearBorders(ActiveImage){
	var projectThumbImages = document.getElementsByClassName("projectThumbImage");
	for(var i = 0;i<projectThumbImages.length;i++){
		projectThumbImages[i].style.borderWidth="0px";
		projectThumbImages[i].style.borderColor="#FFFFFF";
		projectThumbImages[i].style.borderStyle="solid";
	}
}

function addBlueBorder(ImageID){
		$("imgID" + ImageID).style.borderWidth="2px";
		$("imgID" + ImageID).style.borderColor="#65A3D4";
		$("imgID" + ImageID).style.borderStyle="solid";
}
