﻿function init_photo(o){
	o.value=o.getAttribute("value");
	if(isNaN(o.num)) o.num=1;
	var s=[];
	if(!$("post_target")) s.push("<iframe id=post_target name=post_target frameborder=0 width=0 scrolling=no height=0></iframe>");
	s.push("<div id='divform' style='float:none'><form id=postform name=postform method=post target='post_target' action='upload_photo.ashx' style='margin:0px;padding:0px' enctype='multipart/form-data'>");
	var w=parseInt(o.getAttribute("w")),h=parseInt(o.getAttribute("h"));
	s.push("<input type=hidden id=upload_guid name=upload_guid />");
	s.push("<input type=hidden name=width value="+(w>0?w:700)+" />");
	s.push("<input type=hidden name=height value="+(h>0?h:1000)+" />");
	s.push("<input id=m_file name=m_file size=60 style='border:1px solid #7F9DB9;height:21px;width:440px' type=file contenteditable=false>&nbsp;<input type=button value='上传图片' style='width:100px;height:21px;color:red;border:1px solid #7F9DB9;font-weight:bold' onclick='process(this)' />");
	s.push("</form>");
	s.push("<div style='margin:5px 0;display:block;float:none'>图片支持 "+o.getAttribute("ext")+" 格式，大小不超过2M。选中图片后，点击<b>“上传图片”</b>。</div></div>");
	s.push("<div id='divstat' style='display:none;float:none'></div>");
	s.push("<div id='preview' style='display:block'></div>");
	o.innerHTML=s.join('');
	o.set=function(v){
		this.data=this.value=v;
		csource=this;
		var l=$$(csource,"DIV");
		for(var i=0;i<l.length;i++){ if(l[i].id!="preview") continue; l[i].innerHTML=""; break;}
		setphoto(v);
	}
	o.get=function(){return this.value==this.data?null:(this.value?this.value:"");}
	$$(o,"INPUT")[4].source=o;
}
var csource = null;
function setphoto(v){
	var l=$$(csource,"DIV");
	for(var i=0;i<l.length;i++){
		if(l[i].id!="preview") continue;
		l[i].innerHTML=v?"<div style='display:block;float:none;clear:both'><a href='#' style='color:red' onclick='return del_photo(this)'>删除图片</a> <a href='#' onclick='return hide_photo(this)'>隐藏图片</a></div><img src='"+v+"'>":"";
		l[i].source=csource;
		csource.value=v?v:void(0);
		$$(csource,"INPUT")[3].outerHTML="<input id=m_file name=m_file size=60 style='border:1px solid #7F9DB9;height:21px;width:440px' type=file contenteditable=false>";
		break;
	}
}
function del_photo(a){
	var p=F.top(F.top(a,"DIV"),"DIV"),o=p.source;
	if(o.value){
		if(confirm("您确定要删除图片吗？")){
		o.value="";
		p.innerHTML="";
	}}
	return false;
}
function hide_photo(a){
	a.blur();
	var p=F.top(F.top(a,"DIV"),"DIV");
	p.innerHTML="<div style='display:block;float:none;clear:both'><a href='#' style='color:red' onclick='return del_photo(this)'>删除图片</a> <a href='#' onclick='return show_photo(this)'>显示图片</a></div><div>图片预览已经被隐藏了...<div>";
	return false;
}
function show_photo(a){
	a.blur();
	var p=F.top(F.top(a,"DIV"),"DIV"),v=p.source.value;
	p.innerHTML=v?"<div style='display:block;float:none;clear:both'><a href='#' style='color:red' onclick='return del_photo(this)'>删除图片</a> <a href='#' onclick='return hide_photo(this)'>隐藏图片</a></div><img src='"+v+"'>":"";
	return false;
}
function process(src){
	csource=src.source;
	if(check($$(csource,"INPUT")[3])!=true) return;
	var id=Date.parse(new Date());
	$$(csource,"INPUT")[0].value=id;
	$$(csource,"FORM")[0].submit();
}
function abort(s){$("post_target").src="";alert(s);}
function check(o){
	var s=o.value;
	if(!s) return alert('请您选择一个上传的文件！');
	var p=s.lastIndexOf('.');
	if(p==-1) return alert('请您保证输入的文件有正确的后缀！');
	var e=s.substring(p+1).toLowerCase(),ext=csource.getAttribute("ext"),list=ext.split(',');
	for(var i=0;i<list.length;i++)if(e==list[i].toLowerCase()) return true;
	return alert('文件后缀必须是'+ext+'！');
}

