/**
 * Copyright 2006-2008  Mipang.com.
 * All Rights Reserved.
 **/


/**  */
if(!window.__base_uid)
    window.__base_uid = 1000;

/** task list Class */
function TaskList()
{
    this.init();
}
TaskList.prototype.init=function()
{
    this.list = {};
};
TaskList.prototype.Add=function(name,call,CBparams)
{
    var list = this.list;
    if(!list[name]){
	var l = new Object();
	l.stack = [];
	l.cursor = 0;
	list[name]=l;
    }
    var L = list[name];
    L.stack[L.stack.length] = [call,CBparams];
};
TaskList.prototype.Run=function(name)
{
    var list=this.list;

    if(!list[name])
	return;
    var l = list[name];
    if(l.cursor+1 <= l.stack.length){
	l.stack[l.cursor][0]( l.stack[l.cursor][1] );
	l.cursor++;
    }
    if(l.cursor>=l.stack.length){
	try{
	    list[name] = null;
	    delete list[name];
	}catch(e){}
    }
};

/** Tip Class */
function Tip(){}
Tip.prototype.ReLocation=function(){
    var div = GetElement(window,'tip');
    var s=div.style;
    s.top = GetScrollTop(window)+3+'px';
    s.right = '3px';
};
Tip.prototype.Show=function(html)
{
    var h=[''];
    h.push('<div class="bgRed" style="background:#cc0000;color:#fff;font:normal 13px Arial;padding:2px 3px;">'+html+'</div>');

    var div = document.getElementById('tip');
    if(!div){
	div = document.createElement('div');
	div.id = 'tip';

	div.setAttribute('style','');
	var ss = div.style;
	ss.position = 'absolute';
	ss.display = 'none';
	ss.top = '-10000px';

	var p = document.body;
	if(p.hasChildNodes()){
	    p.insertBefore(div,p.firstChild);
	}else{
	    p.appendChild(div);
	}
    }


    if(!div.style) div.setAttribute('style','');
    var s = div.style;
    
    s.position = 'absolute';
    div.innerHTML = h.join('');

    ShowElement(div,true);

    /**s.width = '250px';*/
    s.zIndex=20000;
    this.ReLocation();


    var _this = this;
    var sm = function(){
	_this.ReLocation();
    };
    
    var E = new Events();
     E.Attach(window,'scroll',sm);

};

Tip.prototype.Hide=function()
{
    try{
	ShowElement(GetElement(window,'tip'),false);
    }catch(e){}
};


/** **** */

/** Labels Class */
function Labels()
{
    this.labels=[];
    this.fields={id:0,name:1,permission:2,count:3,unreads:4,apply:5};
    this.AdminUrl = '/labels/labels_admin_xml.ue';
    this.Tip = new Tip();
}
/** Get Labels 
    NOT SUPPORT TASKLIST */
Labels.prototype.GetLabels=function(params)
{
    /**
       params{
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function([labels])
       }
     */

    if(typeof params != 'object')
	return;
    
    var LB = this;

    this.Tip.Show('加载数据<b>...</b>');

    /**args.*/
    var _params = {
	action:'getLabels',
	reltype:params.reltype,
	relid:params.relid
    };

    /**set callback*/
    var AJ = {};
    AJ.GetLabels_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	LB.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/
	/** clear first*/
	LB.labels.length=0;
	
	var f=LB.fields;

	var doc = rsXML.documentElement;
	var labels = doc.getElementsByTagName('label');
	if(labels && labels.length){
	    for(var i=0;i<labels.length;i++){
		var l=labels[i];
		
		var L=[];
		L[f.id]=l.getAttribute('id');
		L[f.name]=getNodeValue(l);
		L[f.permission]=l.getAttribute('permission');
		L[f.count]=l.getAttribute('count');
		L[f.unreads]=l.getAttribute('unreads');
		L[f.apply]=l.getAttribute('apply');
		    
		LB.labels[LB.labels.length] = L;
	    }
	}
	    

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(LB.labels);
	}

    };

    MyAPI.callMethod('GetLabels',_params,AJ,{params:params},this.AdminUrl);

};

