var inits = [];
function o(obj){
    return (typeof(obj) == "string") ? document.getElementById(obj) : obj;
}

function runInits(){
    for (var i = 0; i < inits.length; i++) {
        inits[i].run();
    }
}

function redirect(o){
    window.location.href = o.options[o.selectedIndex].value;
}

function redirectTo(link){
    window.location.href = link;
}

function htmlConfirm(ask, link, ok, cancel){
    htmlConfirmFalse();
    var template = ask + '<input onclick="redirectTo(\'' + link + '\');" type="button" value="' + ok + '"/><input onclick="htmlConfirmFalse();" type="button" value="' + cancel + '"/>';
    var confirm = o("confirm");
    confirm.innerHTML = template;
    confirm.style.display = "block";
}

function htmlConfirm2(ask, script, ok, cancel){
    htmlConfirmFalse();
    var template = ask + '<input onclick="' + script + '" type="button" value="    ' + ok + '    "/><input onclick="htmlConfirmFalse();" type="button" value="    ' + cancel + '    "/>';
    var confirm = o("confirm");
    confirm.innerHTML = template;
    confirm.style.display = "block";
}

function htmlConfirmFalse(){
    var confirm = o("confirm");
    confirm.innerHTML = "";
    confirm.style.display = "none";
}

function inplaceConfirm(id, ask, link, ok, cancel){
    var template = ask + '<a href="' + link + '">' + ok + '</a> <a href="javascript:inplaceConfirmFalse(\'' + id + '\');">' + cancel + '</a>';
    var confirm = o(id + "_confirm");
    confirm.innerHTML = template;
    confirm.style.display = "";
    o(id).style.display = "none";
}

function inplaceConfirmFalse(id){
    o(id + "_confirm").style.display = "none";
    o(id).style.display = "";
}

function addToInits(runimpl){
    inits.push(runimpl);
}

window.onload = runInits;

function showHide(id){
    var obj = o(id);
    if (obj.style.display != "block") 
        obj.style.display = "block";
    else 
        obj.style.display = "none";
}

function selectAll(obj, name){
    var tar = document.getElementsByName(name);
    for (var i = 0; i < tar.length; i++) {
        tar[i].checked = obj.checked;
    }
}

Attach = {
    count: 0,
    files: [],
    render: function(id){
        var container = o(id);
        container.innerHTML = '<a id="init" href="javascript:Attach.init();">添加附件</a><div id="files"></div><a id="add" style="display:none;" href="javascript:Attach.add();">再添加一个文件</a>';
    },
    init: function(){
        o("init").style.display = "none";
        o("add").style.display = "";
        this.add();
    },
    add: function(){
        var filesObject = o("files");
        var file = document.createElement("div");
        index = parseInt(this.files.length) + 1;
        file.id = "file" + index;
        file.innerHTML = "<input type='file' name='files'/><a href='javascript:Attach.del(" + index + ")'>删除</a>";
        filesObject.appendChild(file);
        this.files.push(file.id);
        this.count++;
        //file.firstChild.click(); //safari ok, ff & ie not ok
    },
    del: function(index){
        var filesObject = o("files");
        filesObject.removeChild(o('file' + index));
        this.count--;
        if (this.count == 0) 
            this.destroy();
    },
    destroy: function(){
        o("init").style.display = "";
        o("add").style.display = "none";
    }
}
Attach2 = {
    count: 0,
    files: [],
    render: function(id){
        var container = o(id);
        container.innerHTML = '<a id="init" href="javascript:Attach2.init();">添加附件</a><div id="files"></div><a id="add" style="display:none;" href="javascript:Attach2.add();">再添加一个文件</a>';
    },
    init: function(){
        o("init").style.display = "none";
        o("add").style.display = "";
        this.add();
    },
    add: function(){
        var filesObject = o("files");
        var file = document.createElement("div");
        index = parseInt(this.files.length) + 1;
        file.id = "file" + index;
        file.innerHTML = "<div class='fileitem'><p>选择文件: <input type='file' name='files'/>&nbsp;</p><p>修改名称: <input type='text' name='filesNewFileName' value=''/></p><p>添加说明: <input type='text' name='filesDescription'/>&nbsp;&nbsp;<a href='javascript:Attach2.del(" + index + ")'>删除</a></p></div>";
        filesObject.appendChild(file);
        this.files.push(file.id);
        this.count++;
        //file.firstChild.click(); //safari ok, ff & ie not ok
    },
    del: function(index){
        var filesObject = o("files");
        filesObject.removeChild(o('file' + index));
        this.count--;
        if (this.count == 0) 
            this.destroy();
    },
    destroy: function(){
        o("init").style.display = "";
        o("add").style.display = "none";
    }
}

function processLogin()
{
	var hash=window.location.hash;
	if(hash.startsWith("#login"))
	{
		var u="";
		if(hash.indexOf("#login,u=")!=-1)
			u = hash.substring("#login,u=".length);
		showPop(u);
	}
}

