var Run= window.Run || {};

function Group(O){
	if(!(this instanceof Group)){
		return new Group(O);
	}
	if(!O || typeof O!= 'object') O= {};
	for(var p in O){
		if(O.hasOwnProperty(p)) this[p]= O[p];
	}
}
if(Object.hasOwnProperty){
	if(window.addEventListener){
		Run.handler= function(who, typ, fun, mod){
			mod= !!mod;
			if(who && who.addEventListener){
				who.addEventListener(typ, fun, mod);
			}
			return who;
		}
	}
	else{
		/*@cc_on
		@if(@_jscript_version> 5.5){
			navigator.IEmod= document.documentMode? document.documentMode:
			window.XMLHttpRequest? 7: 6;
			if(window.attachEvent){
				Run.handler= function(who, typ, fun){
					if(who && who.attachEvent){
						who.detachEvent('on'+typ, fun);
						who.attachEvent('on'+typ, fun);
					}
					return who;
				}
			}
		}
		@end
		@*/
	}
}

if(Run.handler){
	if(window.Storage && /^http/i.test(location.protocol)){		
		Run.getStore= function(what, local){
			try{
				var store= local? window.localStorage: window.sessionStorage;
				return what=== true? store: store[what];
			}
			catch(er){}
		}	
	}
	var Memry= Group(window.Memry), Run= Group(Run), Yankee= Group(window.Yankee);
	Group.prototype.merge= function(O, force){
		var tem;
		for(var p in O){
			if(O.hasOwnProperty(p)){
				if(this[p]== undefined || force=== true) this[p]= O[p];
				else if(typeof this[p]== 'object'){
					Run.merge.call(this[p], O[p]);
				}
			}
		}
		return this;
	}
	Group.prototype.assignTo= function(O, O2, force){
		return this.merge.call(O, O2, force);
	}
	Memry.merge({
		aka: Group({
			hooWin: window, hooDoc: document
		}),
		dob: +(new Date), dowhat: [], gadgets: '',
		errorLog: [], idNext: 1, 
		new_atts:new Group, pageLog: [],
		savecook: ['LP', 'addSrc', 'gadgets'],
		setHotQ: [], Shadow: {}
	});
	Memry.Rte= Memry.Rte || (function(){
		var s= '/webworks/', rx=/\\/g,
		L= location.href.replace(rx, '/'),
		p= location.pathname.replace(rx, '/'),
		ax= L.indexOf(s);
		ax= (ax== -1)? L.indexOf(p)+ 1: ax+ s.length;
		return L.substring(0, ax);
	})();
	Run.assignTo(Array.prototype,{
		indexOf: function(what, i){
			if(typeof i!='number')i= 0;
			var L= this.length;
			while(i< L){
				if(this[i]=== what) return i;
				++i;
			}
			return -1;
		},
		unique: function(){
			var L= this.length, i= 0, A= [], tem;
			while(i< L){
				tem= this[i++];
				if((tem || tem=== 0) && A.indexOf(tem)== -1) A[A.length]= tem;
			}
			return A;
		}
	});
	Run.assignTo(String.prototype,{
		filename: function(ext){
			var s= this.replace(/\\/g, '/');
			s= s.substring(s.lastIndexOf('/')+ 1);
			return ext? s.replace(/[?#].+$/, ''): s.split('.')[0];
		},
		reval: function(O){
		 	var s= String(this);
			if(parseFloat(s)===Number(s)) return parseFloat(s);
			if(s=== 'true' || s=== 'false') return s==='true';
			O= O || window;
			var N= s.split('.');
			while(O && N.length) O= O[N.shift()];
			return O || s;
		},
		trim: function(){
			return this.replace(/^\s+/, '').replace(/\s+$/, '');
		},
		unique: function(dlim){
			if(dlim== undefined) dlim=  /[,;]\s*/;
			return (this.split(dlim)).unique();
		}
	});
	Yankee.Timelog=function(str, limit, time){
		if(!(this instanceof Yankee.Timelog)){
			return new Yankee.Timelog(str, limit, time);
		}
		this.startT= time=== true? Memry.dob: +new Date;
		this.timeline= [];
		this.num= 0;
		if(limit!== true) this.limit= limit || 8;
		str= str || 'Begin Logging';
		this.timeline[0]= [this.startT, str];
	}
	Yankee.Timelog.prototype.push= function(str, once){
		var T= this.timeline, d= +new Date, L= T.length, i= L;
		if(once){
			while(i> 1){
				if(T[--i][1]=== str) return this;
			}
		}
		T[L]= [d, str];
		if(this.limit && T.length> this.limit){
			i= T.length- this.limit;
			T.splice(1, i);
			this.num+= i;
		}
		return this;
	}	
	Run.merge({
		addLink: function(url, atts){
			var hoo= 'link_'+ url.filename(), def, el= Memry.aka[hoo];
			if(!el){
				def={
					rel: 'stylesheet', type: 'text/css', href: Run.pathTrack(url)
				}
				if(atts) Run.assignTo(def, atts, true);
				el= Run.newTag('link', def);
				if(Memry.trackpage) Memry.pageLog.push(hoo+ '.css', 1);
			}
			return Run.addtoHead(el, hoo);
		},
		addScript: function(url){
			var tem, atts, el, hoo= url.filename();
			if(Memry.aka[hoo]) return Memry.aka[hoo];
			el= Run.newTag('script',{
				type: 'text/javascript', src: url
			});
			return Run.addtoHead(el, hoo);
		},
		addStyle: function(str, hoo){
			if(!hoo) hoo= 'style_'+(Memry.idNext++);
			str= str || '';
			if(str.indexOf('url(')!= -1) str= Run.pathCss(str);
			var el= Run.newTag('style',{
				type: 'text/css', media: 'screen'
			});
			if(el.styleSheet) el.styleSheet.cssText= str;
			else{
				el.appendChild(document.createTextNode(str));
			}
			return Run.addtoHead(el, hoo);
		},
		addtoHead: function(who, hoo){
			document.getElementsByTagName('head')[0].appendChild(who);
			if(typeof hoo== 'string') Memry.aka[hoo]= who;
			return who;
		},
		AJ: function(boo){
			var o= window.XMLHttpRequest;
			if(o){
				return boo=== true? /^http/i.test(location.protocol): new o;
			}
			return false;
		},
		css: Group(),
		Engage: function(){
			var mf= Memry.loadfiles || [];
			if(!Memry.loadtime){
				Run.first.pageload();
				if(Memry.trackpage) Memry.pageLog.push('Run.Engage', true);
			}
			if(mf.length){
				while(mf.length> 1) Run.loadSrc(mf.shift(), '', '', 1);
				Run.loadSrc(mf.shift(), '', Run.Engage);
			}
			else{
				delete Memry.loadfiles;
				if(Memry.dowhat) Run.fifo(Memry.dowhat);
			}
		},
		fifo: function(what, delay){
			delay= delay || 0;
			var log= Memry.commandLog || Memry.pageLog;
			if(what && what.shift){
				var a= '', fx, f= what.shift() || '';
				if(f.constructor== Array){
					a= f.splice(1, f.length);
					f= f[0];
				}
				fx= Run.isFun(f);
				if(fx){
					if(a.length) fx.apply('', a);
					else fx();
					if(log && typeof f=== 'string') log.push(f);
				}
				if(what.length){
					setTimeout(function(){
						Run.fifo(what, delay);
					},
					delay);
				}
			}
		},
		getCooked: function(wch){
			var C= document.cookie, x, p, tem, CO= {};
			if(!C) return '';
			if(!wch) wch= 'uPrefs';
			C= Run.reSplit(C);
			for(x in C){
				if(wch=== true || wch=== x){
					tem= Run.reSplit(C[x], '&', ':');
					for(p in tem) CO[p]= tem[p];
				}
			}
			return CO;
		},
		forgetit: {},
		isFun: function(fun){
			if(typeof fun=== 'string') fun= fun.reval();
			return typeof fun== 'function'? fun: false;
		},
		isReady: {},
		loadSrc: function(url, init, cb, pause){
			var el, fn;
			if(typeof url== 'string'){
				url= Run.pathJS(url);
				fn= url.filename();
				init= init || 'Run.isReady.'+ fn;
				if(Run.hotMsg && !Memry.aka[fn]) Run.hotMsg('loading... '+ fn+ '.js');
				el= Run.addScript(url);
				Memry.loadQ= Memry.loadQ || [];
				Memry.loadQ.push(init);
				if(cb) Memry.loadQ.push(cb);
			}
			if(!pause && !Memry.Qtimer) Run.readyQ();
			return fn;
		},
		locate: function(url){
			var L= Memry.Rte.length || 1;
			url= url || document.URL;
			url= url.replace(/(\?|\#).*$/, '').replace(/\\/g, '/');
			return url.substring(L-1);
		},
		newTag: function(what, atts){
			var who= document.createElement(what);
			if(atts){
				for(var p in atts){
					if(p.indexOf('css')=== 0) who.style.cssText= atts[p];
					else if(p.indexOf('class')=== 0) who.className= atta[p];
					else who[p]= atts[p];
				}
			}
			return who;
		},
		numSort: function(a, b){
			return a-b;
		},
		pathCss: function(s){
			var rx=  /url *\((['"]?)(\.*\/)+/gi;
			return s.replace(rx, 'url($1'+ Memry.Rte);
		},
		pathJS: function(url){
			if(url.search(/^http\:/i)== 0) return url;
			url= url.replace('$$', '_scripts/').replace('$', '_mr/confun/');
			if(url.indexOf('/')== -1) url= '_mr/'+ url;
			if(!/\.js(\?.+)?$/.test(url)) url+= '.js';
			return Memry.Rte+ url;
		},
		pathTrack: function(url){
			if(url== undefined) return location.href.replace(/\\/g, '/');
			url= url.replace(/\\/g, '/');
			if(url.indexOf('/')== 0) return Memry.Rte+ url.substring(1);
			if(url.indexOf(location.protocol)== 0) return url;
			if(/^(http|mailto|#)/.test(url)) return url;
			var L= location.href.substring(Memry.Rte.length).split(/[\/\\]+/);
			url= url.split(/\.\.\//);
			L= L.slice(0, -url.length);
			L.push(url.pop());
			return Memry.Rte + L.join('/');
		},
		quitPage: function(){
			var ck= [], C= Run.reCook(), p,
			M= Memry.savecook, L= M.length, tem;
			Memry.LP= Run.locate();
			while(L){
				tem= M[--L];
				if(C[tem]!== Memry[tem]){
					ck[ck.length]= tem+ '='+ Memry[tem];
				}
			}
			if(ck.length) Run.reCook(ck.join(';'), 'uPrefs');
			return true;
		},
		readyQ: function(){
			var tem, tem2, temp= true, s, Q= Memry.loadQ || [];
			if(Memry.Qlimit== undefined) Memry.Qlimit= 100;
			--Memry.Qlimit;
			while(Q.length && temp){
				tem= Q[0];
				temp= Run.isFun(tem);
				if(temp){
					Q.shift();
					Memry.Qlimit= 100;
					//tem2= temp();
					if(temp()=== false) tem+= '-failed';
					if(typeof tem== 'string' && Memry.trackpage){
						tem= tem.replace(/Run\.isReady\.(.+)/, 'loaded $1.js');
						Memry.pageLog.push(tem, true);
					}
				}
			}
			if(Memry.Qlimit<= 0) Q.shift();
			if(Q.length== 0){
				delete Memry.loadQ;
				delete Memry.Qtimer;
				if(Run.hotMsg) setTimeout(Run.hotMsg, 400);
			}
			else Memry.Qtimer= setTimeout(function(){
				Run.readyQ();
			},
			100);
		},
		reCook: function(what, wch){
			if(!wch) wch= 'uPrefs';
			var str= [], rte= '/', exp, tem,
			p, d, C= Run.getCooked(wch);
			if(!what) return C;
			if(!C) C= {};
			if(/\=/.test(what)){
				what= Run.reSplit(what);
				Run.assignTo(C, what, true);
			}
			else return C[what];
			for(p in C){
				if(C.hasOwnProperty(p)){
					tem= C[p];
					if(p== 'rte') rte= tem;
					else if(p== 'exp') exp= parseInt(tem);
					else if(tem) str[str.length]= p+ ':'+ tem;
				}
			}
			str= wch+ '='+ str.join('&')+ ';path='+ rte;
			if(exp){
				d= new Date();
				d.setDate(d.getDate()+ exp);
				str+= ';expires='+ d.toUTCString();
			}
			if(self== top) document.cookie= str;
			return str;
		},
		reSplit: function(str, d1, d2){
			d1= d1 ||  /;\s*/;
			d2= d2 ||  /\s*[=:]\s*/;
			var S= str.split(d1), M, ax, tem, O= {};
			while(S.length){
				tem= S.shift();
				M= tem.match(d2);
				if(M){
					ax= M.index;
					O[tem.substring(0, ax)]= tem.substring(ax+ M[0].length);
				}
			}
			return O;
		},
		retrue: function(){
			return true;
		},
		setTracks: function(){
			Memry.errorLog= Yankee.Timelog('Begin Error Logging', true);
			Memry.pageLog= Yankee.Timelog(Run.locate(), true, true);
			var A= [], who, tem, s= document.getElementsByTagName('link'), L= s.length,
			s1= document.getElementsByTagName('script'), L1= s1.length;
			while(L1){
				who= s1[--L1];
				if(who.src) tem= who.src.filename();
				else tem= 'script_'+ Memry.idNext++;
				Memry.aka[tem]= who;
				if(who.src) tem+= '.js';
				A.push(tem);
			}
			while(L){
				who= s[--L];
				tem= 'link_'+who.href.filename();
				Memry.aka[tem]= who;
				A.push(tem+'.css');
			}
			while(A.length){
				Memry.pageLog.push(A.pop());
			}
		},
		tlcSort: function(as, bs){
			var a= String(as).toLowerCase(),
			b= String(bs).toLowerCase();
			if(a=== b) return 0;
			return a> b? 1: -1;
		}
	});
	Run.first={
		inf: function(env){
			var str='mrhoo.js;', 
			as= env.addSrc || 0,
			js= location.search || '',
			dc= Run.reCook('loadx', 'xPrefs') || '',
			rx=/\b(nocode|runAny)\b/,
			OK= navigator.IEmod!== 6 && window== top;
			if(!Array.prototype.reduce) str= 'equalizer.js;'+str;
			js= (js+' '+dc).match(rx) || '';
			if(js){
				js= js[0];
				if(js=== 'runAny') OK= true;
				else if(js=== 'nocode') OK= false;
			}
			if(!OK) Run.handler(window, 'load', Run.first.mrOff);
			else{
				Memry.merge(env, true);
				Memry.merge(Run.getCooked(), true);
				if('subJS' in env)str= env.subJS;
				else if(env.reqJS) str+= env.reqJS;
				
				Memry.addSrc= parseFloat(Memry.addSrc) || 0;
				if(Memry.LP && Memry.LP!= Run.locate()){
					if(as-Memry.addSrc<0) Memry.addSrc= as;
				}
				if(Memry.gadgets) str+= ';mr_apps;';
				if(Memry.addSrc) Memry.trackpage= true;
				
				if(Memry.trackpage) Run.setTracks();
				Memry.loadfiles= str.unique();
				if(env.startfun){
					if(Memry.gadgets) env.startfun+= ';Run.toolbar';
					Memry.dowhat= env.startfun.unique();
				}
				Run.addLink((Memry.reqCss || '/css/holidays.css'));
				if(Memry.sty) Run.addStyle(Memry.sty, 'style_live');
				var prec= Memry.preCode? Run.isFun(Memry.preCode):'';
				
				delete Memry.env;
				Run.handler(window, 'load', Run.Engage);
				if(prec)prec();
			}
		},
		mrOff: function(){
			var L= location.href.replace(/\\/g, '/').replace(/[#?].+$/, ''),
			el, atts={
				id: 'link_activate',
				href: L+ '?runAny',
				title: 'Try the script?'
			}
			el= Run.newTag('a', atts);
			el.appendChild(document.createTextNode('Activate Page?'));
			document.body.appendChild(el);
			Run.handler(el, 'click', function(){
				Run.reCook('loadx=runAny', 'xPrefs');
			});
		},
		pageload: function(){
			if(!Memry.loadtime){
				document.close();
				Memry.loadtime= (+new Date)-Memry.dob;
				Memry.pageLog.push('document loaded');
			}
			Memry.aka.merge({
				hooRoot: document.documentElement,
				hooHead: document.getElementsByTagName('head')[0],
				hooBody: document.body
			});
		}
	}
	window.onunload= Run.quitPage;
	if(Memry.env) Run.first.inf(Memry.env);
}
