//////////////////////////////////////////////////////////////////////////////// COREX

Function.prototype.argacall = function(a)
{
	switch(a.length){
	case 0:	return this(); break;
	case 1: return this(a[0]); break;
	case 2: return this(a[0],a[1]); break;
	case 3: return this(a[0],a[1],a[2]); break;
	case 4: return this(a[0],a[1],a[2],a[3]); break;
	case 5: return this(a[0],a[1],a[2],a[3],a[4]); break;
	case 6: return this(a[0],a[1],a[2],a[3],a[4],a[5]); break;
	case 7: return this(a[0],a[1],a[2],a[3],a[4],a[5],a[6]); break;
	case 8: return this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]); break;
	case 9: return this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]); break;
	case 10:return this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]); break;
	case 11:return this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10]); break;
	case 12:return this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11]); break;
	}
}

Function.prototype.ellipsis = function(oi)
{
	var i = 0, ea = new Array();
	var arguments = this.arguments;
	var length = arguments.length;

	while(oi < length){
		ea[i] = arguments[oi];
		i++;
		oi++;
	}

	return ea;
}


//////////////////////////////////////////////////////////////////////////////// CROSS-BROWSER
if(navigator.userAgent.indexOf('MSIE') != -1){//Internet explorer

	var Compat = {

		HTTP: function()
		{
			return new ActiveXObject('Microsoft.XMLHTTP');
		},

		eventBind: [
			function(obj, evt, fnc){ obj.detachEvent('on'+ evt, fnc); },
			function(obj, evt, fnc){ obj.attachEvent('on'+ evt, fnc); }
		],

		innerText: function(obj, value)
		{
			if(value == null){
				return obj.innerText;
			}else{
				obj.innerText = value;
			}
		},

		bhNoSelect: function(obj, b)
		{
			Compat.eventBind[Number(b)](obj, 'onselectstart', Compat._ehNoDragSelect);
		},

		bhNoDrag: function(obj, b)
		{
			Compat.eventBind[Number(b)](obj, 'ondragstart', Compat._ehNoDragSelect);
		},

		cspOpacity: function(pcnt)
		{
			return 'filter:alpha(opacity='+ pcnt +')';
		},

		EmbedSWF: function(id, movie, width, height, version)
		{
			document.write(''
			+'<OBJECT id="'+ id +'" width="'+ width +'" height="'+ height +'"'
			+' classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000"'
			+' codebase="'+ location.protocol +'://'
			+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab'
			+ ((version)?('#version='+version):'') +'"'
			+'>'
			+'<PARAM name="movie" value="'+ movie +'?cachefix='+ new Date().getTime() +'" />'
			+'<PARAM name="wmode" value="transparent" />'
			+'</OBJECT>');

			return document.getElementById(id);
		},

	//private:
		_ehNoDragSelect: function()
		{
			event.returnValue = false;
		}
	}

}else{//Mozilla

	var Compat = {

		HTTP: function()
		{
			return new XMLHttpRequest();
		},

		eventBind: [
			function(obj, evt, fnc){ obj.removeEventListener(evt, fnc, false); },
			function(obj, evt, fnc){ obj.addEventListener(evt, fnc, false); }
		],

		innerText: function(obj, value)
		{
			if(value == null){
				return obj.textContent;
			}else{
				obj.textContent = value;
			}
		},

		bhNoSelect: function(obj, b)
		{
			if(obj.style.MozUserSelect){
				if(b){
					obj.setAttribute('noSelect_style', obj.style.MozUserSelect);
					obj.setAttribute('noSelect_cursor', obj.style.cursor);
					obj.style.cursor = 'default';
				}else{
					obj.style.MozUserSelect = obj.getAttribute('noSelect_style');
					obj.style.cursor = obj.getAttribute('noSelect_cursor');
					obj.removeAttribute('noSelect_style');
					obj.removeAttribute('noSelect_cursor');
				}
			}else{
				Compat.eventBind[Number(b)](obj, 'mousedown', Compat._ehNoSelect);
			}
		},

		bhNoDrag: function(obj, b)
		{
			Compat.eventBind[Number(b)](obj, 'mousedown', Compat._ehNoDrag);
		},

		cspOpacity: function(pcnt)
		{
			return 'opacity:'+ (pcnt * 0.01);
		},

		EmbedSWF: function(id, movie, width, height, version)
		{
			document.write(''
			+'<OBJECT id="'+ id +'" width="'+ width +'" height="'+ height +'" wmode="transparent"'
			+' data="'+ movie +'" type="application/x-shockwave-flash">'
			+'</OBJECT>'
			);

			return document.getElementById(id);
		},

	//private:
		_ehNoSelect: function(event)
		{
			event.returnValue = false;
			return false;
		},

		_ehNoDrag: function(event)
		{
			if(event){
				if(event.preventDefault){ event.preventDefault(); }
				if(event.stopPropagation){ event.stopPropagation(); }
			}
		}
	}

}//Compat

