﻿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='Upload' 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'>Photo formats, "+o.getAttribute("ext")+" are acceptable. Photo size should not  bigger than 2MB. After selection, you can click on 'upload'.</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)'>Delete Photo</a> <a href='#' onclick='return hide_photo(this)'>Hide Photo</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("Are you sure to delete current photo ?")){
		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)'>Delete Photo</a> <a href='#' onclick='return show_photo(this)'>Hide Photo</a></div><div>Photo preview has been hidden...<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)'>Delete Photo</a> <a href='#' onclick='return hide_photo(this)'>Hide Photo</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('Please choose a photo file!');
	var p=s.lastIndexOf('.');
	if(p==-1) return alert('Please ensure that photo contains the correct file extension!');
	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('File extension only supports '+ext+' !');
}

