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


function SmartInputText(el,params)
{

    if(typeof params != 'object') params = {};

    var EXT = el;this.EL = EXT;
    if(!EXT || EXT.tagName.toLowerCase() != 'input' || (EXT.type != 'text' && EXT.type != 'password'))
	return;

    if(!EXT.v) EXT.v = EXT.value;
    if(!EXT.style) EXT.setAttribute('style','');
    if(!EXT.fc){
	if(EXT.style.color) EXT.fc=EXT.style.color;	
	else EXT.fc = '#000000';
    }
	
    try{
	/**init*/
	EXT.style.border="0px";
	EXT.style.padding="0 1px";
	
	/**draw a table*/
	var t = document.createElement('table');
	t.cellPadding="0";
	t.cellSpacing="0";
	t.border="0";
	t.setAttribute('style','');
	t.style.display="inline";

	if(!params.novalign)
	    t.style.verticalAlign='bottom';
	if(params.valign)
	    t.style.verticalAlign=params.valign;

	var r=t.insertRow(0);
	var td=r.insertCell(0);
	td.setAttribute('style','');
	td.style.padding="0px";
	td.style.border="1px solid #7f9db9";
	var div = document.createElement('div');
	div.setAttribute('style','');
	div.style.border="1px solid #fff";
	td.appendChild(div);
	

	
	var ep =EXT.parentNode;
	var ni = EXT;
	ep.replaceChild(t,EXT);
	div.appendChild(ni);
	
	
	}catch(e){}
	
    /**attach event;*/
    EXT.onfocus = function(){
	if(EXT.value==EXT.v && !params.notip)
	    EXT.value='';
	else
	    EXT.select();
	try{
	    var b1 = EXT.parentNode;
	    var b2 = b1.parentNode;
	    b1.style.borderColor="#d1fdcd";
	    b2.style.borderColor="#54ce43";
	    EXT.style.color='#000';
	    
	}catch(e){}
    };
	
    EXT.onblur = function(){
	if(String(EXT.value).replace(/^\s*/g,'').replace(/\s*$/g,'')=='')
	    EXT.value = EXT.v;		
	try{
	    var b1 = EXT.parentNode;
	    var b2 = b1.parentNode;
	    b1.style.borderColor="#fff";
	    b2.style.borderColor="#7f9db9";
	    EXT.style.color = EXT.fc;
	}catch(e){}
    };

}

/*
 * JS for site search box 
 */

