function LoadPhoto() {}

LoadPhoto.divIDList = null;
LoadPhoto.seqnoList	= null;

LoadPhoto.divIDs = null;
LoadPhoto.imageURLs = null;
LoadPhoto.comments = null;
LoadPhoto.tuyas = null;

LoadPhoto.seqnoes = null;

LoadPhoto.StartLoadPhotoStyle_1 = function()
{
	var loadImageInfoDiv = document.getElementById("load_image_info");
	
	if (loadImageInfoDiv == null)
		return;
	
	var values = loadImageInfoDiv.innerHTML.split ("|");
	
	if (values.length <= 1)
		return;
	
	LoadPhoto.divIDs = values[0].split (",");
	LoadPhoto.imageURLs = values[1].split (",");
	LoadPhoto.comments = values[2].split (",");
	
	if(values[3] != null && values[3] != "")
	  LoadPhoto.tuyas = values[3].split (",");
	else
	{
	  LoadPhoto.tuyas = [];
	  for(var i=0; i<LoadPhoto.divIDs.length;i++)
	    LoadPhoto.tuyas.push("0");
	}
	
	LoadPhoto.loadPhotoByOrder (0);
}

LoadPhoto.loadPhotoByOrder = function (index)
{
  if (index >= LoadPhoto.divIDs.length)
     return;
  
  LoadPhoto.loadSinglePhoto (LoadPhoto.divIDs[index], LoadPhoto.imageURLs[index], LoadPhoto.comments[index], LoadPhoto.tuyas[index]);
  
  setTimeout ("LoadPhoto.loadPhotoByOrder(" + ++index + ")", 50);
}

LoadPhoto.loadSinglePhoto = function(divID, imageURL, commented_times, tuya_times)
{
  var photoDiv = document.getElementById (divID);
  
  if (photoDiv == null)
    return;
  
  if(commented_times == null)
    commented_times = 0;
    
  if(tuya_times == null)
    tuya_times = 0;
    
	var img	= new Image ();
	img.border = 0;
	
	photoDiv.innerHTML = "";
	img.setAttribute("containerID",divID);
	img.setAttribute("commented_times", commented_times);
	img.setAttribute("tuya_times", tuya_times);
	img.setAttribute("isloaded",false);
	
	img.onload = function ()
	{
		if(this.getAttribute("isloaded") == false)
		{
			var photoDiv = document.getElementById (this.getAttribute("containerID"));
			
			photoDiv.style.position = "relative";
			/*
			var kdiv = document.createElement ("div");
			
			if(photoDiv.onclick != null || photoDiv.style.cursor == "hand" || photoDiv.style.cursor == "pointer" || (photoDiv.tagName == "A" && photoDiv.href != null))
			  kdiv.style.cursor = "pointer";
			
			kdiv.style.position = "absolute";
			kdiv.style.filter = "alpha(opacity=0)";
			kdiv.style.backgroundColor = "white";
			kdiv.style.top = 0;
			kdiv.style.left = 0;
			kdiv.style.width = this.width;
			kdiv.style.height= this.height;
			
			photoDiv.appendChild (kdiv);
			*/
			if (photoDiv.tagName == "TD")
			{
				photoDiv.width = this.width;
				photoDiv.height= this.height;
			}
			else if (photoDiv.tagName == "DIV")
			{
				photoDiv.style.width = this.width;
				photoDiv.style.height= this.height;
			}
			else if (photoDiv.tagName == "A")
			{
				photoDiv.style.width = this.width;
				photoDiv.style.height= this.height;
			}
			
			if(this.getAttribute("commented_times") > 0)
			{
				var comm_img = new Image ();
				comm_img.id = this.getAttribute("containerID")+"_comment";
				comm_img.style.position = "absolute";
				comm_img.border = 0;
				comm_img.style.width = 15;
				comm_img.style.height = 15;
				comm_img.style.top = parseInt(this.height) - 18;
				comm_img.style.left = 3;
				comm_img.title = "评论数("+this.getAttribute("commented_times")+")";
				photoDiv.appendChild (comm_img);
				
				comm_img.src = "/v1.0/images/image_1110/icon_has_comment.gif";
			}
		  
		  if(this.getAttribute("tuya_times") > 0)
			{
				var tuya_img = new Image ();
				tuya_img.id = this.getAttribute("containerID")+"_tuya";
				tuya_img.style.position = "absolute";
				tuya_img.border = 0;
				tuya_img.style.width = 15;
				tuya_img.style.height = 15;
				tuya_img.style.top = parseInt(this.height) - 18;
				tuya_img.style.left = 3;
				
				if(this.getAttribute("commented_times") > 0)
				  tuya_img.style.left = 21;
				  
				tuya_img.title = "有涂鸦("+this.getAttribute("tuya_times")+"次)";
				photoDiv.appendChild (tuya_img);
				
				tuya_img.src = "/v1.0/images/image_1110/icon_has_draw.gif";
			}
		  
			this.setAttribute("isloaded", true);
		}
	}
	
	photoDiv.appendChild (img);
	
	img.src = imageURL;
	
	return false;
}

