/*********************************************************
*		AJAX CLASS
*		Current class is responsible for the ajax calls
*		Designed & developed by Dima Svirid, 2007	
*		Class: ajax.js
*	  Extends: system.js
*********************************************************/
$WI.Ajax = function(options) {
	return new $WI.Class.Ajax()._Request(options);
};
$WI.Hash = function(val, hexcase) {
	return new $WI.Class.Hash().MD5(val, hexcase);
};
$WI.Class.Ajax = new $WI.Class({
	Request: function(options) {		
		options._constructor = this;		
		return $WI.Ajax(options);
	},		
	onComplete: function() {		
		//throw main error		
		if(this.options.onComplete) 
			this.options.onComplete.apply(this.options._constructor, arguments);		
		return;
	},	
	onTimeout: function() {		
		//throw main timeout		
		if(this.options.onTimeout) 
			this.options.onTimeout.apply(this.options._constructor);		
		else
			this._onTimeout();
		return;
	},	
	onFailure: function() {
		if(this.options.onFailure)
			this.options.onFailure.Apply(this, arguments);
		return;
	},
	onOpen: function() {
		if(this.options.onOpen)
			this.options.onOpen.Apply(this, arguments);
		return;
	},
	onSent: function() {
		if(this.options.onSent)
			this.options.onSent.Apply(this, arguments);
		return;
	},
	onReceived: function() {
		if(this.options.onReceived)
			this.options.onReceived.Apply(this, arguments);
		return;
	},
	_Request: function(options) {
		this._initOptions(options);
		//check cache
		if(this.options.cache) {
			var ___cache = this._checkCache();
			if(___cache) {
				this._cacheRequest(___cache);
				return this;
			}
		}
		this._detectEngine();				
		this._run();
		this.timeOut(true);
		return this;
	},
	/*******************************************************************************************
	*	:status
	*	200 - Ok
	*	404 - Page is not found	
	*******************************************************************************************/	
	_onComplete: function(cache) {
		this.engine.onreadystatechange = function(){};			
		switch(this.engine.status) {
			case 200:			
				//remove top right cornet preloader		
				if(this.preloader)this.RemoveDOM(this.preloader);				
				//clear timeout error
				if(this.timeout) clearTimeout(this.timeout);
				//prevent and throw global error
				var txt = this.engine.responseText;
				var regx = /(^PrismErrorException:)/gi;
				if(result = new RegExp(regx).exec(txt)) {
					$WI.DOM.Alert({message: txt.replace(regx, '')});
					return;
				}	
				//cache result	
				if(this.options.cache&&!cache) this._setCache(this.engine.responseText);
				this.onComplete($WI.Method.XML.Init(this.engine.responseText), txt, this.options.instance);
				break;
			default:
				if(this.preloader)this.RemoveDOM(this.preloader);	
				this.onFailure();
				break;		
		}
	},
	_onTimeout: function() {
		if(this.preloader)this.RemoveDOM(this.preloader);	
		//this.Confirm({message: "Connection with the server cannot be established! Would you like to retry?", YesFunction: function(){this._retryRequest()}.Apply(this)});		
	},
	_retryRequest: function(){
		this._run();
		this.timeOut(true);
	},
	_detectEngine: function() {		
		this.engine = $WI.System.Check(
																function(){return new XMLHttpRequest();},
																function(){return new ActiveXObject('Msxml2.XMLHTTP');},
																function(){return new ActiveXObject('Microsoft.XMLHTTP');}
																);
	},
	_run: function() {
		//red left top corner preloader
		if(this.options.preloader) {
			var xy = this._getScrollXY();
			this.preloader = this._insertDOM(null, {objType: 'div', display: 'block', position: 'absolute', top: this._fixPx(xy.y), right: this._fixPx(-xy.x), backgroundColor: '#bd1111',	color: '#ffffff',	fontSize: '10pt',	width: '80px',	height: '20px',	padding: '2px',	paddingLeft: '5px',	zIndex: '100000', html: 'Loading...'}, 'insertlast');		
		}		
		
		this.engine.open(this.options.method.toUpperCase(), this.options.url, this.options.async);		
    this.engine.onreadystatechange = this._changeStatus.Apply(this);	
			
		//set headers		
		if (this.options.method.toUpperCase()=='POST') 
			this.engine.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
		this.engine.setRequestHeader('Powered-By', 'PRISM');
		this.engine.setRequestHeader('Pragma', 'no-cache');
		this.engine.setRequestHeader('Cache-Control', 'no-cache');
		this.engine.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
		this.engine.setRequestHeader('Connection', 'close');
		
    this.engine.send((this.options.method.toUpperCase()=='POST')?this.options.parameters:null);
		
	},
	/*******************************************************************************************
	*	:readyState
	*	0 - Not initialized
	*	1 - Open
	*	2 - Sent
	*	3 - Received
	*	4 - Loaded
	*******************************************************************************************/	
	_changeStatus: function() {
		switch(this.engine.readyState) {
			case 1:
				this.onOpen();			break;
			case 2:
				this.onSent();			break;
			case 3:
				this.onReceived();	break;	
			case 4:				
				this._onComplete();	break;
		}
  },
	_initOptions: function(options) {
		 this.options = {
		 								url: '',
										parameters: '',
										method: 'get',
										timeout: 15,			//timeout in seconds
										async: true,
										_constructor: this								
										}
		 $WI._append(this.options, options);
	},
	timeOut: function(set){
		if(set) {
			if(this.options.timeout && this.options.timeout > 0)
				this.timeout = setTimeout(function(){this.timeOut()}.Apply(this), this.options.timeout*1000);
		} else {			
			this.engine.onreadystatechange = function(){};
			this.engine.abort();	
			this._onTimeout();		
		}
		return false;
	},
	_cacheRequest: function(cache){
		this.engine = {status: 200, responseText: cache.result};
		this._onComplete(true);
	},
	_checkCache: function(){
		if(!$WI.GLOBAL_AJAX_REGISTRY) return false;
		for(var i=0;i<$WI.GLOBAL_AJAX_REGISTRY.length;i++) {
			var ___cache = $WI.GLOBAL_AJAX_REGISTRY[i];
			if(___cache.url==this.options.url&&___cache.method==this.options.method&&___cache.parameters==this.options.parameters)
				return ___cache;
		}
	},
	_setCache: function(result){
		if(!$WI.GLOBAL_AJAX_REGISTRY) $WI.GLOBAL_AJAX_REGISTRY = [];		
		$WI.GLOBAL_AJAX_REGISTRY.push({url: this.options.url,
																	 method: this.options.method,
																	 parameters: this.options.parameters,
																	 result: result});
	}
});
$WI.extend($WI.Class.DOM, $WI.Class.Ajax);

