var ap_photextsave,ap_edi_photo,edi_photocunt,edi_photoarray,edi_photostype,edi_dietexts;
var edi_photosize = Array(0,0);
var ap = new photos();
function photos(){
		var com = (!ap)?this:ap;
		//
		//点击图片后弹出大图显示
		com.edi_openmaxphoto = function (imagepath,thisimage,in_id,ifr_win,winclass){
				//生成两个层
				var divbg,divimage,bodysize,new_pimage,nbottool,hbothtml,i,edi_ptext,xhsml,imagedivs;
				ap_edi_photo = at.d("edi_photo_values_" + in_id);
				if (ap_edi_photo){
						if (zin_editor_class == true){
								document.documentElement.style.overflow = "hidden";
						}
						edi_photocunt = 0;edi_photoarray = "";edi_photoarray = Array();edi_photostype = 2;zindexoperfle = 25;
						edi_photoarray = com.edi_addphoarray(thisimage,imagepath);
						bodysize = Array(document.documentElement.clientWidth,document.documentElement.scrollHeight,document.documentElement.scrollTop);
						at.addobject("edi_pmaxbg","","edi_maxphotobg",Array(bodysize[0],bodysize[1],0,0),"",19999998,"");
						//载入图片
						if (thisimage.toLowerCase().indexOf("http://") !== -1){
								imagepath = "";
						}
						new_pimage = "<img src='" + imagepath + thisimage + "' id='new_pimage' border='0' onclick=at.url(this.src,'open') style='filter:alpha(opacity=0);opacity:0;'>";
						nbottool = Array("edi_pho_tleft","edi_pho_tmax","edi_pho_tmin","edi_pho_ttext","edi_pho_tright");
						hbothtml = "";
						for (i = 0;i < nbottool.length;i ++){
								hbothtml += "<div class='" + nbottool[i] + " edi_pho_out' onmouseover=\"ap.edi_phototools(this,'" + i + "','new_pimage')\"></div>";
						}
						//编辑备注与备注
						xhsml = "<div ";
						if (zin_editor_class == false){
								xhsml += "onmousedown=az.mobilediv(this,'edi_pmaxin',event,1,'true') ";
								imagedivs = (bodysize[1] - 530)/2;
						}else{
								xhsml += "onmousedown=at.mobilediv(this,'edi_pmaxin',event) ";
								imagedivs = 100 + bodysize[2];
								document.documentElement.style.overflow = "hidden";
						}
						xhsml += ">" + new_pimage + "</div><div class='edi_pho_bg'><div class='edi_pho_w'>" + hbothtml + "</div></div><div class='edi_cpho_out' onmouseover=ap.edi_closemaxphoto(this,'edi_pmaxbg','edi_pmaxin','edi_photo_values_" + in_id + "','" + ifr_win + "',1) ></div>";
						xhsml += "<div class='edi_pho_btext' id='new_ptextarea' style='display:none;'><div class='edi_pho_bbg'>备注 (每0.5秒自动保存)<div class='edi_ctext_out' onmouseover='at.eclass(this,0,1)' onclick='ap.edi_phototextoper()'></div></div><div><textarea class='edi_pho_bbodys' id='new_ptext_a' name='new_ptext_a'>" + edi_dietexts + "</textarea></div></div>";
						divbg = at.addobject("edi_pmaxin",xhsml,"edi_maxphoto",Array(660,530,(bodysize[0] - 670)/2,imagedivs),"",19999999);
						divbg.onmousedown = function (){
								if (zin_editor_class == false){
										az.mobilediv(this,this.id,window.event,1,'true');
								}else{
										at.mobilediv(this,this.id,window.event);
								}
						}
						//缩略图片大小
						com.edi_nextphotoin(640,480);
				}
		}
		
		//生成图片数组
		com.edi_addphoarray = function (thisi,paths){
				var aimage_value,aimage,thispaths;
				aimage_value = ap_edi_photo.value.split("‖‖");
				edi_dietexts = "";
				for (i = 0;i < aimage_value.length;i ++){
						aimage = aimage_value[i].split("^^^");
						if (aimage[0].toLowerCase().indexOf("http://") !== -1){
								thispaths = "";
						}else{
								thispaths = paths;
						}
						edi_photoarray[i] = Array(thispaths + aimage[0],aimage[1],aimage[2]);
						if (aimage[0] == thisi){
								edi_photocunt = i;
								edi_dietexts = aimage[1];
						}
				}
				return edi_photoarray;
		}
		
		//关闭放大图的层
		com.edi_closemaxphoto = function (obj,bg,bodys_s,iwindid,ifrbody_id,w_class){
				at.eclass(obj,0,1);
				obj.onclick = function (){
						zindexoperfle = 50;
						at.zindexcloseclear(at.d(bg));
						at.zindexcloseclear(at.d(bodys_s));
						//退出自动保存
						if (ap_photextsave)clearTimeout(ap_photextsave);
						//同步value值
						com.edi_inputsave_p(iwindid,ifrbody_id,w_class);
						if (zin_editor_class == true){
								document.documentElement.style.overflow = "scroll";
						}
				}
		}

		//同步两层框架中的value的值
		com.edi_inputsave_p = function (in_id,thisid,in_class){
				var pardiv,ifr_divid,ifr_obj,window_f;
				pardiv = at.d(in_id);
				if (pardiv){
						ifr_obj = in_id.split("_");
						if (in_class == 1){
								if (zin_editor_class == true){
										window_f = window;
								}else{
										window_f = window.frames[String(thisid)];
								}
						}else{
								//返回值到父窗口(非框架)
								if (os_browser.os() == "IE" || os_browser.os() == "Firefox"){
										window_f = window.dialogArguments;
								}else{
										window_f = window.opener;
								}
								//
								//编辑器模式，同时需要调整弹出窗口大小
								if (browser){
										window.dialogWidth = "700px";
										window.dialogHeight = "360px";
								}else{
										window.moveTo(0,0);
										window.resizeTo("750","420");
								}
						}
						ifr_divid = window_f.at.d("upload_more_photo_" + ifr_obj[ifr_obj.length - 1] + "_i");
						if (ifr_divid){
								ifr_divid.value = pardiv.value;
						}
				}
		}
		
		
		//打开下个图片
		com.edi_nextphotoin = function (new_w,new_h){
				var new_pimage;
				window.setTimeout(function(){
						new_pimage = at.d("new_pimage");
						edi_photosize = com.edi_photosizeauto(new_pimage,new_w,new_h);
						if (new_pimage)at.zindexopacity(new_pimage,1);
				},50);
		}
		
		com.edi_phototools = function (obj,tooltype,images){
				var photosize,imageobj,iw,imprent2,imprent3,new_pimage,new_ptext_a,vtophei,nimagesur;
				at.eclass(obj,0,1);
				tooltype = parseInt(tooltype);
				imageobj = at.d(images);
				//Array(document.documentElement.clientWidth,document.documentElement.scrollHeight,document.documentElement.scrollTop);
				photosize = Array(document.documentElement.clientWidth,document.documentElement.clientHeight,document.documentElement.scrollTop);
				imprent2 = imageobj.parentNode.parentNode;
				imprent3 = imageobj.parentNode;
				if (imageobj){
						obj.onclick = function(){
								switch(tooltype){
										//左右移(上一张或下一张）
										case 0:
										case 4:
												//先要清除
												edi_photocunt = (tooltype == 0)?edi_photocunt - 1:edi_photocunt + 1;
												if (edi_photocunt >= 0 && edi_photocunt <= edi_photoarray.length - 1){
														if (zindexoperfle_time)clearTimeout(zindexoperfle_time);
														//at.zindexcloseclear(imageobj);
														nimagesur = edi_photoarray[edi_photocunt][0];
														//alert(imprent3.innerHTML);
														//imprent3.innerHTML = "<a href='" + nimagesur + "' target='_blank'><img src='" + nimagesur + "' id='new_pimage' border='0' style='filter:alpha(opacity=0);opacity:0;'></a>";
														imprent3.innerHTML = "<img src='" + nimagesur + "' id='new_pimage' border='0' onclick=at.url(this.src,'open') style='filter:alpha(opacity=0);opacity:0;'>";
														iw = (edi_photostype == 1)?Array(photosize[0] - 20,photosize[1] - 80):Array(640,480);
														com.edi_nextphotoin(iw[0],iw[1]);
														new_ptext_a = at.d("new_ptext_a");
														if (new_ptext_a){
																new_ptext_a.value = edi_photoarray[edi_photocunt][1];
														}
												}else{
														if (edi_photocunt < 0)edi_photocunt = 0;
														if (edi_photocunt > edi_photoarray.length - 1)edi_photocunt = edi_photoarray.length - 1;
												}
										break;
										//全屏大小，初设大小 //new_pimage_close
										case 1:
										case 2:
												edi_photostype = tooltype;
												if (tooltype == 1){
														vtophei = (zin_editor_class == false)?10:photosize[2] + 10;
														iw = Array(photosize[0] - 20,photosize[1] - 30,10,(photosize[2] + 10),photosize[0] - 20,photosize[1] - 80);
												}else{
														vtophei = (photosize[1] - 530)/2 + 14;
														vtophei = (zin_editor_class == false)?vtophei:photosize[2] + vtophei;
														iw = Array(660,530,(photosize[0] - 670)/2,vtophei,640,480);
												}
												imprent2.style.width = iw[0] + "px";
												imprent2.style.height = iw[1] + "px";
												imprent2.style.left = iw[2] + "px";
												imprent2.style.top = iw[3] + "px";
												if (edi_photosize[0] > 0)imageobj.width = edi_photosize[0];
												if (edi_photosize[1] > 0)imageobj.height = edi_photosize[1];
												window.setTimeout(function(){com.edi_photosizeauto(at.d("new_pimage"),iw[4],iw[5])},50);
										break;
										//录入备注 new_ptextarea
										case 3:
												com.edi_phototextoper();
										break;
								}
						}
				}
		}
		
		//关闭打开图片的备注
		com.edi_phototextoper = function (){
				var phtext,phbodys;
				phtext = Array(at.d("new_ptextarea"),at.d("new_ptext_a"),at.d("edi_pmaxin"));
				if (phtext[0]){
						if (phtext[0].style.display == ""){
								phtext[0].style.display = "none";
								if (phtext[2]){
										phtext[2].onmousedown = function (){
												if (zin_editor_class == false){
														az.mobilediv(phtext[2],phtext[2].id,window.event,1,'true')
												}else{
														at.mobilediv(phtext[2],phtext[2].id,window.event)
												}
										}
								}
								if (ap_photextsave)clearTimeout(ap_photextsave);
						}else{
								phtext[0].style.display = "";
								if (phtext[2])phtext[2].onmousedown = "";
								com.edi_photextsave(phtext[1]);
						}
				}
		}
		
		//自动保存备注
		com.edi_photextsave = function (saveobj){
				var ophostr,opi,opnewvalue,opnew_v,opnew_value = "";
				if (saveobj){
						ophostr = ap_edi_photo.value.split("‖‖");
						for (opi = 0;opi < ophostr.length;opi ++){
								if (opi * 1 == edi_photocunt * 1){
										opnew_v = ophostr[opi].split("^^^");
										opnewvalue = opnew_v[0] + "^^^" + saveobj.value + "^^^" + opnew_v[2];
										edi_photoarray[opi][1] = saveobj.value;
								}else{
										opnewvalue = ophostr[opi];
								}
								opnew_value += ((opnew_value == "")?"":"‖‖") + opnewvalue;
						}
						ap_edi_photo.value = opnew_value;
						ap_photextsave = window.setTimeout(function(){com.edi_photextsave(saveobj)},1000);
				}
		}
		
		//等比例缩放图片
		com.edi_photosizeauto = function (obj,maxwidth,maxheight,imageid){
				var objw,objh;
				if (obj){
						objw = obj.width;
						objh = obj.height;
						if (obj.width > maxwidth){
								obj.width = maxwidth;
								obj.height = objh/objw * maxwidth;
						}
						if (obj.height > maxheight){
								obj.height = maxheight;
								obj.width = objw/objh * maxheight;
						}
						if (!imageid){
								obj.style.marginTop = ((maxheight - obj.height) / 2) + "px";
						}
						
						return Array(objw,objh);
				}
		}
		
		//
		//指定对象中的所有图片缩放
		com.edi_allphotosizeauto = function (objectid,w,h,imageid){
				var odiv,oimage,oi_i,o_images,o_img;
				odiv = at.d(objectid);
				if (odiv){
						oimage = odiv.getElementsByTagName("img");
						for (oi_i = 0;oi_i < oimage.length;oi_i ++){
								o_images = oimage[oi_i];
								//if (imageid && o_images.id.indexOf(imageid) !== -1 || !imageid){
										o_img = com.edi_photosizeauto(o_images,w,h,imageid);
										//alert(o_img[0]);
										//o_images.width = o_img[0];
										//o_images.height = o_img[1];
								//}
						}
				}
		}
		
		//
		//正常传入组
		com.list_photo = function (allimage,arr_id){
				var nphotos,nphotov;
				nphotos = "edi_photo_values_photosi_" + arr_id;
				nphotov = at.d(nphotos);
				//如果没有就增加一个
				if (!nphotov){
						nphotov = at.addobject("","<input type='hidden' name='" + nphotos + "' id='" + nphotos + "' value=\"" + allimage + "\">","");
				}else{
						nphotov.value = allimage;
				}
		}
		//弹出一个图片浏览框
		com.open_photo = function (imageurl,img_id,path){
				var imeg_url,purlall,ipurl,ipathst,ipi,ipathnew = "";
				imeg_url = com.open_photo_clear(imageurl);
				if (imeg_url == false){
						purlall = at.d("edi_photo_values_photosi_" + img_id);
						if (purlall){
								ipurl = purlall.value.split("‖‖");
								imeg_url = com.open_photo_clear(ipurl[0]);
						}
						
				}
				if (imeg_url !== false){
						if (String(path) == "/"){
								ipathst = imeg_url.split("/");
								for (ipi = 0;ipi < ipathst.length;ipi ++){
										if (ipathst[ipi] !== ".."){
												ipathnew += ((ipathnew == "")?"":"/") + ipathst[ipi];
										}
								}
								imeg_url = ipathnew;
						}
						com.edi_openmaxphoto((!path || String(path) == "/")?"../":path,imeg_url,"photosi_" + img_id,"photosi",0);
				}
		}
		//清除多余值
		com.open_photo_clear = function (imgurl){
				var n_img;
				if (imgurl && imgurl !== "" && imgurl !== "undefined"){
						if (imgurl.indexOf("^^^") !== -1){
								n_img = imgurl.split("^^^");
								return n_img[0];
						}else{
								return imgurl;
						}
				}else{
						return false;
				}
		}
		
		//
		//点击图片打开本组
		//使用获取父对象中所有的图片，并将这些图片生成组
		//需要注意的是，父对象中，需要成组的图片，其ID中，需要包括一定的字符，则其他不在组成的图片，其ID不能包括此字符
		com.open_arrlist_photo = function(obj,parentname,notphoto,parid){
				var allimg,alldiv,notimg,api,nphotos,nphotosdiv,allarrayimg = "";
				var thisimg,spimg;
				alldiv = at.d(parentname);
				nphotos = "edi_photo_values_photosi_" + parid;
				if (alldiv){
						nphotosdiv = at.d(nphotos);
						allimg = alldiv.getElementsByTagName("img");
						for (api = 0;api < allimg.length;api ++){
								//
								//是否是需要成组的图片
								if (allimg[api].id.indexOf(notphoto) !== -1){
										//
										//将图片成组
										//upload/photo/201010/qxeflO8dLF1K97uW.jpg^^^^^^1
										spimg = allimg[api].id.replace(notphoto,"");
										allarrayimg += ((allarrayimg == "")?"":"‖‖") + spimg;
										if (allimg[api] == obj){
												thisimg = spimg;
										}
								}
						}
						if (!nphotosdiv){
								if (allarrayimg !== ""){
										com.list_photo(allarrayimg,parid);
								}
						}
						//显示点击效果
						com.open_photo(thisimg,parid);
				}
		}
		
		
		
}

