/*
========================
commonlib v1.0
------------
(c) 2010 Zeg All Rights Reserved
========================
*/

//==================================

//基本功能

//==================================

var __NUKE = {}

//简单的复制一个obj
__NUKE.simpleClone = function (o){
if(o == null || typeof(o) != 'object')
	return o;
var oo = new o.constructor(); // changed (twice)
for(var k in o)
	oo[k] = this.simpleClone(o[k]);
return oo;
}//fe

//继承一个obj
__NUKE.inheritClone = function (o){
if(o == null || typeof(o) != 'object')
	return o;
var oo =function(){};
oo.prototype=o
return new oo;
}//fe

//==================================

//DOM

//==================================

var $ = function(id){return (document.getElementById(id))}
var id2e = $

var put = function(txt){
if (txt)
	{
	txt = txt.replace(/<img(.+?)src=(?:"|')(.+?)(?:'|"| )(.*?)>/ig,function($0,$1,$2,$3){
		if ($0.indexOf('onload=')!=-1){return $0;}
		return ("<img"+$1+"src='_.gif' onload='w_i(\""+$2+"\",this)'"+$3+">")
	});
	}
document.write(txt);
}//fe

var domExtPrototype={ 
'each':function(fn) { 
	var s = this.self 
	if (s.els)
		if(Object.prototype.toString.apply(s.els) === '[object Array]')
			for ( var i = 0, len = s.els.length; i<len; ++i ) 
				fn.call(s.els[i]); 
		else
			for ( var i in s.els) 
				fn.call(s.els[i]); 
	else 
		fn.call(s) 
	return s; 
	}, 
'cls':function(cn) { 
	this.each(function(){this.className += ' '+cn;}); 
	return this.self; 
	}, 
'css':function(o,v) { 
	if (typeof(v)!='undefined') 
		this.each(function(){this.style[o] = v;}); 
	else 
		for (var prop in o)  
			this.css(prop, o[prop]); 
	return this.self; 
	}, 
'on':function(type, fn, old) { 
	if(old)
		var listen = function(el) { 
				el['on'+type]=fn
			};
	else
		var listen = function(el) { 
				if (window.addEventListener) { 
					el.addEventListener(type, fn, false); 
				} else if (window.attachEvent) { 
					el.attachEvent('on'+type, function() {fn.call(el, window.event);}); 
				} 
			}; 
	this.each(function(){listen(this);}); 
	return this.self; 
	}, 
'attr':function(o,v,set) { 
	if (typeof(v)!='undefined'){
		if(set)
			this.each(function(){this[o]=v;}); 
		else
			this.each(function(){this.setAttribute(o,v);});
		}
	else 
		for (var prop in o)  
			this.attr(prop, o[prop]); 
	return this.self; 
	}, 
'aC':function() { 
	var o = this.self; 
	var i=0; 
	if (typeof arguments[0]=='number') 
		{ 
		i=1; 
		if (o.els) 
			o = o.els[arguments[0]] 
		} 
	if (arguments[i+1]) 
		for (;i<arguments.length;i++) 
			o.appendChild(arguments[i]); 
	else 
		o.appendChild(arguments[i]) 
	return o; 
	}, 
'sV':function (o,v){
	if(!this.anyVar)this.anyVar={}
	if (typeof(v)!='undefined') 
		this.anyVar[o]=v; 
	else 
		for (var prop in o)  
			this.sV(prop, o[prop]); 
	return this.self; 
	},
'gV':function (k){
	if(!this.anyVar)this.anyVar={}
	return this.anyVar[k]; 
	}
}//oe

/*
//新建元素 
var x = $('<span/>') 
var x = $('<span>abcd</span>') 
//用id取元素 
var x = $('xxoo') 
//用名字取元素 
var x = $('<span>') 
//链式调用 
$('xxoo')._.cls('xxxxoooo')._.attr('title','abcd')._.aC($('<span/>'),$('<div/>')) 
//一次取多个元素 
var muti = $('<span>','<div>','xxoo') 
var first = muti[0] 
var firstOneWithMoreFunction = $(muti[0]) 
*/

