
function getObj(id) {
	return document.getElementById(id);
}

window.agt = navigator.userAgent.toLowerCase();
window.navigator.params = {urlhash : ''};
var PwJs = new Object();
PwJs.Browser = {
	IE : ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)),
	OPERA : (agt.indexOf("opera") != -1),
	FIREFOX : (agt.indexOf("firefox") != -1)
};

PwJs.Config = {
	CALL_VER : 'call_ver',
	IFRAME_HEIGHT : 'ifmHeight',
	SEND_BY_NAV : 'send_by_navigator',
	SEND_BY_URLHASH : 'send_by_urlhash',
	TIMER : 100
};
//window.send_type = PwJs.Browser.IE ? PwJs.Config.SEND_BY_NAV : PwJs.Config.SEND_BY_URLHASH;
window.send_type = PwJs.Config.SEND_BY_URLHASH;

PwJs.Event = {
	regist : function(el,evname,func) {
		if (el.addEventListener) {
			el.addEventListener(evname,func,true);
		} else if(el.attachEvent) {
			el.attachEvent("on"+evname,func);
		} else {
			el["on"+evname] = func;
		}
		return el;
	},
	unregist : function(el,evname,func) {
		if (el.addEventListener) {
			el.removeEventListener(evname,func,true);
		} else if(el.attachEvent) {
			el.detachEvent("on"+evname,func);
		} else {
			el["on"+evname] = null;
		}
		return el;
	}
};
PwJs.onLoad = function(func) {
	PwJs.Event.regist(window, "load", func);
};
PwJs.getFunc = function(f) {
	var func = window;
	var f_a  = f.split('.');
	for (var i=0; i<f_a.length; i++) {
		func = func[f_a[i]];
	}
	return func;
};

PwJs.Hash = function(href) {
	var arr  = PwJs.Hash.split2(href,'#');
	var hash = arr[1];
	this.map = {};
	if (hash) {
		var arr = hash.split('&');
		for (var i=0; i<arr.length; i++) {
			var o = PwJs.Hash.split2(arr[i],'=');
			this.map[decodeURIComponent(o[0])] = decodeURIComponent(o[1]);
		}
	}
};
PwJs.Hash.prototype.get = function(key){
	return this.map[key] || null;
};
PwJs.Hash.prototype.put = function(key,value){
	this.map[key] = value;
};
PwJs.Hash.prototype.toString = function() {
	var r = [];
	for (var k in this.map) {
		r.push(encodeURIComponent(k) + '=' + encodeURIComponent(this.map[k]));
	}
	return r.join('&');
};
PwJs.Hash.split2 = function(s,separator) {
	var i = s.indexOf(separator);
	return i == -1 ? [s,''] : [s.substring(0,i),s.substring(i+1)];
};
PwJs.Hash.getHash = function(hash){
	var href = 'blank#';
	if (hash) {
		href += hash;
	}
	return new PwJs.Hash(href);
};

PwJs.Server = function(iframeID) {
	this._iframeID		= iframeID;
	this._map			= [];
	this._hashObj		= null;
	this._hash			= null;
	this._oldHash		= '';
	this._oldCall		= null;
};
PwJs.Server.prototype.registHandler = function(func) {
	this._map.push(func);
};
PwJs.Server.prototype.start = function() {
	var its = this;
	PwJs.Server._timer = setInterval(function(){its.onTimer();},PwJs.Config.TIMER);
};
PwJs.Server.prototype.onTimer = function() {
	if (send_type == PwJs.Config.SEND_BY_NAV) {
		this._hashObj = PwJs.Hash.getHash(unescape(window.navigator.params.urlhash));
	} else {
		this._hashObj = PwJs.Hash.getHash(unescape(this._hash));
	}
	if (this._hashObj.toString() != this._oldHash) {
		var ht = this._hashObj.get(PwJs.Config.IFRAME_HEIGHT);
		if (ht) {
			getObj(this._iframeID).setAttribute('height',ht+"px");
		}
		var newCall = this._hashObj.get(PwJs.Config.CALL_VER);
		if (newCall != this._oldCall) {
			this.reCall();
			this._oldCall = newCall;
		}
		this._oldHash = this._hashObj.toString();
	}
};
PwJs.Server.prototype.reCall = function() {
	for (var i=0; i<this._map.length; i++) {
		var f = this._map[i];
		if (this._hashObj.get(f)) {
			PwJs.getFunc(f).apply(null,this._hashObj.get(f).split('__pwps__'));
		}
	}
};