//////////////////////////////////////////////////////////////////////////////// HTTP

/*
PARAMS:
onresponse
	void onresponse(responseText, status)
	status != 200 means http error and responseText == statusText, e.g.: 404 Not Found
*/
function httpGET(url, onresponse)
{
	if(!httpGET.idc){
		httpGET.idc = 1;
		httpGET.request = {};
	}

	function INSTANCE(id, http, onresponse)
	{
		this.ehReadyStateChange = function()
		{
			if(http.readyState == 4){
				if(onresponse){
//					try {
						if(http.status == 200){
							onresponse(http.responseText, http.status);
						}else{
							onresponse(http.statusText, http.status);
						}
//					}catch(e){
						//onresponse is invalid
//					}
				}
				delete httpGET.request[id];
			}
		}
	}

	var id, http;

	id = 'httpGET_'+ httpGET.idc++;
	http = Compat.HTTP();

	httpGET.request[id] = new INSTANCE(id, http, onresponse);

	url += ((url.indexOf('?') == -1) ? '?' : '&') +'cachefix='+ new Date().getTime();

	http.open('GET', url);
	http.onreadystatechange = httpGET.request[id].ehReadyStateChange;
	http.send(null);
}

/*
httpCookie
n	v
+	-		get value of n
+	+		set value of n to v
-	+		expire v used as n
-	-		dump/docwrite cookies content
*/
function httpCookie(n, v)
{
	function get(n)
	{
		var lines, length, i, parts, nvp;

		lines = document.cookie.split('; ');
		length = lines.length;
		i = 0;
		while(i < length){
			parts = lines[i].split(';');
			nvp = parts[0].split('=');
			if(nvp[0] == n){ return nvp[1]; }
			i++;
		}
		return null;
	}

	if(n){
		if(v == null){
			return get(n);
		}else{
			document.cookie = n +'='+ v;
		}
	}else{
		if(v){
			var d;
			n = v;
			v = get(n);
			d = new Date();
			document.cookie = n +'='+ v +'; expires=' + d.toGMTString();
		}else{
			document.write('document.cookie = \''+ document.cookie +'\'<BR>');
		}
	}
}

//////////////////////////////////////////////////////////////////////////////// PROCESS

function ExecQueue()
{
	function execQueue(fnc)
	{
		var arg, o, v;

		if(fnc){
			var arg = execQueue.ellipsis(1);
			execQueue.queue.push({ r:null, f:fnc, a:arg });
		}else{
			while(execQueue.queue.length){
				var o = execQueue.queue.shift();
				var v = o.f.argacall(o.a);
				if(o.r){ o.r(v); }
			}
		}
	}

	execQueue.queue = [];
	execQueue.withReturn = function(rfnc, fnc)
	{
		var arg = execQueue.withReturn.ellipsis(2);
		execQueue.queue.push({ r:rfnc, f:fnc, a:arg });
	}

	return execQueue;
}

bodyOnload = ExecQueue();