var _$ = function (){//fs
var els={} 
var m=null 
var single=true 
for (var i=0,j=0;i<arguments.length;++i,++j) 
	{ 
	var e = arguments[i]; 
	if (typeof e == 'string') { 
		if (e.indexOf('<')==0){ 
			if(m = e.match(/^<(\/?)([a-zA-Z0-9]+)(\/?)>$/)){ 
				if (m[3] || m[1]) 
					els[j] = document.createElement(m[2]); 
				else{ 
					m =document.getElementsByTagName(m[2]) 
					for (var ii=0;ii<m.length;ii++,++j) 
						els[j]=m[ii]; 
					--j; 
					single=false 
					} 
				} 
			else{ 
				m = e.match(/^<([a-zA-Z0-9]+)>(.*?)<\/([a-zA-Z0-9]+)>$/) 
				if(m && m[1]==m[3]){ 
					els[j] = document.createElement(m[1]); 
					if(m[2])els[j].innerHTML=m[2]; 
					} 
				else 
					els[j]=null 
				} 
			} 
		else 
			els[j] = document.getElementById(e); 
		} 
	else 
		els[j] = e 
	} 
els.length = j; 
if(arguments.length==1 && single) 
	{ 
	if(!els[0])return null; 
	var o = els[0]; 
	o._=function(){} 
	o._.prototype=domExtPrototype 
	o._ = new o._ 
	} 
else 
	{ 
	var o = els; 
	o._=function(){} 
	o._.prototype=domExtPrototype 
	o._ = new o._ 
	o._.els=o; 
	} 
o._.self = o 
return o; 
}//fe 

//==================================

//XMLHttpRequest

//==================================