PwJs.Client = function(channelPage,rootID) {
	this._rootID	= rootID;
	this._channelSrc= channelPage;
	this._channelDom= null;
	this._hash		= null;
	this._hashObj	= new PwJs.Hash('');
	this._oldHeight	= null;
	this._func		= [];
	
	if (send_type == PwJs.Config.SEND_BY_NAV) {
	} else {
		this.createChannel();
	}
	var its = this;
	PwJs.onLoad(function() {
		its.callFunc();
		its.setHeight();
	});
};
PwJs.Client.prototype.createChannel = function() {
	var its = this;
	PwJs.onLoad(function() {
		its.createChannelIfm();
	});
};
PwJs.Client.prototype.createChannelIfm = function() {
	var ctnr = document.createElement("div");
	document.getElementsByTagName('body')[0].appendChild(ctnr);
	ctnr.style.display = "none";
	var cifm = document.createElement("iframe");
	cifm.id = 'agentiframe';
	ctnr.appendChild(cifm);
	this._channelDom = cifm;
};
PwJs.Client.prototype.setHeight = function() {
	var client = this;
	setInterval(function() {
		var newHeight;
		try {
			newHeight = getObj(client._rootID).offsetHeight;
		} catch(ex){}
		if (newHeight && newHeight != client._oldHeight) {
			client._hashObj.put(PwJs.Config.IFRAME_HEIGHT,newHeight);
			client._oldHeight = newHeight;
			client.sendHashToServer();
		}
	},100);
};
PwJs.Client.prototype.sendHashToServer = function() {
	var hashstr = this._hashObj.toString();
	if (send_type == PwJs.Config.SEND_BY_NAV) {
		window.navigator.params.urlhash = escape(hashstr);
	} else {
		this.sendHashToChannel(hashstr);
	}
};
PwJs.Client.prototype.sendHashToChannel = function(hash) {
	if (this._channelDom && this._channelSrc) {
		var sign = this._channelSrc.indexOf("?")>-1 ? "&" : "?";
		try {
			this._channelDom.src = this._channelSrc + sign + "t=" + (new Date().getTime()) + "#" + hash;
		} catch(ex){}
	}
};
PwJs.Client.prototype.callFunc = function() {
	if (this._func.length > 0) {
		for (var i=0; i < this._func.length; i++) {
			var o = this._func[i];
			this._hashObj.put(o.name,o.args.join('__pwps__'));
		}
		this._hashObj.put(PwJs.Config.CALL_VER,new Date().getTime());
	}
};
PwJs.Client.prototype.addFunc = function(funcName,args) {
	this._func.push(new PwJs.FuncObject(funcName,args));
};

PwJs.FuncObject = function(funcName, args) {
	this.name = funcName;
	this.args = args;
};

PwJs.Channel = function() {
	var hash = new PwJs.Hash(location.href);
	try {
		parent.parent.server._hash = escape(hash.toString());
	} catch(e){}
};

PwJs.Ajax = function() {
	if (PwJs.Ajax.uniqueInstance != null) {
		return PwJs.Ajax.uniqueInstance;
	}
	this.responseText = null;
	this.request = null;
	this.recall	 = null;
	this.time    = null;
	this.t       = null;
	this.last	 = 0;
	this.serverUrl = 'http://apps.phpwind.net/apps.php?pw_ajax=1';

	PwJs.Ajax.uniqueInstance = this;
};
PwJs.Ajax.uniqueInstance = null;