LoadPhoto.albumPhotoDivIDs = null;
LoadPhoto.albumPhotoURLs = null;
LoadPhoto.albumComments = null;

LoadPhoto.loadAlbumPhoto = function()
{
  if(UI.albumPhotoDivIDs == null || UI.albumPhotoDivIDs == "")
    return ;
  
	LoadPhoto.albumPhotoDivIDs = UI.albumPhotoDivIDs.split (",");
  LoadPhoto.albumPhotoURLs = UI.albumPhotoURLs.split (",");
  LoadPhoto.albumComments = UI.albumComments.split (",");
	
	LoadPhoto.loadAlbumPhotoByOrder (0);
}

LoadPhoto.loadAlbumPhotoByOrder = function (index)
{
  if (index >= LoadPhoto.albumPhotoDivIDs.length)
     return;
  
  LoadPhoto.loadSinglePhoto (LoadPhoto.albumPhotoDivIDs[index], LoadPhoto.albumPhotoURLs[index], LoadPhoto.albumComments[index]);
  
  setTimeout ("LoadPhoto.loadAlbumPhotoByOrder(" + ++index + ")", 50);
}


LoadPhoto.loadCoolAlbumList = function()
{
	var loadImageInfoDiv = document.all["load_image_info"];
	
	if (loadImageInfoDiv == null)
		return;
	
	var values = loadImageInfoDiv.innerHTML.split ("|");
	
	if (values.length <= 1)
		return;
	
	LoadPhoto.divIDList = values[0];
	LoadPhoto.seqnoList = values[3];
	LoadPhoto.divIDs = values[0].split (",");
	LoadPhoto.imageURLs = values[1].split (",");
	LoadPhoto.comments = values[2].split (",");
	LoadPhoto.seqnoes = values[3].split (",");
	
	LoadPhoto.loadCoolAlbumListByOrder (0);
}

LoadPhoto.loadCoolAlbumListByOrder = function(index)
{
  if (index >= LoadPhoto.divIDs.length)
  {
  	//var u = "/v1.0/getTuyaXML.jsp?qzphoto_seqno=" + this.getAttribute("photo_id");
 		//Ajax.sendGetRequest(u, true, null, "TuyaBook.createViewer(document.getElementById('tuya_" + this.getAttribute("containerID") + "'), document.getElementById('" + this.getAttribute("containerID") + "'), Ajax.http_request.responseText)");
 		var u = "/v1.0/getTuyaXMLList.jsp?qzphoto_seqno_list=" + LoadPhoto.seqnoList + "&UI_ID_list=" + LoadPhoto.divIDList;
 		Ajax.sendGetRequest (u, true, null, "LoadPhoto.showTuyaList(Ajax.http_request.responseText)");
  	return;
  }
  
  LoadPhoto.loadCoolAlbumListPhoto (LoadPhoto.divIDs[index], LoadPhoto.imageURLs[index], LoadPhoto.comments[index], LoadPhoto.seqnoes[index]);
  
  setTimeout ("LoadPhoto.loadCoolAlbumListByOrder(" + ++index + ")", 50);
}