/** Apply Labels */
Labels.prototype.Apply=function(params)
{
    /**
       params{
       labels:[labels]
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function([labels])
       }
     */

    if(typeof params != 'object')
	return;

    var labels=params.labels;
    if(!labels || !labels.length)
	return;
    
    var T = this;

    this.Tip.Show('添加分类<b>...</b>');

    var ts='';
    for(var i=0;i<labels.length;i++){
	ts+= (i>0?'&':'')+'label[]='+UrlEncode(labels[i]);
    }


    /**args.*/
    var _params = {
	action:'ApplyMulti',
	reltype:params.reltype,
	relid:params.relid,
	RAW_QUERY:ts
    };

    /**set callback*/
    var AJ = {};
    AJ.ApplyLabels_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	T.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(CBParams.params.labels);
	}

    };

    MyAPI.callMethod('ApplyLabels',_params,AJ,{params:params},this.AdminUrl);
};

/** UnApply ONE Label */
Labels.prototype.UnApply=function(params)
{
    /**
       params{
       labels:[label' id] **ONLY ONE SUPPORT NOW**
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function([labels])
       }
     */

    if(typeof params != 'object')
	return;
    
    var labels = params.labels;
    
    if(!labels || !labels.length)
	return;

    var T = this;

    this.Tip.Show('撤销分类<b>...</b>');



    /**args.*/
    var _params = {
	action:'UnApply',
	reltype:params.reltype,
	relid:params.relid,
	id:params.labels[0]
    };

    /**set callback*/
    var AJ = {};
    AJ.UnApplyLabels_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	T.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(CBParams.params.labels);
	}

    };

    MyAPI.callMethod('UnApplyLabels',_params,AJ,{params:params},this.AdminUrl);
};

/** Apply Labels */
Labels.prototype.CreateAndApply=function(params)
{
    /**
       params{
       label:label name
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function(label)
       }
     */

    if(typeof params != 'object')
	return;

    var label=params.label;
    if(!label || Trim(label)=='')
	return;
    
    var T = this;

    this.Tip.Show('添加分类<b>...</b>');

    /**args.*/
    var _params = {
	action:'CreateLabelAndApply',
	reltype:params.reltype,
	relid:params.relid,
	name:label
    };

    /**set callback*/
    var AJ = {};
    AJ.CreateAndApply_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	T.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(CBParams.params.labels);
	}

    };

    MyAPI.callMethod('CreateAndApply',_params,AJ,{params:params},this.AdminUrl);
};

/***/

/** Tags Class */
function Tags()
{
    this.tags=[];
    this.fields={name:0,apply:1};
    this.AdminUrl = '/tags/tags_admin_xml.ue';
    this.Tip = new Tip();
}
/** Get Tagss 
    NOT SUPPORT TASKLIST */
Tags.prototype.GetTags=function(params)
{
    /**
       params{
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function([tags])
       }
     */

    if(typeof params != 'object')
	return;
    
    var T = this;

    this.Tip.Show('加载数据<b>...</b>');

    /**args.*/
    var _params = {
	action:'GetAppliedTags',
	reltype:params.reltype,
	relid:params.relid
    };

    /**set callback*/
    var AJ = {};
    AJ.GetTags_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	T.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/
	/** clear first*/
	T.tags.length=0;
	
	var f=T.fields;

	var doc = rsXML.documentElement;
	var tags = doc.getElementsByTagName('tag');
	if(tags && tags.length){
	    for(var i=0;i<tags.length;i++){
		var t=tags[i];
		
		var L=[];
		L[f.name]=getNodeValue(t);
		L[f.apply]=t.getAttribute('apply');
		    
		T.tags[T.tags.length] = L;
	    }
	}
	    

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(T.tags);
	}

    };

    MyAPI.callMethod('GetTags',_params,AJ,{params:params},this.AdminUrl);

};
/** Apply Tags */
Tags.prototype.Apply=function(params)
{
    /**
       params{
       tags:[tags]
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function([tags])
       }
     */

    if(typeof params != 'object')
	return;

    var tags=params.tags;
    if(!tags || !tags.length)
	return;
    
    var T = this;

    this.Tip.Show('添加标签<b>...</b>');

    var ts='';
    for(var i=0;i<tags.length;i++){
	ts+= (i>0?'&':'')+'name[]='+UrlEncode(tags[i]);
    }


    /**args.*/
    var _params = {
	action:'AddMulti',
	reltype:params.reltype,
	relid:params.relid,
	RAW_QUERY:ts
    };

    /**set callback*/
    var AJ = {};
    AJ.ApplyTags_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	T.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(CBParams.params.tags);
	}

    };

    MyAPI.callMethod('ApplyTags',_params,AJ,{params:params},this.AdminUrl);
};