PwJs.Ajax.AjaxObj = function() {
	var s = document.createElement('div');
	s.style.display = 'none';
	s.innerHTML = '<iframe id="ajaxiframe" name="ajaxiframe" width="0" height="0" src=""></iframe>';
	document.body.appendChild(s);
	this.iframe = s.firstChild;

	this.post = function(url,data) {
		if (typeof data == 'string' && data != '') {
			var f = document.createElement('form');
			f.name	 = 'ajaxform';
			f.target = 'ajaxiframe';
			f.method = 'post';
			f.action = url;
			var ds = data.split("&");
			for (var i = 0; i < ds.length; i++) {
				if (ds[i]) {
					var v	 = ds[i];
					var el	 = document.createElement('input');
					el.type  = 'hidden';
					el.name  = v.substr(0,v.indexOf('='));
					el.value = v.substr(v.indexOf('=')+1);
					f.appendChild(el);
				}
			}
			document.body.appendChild(f);
			f.submit();
			document.body.removeChild(f);
		} else if (typeof data == 'object') {
			data.target = 'ajaxiframe';
			data.submit();
		} else {
			self.ajaxiframe.location = url;
		}
	}
};

PwJs.Ajax.prototype.send = function(url,data,callback) {
	if (this.request == null) {
		this.request = new PwJs.Ajax.AjaxObj();
	}
	this.responseText = '';
	var its = this;

	var nowtime	= new Date().getTime();
	if (nowtime - this.last < 1500) {
		clearTimeout(this.t);
		this.t = setTimeout(function(){its.send(url,data,callback)},1500 + this.last - nowtime);
		return;
	}
	this.last = nowtime;

	if (true) {
		url = this.serverUrl + '&pw_query=' + PwJs.Ajax.convert(url);
		for (var i in PwJs.App.Config) {
			url += '&' + i + '=' + PwJs.Ajax.convert(PwJs.App.Config[i]);
		}
	}
	url	+= (url.indexOf("?") >= 0) ? "&nowtime=" + nowtime : "?nowtime=" + nowtime;
	
	this.recall = callback;
	if (typeof this.recall == "function") {
		PwJs.Event.regist(this.request.iframe, 'load', PwJs.Ajax.load);
	}
	this.request.post(url,data);
};
PwJs.Ajax.load = function() {
	var its = PwJs.Ajax.uniqueInstance;
	if (PwJs.Browser.IE) {
		its.responseText = (typeof its.request.iframe.contentWindow.document.XMLDocument != 'undefined') ? its.request.iframe.contentWindow.document.XMLDocument.text : null;
	} else {
		its.responseText = its.request.iframe.contentWindow.document.documentElement.firstChild.nodeValue;
	}
	PwJs.Event.unregist(its.request.iframe,'load', PwJs.Ajax.load);
	its.recall();
};
PwJs.Ajax.convert = function(str) {
	return str.replace(/\&/g,'%26');
};

PwJs.App = function() {
};

PwJs.Menu = function() {
	if (PwJs.Menu.uniqueInstance != null) {
		return PwJs.Menu.uniqueInstance;
	}
	this.pid  = null;
	this.obj  = null;
	this.w	  = null;
	this.h	  = null;
	this.t	  = 0;
	this.menu = null;
	PwJs.Menu.uniqueInstance = this;

	this.init();
};

PwJs.Menu.prototype.init = function() {
	this.menu = document.createElement('div');
	document.body.insertBefore(this.menu,document.body.firstChild);
};

PwJs.Menu.prototype.guide = function() {
	this.menu.innerHTML = '<div class="bor" style="padding:13px 30px"><img src="'+imgpath+'/loading.gif" align="absbottom" /> 页面加载中... </div>';
	this.menu.className = 'menu';
	this.menupz(this.obj,1);
};

PwJs.Menu.prototype.close = function() {
	this.t = setTimeout("PwJs.Menu.closep();",100);
};