function EventGate(msDelay, ceCount, gateFunc)
{
	if(!EventGate.idc){
		EventGate.idc = 1;
		EventGate.instance = { }
	}

	function INSTANCE(I)
	{
		this.eventCounter = 0;

		this.isOpen = function()
		{
			if(I.isopen){ return true; }

			if(I.ceCount){
				I.ceIndex++;
				if(I.ceIndex == I.ceCount){
					I.ceIndex = 0;
					this.execute();
					return false;
				}
			}

			if(I.msDelay){
				this.reschedule();
			}
			return false;
		}

		this.cancel = function()
		{
			I._this.eventCounter = 0;

			if(I.cto){
				clearTimeout(I.cto);
				I.cto = null;
				return true;
			}
			return false;
		}

		this.execute = function()
		{
			I.isopen = true;

			I.gateFunc();

			I.cto = null;
			I.isopen = false;
			I._this.eventCounter = 0;
		}

//private
		this.reschedule = function()
		{
			I._this.eventCounter++;

			if(I.cto){ clearTimeout(I.cto); }

			I.cto = setTimeout('EventGate.instance.'+ I.id +'.execute()', I.msDelay);
		}
	}

	var id, I;

	id = 'EventGate_'+ EventGate.idc++;

	I = {
		id: id,

		_this: null,
		isopen: false,

		msDelay: msDelay,
		ceCount: ceCount,
		gateFunc: gateFunc,

		to: null,
		ceIndex: 0
	}

	I._this = EventGate.instance[id] = new INSTANCE(I);

	return EventGate.instance[id];
}

//////////////////////////////////////////////////////////////////////////////// APPLICATION

var MeLoudPlayer = {

	write: function()
	{
		if(MeLoudPlayer.I){ return; }

		var object, pgLoad, pgPlay;

		document.write('<DIV id="MLPc" style="position:absolute;top:0px;left:0px;width:880px;height:150px;margin:0px;padding:0px;overflow:hidden;">');
		object = Compat.EmbedSWF('MLP','/assets/MeLoudPlayer.swf', 880, 150, '9,0,124,0');
		document.write('</DIV>');

		document.write('<DIV id="MLPpgLoad" style="position:absolute;top:148px;left:0px;width:0px;height:2px;margin:0px;padding:0px;overflow:hidden;background-color:#FFC000;"></DIV>');
		document.write('<DIV id="MLPpgPlay" style="position:absolute;top:148px;left:0px;width:0px;height:2px;margin:0px;padding:0px;overflow:hidden;background-color:#FFFF00;"></DIV>');
		pgLoad = document.getElementById('MLPpgLoad');
		pgPlay = document.getElementById('MLPpgPlay');


		MeLoudPlayer.I = {

			url: null,
			state: 1,
			loaded: false,

			swf: {
				object: object,
				pgLoad: pgLoad,
				pgPlay: pgPlay
			},

			handler: function(o)
			{
				switch(o.event){
				case 1://LOAD_OPEN
					MeLoudPlayer.I.state = 2;
//MeLoudPlayer.toggle();
				break;
				case 2://LOAD_ERROR
				break;
				case 3://LOAD_ID3
				break;
				case 4://LOAD_COMPLETE
					MeLoudPlayer.I.swf.pgLoad.style.width = '880px';
				break;
				case 5://BOTH_PROGRESS
					if(o.loading){ MeLoudPlayer.I.swf.pgLoad.style.width = o.loadpgx +'px'; }
					if(o.playing){ MeLoudPlayer.I.swf.pgPlay.style.width = o.playpgx +'px'; }
				break;
				case 6://PLAY_COMPLETE
					MeLoudPlayer.I.swf.pgPlay.style.width = '880px';
					MeLoudPlayer.I.state = 0;
				break;
				}
			},

			initialize: function()
			{
				if(MeLoudPlayer.I.swf.object.mp3pu_init_pu){
					MeLoudPlayer.I.swf.object.mp3pu_init_pu('MeLoudPlayer.I.handler', 880, 880, 100, 0);
					MeLoudPlayer.I.swf.object.mp3pu_init_ss(0xFFC0C0, 0xC0C0FF, 0xFFFF00);

					MeLoudPlayer.I.loaded = true;
					MeLoudPlayer.I.execQueue();
				}else{
					setTimeout('MeLoudPlayer.I.initialize();', 250);
				}
			},

			exec: function(name, arg)
			{
				if(MeLoudPlayer.I.loaded){
					if(arg == null){
						MeLoudPlayer.I.swf.object['mp3pu_'+ name]();
					}else{
						MeLoudPlayer.I.swf.object['mp3pu_'+ name](arg);
					}
				}else{
					MeLoudPlayer.I.execQueue(MeLoudPlayer.I.exec, name, arg);
				}
			},

			execQueue: ExecQueue()
		}//MeLoudPlayer.I

		bodyOnload(MeLoudPlayer.I.initialize);
	},

	load: function(url){ MeLoudPlayer.I.exec('load', url); MeLoudPlayer.I.url = url; },
	stop: function(){ MeLoudPlayer.I.exec('stop'); },
	play: function(){ MeLoudPlayer.I.exec('play'); },
	pause: function(){ MeLoudPlayer.I.exec('pause'); },
	volume: function(i){ MeLoudPlayer.I.exec('volume', i); },
	panama: function(i){ MeLoudPlayer.I.exec('panama', i); },
	volinc: function(inc){ MeLoudPlayer.I.exec('volinc', inc); },
	paninc: function(inc){ MeLoudPlayer.I.exec('paninc', inc); },
	toggle: function()
	{
		switch(MeLoudPlayer.I.state){
		case 0: MeLoudPlayer.load(MeLoudPlayer.I.url); MeLoudPlayer.I.state=2; break;
		case 1: MeLoudPlayer.play(); MeLoudPlayer.I.state=2; break;
		case 2: MeLoudPlayer.pause(); MeLoudPlayer.I.state=1; break;
		}
	}
}