function SiteSearch(params)
{
    this.EXT={};
    this.handle_str = '_MP_SiteSearch_';
    window[this.handle_str] = this;

    try{
	this.main_url = MIPANG.location.scheme + '://' + MIPANG.location.main_host;
    }catch(ex){
	this.main_url = '';
    }
    this.init(params);
}
SiteSearch.prototype.init=function(params)
{
    var EXT = this.EXT;
    var _this = this;

    EXT.tip = '站内搜索...';
    EXT.els = {
	site_s:_ge('site_s'),
	site_s_q:_ge('site_s_q_id'),
	site_s_t:_ge('site_s_t_id'),
	site_s_t_title:_ge('_s_t_list'),
	site_s_btn:_ge('site_s_btn_id'),
	site_s_f:_ge('site_s_f')
    };
    EXT.st = {st_default:0};
    if(params && params.st && params.st.length){
	EXT.st.pool = params.st;
	if(params.st_default || params.st_default){
	    EXT.st['st_default'] = params.st_default;
	}
    }
    EXT.st.current = EXT.st.st_default;

    if(EXT.els.site_s_q.value == ''){
	EXT.els.site_s_q.value = EXT.tip;
    }
    AddClass(EXT.els.site_s,'site_s_inactive');

    EXT.els.site_s_q.onfocus=function(){
	AddClass(EXT.els.site_s,'site_s_active');
	try{this.select();}catch(e){}
	try{
	    if(this.value==EXT.tip){
		this.value='';
	    }else{
	    }
	}catch(ex){}
    };

    /** not used*/
    var q_onenter = function(e){
	e = e||window.event;
	var keycode = EV_GetKeyCode(e);
	if(keycode == 13){
	    /**enter*/
	    var el = EV_GetEventTarget(e);
	    if(el.id && el.id==EXT.els.site_s_q.id)
		_this.go();
	}
    };

    /**this.EV_Attach(window,'keyup',q_onenter);*/
    /** submit by form ,enter key */
    EXT.els.site_s_f.onsubmit=function(e){
	var q = Trim(_ge('site_s_q_id').value);
	var t = EXT.st.pool[EXT.st.current][0];
	if(q && q.length){
	    var f_q = _ge('_ss_f_q');
	    var f_t = _ge('_ss_f_t');
	    if(!f_q){
		f_q = document.createElement('input');
		f_q.id = '_ss_f_q';
		f_q.type='hidden';
		f_q.name='q';
		this.appendChild(f_q);
	    }
	    if(!f_t){
		f_t = document.createElement('input');
		f_t.id = '_ss_f_t';
		f_t.type='hidden';
		f_t.name='t';
		this.appendChild(f_t);
	    }
	    f_q.value = q;
	    f_t.value = t;
	    
	    return true;
	}
	return false;
    };

    EXT.els.site_s_q.onblur=function(){
	RemoveClass(EXT.els.site_s,'site_s_active');

	try{
	if(this.value == ''){
	    this.value = EXT.tip;
	}else{
	}
	}catch(ex){}
    };
    
    EXT.els.site_s_btn.onclick=function(){
	_this.go();
    };

    this._setEvent_r(EXT.els.site_s_t,'onmouseover',this.type_list_show);
    
    /** 
    var b_onclick=function(e){
	e = e || window.event;
	var el = EV_GetEventTarget(e);
	var tl = _ge('site_s_t_list_box');
	if(!tl) return;
	var loop=4;
	while(el && loop--){
	    if(el.id && el.id == tl.id){
		return;
	    }
	    el = el.parentNode;
	}
	_this.type_list_hide();
    };

    this.EV_Attach(document.body,'click',b_onclick);
    */
};
SiteSearch.prototype._setEvent_r=function(el,en,f){
    /** set Recursive */
    while(el){
	try{el[en] = f;}catch(e){}
	if(el.hasChildNodes()){
	    for(var i=0;i<el.childNodes.length;i++){
		this._setEvent_r(el.childNodes[i],en,f);
	    }
	}
	return;
    }
    return;
};
SiteSearch.prototype.type_list_show=function(e){
    var _this = window['_MP_SiteSearch_'];
    var EXT = _this.EXT;
    
    if(EXT._tl_hide_timestamp && (new Date()).getTime() - EXT._tl_hide_timestamp < 100)
	return;

    if(!EXT._tl_show)
	EXT._tl_show = true;
    else
	return;

    var div = CreateDIV2(window,'site_s_t_list_box');
    if(!div.style) div.setAttribute('style','');
    var ds = div.style;
    var t_offset = GetPageOffset(EXT.els.site_s_t);
    ds.top = (t_offset.y-1)+'px';
    ds.left = (t_offset.x-EXT.els.site_s_btn.offsetWidth)+'px'
    ds.display = '';
    var h=[];
    for(var i=0;i<EXT.st.pool.length;i++){
	var t = EXT.st.pool[i];
	var d = EXT.st.st_default;
	h.push('<span class="tl'+(i+1 == EXT.st.pool.length?' tl_last':'')+(d==i?' tl_sel':'')+'" onmouseover="javascript:AddClass(this,\'tl_hover\');" onmouseout="javascript:RemoveClass(this,\'tl_hover\');" onclick="javascript:window.'+_this.handle_str+'.pre_go('+i+');">'+HtmlEscape(t[1])+'</span>');

    }
    div.innerHTML = h.join('');
    ShowElement(div,true);

    /**EV attach*/
    if(!div.ev_attached){
	var div_onmouseover=function(e){
	    e = e || window.event;
	    if(!_this.EXT._tl_show) return;
	    var el = EV_GetEventTarget(e);
	    var tl = _ge('site_s_t_list_box');
	    if(!tl) return;
	    var loop=4;
	    while(el && loop--){
		if(el.id && (el.id == tl.id || el.id == EXT.els.site_s_t.id)){
		    return;
		}
		el = el.parentNode;
	    }
	    _this.type_list_hide();
	};
	_this.EV_Attach(document.body,'mouseover',div_onmouseover);
	div.ev_attached=true;
    }
};
SiteSearch.prototype.type_list_hide=function(){
    ShowElement(_ge('site_s_t_list_box'),false);
    this.EXT._tl_show = false;
    this.EXT._tl_hide_timestamp = (new Date()).getTime();
};
SiteSearch.prototype.go=function(){
    var EXT = this.EXT;
    var q = String(EXT.els.site_s_q.value);
    if(q && q.length){
	var url = this.main_url+'/search?t='+EXT.st.pool[EXT.st.current][0]+'&q='+UrlEncode(q);
	var r =	window.open(url,'_blank');
	/**
	if(!r)
	    EXT.els.site_s_f.submit();
	*/
	if(!r)
	    top.location.href = url;
	if(!r){
	    var fake = CreateDIV2(window,'site_s_fake_form');
	    if(!fake.style) fake.setAttribute('style','');
	    var fs=fake.style;
	    fs.position='absolute';
	    fs.left=fs.top='-1000px';
	    fs.width=fs.height='1px';
	    fake.innerHTML = '<form action="'+this.main_url+'/search" method="get"><input name="t" type="hidden" value="'+EXT.st.pool[EXT.st.current][0]+'"/><input name="q" type="hidden" value="'+HtmlEscape(q)+'" /><input type="submit" value="Submit" /></form>';
	    setTimeout(function(){
		    var ff=_ge('site_s_fake_form');
		    var f=ff.getElementsByTagName('form')[0];
		    f.setAttribute('target','ssblank');
		    setTimeout(function(){f.submit();},200);
		},100);
	}
    }

};
SiteSearch.prototype.pre_go=function(t){
    var EXT = this.EXT;
    EXT.st.current=t;
    EXT.els.site_s_t_title.innerHTML = HtmlEscape(EXT.st.pool[t][1]);
    this.type_list_hide();
    this.go();
};
SiteSearch.prototype.EV_Attach=function(o,a,f,i){
    i = !(!i);
    if(o.addEventListener){
	o.addEventListener(a,f,i);
    }else if(o.attachEvent){
	o.attachEvent("on"+a,f);
    }
};