LoadPhoto.loadCoolAlbumListPhoto = function(divID, imageURL, commented_times, photo_id)
{
  var photoDiv = document.getElementById (divID);
  
  if (!photoDiv || !photo_id)
    return;
  
  photoDiv.style.zIndex = 1;
  
	var img	= new Image ();
	img.border = 0;
	
	photoDiv.innerHTML = "";
	photoDiv.appendChild (img);
	img.setAttribute("containerID", divID);
	img.setAttribute("commented_times", commented_times);
	img.setAttribute("isloaded", false);
	
	if(photo_id != null && photo_id != "")
	{ 
	  img.setAttribute("photo_id", photo_id);
	  img.style.cursor = "hand";
	  img.onclick = function()
  	{
  	  CoolAlbum.transPageStyle("photo",this.photo_id,null,600); 
  	}
	}
	
	img.onload = function ()
	{
		if(this.getAttribute("isloaded") == false)
		{
			var photoDiv = document.getElementById(this.getAttribute("containerID"));
			photoDiv.style.position = "relative";
			/*
			var kdiv = document.createElement ("div");
			
			kdiv.style.position = "absolute";
			kdiv.style.zIndex = 25;
			kdiv.style.filter = "alpha(opacity=0)";
			kdiv.style.backgroundColor = "white";
			kdiv.style.top = 0;
			kdiv.style.left = 0;
			kdiv.style.width = this.width;
			kdiv.style.height= this.height;
			
			//photoDiv.appendChild (kdiv);
			*/
			if (photoDiv.tagName == "TD")
			{
				photoDiv.width = this.width;
				photoDiv.height= this.height;
			}
			else if (photoDiv.tagName == "DIV")
			{
				photoDiv.style.width = this.width;
				photoDiv.style.height= this.height;
			}
			else if (photoDiv.tagName == "A")
			{
				photoDiv.style.width = this.width;
				photoDiv.style.height= this.height;
			}
			
			if(this.getAttribute("commented_times") > 0)
			{
				photoDiv.style.position = "relative";
				var comm_img = new Image ();
				comm_img.id = this.getAttribute("containerID")+"_comment";
				comm_img.style.position = "absolute";
				comm_img.style.zIndex = 30;
				comm_img.border = 0;
				comm_img.style.width = 15;
				comm_img.style.height = 15;
				comm_img.style.top = parseInt(this.height) - 18;
				comm_img.style.left = 3;
				comm_img.title = "评论数("+this.getAttribute("commented_times")+")";
				photoDiv.appendChild (comm_img);
				
				comm_img.src = "/v1.0/images/image_1110/icon_has_comment.gif";
			}
		  
			this.setAttribute("isloaded", true);
		}
	}
			
	img.src = imageURL;
	
	return false;
}

LoadPhoto.showTuya = function (tuya_xmlcontent, divID)
{
	TuyaBook.createViewer (null, divID, tuya_xmlcontent);
}

LoadPhoto.showTuyaList = function (tuya_xmlcontent_list)
{
	var xmlcontents = tuya_xmlcontent_list.split ("----------");
	
	for (var i = 0; LoadPhoto.divIDs && i < LoadPhoto.divIDs.length; i++)
		TuyaBook.createViewer ('tuya_' + LoadPhoto.divIDs[i], LoadPhoto.divIDs[i], xmlcontents[i]);
}

LoadPhoto.createTuya = function (photo_ID,photo_UI_ID)
{
	var u = '/v1.0/getTuyaXML.jsp?qzphoto_seqno=' + photo_ID;
	Ajax.sendGetRequest(u, true, null, "TuyaBook.createEditor('tuya_" + photo_UI_ID + "','" + photo_UI_ID + "',Ajax.http_request.responseText)");
}

/*
  date   : 2007-03-27;
  author : beer wing;
*/
LoadPhoto.photoDivIDs = null;
LoadPhoto.photoURLs = null;
LoadPhoto.photoComments = null;
LoadPhoto.photoTuyas = null;

LoadPhoto.loadPhotoOfDivID = function(DivID)
{
  var loadImageInfoDiv = document.getElementById(DivID);
	
	if (loadImageInfoDiv == null)
		return;
	
	var values = loadImageInfoDiv.innerHTML.split ("|");
	
	if (values.length <= 1)
		return;
	
	LoadPhoto.photoDivIDs = values[0].split (",");
	LoadPhoto.photoURLs = values[1].split (",");
	LoadPhoto.photoComments = values[2].split (",");
	
	if(values[3] != null && values[3] != "")
	  LoadPhoto.photoTuyas = values[3].split (",");
	else
	{
	  LoadPhoto.photoTuyas = [];
	  
	  for(var i=0; i<LoadPhoto.photoDivIDs.length; i++)
	    LoadPhoto.photoTuyas.push("0");
	}
	
	LoadPhoto.loadPhotoOfDivIDByOrder (0);
}