/** UnApply ONE Tag */
Tags.prototype.UnApply=function(params)
{
    /**
       params{
       tags:[tag'name] ** ONLY ONE SUPPORT NOW **
       reltype:'Blog/Image'
       relid:'blog/img id'
       callback:function(tag)
       }
     */

    if(typeof params != 'object')
	return;
    
    var tags = params.tags;
    if(!tags || !tags.length)
	return;

    var T = this;

    this.Tip.Show('删除标签<b>...</b>');



    /**args.*/
    var _params = {
	action:'Remove',
	reltype:params.reltype,
	relid:params.relid,
	name:params.tags[0]
    };

    /**set callback*/
    var AJ = {};
    AJ.UnApplyTags_onLoad = function(success,rsXML,rsText,params,CBParams)
    {	
	/** hide tip*/
	T.Tip.Hide();

	/***/

	/**	alert(rsText);*/
	if(success.code!=0){
	    alert('发生错误(#'+success.code+'):'+success.desc);
	    return;
	}
	/**success.*/

	/** callback?*/
	if(CBParams.params.callback){
	    CBParams.params.callback(CBParams.params.tags);
	}

    };

    MyAPI.callMethod('UnApplyTags',_params,AJ,{params:params},this.AdminUrl);
};
/***/

/** Dialog Class */
function Dialog(){
    this.on = false;
    this.EXT = {};
    this.instance = this;
    this.callback=null;
    this.init();
}
Dialog.prototype.init=function()
{
    var EXT = this.EXT;

    EXT.instance = this;
    EXT.Event = new Events();

    this.handle_str = '_DialogClass_'+(new Date()).getTime()+(window.__base_uid++); 
    
    window[this.handle_str] = this;


    EXT.BG_id = 'DBG';
    EXT.FG_id = 'DFG';

    EXT.IF_id = 'DIF';
    
    EXT.BG = CreateDIV2(window,EXT.BG_id);

    var bg=EXT.BG;
    
    var opacity=function(a,b){
	if("opacity" in a.style){
	    a.style.opacity=b?0.5:1
	}else if("MozOpacity" in a.style){
	    a.style.MozOpacity=b?0.5:1
	}else if("filter" in a.style){
	    a.style.filter=b?"alpha(opacity=50)":"alpha(opacity=100)"
	}
    };
    var bef=function(e){e=e||window.event;e.cancelBubble=true;if("stopPropagation" in e){e.stopPropagation()}return false;};
    var bet=function(e){e=e||window.event;e.cancelBubble=true;if("stopPropagation" in e){e.stopPropagation()}return true;};
    
    bg.setAttribute('style','');

    bg.style.width=(bg.style.height="100%");
    bg.style.left=(bg.style.top=(bg.style.right=(bg.style.bottom="0px")));
    bg.style.zIndex=200;
    bg.style.background="#fff";
    opacity(bg,true);
    bg.style.position="absolute";
    bg.onmousedown=(bg.onmouseup=(bg.onmousemove=(bg.onkeydown=(bg.onkeyup=(bg.onkeypress=(bg.onclick=(bg.ondblclick=bef)))))));

    ShowElement(bg,false);
    
    EXT.FG = CreateDIV2(window,EXT.FG_id);
    var fg=EXT.FG;

    fg.onmousedown=(fg.onmouseup=(fg.onmousemove=(fg.onkeydown=(fg.onkeyup=(fg.onclick=(fg.ondblclick=bet))))));
    fg.onkeypress=bet;
    fg.style.position="absolute";
    fg.style.display="none";
    fg.style.zIndex=201;
    opacity(fg,false);
    fg.style.overflow='auto';/** fix bug for cursor lost in ff */

    ShowElement(fg,false);


    /** create iframe layer **/

    var iframe = document.createElement('iframe');
    iframe.id = EXT.IF_id;
    iframe.frameBorder='0';
    iframe.setAttribute('style','');
    iframe.style.position='absolute';
    iframe.style.zIndex = 190;
    iframe.style.top = iframe.style.left = '0px';
    iframe.style.display = 'none';
    EXT.IF = iframe;
    
    if(document.body.hasChildNodes())
	document.body.insertBefore(iframe,document.body.firstChild);
    else
	document.body.appendChild(iframe);

    /**onloadRegisterHandler(function(){document.body.appendChild(iframe);});**/



};