var Shield = {

	show: function()
	{
		if(!Shield.I){ return; }
		if(Shield.I.visible){ return; }
		
		Compat.eventBind[1](Shield.I.eventParent, 'resize', Shield.I.overlap);
		Compat.eventBind[1](Shield.I.eventParent, 'scroll', Shield.I.overlap);

		Compat.bhNoSelect(Shield.I.scrollParent, true);
		Compat.bhNoDrag(Shield.I.shield, true);

		//not use: Firefox bug: swfobj wmode!=window stops functioning
		//I.scrollParent.style.overflow = 'hidden';

		Shield.I.shield.style.visibility = 'visible';
		Shield.I.visible = true;

		Shield.I.overlap();
	},

	hide: function()
	{
		if(!Shield.I){ return; }
		if(!Shield.I.visible){ return; }

		Compat.eventBind[0](Shield.I.eventParent, 'resize', Shield.I.overlap);
		Compat.eventBind[0](Shield.I.eventParent, 'scroll', Shield.I.overlap);

		Compat.bhNoDrag(Shield.I.shield, false);
		Compat.bhNoSelect(Shield.I.scrollParent, false);

		Shield.I.shield.style.width = '0px';
		Shield.I.shield.style.height = '0px';

		Shield.I.shield.style.visibility = 'hidden';
		Shield.I.visible = false;
	},

	write: function()
	{
		if(Shield.I){ return; }

		document.write('<DIV'
		+' id="PML_shield"'
		+' style="'
		+' top:0px; left:0px; width:0px; height:0px;'
		+' position:absolute;'
		+' visibility:hidden;'
		+' '+ Compat.cspOpacity(50) +';'
		+' background-color:#000000;'
		+'"><\/DIV>');

		Shield.I = {

			shield: document.getElementById('PML_shield'),
			eventParent: window,
			scrollParent: document.documentElement,

			visible: false,

			overlap: function()
			{
				if(!Shield.I.visible){ return; }
				var p, s;

				p = Shield.I.scrollParent;
				s = Shield.I.shield.style;

				s.top = p.scrollTop +'px';
				s.left = p.scrollLeft +'px';
				s.width = p.clientWidth +'px';
				s.height = p.clientHeight +'px';
			}
		}
	}
}