LoadPhoto.loadPhotoOfDivIDByOrder = function (index)
{
  if (index >= LoadPhoto.photoDivIDs.length)
     return;
  
  LoadPhoto.loadSinglePhoto (LoadPhoto.photoDivIDs[index], LoadPhoto.photoURLs[index], LoadPhoto.photoComments[index],LoadPhoto.photoTuyas[index]);
  
  setTimeout ("LoadPhoto.loadPhotoOfDivIDByOrder(" + ++index + ")", 50);
}

LoadPhoto.smallImageIDs  = null;
LoadPhoto.smallImageUrls = null;

LoadPhoto.loadWatchSmallImage = function(smallImagesID,smallImagesUrl)
{
  LoadPhoto.smallImageIDs  = smallImagesID.split(",");
  LoadPhoto.smallImageUrls = smallImagesUrl.split(",");
  
  LoadPhoto.loadWatchSmallImageByOrder(0);
}

LoadPhoto.loadWatchSmallImageByOrder = function (index)
{
  if (index >= LoadPhoto.smallImageIDs.length)
     return;
  
  LoadPhoto.loadSingleSmallImage (LoadPhoto.smallImageIDs[index], LoadPhoto.smallImageUrls[index]);
  
  setTimeout ("LoadPhoto.loadWatchSmallImageByOrder(" + ++index + ")", 50);
}

LoadPhoto.loadSingleSmallImage = function(imageID,imageURL)
{
  if(imageID == null || imageID == "")
      return ;
  
  if(imageURL == null || imageURL == "")
      return ;
  
  var smallImageA = document.getElementById (imageID);
  
  if(smallImageA)
  {
    smallImageA.innerHTML = "<img src='"+imageURL+"' width=40 height=40 border=0>";
    smallImageA.style.position = "relative";
    /*
    var kdiv = document.createElement ("div");
		
		kdiv.style.cursor = "hand";
		
		kdiv.style.position = "absolute";
		kdiv.style.filter = "alpha(opacity=0)";
		kdiv.style.backgroundColor = "white";
		kdiv.style.top = 0;
		kdiv.style.left = 0;
		kdiv.style.width = 40;
		kdiv.style.height= 40;
		
		smallImageA.appendChild (kdiv);
		*/
  }
}

LoadPhoto.preLoadPhotoList = null;

LoadPhoto.preLoadPhoto = function()
{
	if (LoadPhoto.preLoadPhotoList == null)
		return;
	
	if (LoadPhoto.preLoadPhotoList.length <= 1)
		return;
	
	LoadPhoto.preLoadPhotoByOrder (0);
}

LoadPhoto.preLoadPhotoByOrder = function (index)
{
  if (index >= LoadPhoto.preLoadPhotoList.length)
     return;
  
  if(LoadPhoto.preLoadPhotoList[index] != null)
    LoadPhoto.loadSinglePhoto (LoadPhoto.preLoadPhotoList[index].containerID, LoadPhoto.preLoadPhotoList[index].imageURL, LoadPhoto.preLoadPhotoList[index].commented_count, LoadPhoto.preLoadPhotoList[index].tuya_count);
  
  setTimeout ("LoadPhoto.preLoadPhotoByOrder(" + ++index + ")", 50);
}


function UIAction() {}

UIAction.favoritePhoto = function(photo_id)
{
	Ajax.sendGetRequest("/v1.0/userEditAction.jsp?action=addFavorite&link_type=qzphoto&link_ID="+photo_id, true, null, "UIAction.callBackFavoritePhoto('"+photo_id+"')");
	return false;
}

UIAction.callBackFavoritePhoto = function(photo_id)
{
	var value = Ajax.http_request.responseText;
	if(value != null && value != "" && value.indexOf("true") != -1)
	{
		try
		{
			if(viewer != null)
			{
				var n = viewer.getArrayCurrentNo(photo_id);
				var favorited_times = parseInt(viewer.allPhotoList[n].favorited_times)+1;
				viewer.allPhotoList[n].favorited_times = favorited_times;
				var favorited = "(<a href='/v1.0/qzuserList.jsp?link_mode=favorite&link_type=qzphoto&link_ID="+viewer.allPhotoList[n].id+"' target='_blank'>"+favorited_times+"</a>)";
				document.all["coolalbum_photo_sc_user_times"].innerHTML = favorited;
			}
		}
		catch(e) {}
		
		
		alert("照片加入收藏成功。");
	}
	else if(value != null)
		alert("这张照片之前已收藏。");
}