Dialog.prototype._CloseDialog=function(btn_idx)
{
    try{
	if(this.callback){/** callback must return true/false */
	    var r = this.callback(btn_idx);
	    if(!r)
		return;
	}
    }catch(e){}

    this.Hide();
};
Dialog.prototype.ResizeBox=function()
{
    var EXT = this.EXT;

    var h=GetWindowHeight(window);
    var w=GetWindowWidth(window);
    var bh = Math.max(document.getElementsByTagName('body')[0].offsetHeight,h);
    EXT.BG.style.height = bh+'px';
    /**EXT.BG.style.width = w+'px'; */
    
    var e=(w-EXT.FG.offsetWidth)/2+GetScrollLeft(window);
    var f=(h-EXT.FG.offsetHeight)/2+GetScrollTop(window);
    EXT.FG.style.left=e+"px";
    EXT.FG.style.top=f+"px";
    

    if(EXT.IF){

	EXT.IF.style.left = e + 'px';
	EXT.IF.style.top  = f + 'px';
	EXT.IF.style.width = EXT.FG.offsetWidth + 'px';
	EXT.IF.style.height = EXT.FG.offsetHeight + 'px';

    }
};
Dialog.prototype.ShowBox=function(html)
{
    var EXT=this.EXT;
    var Event = EXT.Event;

    var _this = this;

    var sm = function(){
	_this.ResizeBox();
    };


    EXT.FG.style.visibility='hidden';

    EXT.FG.innerHTML = html;

    Event.Attach(window,'resize',sm);
    Event.Attach(window,'scroll',sm);

    EXT.FG.style.top='0px';
    EXT.FG.style.left='0px';
    
    ShowElement(EXT.BG,true);
    ShowElement(EXT.FG,true);

    if(EXT.IF){
	ShowElement(EXT.IF,true);
    }

    sm();

    EXT.FG.style.visibility='visible';

};
Dialog.prototype.Show=function(callback,title,body,btns)
{
    /** params:{
	callback:function(btn_idx){}
	title:html_title
	body:html_body
	btns:['html_btn',...]
	}*/
    var EXT = this.EXT;
    
    if(this.on)
	return;

    this.on=true;
    this.callback=callback;
    var e=['<table class="dialog" cellspacing="0" cellpadding="0"><tr><td><div class="t1">&nbsp;</div><div class="t2">&nbsp;</div>'];

    e.push('<div class="border titlebar">'+title+"</div>");
    e.push('<div class="border body" id="',"dialogcontent",'">');
    e.push(body);
    e.push('<div style="text-align:center;"><table class="buttontable" style="margin-left:auto;margin-right:auto;text-align:;" cellspacing="0" cols="');
    e.push(btns.length,'"><tr>');
    for(var f=0;f<btns.length;++f){
	e.push('<td align="center"><span onclick="javascript:window.'+this.handle_str+'._CloseDialog.call(window.'+this.handle_str+',',f,')">',btns[f],"</span></td>");
    }
    e.push("</tr></table></div></div>");

    e.push('<div class="b2">&nbsp;</div><div class="b1">&nbsp;</div></td></tr></table>');

    this.ShowBox(e.join(""));

};
Dialog.prototype.Hide=function()
{
    var EXT=this.EXT;
    
    ShowElement(EXT.FG,false);
    ShowElement(EXT.BG,false);

    if(EXT.IF){
	ShowElement(EXT.IF,false);
    }

    this.on=false;
};