/** down menu **/

function mp_down_menu()
{
    this.__name__ = 'mp_down_menu_' + gen_unique();
    this.zIndex = 50;/* default zindex **/
    this.menuID = 'mpDMbox';
}
mp_down_menu.prototype.set_context=function(el,options)
{
    this.context = el;
    this.context_options = options || {};
    return this;
};
mp_down_menu.prototype.set_list=function(list)
{
    /**list::[] **/
    this.list = list;
    return this;
};
mp_down_menu.prototype.set_options=function(options)
{
    for(x in options)
	this[x] = options[x];
    
    return this;
};
mp_down_menu.prototype.init=function()
{
    onloadRegisterHandler(function(){ this.__init(); }.bind(this));
    return this;
}
mp_down_menu.prototype.__init=function()
{

    if(this.context[this.__name__ + 'onclick'])
	return this;

    this.__init_css();

    var container = document.createElement('div');
    container.id = this.__name__ + '_container_' + gen_unique();
    container.setAttribute('style','');
    container.style.position = 'absolute';
    container.style.zIndex = this.zIndex;
    container.style.left = container.style.top = '-1000px';
    container.style.display = 'none';

    this.container = container;

    

    addEventBase(this.container,'click',function(e){
	    stopPropagation(e);
	},this.__name__+'_container_onclick');

    

    onloadRegisterHandler(function(){
	    document.body.appendChild(this.container);
	}.bind(this));


    var iframe = document.createElement('iframe');
    iframe.id = this.__name__ + '_iframe_' + gen_unique();
    iframe.frameBorder='0';
    iframe.setAttribute('style','');
    iframe.style.position = 'absolute';
    iframe.style.zIndex = this.zIndex-1;
    iframe.style.left = this.container.style.top = '-1000px';
    iframe.style.display='none';

    /**filter:alpha(opacity=0);*/
    if("opacity" in iframe.style){
	iframe.style.opacity=0;
    }else if("MozOpacity" in iframe.style){
	iframe.style.MozOpacity=0;
    }else if("filter" in iframe.style){
	iframe.style.filter="alpha(opacity=0)";
    }
    this.iframe = iframe;

    onloadRegisterHandler(function(){
	    document.body.appendChild(this.iframe);
	}.bind(this));


    addEventBase(this.context,'click',function(e){
	    this.show();
	stopPropagation(e);
	return false;
	}.bind(this),this.__name__+'_context_onclick');


    this.context[this.__name__ + 'onclick'] = true;

    addEventBase(document,'click',function(e){
	    this.hide();
	}.bind(this),this.__name__+'_body_onclick');

    return this;
};
mp_down_menu.prototype.hide=function()
{

    this.container.style.display = 'none';
    this.iframe.style.display = 'none';

    this.container.style.top = this.container.style.left = '-1000px';
    this.iframe.style.top = this.iframe.style.left = '-1000px';

    return this;
};
mp_down_menu.prototype.show=function()
{
    
    set_inner_html(this.container,this.__gen_html());

    this.container.style.display = '';
    this.iframe.style.display = '';

    this.position_fix();

    return this;
};