UIAction.deletePhoto = function(photo_id,album_id)
{
	if(confirm("您确认要求删除该照片？"))
		Ajax.sendGetRequest("/v1.0/albumEditAction_realtime.jsp?action=deletePhoto&qzphoto_seqno_list="+photo_id+"&qzalbum_seqno="+album_id, true, null, "UIAction.callBackDeletePhoto("+photo_id+")");
	
	return false;
}

UIAction.callBackDeletePhoto = function(photo_id)
{
	var xmlContent = Ajax.http_request.responseText;
	Ajax.parseXML (xmlContent);
	var root = Ajax.getXMLRootNode ();
	var is_OK = Ajax.getXMLSingleNodeValue (root, "is_OK");
	
	if(is_OK == "true")
	{
		try
		{
			if(CoolAlbum.pageStyles)
			{
				var style = CoolAlbum.pageStyles[0];
				var l = CoolAlbum.albumInfo.photoList.length;
				
				for(var i = 0; i < l; i++)
				{
					var photo = CoolAlbum.albumInfo.photoList.pop();
					
					if(photo.id == photo_id)
					{
						i--;
						l--;
					}
					else 
						CoolAlbum.albumInfo.photoList.unshift (photo);
				}
				
				if(l == 0)
				{
					location.href = '/v1.0/myhome.jsp';
					return ;
				}
				
				for(var i = 0; i < l; i++)
					CoolAlbum.albumInfo.photoList[i].arrayNo = i;
				
				if(style == "photo")
				{
					if(AlbumWatch.currentPhotoArrayNo == l) 
						AlbumWatch.currentPhotoArrayNo--;
					
					AlbumWatch.refreshAlbumWatch();
					
					var currSmallImage = document.getElementById ("smallImage_"+AlbumWatch.photoList[AlbumWatch.currentPhotoArrayNo].id);
				  
				  if(currSmallImage)
				  {
				    currSmallImage.style.borderWidth = 3;
				    AlbumWatch.loadBigImage();
				  }
				}
				else if(style == "list")
				{
					document.getElementById ("coolalbum_list_framework_"+photo_id).style.display = "none";
				}
				else if(style == "album")
				{
					document.getElementById ("qzphoto_td_"+photo_id).innerHTML = "该相片已删除";
				}
			}
		}
		catch(e)
		{
			var photoFrameWork = document.getElementsByName("qzphoto_"+photo_id+"_framework");
			
			for(var i =0; i < photoFrameWork.length; i++)
			{
				photoFrameWork[i].innerHTML = "相片已删除";
			}
		}
	}
	else
		alert(Ajax.getXMLSingleNodeValue (root, "error_message"));
}

UIAction.rotateImage = function (authorID,photoID, file_name, orient)
{
	//orient:1,2,3,-1
	var deg = 90*orient;
	
	if (file_name.indexOf ("?") > 0)
		file_name = file_name.substring (0, file_name.indexOf ("?"));
	
	var url = escape ("http://image.51qiezi.com/upload/photoMagick_ajax.jsp?action=rotate&qzuser_seqno=" + authorID + "&name=" + file_name + "&rotate=" + deg + "&photoSeqno=" + photoID);
	Ajax.sendGetRequest ("ajaxProxy.jsp?target_URL=" + url, true, null, 'alert("照片旋转OK.")');
}

UIAction.setAsCover = function (photoID, albumID)
{
	Ajax.sendGetRequest ("/v1.0/albumEditAction.jsp?action=setHeadPhoto&type=qzalbum&ID=" + albumID + "&update_fields_list=qzphoto_seqno&qzphoto_seqno=" + photoID, true, null, 'alert("设置封面完成!")');
}

UIAction.recommendPhotoToSelf = function (photoID)
{
  return UIAction.recommendToSelf(photoID,'qzphoto');
}

UIAction.recommendToSelf = function (linkID,linkType)
{
  if(confirm("您要把它放到您的个人主页吗?"))
  {
    Ajax.sendGetRequest ("/v1.0/ajax/photo/photoEditAction_ajax.jsp?qzaction=recommendToSelf&link_ID="+linkID+"&link_type="+linkType, true, null, "UIAction.callBackRecommendToSelf()");
  }
  
  return false;
}

UIAction.callBackRecommendToSelf = function()
{
  var value = Ajax.http_request.responseText;
  
  if(value.indexOf("true") != -1)
	{
		alert("推荐成功了。");
	}
	else
		alert(value.split("|")[2]);
}