/***/

/** Events Class */
function Events(){this.init();}
Events.prototype.init=function()
{
    if(!Function.prototype._ME_bind){
	Function.prototype._ME_bind=function(a){
	    if(typeof this!="function"){
		throw new Error("Bind must be called as a method of a function object.");
	    }
	    var b=this;
	    var c=Array.prototype.splice.call(arguments,1,arguments.length);
	    return function(){
		var d=c.concat();
		for(var e=0;e<arguments.length;e++){
		    d.push(arguments[e]);
		}
		return b.apply(a,d);
	    };
	};
    }
    this.a={};
    this.b=0;
};
Events.prototype._c=function(f){if(!f.le){f.le=(++this.b);}return f.le;};
Events.prototype._d=function(f,g,h,i)
{
    var j=this._c(f);
    var k=this._c(h);
    i=!(!i);
    var l=j+"_"+g+"_"+k+"_"+i;
    return l;
};
Events.prototype.Attach=function(f,g,h,i)
{
    var _this = this;
    var e=function(f){
	var g=Array.prototype.splice.call(arguments,1,arguments.length);
	return _this.a[f].listener.apply(null,g);
    };
    
    var j=this._d(f,g,h,i);
    if(j in this.a){
	return j;
    }
    var k=e._ME_bind(null,j);
    
    this.a[j]={listener:h,proxy:k};
    if(f.addEventListener){
	f.addEventListener(g,k,i);
    }else if(f.attachEvent){
	f.attachEvent("on"+g,k);
    }else{
	throw new Error("Node {"+f+"} does not support event listeners.");
    }
    return j;
};
Events.prototype.Detach=function(f,g,h,i)
{
    var j=this._d(f,g,h,i);
    if(!(j in this.a)){
	return false;
    }
    var k=this.a[j].proxy;
    if(f.removeEventListener){
	f.removeEventListener(g,k,i);
    }else if(f.detachEvent){
	f.detachEvent("on"+g,k);
    }
    delete this.a[j];
    return true;
};
/***/