var HTTP = (function()
{
var xmlhttp = false,e1,e2,e3;
try{
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
catch(e1)
	{
	try{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	catch(e2){
		xmlhttp = false;
		}
	}
if(!xmlhttp && typeof XMLHttpRequest!="undefined")
	{
		try{
			xmlhttp = new XMLHttpRequest();
			}
		catch (e3){
			xmlhttp = false;
			}
	}
//if (!xmlhttp)window.alert(e1 + e2 + e3);
return xmlhttp;
})();

//==================================

//AJAX & SJAX

//==================================

var script_muti_get_var_store = null
var httpDataGetter = {

script_muti_syncget : function(url,f){
if (typeof(url)=='string')
	url=[url]
var u
while (u = url.shift())
	{
	HTTP.abort();
	HTTP.open('GET', u, false)
	HTTP.send('');
	if (HTTP.status==404)continue;
	u = HTTP.responseText
	if(u.indexOf('window.script_muti_get_var_store')!=-1){
		u = u.replace('window.script_muti_get_var_store','var u')
		eval(u)
		}
	u = f(u)
	if(u===false)continue;
	return u
	}
return undefined
},
//fe

script_muti_get_set_costom_value:function(v){
this._SMG.setCustomVar(v)
},//

script_muti_get:function(u,h,hf,c,cN)
{
this._SMG.get(u,h,hf,c,cN)
},//



_SMG:{
	getting:false,
	waiting:false,
	queue:[],
	cache:{},
	script:null,

	clone:function(o){ return __NUKE.simpleClone(o) },//fe

	setCustomVar:function (v){window.script_muti_get_var_store = v;},//fe

	//u: url array
	//h: success handler, return bool true to exit, return bool false to try next url
	//hf: all fail(no url) handler
	//c: script charset
	get:function(u,h,hf,c,cN){
		if (typeof(u)=='string')  u = [u];
		if (!c)c={}
		else if (typeof(c)=='string') c={'charset':c};
		if (cN)c.varName=cN
		this.queue.push({u:u,h:h,hf:hf,c:c})
		var self = this;
		window.setTimeout(function(){self.loop()},0)
		},//fe

	loop:function (noCheck){
		if(!noCheck){
			if (this.getting)
				return;
			}
		if(!this.queue.length){
			this.getting = false
			return;
			}
		else
			this.getting=true
		var u= this.queue.shift()
		this.act(u)
		},//fe

	act:function(u){
		var self=this
		var handler = function(){
			if (this.readyState && this.readyState != 'loaded' && this.readyState != 'complete')
				return
			if (u.c.varName)
				window.script_muti_get_var_store = window[u.c.varName];
			if (u.h(window.script_muti_get_var_store)){
				if(!u.c.noCache)self.cache[this.src.toLowerCase()]=self.clone(window.script_muti_get_var_store)
				self.waiting = window.setTimeout(function(){self.loop(1)},0)
				return true
				}
			u.u.shift();
			if (u.u.length)
				self.waiting = window.setTimeout(function(){self.act(u)},0)
			else{
				u.hf();
				this.waiting = window.setTimeout(function(){self.loop(1)},0)
				}
			}
		if(!u.c.noCache){
			if (u.u[0].indexOf('http://')==-1) var k = window.location.href.replace(/(http:\/\/.+?)(\/|$).*/,'$1')+u.u[0];
			else var k = u.u[0];
			if (this.cache[k.toLowerCase()]){
				u.h(this.cache[k.toLowerCase()]);
				this.waiting = window.setTimeout(function(){self.loop(1)},0)
				return;
				}
			}
		var h = document.getElementsByTagName('head')[0]
		if (this.script)
			h.removeChild(this.script)
		var s = document.createElement('script');
		if (u.c.charset) s.charset = u.c.charset;
		s.type = 'text/javascript'
		s.onerror = s.onload = s.onreadystatechange = handler
		window.script_muti_get_var_store = null;
		if(u.c.varName)window[u.c.varName]=null
		s.src= u.u[0];
		this.script=s
		h.insertBefore(s,h.firstChild)
		}//fe
	}//ce




}//ce

//==================================

//DOM STORE

//==================================

if(window.ActiveXObject && !window.localStorage) {document.documentElement.addBehavior("#default#userdata");};
if (window.ActiveXObject || window.globalStorage || window.localStorage)
{//if
var domStorageFuncs = {
domain:'',
init : function(v){this.domain=v},
set : function(key, value) {
	if (!this.domain)return;
	if (window.localStorage){//ie8
		window.localStorage.setItem(this.domain+'_'+key, value);
		}
	else if(window.globalStorage) {//for firefox2.0+
		window.globalStorage[this.domain].setItem(key, value);
		}
	else if(window.ActiveXObject) {//for ie5.0+
		with(document.documentElement){
			try{
				load(key);
				setAttribute("js", value);
				save(key);
				}
			catch (ex){
				setAttribute("js", value);
				save(key);
				}
			}
		}
},
get : function(key) {
	if (!this.domain)return;
	if (window.localStorage){//ie8
		return window.localStorage.getItem(this.domain+'_'+key);
		}
	else if(window.globalStorage) {
		return window.globalStorage[this.domain].getItem(key);
		}
	if(window.ActiveXObject) {
		with(document.documentElement){
			try{
					load(key);
					return getAttribute("js");
				}
			catch (ex){
					return null;
				}
			}
		}
	else{
		return null;
	};
},
remove : function(key) {
	if (!this.domain)return;
	if (window.localStorage){//ie8
		return window.localStorage.removeItem(this.domain+'_'+key);
		}
	else if(window.globalStorage) {
		window.globalStorage[this.domain].removeItem(key);
		}
	if(window.ActiveXObject) {
		with(document.documentElement){
			try{
				load(key);
				expires = new Date(315532799000).toUTCString();
				save(key);
				}
			catch (ex){};
			}
		}
}
}//end domStorage

}//end if

//==================================

//cookieAndSerialize

//==================================

var cookieAndSerialize = function (domain,path,misccookiename){
cookieFuncs.init(domain,path,misccookiename)
return cookieFuncs;
}//fe forward compatible for nga

var cookieFuncs = {
'cookieCache':{},
'domain':'',
'path':'',
'date':null,
'now':0,
'misccookiename':'',

'init':function (domain,path,misccookiename){
if (domain)
	this.domain = domain;
else
	this.domain = window.location.href.toLowerCase().replace(/^http:\/\//,'').replace(/(\/|:).*/,'').replace(/^[^\.]+\.([^\.]+\.)/,'$1');
this.path = path;
this.date = new Date;
this.now = this.date.getTime();
this.misccookiename = misccookiename
},//fe

'setCookieInSecond':function (name,value,sec)
{
this.date.setTime(this.now + sec*1000);
document.cookie = name + "="+ escape (value) + ";domain="+this.domain+";path="+this.path+";expires=" + this.date.toUTCString();
},//fe

'setMiscCookieInSecond':function (name,value,sec)
{
this.extractMiscCookie();
if (sec>0)
	{
		this.date.setTime(this.now + sec*1000);
		this.cookieCache[this.misccookiename][name] = {};
		this.cookieCache[this.misccookiename][name]['v'] = value;
		this.cookieCache[this.misccookiename][name]['t'] = this.date.toUTCString();
	}
else
	delete this.cookieCache[this.misccookiename][name];
this.setCookieInSecond(this.misccookiename,this.json_encode(this.cookieCache[this.misccookiename]),31536000);
},//fe

'extractMiscCookie':function(){
var c = this.cookieCache
var n = this.misccookiename;
if (typeof(c[n]) != 'object')
	{
		this.getCookie(n);
		if (typeof(c[n])=='string'){
			if(c[n].charAt(0)=='{'){
				var tmp = {}
				try{eval('var tmp='+c[n]+';');}catch(e){}
				c[n] = tmp;
				}
			else
				c[n]={}
			}
		else
			c[n]={}
	}
},//fe

'getMiscCookie':function (name)
{
this.extractMiscCookie();
if (this.cookieCache[this.misccookiename][name] && (Date.parse(this.cookieCache[this.misccookiename][name]['t'])>=this.now) )
	return this.cookieCache[this.misccookiename][name]['v'];
else
	{
		if (this.cookieCache[this.misccookiename][name])
			{
				delete this.cookieCache[this.misccookiename][name];
				this.setCookieInSecond(this.misccookiename,this.json_encode(this.cookieCache[this.misccookiename]),31536000);
			}
		return null;
	}
},//fe

'getCookie':function (name){
if (typeof(this.cookieCache[name])=='undefined')
	{
		var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
		if (arr)
			{
				this.cookieCache[name] = unescape(arr[2]);
			}
		else
			{
				this.cookieCache[name] = null;
			}
	}
return this.cookieCache[name];
},//fe

'ifMiscCookie':function (){
if (typeof(this._ifMiscCookie)=='boolean') return this._ifMiscCookie;
else if (document.cookie.match(new RegExp("(?:^| )"+this.misccookiename+"="))) this._ifMiscCookie = true;
else this._ifMiscCookie = false;
return this._ifMiscCookie;
},//fe

'json_encode':function(v) { 
switch (typeof(v)) { 
	case 'string':
		return '"' + v.replace('"','\\"') + '"';
	case 'number':
		return v.toString(10);
	case 'boolean':
		return v.toString();
	case 'object':
		var buf = []
		for (var k in v)
			{
			if (typeof(k)=='string')
				buf.push('"'+k.replace('"','\\"') + '":' + this.json_encode(v[k]));
			else
				buf.push(k + ':' + this.json_encode(v[k]));
			}
		if (v.constructor==Array)
			return '[' + buf.join(',') + ']';
		else
			return '{' + buf.join(',') + '}';
	default: 
		return 'null';
	}
}//fe

}//ce

//==================================

//Image / style lazy loader

//==================================

var w_i = function(s,o){
o.onload = null;
window.setTimeout(function(){o.src=s},100);
}//fe

var w_s = function(s,o,writeSelf){
if (!writeSelf)
	o = o.parentNode;
if (s.indexOf(':')==-1){
	if (s.charAt(0)==' ')
		window.setTimeout(function(){o.className=o.className+s},100);
	else
		window.setTimeout(function(){o.className=s},100);
	}
else{
	if (s.charAt(0)==';')
		window.setTimeout(function(){o.style.cssText=o.style.cssText+s},100);
	else
		window.setTimeout(function(){o.style.cssText=s},100);
	}
}//fe

var loader = {
'w_i':w_i,
'w_s':w_s,
'css':function (src){
var x = document.createElement('link')
x.href = src
x.rel = 'stylesheet'
x.type = 'text/css'
var h = document.getElementsByTagName('head')[0]
h.insertBefore(x,h.firstChild)
},//fe
'script':function (src,callback,charset){
var x = document.createElement('script')
x.src=src
if(charset)x.charset = charset
if (callback) {
	x._loader_callback = callback
	x.onreadystatechange = x.onload = function() {
		if (this.readyState && this.readyState != 'loaded' && this.readyState != 'complete')return;
		//commonui._debug.push('end '+this.src)
		this._loader_callback();
		}
	}
var h = document.getElementsByTagName('head')[0]
h.insertBefore(x,h.firstChild)
//commonui._debug.push('start '+src)
}
}//ce

//==================================

//Forward compatible for nga

//==================================
if (!__IMG_BASE)
{
var __AJAX_DOMAIN = window.location.href.toLowerCase().replace(/^http:\/\//,'').replace(/(\/|:).*/,'').replace(/^[^\.]+\.([^\.]+\.)/,'$1');
var __IMG_BASE = 'http://img.'+__AJAX_DOMAIN;
var __CKDOMAIN = '.'+__AJAX_DOMAIN;
}