PwJs.Menu.prototype.move = function(e) {
	if (PwJs.Browser.IE) {
		document.body.onselectstart = function(){return false;}
	}
	var e  = PwJs.Browser.IE ? window.event : e;
	var o  = this.menu;
	var x  = e.clientX;
	var y  = e.clientY;
	this.w = e.clientX - parseInt(o.offsetLeft);
	this.h = e.clientY - parseInt(o.offsetTop);
	var its = this;
	document.onmousemove = function(e) {
		its.moving(e);
	}
	document.onmouseup = function(e) {
		its.moved(e);
	}
};

PwJs.Menu.prototype.moving = function(e) {
	var e  = PwJs.Browser.IE ? window.event : e;
	var x  = e.clientX;
	var y  = e.clientY;
	this.menu.style.left = x - this.w + 'px';
	this.menu.style.top  = y - this.h + 'px';
};

PwJs.Menu.prototype.moved = function() {
	if (PwJs.Browser.IE) {
		document.body.onselectstart = function(){return true;}
	}
	document.onmousemove = '';
	document.onmouseup   = '';
};

PwJs.Menu.prototype.open = function(idName,object,type,pz) {
	clearTimeout(this.t);
	if (typeof type == "undefined") type = 1;
	if (typeof pz == "undefined") pz = 0;
	this.menu.innerHTML = getObj(idName).innerHTML;
	this.menu.className = getObj(idName).className;
	this.menupz(object,type,pz);

	if (type != 2) {
		var its = this;
		if (getObj(object)) {
			getObj(object).onmouseout = function() {
				its.close();
				getObj(object).onmouseout = '';
			}
		}
		this.menu.onmouseout = function(){
			its.close();
		}
		this.menu.onmouseover = function() {
			clearTimeout(its.t);
		}
	}
};

PwJs.Menu.prototype.menupz = function(obj,type,pz) {
	this.menu.onmouseout = '';
	this.menu.style.display = '';
	this.menu.style.cssText = 'FILTER:Alpha(opacity=95);opacity:0.95;left:-500px;z-index:3000';
	this.menu.style.visibility = 'visible';
	if (typeof obj == 'string') {
		obj = getObj(obj);
	}
	if (obj == null) {
		this.menu.style.top  = (PwJs.Menu.ietruebody().clientHeight - this.menu.offsetHeight)/3 + ietruebody().scrollTop + 'px';
		this.menu.style.left = (PwJs.Menu.ietruebody().clientWidth - this.menu.offsetWidth)/2 + 'px';
	} else {
		var top  = PwJs.Menu.findPosY(obj);
		var left = PwJs.Menu.findPosX(obj);
		var pz_h = Math.floor(pz/10);
		var pz_w = pz % 10;
		
		if (pz_h!=1 && (pz_h==2 || top < PwJs.Menu.ietruebody().clientHeight/2)) {
			top += PwJs.Menu.ietruebody().scrollTop + obj.offsetHeight;
		} else {
			top += PwJs.Menu.ietruebody().scrollTop - this.menu.offsetHeight;
		}
		if (pz_w!=1 && (pz_w==2 || left > (PwJs.Menu.ietruebody().clientWidth)*3/5)) {
			left -= this.menu.offsetWidth - obj.offsetWidth;
		}
		this.menu.style.top  = top  + 'px';
		this.menu.style.left = left + 'px';
	}
};

PwJs.Menu.prototype.IsShow = function() {
	return (this.menu.hasChildNodes() && this.menu.style.display != 'none') ? true : false;
};

PwJs.Menu.prototype.showIframe = function (iframeID){
	var ifm=document.getElementById(iframeID);
	ifm.style.position="static";
	ifm.style.left="0px";
	ifm.style.top="0px";
	ifm.removeAttribute("style");
};
PwJs.Menu.uniqueInstance = null;

PwJs.Menu.closep = function() {
	if (PwJs.Menu.uniqueInstance) {
		PwJs.Menu.uniqueInstance.menu.style.display = 'none';
	}
};

PwJs.Menu.findPosX = function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft - PwJs.Menu.ietruebody().scrollLeft;
};

PwJs.Menu.findPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop - PwJs.Menu.ietruebody().scrollTop;
};

PwJs.Menu.ietruebody = function() {
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
};