/** Window Class Version 0.1 */
function Window(){
    this.initiazlied = false;
    this.on = false;
    this.EXT = {};
    this.instance = this;
    this.callback=null;
    this.init();
}
Window.prototype.init=function()
{
    var EXT = this.EXT;

    EXT.instance = this;

    this.initialized = true;

    EXT.Event = new Events();

    ExportHandle(this,'WindowClass');

    EXT.BG_id = this.handle_str+'window_BG';
    EXT.FG_id = this.handle_str+'window_FG';

    EXT.min_box_id = this.handle_str+'min_box_id';
    EXT.mm_box_id = this.handle_str+'mm_box_id';
    /** preload imgs */

    EXT.btn_min_img = ['/images/w_min_white.gif','/images/w_min_gray.gif'];
    EXT.btn_max_img = ['/images/w_max_white.gif','/images/w_max_gray.gif'];
    EXT.btn_img_cache=[];
    for(var i=0;i<EXT.btn_min_img.length;i++){
	var _i = new Image();_i.src=EXT.btn_min_img[i];
	EXT.btn_img_cache.push(_i);
    }
    for(var i=0;i<EXT.btn_max_img.length;i++){
	var _i = new Image();_i.src=EXT.btn_max_img[i];
	EXT.btn_img_cache.push(_i);
    }
    /**/

    /** background */
    EXT.BG = CreateDIV2(window,EXT.BG_id);

    var bg=EXT.BG;
    
    var opacity=function(a,b){
	if("opacity" in a.style){
	    a.style.opacity=b?0.5:1
	}else if("MozOpacity" in a.style){
	    a.style.MozOpacity=b?0.5:1
	}else if("filter" in a.style){
	    a.style.filter=b?"alpha(opacity=50)":"alpha(opacity=100)"
	}
    };
    var bef=function(e){e=e||window.event;e.cancelBubble=true;if("stopPropagation" in e){e.stopPropagation()}return false;};
    var bet=function(e){e=e||window.event;e.cancelBubble=true;if("stopPropagation" in e){e.stopPropagation()}return true;};
    
    bg.setAttribute('style','');

    bg.style.width=(bg.style.height="100%");
    bg.style.left=(bg.style.top=(bg.style.right=(bg.style.bottom="0px")));
    bg.style.zIndex=100;
    bg.style.background="#fff";
    opacity(bg,true);
    bg.style.position="absolute";
    bg.onmousedown=(bg.onmouseup=(bg.onmousemove=(bg.onkeydown=(bg.onkeyup=(bg.onkeypress=(bg.onclick=(bg.ondblclick=bef)))))));

    ShowElement(bg,false);
    
    /** Front ground */
    EXT.FG = CreateDIV2(window,EXT.FG_id);
    var fg=EXT.FG;

    fg.onmousedown=(fg.onmouseup=(fg.onmousemove=(fg.onkeydown=(fg.onkeyup=(fg.onclick=(fg.ondblclick=bet))))));
    fg.onkeypress=bet;
    fg.style.position="absolute";
    fg.style.display="none";
    fg.style.zIndex=101;
    opacity(fg,false);
    fg.style.overflow='auto';/** fix bug for cursor lost in ff */

    ShowElement(fg,false);

    /** min window box */
    var min = EXT.min_box = CreateDIV2(window,EXT.min_box_id);
    
    min.onmousedown=(min.onmouseup=(min.onmousemove=(min.onkeydown=(min.onkeyup=(min.onclick=(min.ondblclick=bet))))));
    min.onkeypress=bet;
    min.style.position="absolute";
    min.style.display="none";
    min.style.zIndex=111;
    min.style.top = min.style.left = '-10000px';
    /** opacity(fg,false);*/
    min.style.overflow='auto';/** fix bug for cursor lost in ff */

    ShowElement(min,false);
    
    /** move box */
    var mm = EXT.mm_box = CreateDIV2(window,EXT.mm_box_id);
    mm.onmousedown=(mm.onmouseup=(mm.onmousemove=(mm.onkeydown=(mm.onkeyup=(mm.onclick=(mm.ondblclick=bet))))));
    mm.onkeypress=bet;
    mm.style.position="absolute";
    mm.style.display="none";
    mm.style.zIndex=111;
    mm.style.top = mm.style.left = '-10000px';
    /** opacity(fg,false);*/
    mm.style.overflow='hidden';/**auto';*/ 
    /** fix bug for cursor lost in ff */
    mm.className = 'window_mm';
    mm.innerHTML = '&nbsp;';
    ShowElement(min,false);
};

Window.prototype._CloseWindow=function(btn_idx)
{
    try{
	if(this.callback){/** callback must return true/false */
	    var r = this.callback(btn_idx);
	    if(!r)
		return;
	}
    }catch(e){}

    this.Hide();
};

