/*	=====================================================	*/
/*	PhotoLoader																				*/
/*	=====================================================	*/

var CategoryPhotosXmlUrlTmpl = "/wsdl/categoryphotos.xml.php?id=";
var PhotoSmallUrlTmpl = "/special/components/graph/photo.small.image.php?id=";
var PhotoBigUrlTmpl = "/bigphoto.php?id=";
var SubCategoryMenuElementIDTmpl = "subCategoryMenuItem_";

PhotoLoader = function(divPhotoId)
{	
	this.DivPhoto = document.getElementById(divPhotoId);
	this.ArrPhotosId = new Array();
	this.ArrPhotos = new Array();
	this.CurrentItemInArrPhotosId = 0;
	this.CurrentSubCategoryMenuElement = null;
	
	this.OnShowedPhotoHandler = null;
	
	var currentObject = this;
	currentObject.ClickDivPhoto = function() {currentObject.OnClickDivPhoto();}
	this.DivPhoto.onclick = currentObject.ClickDivPhoto;
	
	//AjaxLoading Image
	this.AjaxLoadingImage = document.createElement("img");	
	this.AjaxLoadingImage.className = "imgPhoto";
	this.AjaxLoadingImage.style.cursor = "wait";
	this.AjaxLoadingImage.src = "/images/indicatorbig.gif";
	
	//NoImage Image
	this.NoImage = document.createElement("img");
	this.NoImage.src = "/images/no_image.gif";
	this.NoImage.className = "imgPhoto";
}

PhotoLoader.prototype.LoadCategoryPhotos = function(categoryId)
{	
	//this.DivPhoto.appendChild(this.AjaxLoadingImage);
	this.ShowPhotoInDiv(this.AjaxLoadingImage);
	this.CurrentItemInArrPhotosId = 0;
	
	this.CurrentCategoryId = categoryId;
	
	this.ArrPhotosId = new Array();
	
	this.SelectSubCategoryMenuElement();
	
	var urlCategoryPhotos = CategoryPhotosXmlUrlTmpl + this.CurrentCategoryId;
	
	var objAjaxRequest = new AjaxRequest(urlCategoryPhotos, "PhotoLoader");
	
	var currentObject = this;
	currentObject.LoadadedCategoryPhotos = 
		function(objAjaxRequest) {currentObject.OnLoadadedCategoryPhotos(objAjaxRequest);}	
	objAjaxRequest.setHandler(currentObject.LoadadedCategoryPhotos);
	objAjaxRequest.execute();
}

PhotoLoader.prototype.OnLoadadedCategoryPhotos = function(objAjaxRequest)
{
	var xmlDoc = objAjaxRequest.resultXML;	
	
	var oNodeList = xmlDoc.childNodes;
	for (var i=0; i<oNodeList.length; i++) 
	{
		var item = oNodeList.item(i);
		var imgId = item.firstChild.data;
		
		this.ArrPhotosId.push(imgId);
		
		var objImg = document.createElement("img");
		objImg.src = PhotoSmallUrlTmpl + imgId;
		objImg.className = "imgPhoto";		
		this.ArrPhotos[imgId] = objImg;
	}
	this.ShowPhoto();
}

PhotoLoader.prototype.OnClickDivPhoto = function()
{
	if (this.ArrPhotosId.length == 0 || this.CurrentItemInArrPhotosId >= this.ArrPhotosId.length)
		return;
	
	var photoId = this.ArrPhotosId[this.CurrentItemInArrPhotosId];	
	var url = PhotoBigUrlTmpl + photoId;
	
	window.open(url);
}

PhotoLoader.prototype.ShowPhoto = function()
{
	var bReturn = false;
	
	if (this.ArrPhotosId.length == 0 || this.CurrentItemInArrPhotosId >= this.ArrPhotosId.length)
	{
		this.ShowPhotoInDiv(this.NoImage);
		bReturn = false;
	}
	else
	{	
		var photoId = this.ArrPhotosId[this.CurrentItemInArrPhotosId];	
		var objImg = this.ArrPhotos[photoId];
		
		//alert("ShowPhoto " + objImg.complete);
		if (objImg.complete) this.ShowPhotoInDiv(objImg);
		else 
		{
			this.ShowPhotoInDiv(this.AjaxLoadingImage);
			var curentObject = this;
			objImg.onload = function() {curentObject.OnLoadedImage(photoId);}			
		}
		
		bReturn = true;
	}
	
	if (this.OnShowedPhotoHandler && typeof(this.OnShowedPhotoHandler) =="function")
		this.OnShowedPhotoHandler();
	
	return bReturn;
}

PhotoLoader.prototype.OnLoadedImage = function(photoId)
{		
	//alert(objImg.readyState);
	if (photoId == this.ArrPhotosId[this.CurrentItemInArrPhotosId] /*&& objImg.readyState == "complete"*/)
	{
		var objImg = this.ArrPhotos[photoId];
		this.ShowPhotoInDiv(objImg);		
	}
}