function Dialog(title, width, height, cbWriteContent, onCloseClick)
{
	if(!Dialog.idc){
		Dialog.idc = 1;
		Dialog.instance = { }
	}

	function INSTANCE(I)
	{
		this.open = function(url)
		{
			I.eventgateCenter.execute();

			Compat.eventBind[1](window, 'resize', this.center);
			Compat.eventBind[1](window, 'scroll', this.center);

			I.container.style.visibility = 'visible';
		}

		this.close = function()
		{
			I.container.style.visibility = 'hidden';

			Compat.eventBind[0](window, 'resize', this.center);
			Compat.eventBind[0](window, 'scroll', this.center);
		}

		this.showButton = function()
		{
			if(I.buttonState){ return; }

			I.buttonState = 1;
			I.titleButton.style.visibility = 'inherit';
		}

		this.hideButton = function()
		{
			if(!I.buttonState){ return; }

			I.buttonState = 0;
			I.titleButton.style.visibility = 'hidden';
		}

		this.title = function(text)
		{
			return Compat.innerText(I.titleBar, text);
		}

		this.center = function()
		{
			if(I.eventgateCenter.isOpen()){
				var cxcl, cycl, cxsc, cysc;

				cxcl = I.scrollParent.clientWidth;
				cycl = I.scrollParent.clientHeight;
				cxsc = I.scrollParent.scrollLeft;
				cysc = I.scrollParent.scrollTop;

				I.container.style.left = ((parseInt(cxcl * 0.5) + cxsc) - parseInt(I.width * 0.5)) +'px';
				I.container.style.top =  ((parseInt(cycl * 0.5) + cysc) - parseInt(I.height * 0.5) - 24) +'px';
			}
		}
	}

	var id = 'Dialog_'+ Dialog.idc++;

	document.write(''
	+'<DIV id="'+ id +'" style="position:absolute;left:0px;top:0px;width:'+ width +'px;visibility:hidden;overflow:hidden;background-color:#0000A0;">'
	+'<TABLE cellspacing=2 cellpadding=0 border=0><TR><TD>'

		+'<TABLE cellspacing=0 cellpadding=0 border=0 style="background-color:#0000FF;"><TR>'
		+'<TD id="'+ id +'_ttlbar" width=4096 style="color:#FFFFFF;font:9pt verdana;font-weight:bold;padding:0px 8px;">'+ title +'</TD>'
		+'<TD>'
		+'<IMG id="'+ id +'_ttlbut" src="/assets/gui/but_close.gif" width=21 height=21 border=0 style="visibility:inherit;vertical-align:middle;margin:2px;">'
		+'</TD>'
		+'</TR></TABLE>'

	+'</TD></TR><TR><TD style="background-color:#FFFFFF;">');

		cbWriteContent();

	document.write(''
	+'</TD></TR></TABLE>'
	+'</DIV>'
	);
	
	var I = {
		id: id,
		width: width,
		height: height,
		container: document.getElementById(id),
		titleBar: document.getElementById(id +'_ttlbar'),
		titleButton: document.getElementById(id +'_ttlbut'),
		eventParent: window,
		scrollParent: document.documentElement,
		eventgateCenter: null,
		buttonState: 1
	}

	Dialog.instance[id] = new INSTANCE(I);

	I.eventgateCenter = EventGate(250, 10, Dialog.instance[id].center);

	Compat.eventBind[1](I.titleButton, 'click', (onCloseClick) ? onCloseClick : Dialog.instance[id].close);

	return Dialog.instance[id];
}

