var QQ = typeof QQ == 'undefined' ? {} : QQ;
var qq = window.QQ = window.qq = QQ;

var W = window;
var D = document;
function nully(){};
function $(id) { return typeof id=='string'&&id!=''?document.getElementById(id):false; }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function trim(s) { return typeof s == 'string' ? s.trim() : ''; }

function $scroll() {
	var de = D.documentElement || D.body;
 	 if( de && typeof de.scrollTop  == 'number' && typeof W.pageYOffset  == 'undefined'  ) {
	    return {top:de.scrollTop,left:de.scrollLeft};	
	 } else if( typeof W.pageYOffset == 'number' ) {
	    return {top:W.pageYOffset,left:W.pageXOffset};
	 }
	 return {top:0,left:0};
}
//
function $size(e) {
	var wh = {w:600,h:500};
	if( typeof  W.innerWidth  == 'number' ) {
	    wh.w = W.innerWidth - 12;
	    wh.h = W.innerHeight - 12;
	} else {
		var de = D.documentElement || D.body;	
    	wh.w =de.clientWidth;
	    wh.h = de.clientHeight;
	}
	if( e ) {
		var lt = $scroll();	
		wh.top = Math.max(0,(Math.ceil((wh.h/2) - (e.offsetHeight/2))+lt.top));
		wh.left = Math.max(0,(Math.ceil((wh.w/2) - (e.offsetWidth/2))+lt.left));
		e.style.top = (isNaN(wh.top)?'100px':wh.top+'px');
		e.style.left = (isNaN(wh.left)?'100px':wh.left+'px');
	}
	return wh;
}

var Events = window.Events = {
	
	onPreload : nully,
	isMouseDown : false,
	onLoads : [],
	
	setup : function() {
		Events.add('load',function() { Events.onLoad(true); },window);		
		try {
		if( typeof document.readyState != 'undefined' ) {
			window.initTimer = setInterval(function() { if( /interactive|complete|loaded/.test(document.readyState)) { clearInterval(window.initTimer);Events.onPreload.call(window); Events.onPreload = nully; }},250);
		} else if( document.addEventListener ) {
			try { document.addEventListener('DOMContentLoaded',function() { Events.onPreload.call(window); Events.onPreload = nully; },false); } catch(e) { }
		}
		} catch(e) { }
	},
	
	onLoad : function(f,obj) {
		if( typeof f == 'boolean' && f == true) {
			var o;
			while( Events.onLoads.length ) {
				o = Events.onLoads.shift();
				o.f.call(o.obj);
			}
			return;
		}
		if( typeof obj == 'undefined' ) obj = window;
		Events.onLoads.push({f:f,obj:obj});
	},
	
	add : function(s,f,e) {
		var ele = typeof e != 'undefined' ? (typeof e == 'string' ? $(e) : e) : document;
		s = s.toLowerCase();
		if( ele.addEventListener ) { 
			if( s.substr(0,2) == 'on' ) { s = s.substr(2); }
			return ele.addEventListener(s,f,false); 
		} else if( ele.attachEvent )  { 
			if( s.substr(0,2) != 'on' ) { s = 'on' + s; }		
			return ele.attachEvent(s,f); 
		}
		return false;
	}, 
	
	remove : function(s,f,e) {
		var ele = typeof e != 'undefined' ? (typeof e == 'string' ? $(e) : e) : document;
		s = s.toLowerCase();
		if( ele.removeEventListener ) { 
			if( s.substr(0,2) == 'on' ) { s = s.substr(2); }
			return ele.removeEventListener(s,f,false); 
		} else if( ele.dettachEvent )  { 
			if( s.substr(0,2) != 'on' ) { s = 'on' + s; }		
			return ele.dettachEvent(s,f); 
		}
		return false;
	},
	
	addHandler : function(s,f,e) {
		var _f = function() {
			var ev = window.event || arguments[0];
			var ele = ev.srcElement || ev.target || window;
			if( ele && ele.nodeType == 3 ) ele = ele.parentNode;
			var ret = !!f.call(ele,ev);
			ev.returnValue = ret;
			ev.cancelBubble = true;
			if( ev.stopPropagation ) ev.stopPropagation();
			return ret;
		}
		Events.add(s,_f,e);
	},
	
	returnFalse : function(e) {
		if( e.stopPropagation ) e.stopPropagation();
		e.cancelBubble = true;
		e.returnValue = false;
		return false;
	},
	
	returnTrue : function(e) {
		if( e.stopPropagation ) e.stopPropagation();
		e.cancelBubble = true;
		e.returnValue = true;
		return true;
	}
	
};

var HTTP = {ajax:null,busy:false,cb:null,cb2:null,ele:null,queue:[],
cleanup:
			function() {	HTTP.cb = null; HTTP.cb2 = null; HTTP.ele = null; },
init:
			function() {
				if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
				else if (window.ActiveXObject) { 
					var a = ['Msxml2.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.5.0',	'Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
					for(var i = 0 ; i < 5 ; i++) {	try{ return new ActiveXObject(a[i]); } catch(e) { } }
				}
			  return false;	
			},
onrsc :
		function() {
			try {
					if( this.ajax === null ) return false;
					if (this.ajax.readyState != 4) { return true; }
					var r = true;
					if( this.cb !== null ) { r = this.cb.call(this.ele,this.ajax.responseText); } 
					if( this.ele !== null && r !== false) { this.ele.innerHTML = this.ajax.responseText; }
					if( this.cb2 !== null ) this.cb2.call(this.ele);
					this.cleanup.call(this);
				} catch(e) { this.busy = false; err(e,'ajax'); return false; }
				this.busy = false;
				if( this.queue.length > 0 ) {
					this.Go.apply(this,this.queue.shift());
				}
				return true;
		},
encode :
			function(o) {
				 if( typeof o != 'object' ) return '';
				 if( typeof o._r != 'undefined' ) { o._r = Math.random(); }
				 var a=[],enc = encodeURIComponent||escape;
				 for( var p in o ) { if( o[p] !== null ) { a.push(p+'='+enc(o[p])); } }
				 return a.join('&');
			},
Go :
		function( url, cb, ele, override ) {
			if( this.busy ) { 
				if( override ) { try { this.ajax.abort();  } catch(e) {} this.busy = false;  this.ajax = null; }
				else { this.queue.push(arguments); return; }
			}
			this.busy = true;
			this.cleanup();
			if( this.ajax === null ) { this.ajax = this.init();  if ( this.ajax === null ) { return err('ajax init'); } }
			if( url instanceof Array ) {
				url = url[0] + (url[0].indexOf('?') == -1 ? '?' : '') + this.encode(url[1]);
			}
			if( url.charAt(0) != '/'  ) { 
				if( url.indexOf('.dv8') != -1) { url = '/e/ajax/' + url; }
				else if( url.indexOf('.php') != -1) { url = '/w/' + url; }
			}
			if( typeof ele == 'function' ) {
				this.cb2 = ele; ele = cb;
			} else {
				this.cb = cb;
			}
			if( typeof this.cb != 'function' ) this.cb = null;
			if( ele === null || typeof ele == 'undefined' ) this.ele = null;
			else if( typeof(ele) == 'string' ) this.ele = $(ele);
			else this.ele = ele;
			this.ajax.open('GET', url, true);
			this.ajax.onreadystatechange = function() { QJax.onrsc.call(HTTP); };
			this.ajax.send(null);
		},
Get : function() { QJax.Go.apply(HTTP,arguments); } };
