var arrImages = new Array();
var imagesDisplayed = 0;
var arrDisplay = new Array();
var previewDisplay = "";
var currImage = 0;
var isSlidePlaying = true;


$(document).ready(function() {
	searchImage();
	$(".imageItem").each( function() { 
								   var objImage = new Object();
								   objImage.id = $(this).attr("id").replace('i',''); 
								   arrImages[arrImages.length] = objImage; });
	startSlideShow();
});

function populateImages() {
	var content = ""; 
	var i = 0;
	var counter = imagesDisplayed;
	if (arrImages.length > 0)	{
		for (i=counter;i<arrImages.length;i++) {
			content += "<div id=\"imageItem"+arrImages[i].id+"\" class=\"libItemContainer\">";
			content += "<div class=\"imageItem libItem\" onclick=\"previewImage('"+arrImages[i].id+"','main');\" id=\"i"+arrImages[i].id+"\">";
			content += "<div class=\"imageThumb\"><img src=\""+arrImages[i].thumb+"\" /></div>";
			content += "<span class=\"libItemTitle\"><span class='videoLength'>"+arrImages[i].date+ "</span> - "+arrImages[i].desc+"</span>";
			content += "</div>";
			content += "</div>";
			imagesDisplayed++;
		}
		$("#imagesList").append(content);
	} else {
		$("#imagesList").html("<div class='center addMarginTop'>"+labelNoPhotosFound+"</div>");
	}
}

function searchImage() {
	/*$("#imageResults").html("&nbsp;<img align=\"absmiddle\" src=\"/global/javascript/ajax/loader.gif\"/>");*/
	$("#imagesList").html("");
	$.ajax({ url: "/Global/CustomTags/slideShow/photoJson.cfm?isAjaxRequest&publish="+publish+"&conference="+conference+"&category="+category+"&language="+language,
			 cache: false,
			 dataType: "json",
			 success: function(data) { arrImages = data.results; populateImages(); },
	 		 error: function() { alert('There was an error processing your Search Image request. Please try again'); }
	});
}

function previewImage(imageID, collection,conference,category) { 
	arrDisplay = arrImages;
	previewDisplay = collection;
	currImage = parseInt(imageID);
	$("#imagePreview #image").html("<div style='height:338px;'>&nbsp;</div>");
	$("#imageLoading").show();
	if ( $("#imagePreview").modal( { onClose: function (dialog) { $.modal.close(); stopSlideShow(); } } ) ) {
		$("#simplemodal-container").hide();
		$("#simplemodal-container").fadeIn();
	}
	
	$.ajax({ url: "/Global/CustomTags/slideShow/photoJson.cfm?isAjaxRequest&id="+imageID+"&language="+language+"&publish="+publish,
			 cache: false,
			 dataType: "json",
			 success: function(data) {   var imgData = new Image();
										 imgWidth = (imgData.width>480?imgData.width:480);
										 imgData.src = data.results[0].url;
									    $(imgData).load(function () { $.modal.setPosition(); 

											//imgData.src = data.results[0].url;
											$("#imagePreview").css("width",imgWidth);
											$("#imagePreview #image").html("<img src=\""+data.results[0].url+"\" />");
										} ).attr('src', data.results[0].url);
										
										$("#imagePreview #desc").html(data.results[0].desc + " <span class=\"gray\">("+data.results[0].date+")</span>");
										$("#imagePreview #prev").css("height",imgData.height).css("width",parseInt(imgWidth/2));
										$("#imagePreview #next").css("height",imgData.height).css("width",parseInt(imgWidth/2));
										$("#imageLoading").hide();
										$("#imagePrevContent").fadeTo(400,1, function() {  $.modal.setPosition(); } );
										if(isSlidePlaying)
											setTimeout("nextImage(true);",3000);
			 },
	 		 error: function()  { alert('There was an error processing your request. Please try again'); }
											
	});

}

function nextImage(slideShow) {
	var index = imageIndex();
	var nextIndex = 0;
	if (index > -1 && index < arrDisplay.length-1)
		nextIndex = index+1;

	if (!(slideShow && !isSlidePlaying)) {
		$("#imageLoading").show();
		$("#imagePrevContent").fadeTo(400,0.01);
		setTimeout("previewImage(arrDisplay["+nextIndex+"].id,'"+previewDisplay+"');",400);
	}
	
}

function prevImage() {
	var index = imageIndex();
	var prevIndex = arrDisplay.length-1;
	if (index > -1 && index > 0)
		prevIndex = index-1;

	$("#imageLoading").show();
	$("#imagePrevContent").fadeTo(400,0.01);
	setTimeout("previewImage(arrDisplay["+prevIndex+"].id,'"+previewDisplay+"')",400);
}

function imageIndex() {
	for (var i=0;i<arrDisplay.length;i++) { 
		if (arrDisplay[i].id == currImage) {
			return i;
		}
	}
	return -1;
}

function startSlideShow(imageID) {
	var index = 0;
	if (typeof imageID == "undefined") {
		if (arrDisplay.length > 0) {
			isSlidePlaying=true; 
			previewImage(arrDisplay[0].id,"main");
		}
	} else {
		isSlidePlaying=true;
		nextSlide();
	}
}

function stopSlideShow() {
	isSlidePlaying = false;
}