/*
TODO:
+ dialog; hide titlebar and occupy full client area


Ops is the initializer in the iPayapp page and the host iframe of payment pages.
*/
var Ops = {
//public:
	write: function()
	{
		if(Ops.I){ return; }

		Ops.I = {
			bOpen: false,

			width: 640,
			height: 480,
			baseURL: window.location +'ops.php',
			dialog: null,
			iframe: null,
			clientDocument: null,
			continueButton: null,

			start: function(query)
			{
				Ops.I.bOpen = true;

				Shield.show();
				Ops.I.iframe.src = Ops.I.baseURL + query;
				Ops.I.dialog.open();
				Ops.module.showButton.dialogClose(true);
			},

			finish: function()
			{
				if(Ops.module.oncancel){
					try{
						Ops.module.oncancel();
					}catch(e){
						//non-resident object 
					}

					Ops.module.oncancel = null;
				}

				Ops.I.keepAlive.shutdown();

				httpCookie(null, 'opstx');

				Ops.I.dialog.close();
				Ops.I.iframe.src = '';
				Shield.hide();

				Ops.I.bOpen = false;
			},

			handshake: function(clientDocument, itvkal)
			{
				Ops.I.clientDocument = clientDocument;

				Ops.I.keepAlive.initialize(itvkal);

				return Ops.I.dialog.title(clientDocument.title);
			},

			writeContinueButton: function()
			{
				Ops.I.clientDocument.write('<INPUT id="continueButton" type="button" value="continue..." class="continue" onclick="Ops.I.ehContinue(window)">');
				Ops.I.continueButton = Ops.I.clientDocument.getElementById('continueButton');
			},

			ehContinue: function(window)
			{
				window.location.href = Ops.I.baseURL + '?node=ops.continue';
			},

			keepAlive: {
				msTimeout: null,
				timeoutId: null,

				initialize: function(itvkal)
				{
					Ops.I.keepAlive.shutdown();

					if(!itvkal){ return; }

					Ops.I.keepAlive.msTimeout = itvkal * 1000;
					Ops.I.keepAlive.scedule(0);
				},

				shutdown: function()
				{
					if(!Ops.I.keepAlive.msTimeout){ return; }

					Ops.I.keepAlive.msTimeout = null;

					if(Ops.I.keepAlive.timeoutId){
						clearTimeout(Ops.I.keepAlive.timeoutId);
						Ops.I.keepAlive.timeoutId = null;
					}
				},

				scedule: function(ms)
				{
					if(!Ops.I.keepAlive.msTimeout){ return; }

					if(!ms){ ms = Ops.I.keepAlive.msTimeout; }

					if(Ops.I.keepAlive.timeoutId){ clearTimeout(Ops.I.keepAlive.timeoutId); }

					Ops.I.keepAlive.timeoutId = setTimeout('Ops.I.keepAlive.request()', ms);
				},

				request: function()
				{
					if(!Ops.I.keepAlive.msTimeout){ return; }

					Ops.I.keepAlive.timeoutId = null;
					Ops.module.httpGET(Ops.I.baseURL +'?node=ops.ajaxKeepAlive', Ops.I.keepAlive.response);
				},

				response: function(responseText, status)
				{
					if(!Ops.I.keepAlive.msTimeout){ return; }

					var ms = ((status == 200) && (responseText == 'OK')) ? 0 : 5000;//on error scedule request every 5 sec.

					Ops.I.keepAlive.scedule(ms);
				}
			}
		}

		function write()
		{
			document.write(''
			+'<IFRAME id="iframe_ops" name="iframe_ops"'
			+' style="width:100%; height:'+ Ops.I.height +'px;"'
			+' src=""'
			+' scrolling="auto" frameborder="0"'
			+'></IFRAME>'
			);
		}

		Shield.write();

		Ops.I.dialog = Dialog('', Ops.I.width, Ops.I.height, write, Ops.I.finish);
		Ops.I.iframe = document.getElementById('iframe_ops');

		bodyOnload(Ops.resume);
	},

	module: {
		httpGET: httpGET,
		innerText: Compat.innerText,

		oncancel: null,

		showButton: {
			dialogClose: function(b)
			{
				Ops.I.dialog[(b) ? 'showButton' : 'hideButton']();
			},

			opsContinue: function(b)
			{
				Ops.I.continueButton.style.visibility = (b) ? 'inherit': 'hidden';
			}
		}
	},

	resume: function()
	{
		if(!httpCookie('opstx')){ return; }

		Ops.I.start('?resume='+ escape(window.location.search.substr(1)));
	},

	pay: function(productId)
	{
		Ops.I.start('?node=ops.start&productId='+ productId);
	}
}