mp_down_menu.prototype.position_fix = function()
{
    var cp = GetPageOffset(this.context);
    var cw = this.context.offsetWidth;
    var ch = this.context.offsetHeight;
    
    var mw = this.container.offsetWidth;
    var mh = this.container.offsetHeight;

    this.container.style.top = this.iframe.style.top = (cp.y + ch + 2) + 'px';
    if(typeof this.context_options != 'undefined' &&
       typeof this.context_options.align != 'undefined' &&
       this.context_options.align == 'right' ){
	this.container.style.left = this.iframe.style.left = (cp.x + cw - mw) + 'px';
    }else{
	this.container.style.left = this.iframe.style.left = cp.x + 'px';
    }

    this.iframe.style.width = mw + 'px';
    this.iframe.style.height = mh + 'px';
    
    return this;
};

mp_down_menu.prototype.__gen_html=function()
{
    var html = '<div id="'+this.menuID+'">';
    
    for(var i=0;i<this.list.length;i++){
	var a = this.list[i];
	html+= '<div class="mItem"'
	    +' onmouseover="javascript:this.className=\'mItem mItemMO\';return false;"'
	    +' onmouseout="javascript:this.className=\'mItem\';return false;">'+a+'</div>';
    }
    html += '</div>';
    return html;
};
mp_down_menu.prototype.__init_css=function()
{
    var css = this.css || null;

    if(!css){
	/** default css **/
	css = '#mpDMbox { background-color:#ebf4fa;border-width:1px; border-style:solid; border-color:#69f #36c #36c #69f;'
	    +' padding:3px 0;text-align:left;font-size:13px; }';
	css += '#mpDMbox .mItem { padding:0 0 1px 0; }';
	css += '#mpDMbox a { color:#36c;background-color:transparent; text-decoration:none; display:block;padding:3px 8px 2px 8px;}';
	css += '#mpDMbox a:hover { color:#36c;background-color:transparent; text-decoration:underline;}';
	css += '#mpDMbox .mItemMO { background-color:#36c;color:#fff; }';
	css += '#mpDMbox .mItemMO a, ';
	css += '#mpDMbox .mItemMO a:hover{ background-color:#36c;color:#fff;text-decoration:none; }';

    }

    MP_appendCssText(this.__name__ + '_css' ,css);
	
    return this;
};