Window.prototype.Minsize=function()
{
    if(!this.initialized) return;

    var EXT = this.EXT;
    var Event = EXT.Event;

    var _this = this;
    
    var min = EXT.min_box;
    var e=['<table class="window window_min" cellspacing="0" cellpadding="0"><tr><td><div class="t1">&nbsp;</div><div class="t2">&nbsp;</div>'];
    e.push('<div class="border titlebar"><table cellspacing="0" cellpadding="0"><tr><td onclick="javascript:window.'+this.handle_str+'.Maxsize();return false;" style="white-space:nowrap;padding:0;cursor:pointer;">'+EXT.title+'</td><td style="padding:0 0 0 10px;white-space:nowrap;"><div class="top_btn"><span><img src="/images/w_min_gray.gif" alt="最小化" title="最小化" /></span> <span onclick="javascript:window.'+this.handle_str+'.Maxsize();return false;"><img src="/images/w_max_white.gif" alt="最大化" title="最大化" /></span></label></div></td></tr></table></div>');
    e.push('<div class="b2">&nbsp;</div><div class="b1">&nbsp;</div></td></tr></table>');


    /** 1. fill content in min window */

    min.style.visibility = 'hidden';
    min.innerHTML = e.join('');

    var _sm2 = window[this.handle_str+'resize_min_window'] = function(){
	_this.Resize_Min_Window();
    };

    /** Attach Event */
    Event.Attach(window,'resize',_sm2);
    Event.Attach(window,'scroll',_sm2);

    /** pre show min window */
    ShowElement(min,true);
    _sm2();
    
    /** 
     * 2. show mm box 
     * -------------
     * |           |
     * -------------
     */

    var h=GetWindowHeight(window);
    var w=GetWindowWidth(window);
    

    EXT.mm_info ={
	w:w,
	h:h,
	ww:EXT.FG.offsetWidth,
	wh:EXT.FG.offsetHeight,
	wl:String(EXT.FG.style.left).replace(/(\..*$|[^0-9])/g,''),
	wt:String(EXT.FG.style.top).replace(/(\..*$|[^0-9])/g,''),
	mw:EXT.min_box.offsetWidth,
	mh:EXT.min_box.offsetHeight,
	ml:0,
	mt:(h-30)+GetScrollTop(window),
	step_start:1,
	step:6
    };

    var mms = EXT.mm_box.style;
    mms.border='3px dashed #ccc';
    mms.width=(EXT.mm_info.ww-6)+'px';
    mms.height=(EXT.mm_info.wh-6)+'px';
    mms.left = EXT.mm_info.wl+'px';
    mms.top = EXT.mm_info.wt +'px';
    ShowElement(EXT.mm_box,true);

    EXT.mm_info.step_handle = 
    setInterval(function(){
	    var EXT = _this.EXT;
	    var info = EXT.mm_info;
	    var mms = EXT.mm_box.style;

	    if(info.step_start>info.step){
		mms.left=mms.top = '-10000px';
		ShowElement(EXT.mm_box,false);

		if(info.step_handle){
		    try{
		    clearInterval(info.step_handle);
		    info.step_handle=null;
		    }catch(e){}
		}
	    }
	    mms.width = Math.max(info.ww - ((info.ww - info.mw) / info.step) * info.step_start + 6 , 6)+'px';
	    mms.height = Math.max(info.wh - ((info.wh - info.mh) / info.step) * info.step_start + 6 , 6)+'px';
	    mms.left = Math.max(info.wl - ((info.wl - info.ml) / info.step) * info.step_start ,0 ) + 'px';
	    mms.top = Math.max(info.wt - ((info.wt - info.mt) / info.step) * info.step_start ,0 ) + 'px';
	    
	    info.step_start ++;
	},60);

    /** hide Main Window */
    /** Unattach Event */
    var sm = window[this.handle_str+'resize'];

    Event.Detach(window,'resize',sm);
    Event.Detach(window,'scroll',sm);
    /**/

    EXT.BG.style.top = EXT.BG.style.left = '-10000px';
    EXT.FG.style.top = EXT.FG.style.left = '-10000px';
    ShowElement(EXT.BG,false);
    ShowElement(EXT.FG,false);


    /** Show min Window */
    setTimeout(function(){
	    EXT.min_box.style.visibility = 'visible';
	},60*EXT.mm_info.step + 100);
};
Window.prototype.Maxsize=function()
{
    if(!this.initialized) return;
    var EXT = this.EXT;
    var Event = EXT.Event;
    var _this = this;

    /** Show Main Window */
    var sm = window[this.handle_str+'resize'] = function(){
	_this.ResizeBox();
    };
    /** Attach Event */
    Event.Attach(window,'resize',sm);
    Event.Attach(window,'scroll',sm);

    EXT.FG.style.top='0px';
    EXT.FG.style.left='0px';
    
    ShowElement(EXT.BG,true);
    ShowElement(EXT.FG,true);

    sm();

    /** Hide min Window */

    /** Unattach Event */
    var _sm2 = window[this.handle_str+'resize_min_window'];

    Event.Detach(window,'resize',_sm2);
    Event.Detach(window,'scroll',_sm2);
    /**/
    EXT.min_box.style.left = EXT.min_box.style.top = '-10000px';
    ShowElement(EXT.min_box,false);
};