PhotoLoader.prototype.SetOnShowedPhotoHandler = function(handler)
{
	this.OnShowedPhotoHandler = handler;
}

PhotoLoader.prototype.Next = function()
{	
	if (this.CurrentItemInArrPhotosId >= this.ArrPhotosId.length - 1)	return false;
	this.CurrentItemInArrPhotosId++;
	this.ShowPhoto();
}

PhotoLoader.prototype.Prev = function()
{
	if (this.CurrentItemInArrPhotosId <= 0)	return false;
	this.CurrentItemInArrPhotosId--;
	this.ShowPhoto();
}

PhotoLoader.prototype.GetCountPhotos = function()
{
	return this.ArrPhotosId.length;
}

PhotoLoader.prototype.GetCurrentPhotosIndex = function()
{
	return this.CurrentItemInArrPhotosId;
}

PhotoLoader.prototype.SelectSubCategoryMenuElement = function()
{
	if (this.CurrentSubCategoryMenuElement != null) 
		this.CurrentSubCategoryMenuElement.className = "Item";
	
	this.CurrentSubCategoryMenuElement = 
		document.getElementById(SubCategoryMenuElementIDTmpl + this.CurrentCategoryId);
	if (this.CurrentSubCategoryMenuElement)
		this.CurrentSubCategoryMenuElement.className = "activeItem";
}

PhotoLoader.prototype.ShowPhotoInDiv = function(objPhoto)
{
	if (this.DivPhoto.hasChildNodes())
		this.DivPhoto.removeChild(this.DivPhoto.childNodes[0]);	
	this.DivPhoto.appendChild(objPhoto);	
}
/*	=====================================================	*/
/*	END PhotoLoader																			*/
/*	=====================================================	*/

/*	=====================================================	*/
/*	PhotoNavigator																			*/
/*	=====================================================	*/

PhotoNavigator = function(photoNavId, objPhotoLoader)
{
	this.TEXT_TMPL = "page {0} / {1}";
	
	this.PhotoNavId = photoNavId;
	this.Element = document.getElementById(this.PhotoNavId);
	
	this.PhotoLoader = objPhotoLoader;
	
	this.SpanPrev = document.getElementById(this.PhotoNavId + "_Prev");
	this.SpanNext = document.getElementById(this.PhotoNavId + "_Next");
	this.SpanText = document.getElementById(this.PhotoNavId + "_Text");
	
	this.ImgPrev = this.SpanPrev.childNodes[0];
	this.ImgNext = this.SpanNext.childNodes[0];
	this.ImgPrev.src = "/images/nav_prev_disable.gif";
	this.ImgNext.src = "/images/nav_next_disable.gif";
		
	var currentObject = this;
	currentObject.ClickNext = function() {currentObject.Next();}	
	this.SpanNext.onclick = currentObject.ClickNext;
	currentObject.ClickPrev = function() {currentObject.Prev();}	
	this.SpanPrev.onclick = currentObject.ClickPrev;
	currentObject.ShowedPhoto = function() {currentObject.OnShowedPhoto();}	
	this.PhotoLoader.SetOnShowedPhotoHandler(currentObject.ShowedPhoto);
}

PhotoNavigator.prototype.Next = function()
{
	this.PhotoLoader.Next();
	this.WriteData();
}

PhotoNavigator.prototype.Prev = function()
{
	this.PhotoLoader.Prev();
}

PhotoNavigator.prototype.OnShowedPhoto = function()
{
	this.WriteData();
}

PhotoNavigator.prototype.WriteData = function()
{
	var iCurPhoto = this.PhotoLoader.GetCurrentPhotosIndex();
	var cntPhotos = this.PhotoLoader.GetCountPhotos();	
	
	var txt = this.TEXT_TMPL.replace("{0}", cntPhotos == 0 ? 0 : iCurPhoto+1);
	txt = txt.replace("{1}", cntPhotos);
	this.SpanText.innerHTML = txt;	
	
	//Prev
	if (iCurPhoto == 0) 
	{
		this.SpanPrev.className = "navDisabled";	
		this.ImgPrev.src = "/images/nav_prev_disable.gif";
	}
	else
	{
		this.SpanPrev.className = "navEnabled";
		this.ImgPrev.src = "/images/nav_prev.gif";
	}
	
	//Next
	if (iCurPhoto >= cntPhotos - 1)
	{
		this.SpanNext.className = "navDisabled";	
		this.ImgNext.src = "/images/nav_next_disable.gif";
	}
	else 
	{
		this.SpanNext.className = "navEnabled";
		this.ImgNext.src = "/images/nav_next.gif";
	}
}

/*	=====================================================	*/
/*	END PhotoNavigator																		*/
/*	=====================================================	*/