function G(obj){
	return (typeof(obj)=="string" ? document.getElementById(obj) : obj);
}

function g(id){
    var ele;
    if (typeof(id) == "string") 
        ele = document.getElementById(id);
    return ele;
}

function checkSearch(obj){
    if (g(obj).value == "") 
        return false;
    return true;
}

function showPhotoPop(obj){
	if (!$("photo-pop")) {
		var wrapper = document.createElement("div");
		wrapper.innerHTML = '<div class="mask">&nbsp;</div> \
		<div class="pop"> \
			<a href=""><img src="' + Site.imgBase + '/icon_add.gif"/></a> <a href=""><img src="' + Site.imgBase + '/icon_sendmsg.gif"/></a> \
		</div> \
		</div>';
		wrapper.id = "photo-pop";
		wrapper.className="photo-pop";
		obj.appendChild(wrapper);
	}
}

function hidePhotoPop(obj){
	alert(obj);
	if ($("photo-pop"))
	obj.removeChild($("photo-pop"));
}

String.prototype.wbr = function(step){
	var ostr = this.replace(/<!\-\-([\S\s]*)\-\->/gi,"").replace(/[\t\f\r\n]/gi,"");
	function w(str,step){
    	var t = "";str = str.replace(/&nbsp;/g ," ").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");
    	for(var i=0;i<str.length;i++) if( i % step == 0) t += str.charAt(i)+"<wbr/>"; else t+=str.charAt(i);
    	return t;
    };
    
    function clearNobr(str){
    	var _tmp = [];
    	while(/(<nobr>((?!<\/nobr>).)+<\/nobr>)/mi.test(str)){
	      _tmp.push(RegExp.$1);
	      str=str.replace(RegExp.$1, "\x0f\x0e"+ (_tmp.length-1) +"\x0f");
	    }
	    for(var i=0; i<_tmp.length; i++){
	      str=str.replace("\x0f\x0e"+i+"\x0f", _tmp[i].replace(/<wbr\/>/g,""));
	    }
	    return str;
    }
    
    var flag=-1,badPoint=-1,open=true,temp="",result="",tag="";
    for(var n=0;n<ostr.length;n++){
    	var ch = ostr.charAt(n);
    	if(ch == "<"){
    		flag = n;
    		open = false;
    		if(!open && badPoint<0) badPoint = flag;
    	}else if(ch == ">" && flag != -1){
    			if(flag > badPoint) temp+=(ostr.substring(badPoint,flag-badPoint));
    			result += w(temp,step)+"<"+tag+">";
    			open = true; badPoint=-1; temp = ""; tag="";flag=-1;
    	}else if(open) temp += ch;else if(!open) tag += ch;
    }
    if(temp!="") result += w(temp,step);
    if(!open) result += w(ostr.substring(badPoint,ostr.length-badPoint+1),step);
    return  clearNobr(result);
/*
	var len = step;
	
	if(parseInt(len)<3) len = 3;
		var reg = new RegExp("([\u0021-\u0451\u2010-\u33D5\uFE30-\uFFE5]{"+ len +"})","g");
		var str = this.replace(/<!\-\-([\S\s]*?)\-\->/g,"").replace(/\r\t\f\n/g,"");
	    if(str.length==0) return str; var _tmp=[];
	    while(/(<nobr>((?!<\/nobr>).)+<\/nobr>|<\/?[a-z]+[^>]*>|&[a-z]+;)/mi.test(str)){
	      _tmp.push(RegExp.$1);
	      str=str.replace(RegExp.$1, "\x0f\x0e"+ (_tmp.length-1) +"\x0f");
	    }
	    str=str.replace(reg,"$1<wbr/>");
	    for(var i=0; i<_tmp.length; i++){
	      str=str.replace("\x0f\x0e"+i+"\x0f", _tmp[i]);
	    }
	    return str;
	    */
}

function addWbr(holder,step){
	if(!G(holder)) return;
	G(holder).innerHTML = G(holder).innerHTML.wbr(step);
}
function check(id){
	var query = G(id).value;
	if(query && query!="") window.location.href=G(id).form.action+"?query="+encodeURIComponent(encodeURIComponent(query));
	return false;
}
function showPop(dest){		
	var body = document.getElementsByTagName("body")[0];
	var noscroll = ($('_noscroll') ? $('_noscroll') :document.createElement("div"));
	noscroll.className="noscroll";
	noscroll.id="_noscroll";
	noscroll.appendChild($('container'));
	body.appendChild(noscroll);
	$('m').appear({duration:0.1,to:0.8});
	$('t').show();
	if(dest!=undefined)
		$('u').value=dest;
}
function hidePop(){
	var body = document.getElementsByTagName("body")[0];
	body.appendChild($('container'));
	$('_noscroll') ? body.removeChild($('_noscroll')) :"";
	$('m').fade({duration:0.1,from:0.8});
	$('t').hide();
}

function search(id, prefix){
	G(id).form.action = prefix;
}

function mainPageSearch(prefix){
	search("multiple-search-input",prefix);
}