Window.prototype.ShowBox=function(html)
{
    var EXT=this.EXT;
    var Event = EXT.Event;

    var _this = this;

    var sm = window[this.handle_str+'resize'] = function(){
	_this.ResizeBox();
    };


    EXT.FG.style.visibility='hidden';

    EXT.FG.innerHTML = html;

    Event.Attach(window,'resize',sm);
    Event.Attach(window,'scroll',sm);

    EXT.FG.style.top='0px';
    EXT.FG.style.left='0px';
    
    ShowElement(EXT.BG,true);
    ShowElement(EXT.FG,true);

    sm();

    EXT.FG.style.visibility='visible';

};
Window.prototype.ResizeBox=function()
{
    var EXT = this.EXT;

    var h=GetWindowHeight(window);
    var w=GetWindowWidth(window);
    var bh = Math.max(document.getElementsByTagName('body')[0].offsetHeight,h);
    EXT.BG.top = EXT.BG.left = '0px';
    EXT.BG.style.height = bh+'px';
    /**EXT.BG.style.width = w+'px'; */
    
    var e=(w-EXT.FG.offsetWidth)/2+GetScrollLeft(window);
    var f=(h-EXT.FG.offsetHeight)/2+GetScrollTop(window);
    EXT.FG.style.left=e+"px";
    EXT.FG.style.top=f+"px";
};
Window.prototype.Resize_Min_Window=function()
{
    var EXT = this.EXT;

    var h=GetWindowHeight(window);
    var w=GetWindowWidth(window);
    
    var l=0;/**(w-EXT.FG.offsetWidth)/2+GetScrollLeft(window);*/
    var t=(h-EXT.min_box.offsetHeight)+GetScrollTop(window);
    EXT.min_box.style.left=l+"px";
    EXT.min_box.style.top=t+"px";
};



Window.prototype.Show=function(callback,title,body,btns)
{
    /** params:{
	callback:function(btn_idx){}
	title:html_title
	body:html_body
	btns:['html_btn',...]
	}*/
    var EXT = this.EXT;
    
    if(this.on){
	alert('已经有一个Window实例,当前版本不支持同时打开多个Window实例!');
	return;
    }
    /** save title here for min window using. */
    EXT.title = title;

    this.on=true;
    this.callback=callback;
    var e=['<table class="window" cellspacing="0" cellpadding="0"><tr><td><div class="t1">&nbsp;</div><div class="t2">&nbsp;</div>'];

    e.push('<div class="border titlebar"><div class="top_btn"><span onclick="javascript:window.'+this.handle_str+'.Minsize();return false;"><img src="/images/w_min_white.gif" alt="最小化" title="最小化" /></span> <span><img src="/images/w_max_gray.gif" alt="最大化" title="最大化" /></span></label></div>'+title+"</div>");
    e.push('<div class="border body" id="',"dialogcontent",'">');
    e.push(body);
    e.push('<div style="text-align:center;">');
    /** 
	e.push('<table class="buttontable" style="margin-left:auto;margin-right:auto;text-align:;" cellspacing="0" cols="');
    e.push(btns.length,'"><tr>');
    for(var f=0;f<btns.length;++f){
	e.push('<td align="center"><span onclick="javascript:window.'+this.handle_str+'._CloseWindow.call(window.'+this.handle_str_',f,')">',btns[f],'</span></td>');
    }
    e.push("</tr></table>");
    */
    e.push("</div></div>");

    e.push('<div class="b2">&nbsp;</div><div class="b1">&nbsp;</div></td></tr></table>');

    this.ShowBox(e.join(""));

};
Window.prototype.Hide=function()
{
    var EXT=this.EXT;
    
    ShowElement(EXT.FG,false);
    ShowElement(EXT.BG,false);

    this.on=false;
};

/***/