/*********************************************************
*		XML STATIC METHODS
*********************************************************/
$WI.Method.XML = {
	Init: function(text) {	
		return new $WI.Class.XML().Init(text);				
	},	
	CreateNode: function(xmlDoc, what, value, where) {
		if(!what||!xmlDoc) return;
		if(where) var x=xmlDoc.getElementsByTagName(where);
		else var x=xmlDoc.documentElement;
		
		var newel=xmlDoc.createElement(what);
  	var newtext=xmlDoc.createTextNode(value);
  			newel.appendChild(newtext);
  			x.appendChild(newel);				
				
		return newel;
	},	
	toString: function(obj){
		var ret = "";
		for (var i=0; i < obj.length; i++) {
	    ret += obj._nodes[i].toString();
	  }
  	return ret;
	} 	
};
$WI.XML = $WI.Method.XML;
/*********************************************************
*		XML CLASS
*********************************************************/
$WI.Class.XML = new $WI.Class({
	Init: function(text) {		
		try{
			if (this._isIE()) {
		    	$WI.System.Check (
		    		function() { axDom = new ActiveXObject("MSXML2.DOMDocument.4.0"); },
	    			function() { axDom = new ActiveXObject("MSXML2.DOMDocument.5.0"); },
					function() { axDom = new ActiveXObject("MSXML2.DOMDocument.3.0"); },
		    		function() { axDom = new ActiveXObject("MSXML2.DOMDocument"); },
		    		function() { axDom = new ActiveXObject("Microsoft.XmlDom"); }
			    );
			    this.xmlDoc = axDom;
			} else {
				this.xmlDoc = document.implementation.createDocument("", "", null);
			}
			if (this._isIE()) {
	      this.xmlDoc.async = false;
		  	this.xmlDoc.loadXML(text);
			} else {
		   var parser=new DOMParser();
		   this.xmlDoc = parser.parseFromString(text,"text/xml");
			}
		} catch(err){
			this.xmlDoc = null;
		}
		return this;
	},
	List: function(root, xml) {
		if(!root) return [];
		if(!xml) var xml = this.xmlDoc;
		var root = xml.getElementsByTagName(root);		
		var list = [];
		var obj = {};
		if(root.length>0)
			var children = this._getChildren(root[0]);
		else
			 return [];
		//get attributes		
		for(var i=0;i<children.length;i++) {
			var atribs = this._getChildren(children[i]);
			obj = {};			
			if(atribs.length>0) {					
				for(var j=0;j<atribs.length;j++) {	
					if(this._getChildren(atribs[j]).length>0)
						obj[atribs[j].tagName] = this.List(atribs[j].tagName, atribs[j].parentNode);
					else	
						obj[atribs[j].tagName] = (atribs[j].firstChild)?atribs[j].firstChild.nodeValue:'';
				}
			}			
			list.push(obj);
		}
  	return list;
	}, 	
	getAttributes: function(xpath) {
		var el = this.getNode(xpath);
		var attrs = {};
		if(el.attributes) 
			for(var i=0;i<el.attributes.length;i++)
				attrs[el.attributes[i].name] = el.attributes[i].value;	
		return attrs;
	},	
	getNode: function(xpath) {
		if(this._isIE() || this._isOpera()) {
			var result = this.xmlDoc.selectSingleNode(xpath);
		} else if(this._isSafari2()) {
			var result = this.getNodeByTagName(xpath);
		} else {			
			
			var evaluator = new XPathEvaluator();				
			var result = evaluator.evaluate(xpath, this.xmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		}
		return result;
	},
	getNodeByTagName: function(xpath, xml) {
		var node = null;
		if(!xml) {			
			var xml = this.xmlDoc;
			var xpath = xpath.replace(/\/\//,'').split('/');			
		}	
		var root = xml.getElementsByTagName(xpath[0]);	
		var _xpath = xpath.slice(1);
		if(root.length>0&&_xpath&&_xpath.length>0)
			return this.getNodeByTagName(_xpath, root[0]);	
		else 
			return root[0];
	},
	getNodeValue: function(xpath) {
		var value = null;
		try {
			var node = this.getNode(xpath);		
			if ((this._isIE()  || this._isOpera()) && node) value = node.text;
			else if (this._isSafari2() && node.firstChild) value = node.firstChild.nodeValue;
			else if (!this._isIE() && !this._isOpera() && node.singleNodeValue) value = node.singleNodeValue.textContent;
		} catch (e) {}		
		return value;
	},
	getNodes: function(xpath) {
		var nodes = [];
		if (this._isIE()) {
			var result = this.xmlDom.selectNodes(xpath);
			for (var i = 0; i < result.length; i++) {
				var aNode = result[i];
				nodes.push(aNode);
			}
		} else {
			var evaluator = new XPathEvaluator();			
			var result = evaluator.evaluate(xpath, this.xmlDom, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
			while ((aNode = result.iterateNext()) != null) {
				 nodes.push(aNode);
			}
		}
		return nodes;
	},
	getNodeValues: function(xpath) {
		var values = [];
		try {
			var nodes = this.getNodes(xpath);		
			for (var i = 0; i < nodes.length; i++) {
				var node = nodes[i];	
				if (this._isIE() && node) {
					value = node.text;
				} else if (!this._isIE()) {
					value = node.firstChild.nodeValue;
				}		
				values.push(value);
			}
		} catch (e) {}		
		return values;
	}
});
/*********************************************************
*		HASH CLASS
*********************************************************/
$WI.Class.Hash = new $WI.Class({
	MD5: function(val, hexcase) {	
		this.val = val;
		this.chrsz = 8;
		if(hexcase) this.hexcase = 1; else this.hexcase = 0;			
		this.val = this.str2binl();		
		this.val = this.core_md5(val.length*this.chrsz);		
		this.val = this.binl2hex();
		return this.val;
	},
	str2binl: function() {
	  var bin = Array();
	  var mask = (1 << this.chrsz) - 1;
	  for(var i = 0; i < this.val.length * this.chrsz; i += this.chrsz)
	    bin[i>>5] |= (this.val.charCodeAt(i / this.chrsz) & mask) << (i%32);
	  return bin;
	},
	binl2hex: function ()	{
	  var hex_tab = this.hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
	  var str = "";
	  for(var i = 0; i < this.val.length * 4; i++) {
	    str += hex_tab.charAt((this.val[i>>2] >> ((i%4)*8+4)) & 0xF) +
	           hex_tab.charAt((this.val[i>>2] >> ((i%4)*8  )) & 0xF);
	  }
	  return str;
	},
	core_md5: function(len) {
	  this.val[len >> 5] |= 0x80 << ((len) % 32);
	  this.val[(((len + 64) >>> 9) << 4) + 14] = len;	
	  var a =  1732584193;
	  var b = -271733879;
	  var c = -1732584194;
	  var d =  271733878;	
	  for(var i = 0; i < this.val.length; i += 16) {
	    var olda = a;
	    var oldb = b;
	    var oldc = c;
	    var oldd = d;	
	    a = this.md5_ff(a, b, c, d, this.val[i+ 0], 7 , -680876936);d = this.md5_ff(d, a, b, c, this.val[i+ 1], 12, -389564586);c = this.md5_ff(c, d, a, b, this.val[i+ 2], 17,  606105819);b = this.md5_ff(b, c, d, a, this.val[i+ 3], 22, -1044525330);a = this.md5_ff(a, b, c, d, this.val[i+ 4], 7 , -176418897);d = this.md5_ff(d, a, b, c, this.val[i+ 5], 12,  1200080426);c = this.md5_ff(c, d, a, b, this.val[i+ 6], 17, -1473231341);b = this.md5_ff(b, c, d, a, this.val[i+ 7], 22, -45705983);a = this.md5_ff(a, b, c, d, this.val[i+ 8], 7 ,  1770035416);d = this.md5_ff(d, a, b, c, this.val[i+ 9], 12, -1958414417);c = this.md5_ff(c, d, a, b, this.val[i+10], 17, -42063);b = this.md5_ff(b, c, d, a, this.val[i+11], 22, -1990404162);a = this.md5_ff(a, b, c, d, this.val[i+12], 7 ,  1804603682);d = this.md5_ff(d, a, b, c, this.val[i+13], 12, -40341101);c = this.md5_ff(c, d, a, b, this.val[i+14], 17, -1502002290);b = this.md5_ff(b, c, d, a, this.val[i+15], 22,  1236535329);a = this.md5_gg(a, b, c, d, this.val[i+ 1], 5 , -165796510);d = this.md5_gg(d, a, b, c, this.val[i+ 6], 9 , -1069501632);c = this.md5_gg(c, d, a, b, this.val[i+11], 14,  643717713);b = this.md5_gg(b, c, d, a, this.val[i+ 0], 20, -373897302);a = this.md5_gg(a, b, c, d, this.val[i+ 5], 5 , -701558691);d = this.md5_gg(d, a, b, c, this.val[i+10], 9 ,  38016083);c = this.md5_gg(c, d, a, b, this.val[i+15], 14, -660478335);b = this.md5_gg(b, c, d, a, this.val[i+ 4], 20, -405537848);a = this.md5_gg(a, b, c, d, this.val[i+ 9], 5 ,  568446438);d = this.md5_gg(d, a, b, c, this.val[i+14], 9 , -1019803690);c = this.md5_gg(c, d, a, b, this.val[i+ 3], 14, -187363961);b = this.md5_gg(b, c, d, a, this.val[i+ 8], 20,  1163531501);a = this.md5_gg(a, b, c, d, this.val[i+13], 5 , -1444681467);d = this.md5_gg(d, a, b, c, this.val[i+ 2], 9 , -51403784);c = this.md5_gg(c, d, a, b, this.val[i+ 7], 14,  1735328473);b = this.md5_gg(b, c, d, a, this.val[i+12], 20, -1926607734);a = this.md5_hh(a, b, c, d, this.val[i+ 5], 4 , -378558);d = this.md5_hh(d, a, b, c, this.val[i+ 8], 11, -2022574463);c = this.md5_hh(c, d, a, b, this.val[i+11], 16,  1839030562);b = this.md5_hh(b, c, d, a, this.val[i+14], 23, -35309556);a = this.md5_hh(a, b, c, d, this.val[i+ 1], 4 , -1530992060);d = this.md5_hh(d, a, b, c, this.val[i+ 4], 11,  1272893353);c = this.md5_hh(c, d, a, b, this.val[i+ 7], 16, -155497632);b = this.md5_hh(b, c, d, a, this.val[i+10], 23, -1094730640);a = this.md5_hh(a, b, c, d, this.val[i+13], 4 ,  681279174);d = this.md5_hh(d, a, b, c, this.val[i+ 0], 11, -358537222);c = this.md5_hh(c, d, a, b, this.val[i+ 3], 16, -722521979);b = this.md5_hh(b, c, d, a, this.val[i+ 6], 23,  76029189);a = this.md5_hh(a, b, c, d, this.val[i+ 9], 4 , -640364487);d = this.md5_hh(d, a, b, c, this.val[i+12], 11, -421815835);c = this.md5_hh(c, d, a, b, this.val[i+15], 16,  530742520);b = this.md5_hh(b, c, d, a, this.val[i+ 2], 23, -995338651);a = this.md5_ii(a, b, c, d, this.val[i+ 0], 6 , -198630844);d = this.md5_ii(d, a, b, c, this.val[i+ 7], 10,  1126891415);c = this.md5_ii(c, d, a, b, this.val[i+14], 15, -1416354905);b = this.md5_ii(b, c, d, a, this.val[i+ 5], 21, -57434055);a = this.md5_ii(a, b, c, d, this.val[i+12], 6 ,  1700485571);d = this.md5_ii(d, a, b, c, this.val[i+ 3], 10, -1894986606);c = this.md5_ii(c, d, a, b, this.val[i+10], 15, -1051523);b = this.md5_ii(b, c, d, a, this.val[i+ 1], 21, -2054922799);a = this.md5_ii(a, b, c, d, this.val[i+ 8], 6 ,  1873313359);d = this.md5_ii(d, a, b, c, this.val[i+15], 10, -30611744);c = this.md5_ii(c, d, a, b, this.val[i+ 6], 15, -1560198380);b = this.md5_ii(b, c, d, a, this.val[i+13], 21,  1309151649);a = this.md5_ii(a, b, c, d, this.val[i+ 4], 6 , -145523070);d = this.md5_ii(d, a, b, c, this.val[i+11], 10, -1120210379);c = this.md5_ii(c, d, a, b, this.val[i+ 2], 15,  718787259);b = this.md5_ii(b, c, d, a, this.val[i+ 9], 21, -343485551);	
	    a = this.safe_add(a, olda);
	    b = this.safe_add(b, oldb);
	    c = this.safe_add(c, oldc);
	    d = this.safe_add(d, oldd);
	  }
	  return Array(a, b, c, d);	
	},
	md5_cmn: function (q, a, b, x, s, t) {
	  return this.safe_add(this.bit_rol(this.safe_add(this.safe_add(a, q), this.safe_add(x, t)), s),b);
	},
	md5_ff: function (a, b, c, d, x, s, t) {
	  return this.md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
	},
	md5_gg: function (a, b, c, d, x, s, t) {
	  return this.md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
	},
	md5_hh: function (a, b, c, d, x, s, t) {
	  return this.md5_cmn(b ^ c ^ d, a, b, x, s, t);
	},
	md5_ii: function (a, b, c, d, x, s, t) {
	  return this.md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
	},
	safe_add: function (x, y) {
	  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
	  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
	  return (msw << 16) | (lsw & 0xFFFF);
	},
	bit_rol: function (num, cnt) {
	  return (num << cnt) | (num >>> (32 - cnt));
	}
});

/*********************************************************
*		HISTORY FUNCTIONALITY CLASS
*********************************************************/
$WI.Class.History = new $WI.Class({
	Init: function(options) {	
		if(!options) var options = {};
		this.options = options;
		//create red preloader message, google style
		if(this.options.preloader) {
			var xy = this._getScrollXY();
			this.preloader = this._insertDOM(null, {objType: 'div', display: 'none', position: 'absolute', top: this._fixPx(xy.y), right: this._fixPx(-xy.x), backgroundColor: '#bd1111',	color: '#ffffff',	fontSize: '10pt',	width: '80px',	height: '20px',	padding: '2px',	paddingLeft: '5px',	zIndex: '100000', html: 'Loading...'}, 'insertlast');		
		}		
		if(!this.history_interval)
			this.history_interval = setInterval(function(){this.Go()}.Apply(this), 100);
		this.Parse();
		this.Reg(this.GetBrowserURLAnchor(document.location.href, true));		
  },
	Parse: function() {
		this._handleLinks();
	},
	Go: function(url) {			
		var sURL = this.GetBrowserURLAnchor();	
		if(sURL&&(!this.prev||(this.prev&&this.prev!=sURL))) {
			this.entrance = false;
			this.prev = sURL;
			this._go(sURL);		
		} else if(!this.entrance&&sURL=='') {			//this block is required to get back to the homepage
			this.entrance = true;
			this._go(sURL);		
		}	
		//hide red preloader
		if(this.preloader)
			this._isDisplay(this.preloader, false);
	},	
	Reg: function(str) {			
		if(str.substring(0, 1)=='/') str = str.substring(1);
		var is_replace = (str.substring(0, 1)=='.');	//replace mode
		var result = [];
		
		if(result = new RegExp(/(top[/]false)/gi).exec(str)) {
		} else {
			if(this.options && this.options.scrollTopPage && $_VAR(this.options.scrollTopPage, true)) {
				if(document.documentElement.scrollTop)
					document.documentElement.scrollTop = '0px';
				else
					document.body.scrollTop = '0px';
			}
		}		
		//$WI.trace(str)		
		if(is_replace) {
			var _str = this.GetBrowserURLAnchor();			
			var _pra = str.substring(2).split('/');			
			for(var i=0;i<_pra.length;i = i + 2) {
				if(_pra[i+1]=='$')
					_str = this._removeKey(_str, _pra[i]);
				else
					_str = this._replaceValue(_str, _pra[i], _pra[i+1]);
			}
			str = _str;			
		}	
		str = escape(str);
		
		if(this._isIE()) {			
			if(!this.iframe)
				this.iframe = this._insertDOM(null, {objType: 'iframe', frameBorder: 0, width: '0px', height: '0px'}, 'insertlast');			
			var uuid = $WI.UUID();
			this.iframe.src = '/api/src/html/back.html?uuid=' + uuid + '|' + str;	
		}	else
			location.href = '#' + str;
		
		//show red preloader
		if(this.preloader) {
			var wh = this._getClientWH();			
			this._isDisplay(this.preloader, true);	
			var w = this._getWidth(this.preloader);
			var xy = this._getScrollXY();
			this._applyConfig(this.preloader, {top: this._fixPx(xy.y), right: this._fixPx(-xy.x)});	
		}
	},
	GetParamValue: function(key) {
		var str = this.GetBrowserURLAnchor();
		var arr = str.split('/');
		for(var i=0;i<arr.length;i = i + 2)
			if(arr[i]==key)
				return arr[i+1];
	},
	GetBrowserURLAnchor: function(fullURL, loc) {	//loc true force to take data from the browser location field	
		if(loc) {
			var separator = '#';
			var fullURL = document.location.href;
		} else {
			if(this._isIE()) var separator = '|'; else  var separator = '#';
			if(!fullURL) var fullURL = (this._isIE()&&this.iframe)?this._getDoc().title:document.location.href;		
		}		
		if(fullURL.indexOf(separator)==-1) return '';
		return fullURL.substring(fullURL.indexOf(separator)+1, fullURL.length);	
	},	
	RemoveKey: function(key){
		var str = this.GetBrowserURLAnchor();
		str = this._removeKey(str, key);
		$WI.History.Reg(str);	
	},
	RemoveValue: function(key, val) {
		var str = this.GetBrowserURLAnchor();
		var arr = str.split('/');
		for(var i=0;i<arr.length;i = i + 2) {
			if(arr[i]==key) {
				var _arr = arr[i+1].split(',');
				var _num = _arr.Search(val);
				if(_num!=-1) _arr.splice(_num, 1);
				else _arr = [];				
				arr[i+1] = _arr.slice(0);
				break;
			}
		}	
		str = '';
		for(var i=0;i<arr.length;i = i + 2) {
			if(i!=0) str += '/'; 
			str += arr[i] + '/' + arr[i+1];
		}
		$WI.History.Reg(str);	
	},
	_removeKey: function(str, key) {
		var arr = str.split('/');
		var newarr = [];
		if(arr.length==1) arr = [];
		for(var i=0;i<arr.length;i = i + 2)
			if(arr[i]!=key) 
				newarr.splice(newarr.length, 2, arr[i], arr[i+1]);
		
		return this._formStr(newarr);
	}, 
	_replaceValue: function(str, key, val) {
		var arr = str.split('/');
		if(arr.length==1) arr = [];
		var added = false;
		for(var i=0;i<arr.length;i = i + 2) {
			if(arr[i]==key) {
				arr[i+1] = val;
				//var _arr = arr[i+1].split(',');
				//alert(_arr)
				//var _num = _arr.Search(val);				
				//if(_num!=-1) _arr.splice(_num, 1);
				//else _arr = [];				
				//arr[i+1] = _arr.slice();
				added = true;
				break;
			} 
		}	
		//add elements at the end if they are not there yet
		if(!added) 
			arr.splice(arr.length, 2, key, val);

		return this._formStr(arr);
	},  
	_formStr: function(arr) {
		str = '';
		for(var i=0;i<arr.length;i = i + 2) {
			if(i!=0) str += '/'; 
			str += arr[i] + '/' + arr[i+1];
		}
		return str;
	},
	_go: function(sURL) {		
		if(this._isIE())
			location.href = '#' + escape(sURL);
		var aUrl = sURL.split('/');		
		this.prev = sURL;			
		
		this.options._construct.History(sURL, aUrl);
	},
	_getDoc: function() {
		if(!this.iframe) return document;
		if (this.iframe.contentDocument)  
      return this.iframe.contentDocument;
    else
      return this.iframe.contentWindow.document;			
  },
	_handleLinks: function() {
		this._handleLinksLoop(D.getElementsByTagName('a'));
		this._handleLinksLoop(D.getElementsByTagName('div'));
	},
	_handleLinksLoop: function(children) {
		var len = children.length;		
		var rexp = /(#wi\/)([A-z/\d\/\-_]*)/gi;	
		var _link = '';		
		
		for(var i=0;i<len;i++) {			
			if(children[i].tagName.toLowerCase()=='a') _link = children[i].href;
			else _link = children[i].onclick;
				
			if(!_link) continue;
			if(result = new RegExp(rexp).exec(_link)) {
				if(result.length==3) { 
					
					if(children[i].tagName.toLowerCase()=='a')
						children[i].href = "javascript:$WI.Go('" + result[2] + "')";		
					//else
						//if(children[i].className=='turnit-stock-clip-thumb cdc')$WI.trace(result[2]);
						//this.AddDOMEvent({obj: children[i], type: 'click', onevent: function(){alert(param);$WI.History.Reg(param)}.Apply(this, [result[2]])});
						//children[i].onclick = "$WI.History.Reg('" + result[2] + "')";		
						
						//children[i].onclick = alert('ff')
				}
			}
		}
	}
});
$WI.History = new $WI.Class.History();
$WI.Go = function(str){$WI.History.Reg(str);};
