/*
********************************************************************************
* 
* JS/SYSTEM.JS
* Plore 2.0: Client
* 
* v1.0 (12012011)
* 
********************************************************************************
*/
var _,			// Plore Client
	$APP,		// Optional Custom Application (Page Level)
	$SPLASH,	// Transition Layer
	$SCROLLER,	// Dynamic Scroller (iScroll)
	$_,			// Core Functions
	$;			// 3rd-Party Library


/*
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
class Plore()
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
*/
function Plore()
{
	var d = document,
		u = d.location.href;
	
	this.PG_BROWSE		= $E(__.PG_BROWSE_ID);
	this.PG_CANVAS		= $E(__.PG_CANVAS_ID);
	this.PG_FOOTER		= $E(__.PG_FOOTER_ID);
	this.PG_HEADER		= $E(__.PG_HEADER_ID);
	this.PG_INSIDE		= $E(__.PG_INSIDE_ID);
	this.PG_MIDDLE		= $E(__.PG_MIDDLE_ID);
	this.PG_MYBODY		= $E(__.PG_MYBODY_ID);
	this.PG_NAME		= /([^\/]+)(\.[a-z]+)?(\?.*)?$/i.test(u) ? RegExp.$1.toLowerCase() : __.DEF_PG;
	this.PG_SECTION		= /\/data\/([^\/\.]+)/.test(u) ? RegExp.$1 : __.DEF_PG;
	this.PG_APPNAME		= this.get('appname',this.PG_NAME);
	this.PG_ID			= d.body.id ? d.body.id : (d.body.id='__'+this.PG_SECTION);
	this.QUERYSTRING	= null;
	this.QUERY			= null;
	this.QUERYSUBJ		= null;
	this.SCREENSIZE		= [screen.width,screen.height];
	this.SCREEN_W		= this.SCREENSIZE[0];
	this.SCREEN_H		= this.SCREENSIZE[1];
	this.SPLASH			= $E(__.SPLASH_ID);
	this.SYS_REFRESHES	= 0;
	this.WE_ARE_HOME	= this.PG_NAME == __.DEF_PG;
	
	this.reconfigure();
	this.refresh('query');
	this.init();
};

Plore.prototype =
{
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : init()
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	init : function()
	{
		window[
			isAppMob
			  ? 'onorientationchange'
			  : 'onresize'
			] = function()
		{
			_.refresh()
		};
		
		if (this.refresh())
		{
			var u = $_.str.util.ucfirst,
				x = [],
				k,
				m;
			
			for (k in this)
			{
				if (typeof this[k] !== 'function')
				{
					m = 'module'+u(k,true);
					
					if (
					  window[m]
					  && typeof window[m] === 'function')
					{
						x.push(
							  "$"+k.toUpperCase()
							+ "=new "+m+"(this."+k+",this)"
							);
					}
				}
			}
			
			if (x.length) eval(x.join(';'))
		}
		
		if (this.MD) this.MD.init();
		if (window[this.PG_APPNAME]) eval('$APP=new '+this.PG_APPNAME+'(this)');
	},
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : bool reconfigure()
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	reconfigure : function()
	{
		if (isAppMob)
		{
			this.SCREENSIZE	= [screen.width,screen.height];
			this.SCREEN_W	= this.SCREENSIZE[0];
			this.SCREEN_H	= this.SCREENSIZE[1];
			
			{
				this.MD = new MobileDevice(this)
			}
		}
		else
		{
			this.SCREENSIZE	= $_.get.windowInnerSize();
			this.SCREEN_W	= this.SCREENSIZE[0];
			this.SCREEN_H	= this.SCREENSIZE[1];
		}
		
		if (this.SYS_REFRESHES == 0)
		{
			if (__.AUTO_TRANSITION_LINKS) this.FixMyLinks();
			this.DoAutoComplete(__.INPUT_AUTOCOMPLETE);
		}
		
		return true
	},
	
	
	/*
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	*/
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : mixed Show([now: Boolean])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	Show : function(now)
	{
		if (!now) return setTimeout("_.Show(true)",__.SPLASH_DELAY);
		else
		{
			var _ = $_,
				s = this.SPLASH,
				d = 1000;
		}
		
		if (s)
		{
			_.tween.opacity(
				s,
				null,
				0,
				{
					onComplete : new Function(
						  "var e = $E('"+s.id+"');"
						+ "e.style.visibility=$_.get.opacityOf(e)==0"
						+ "?'hidden':'visible'"
						),
					
					duration : Math.floor(d/2)
				}
				);
		}
		
		_.tween.opacity(
			this.PG_MIDDLE,
			null,
			100,
			{
				duration : d
			}
			);
		
		return this
	},
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : mixed Hide([now: Boolean])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	Hide : function(now)
	{
		if (!now) return setTimeout("_.Hide(true)",__.SPLASH_DELAY);
		else
		{
			var _ = $_,
				s = this.SPLASH,
				d = 1000;
		}
		
		if (
		  s
		  && _.get.opacityOf(s) < 100)
		{
			{
				s.style.visibility = 'visible'
			}
			
			_.tween.opacity(
				s,
				null,
				100,
				{duration:d}
				);
		}
		
		_.tween.opacity(
			this.PG_MIDDLE,
			null,
			0,
			{
				onComplete : function()
					{
						var e = [
								document.body,
								_.PG_CANVAS
								],
							o;
						
						while (e.length)
						{
							if (o = e.shift())
							{
								o.style.backgroundImage = 'none'
							}
						}
					},
				
				duration : d
			}
			);
		
		return this
	},
	
	
	/*
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	*/
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : mixed reset([subject: String])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	reset : function(subj)
	{
		switch (subj)
		{
			default: break;
		}
		
		return
	},
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : mixed refresh([subject: String])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	refresh : function(k)
	{
		if (k)
		{
			switch (k)
			{
				case 'query':
				var a = 'QUERYSTRING',
					b = 'QUERY',
					c = 'QUERYSUBJ',
					d = __.DEF_PG,
					m = /^([a-z\d]+(\-[a-z\d]+)*)/i,
					u = document.location.href;
				
				this[a] = /\?(.+)$/.test(u) ? RegExp.$1 : "";
				this[b] = this[a] == "" ? {} : $_.util.ParseQueryStr(this[a]);
				this[c] = m.test(this[a]) ? RegExp.$1.toLowerCase() : d;
				
				return this[a];
				break;
				
				case 'document':
				this.Hide(true);
				setTimeout("window.location.reload()",2000);
				break;
			}
		}
		else
		{
			this.SYS_REFRESHES++;
			
			if (this.reconfigure())
			{
				if (isOri)
				{
					document.body.className = /[1-9]/.test(window.orientation)
					  ? '__orientation_l'
					  : '__orientation_p'
				}
				
				if (this.MD) this.MD.ChangeOrientation();
				return this.SYS_REFRESHES
			}
		}
		
		return
	},
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : int go(truncated: Path)
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	go : function(k)
	{
		if ($_.get.opacityOf(this.SPLASH) < 100)
		{
			{
				k = k == null ? "" : k.toLowerCase()
			}
			
			var d = 2000,
				f = function(){_.Hide(true)},
				u,
				p;
			
			if (/^\/|^\/$|\.[a-z]+$/i.test(k)) u = k;
			else
			{
				p = (!k || k == __.DEF_PG) ? '/' : ('data/'+k);
				u = p == '/' ? p : ((this.WE_ARE_HOME?"":"/")+p+'.html');
			}
			
			return $_.web.Redir(
				u,
				d,
				f
				);
		}
		
		return
	},
	
	
	/*
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	*/
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : mixed DoAutoComplete(activate: Boolean[, node: HtmlInputElement])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	DoAutoComplete : function(bOn,e)
	{
		if (e)
		{
			if (
			  $_.is.typeOf.HtmlNode(e,'input')
			  && /^text$|^password$/i.test(e.type))
			{
				v = bOn ? ['on',true] : ['off',false];
				
				e.autocorrect = v[
					typeof e.autocorrect === 'boolean'
					  ? 1
					  : 0
					];
				
				e.autocapitalize = v[0];
				return e
			}
		}
		else
		{
			var bag = document.body.getElementsByTagName('input'),
				i = 0,
				l = bag.length,
				n = 0;
			
			while (i < l)
			{
				if (this.DoAutoComplete(bOn,bag[i++]))
				{
					n++
				}
			}
			
			return bag
		}
		
		return
	},
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : FixMyLinks()
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	FixMyLinks : function()
	{
		var c = function(u){return $_.web.url.decode($_.sanitize.url(u,1))},
			a = document.getElementsByTagName('a'),
			p = [
				/^\/$|\.[a-z]+$/i,
				/\bopen\b\/([\w-]+)(?:\/(\d+(?:-\d+)?))?$/i,
				/^\/*([a-z0-9-]+(\/[a-z0-9-]+)*)\/?$/
				],
			i = 0,
			j = 0,
			l = p.length,
			u,
			o;
		
		while (o = a[i++])
		{
			if (o.onclick) continue;
			else
			{
				u = c(o.href);
				j = 0;
			}
			
			while (j < l)
			{
				if (p[j++].test(u))
				{
					switch (j)
					{
						case 1:
						o.onclick = new Function(
							"_.go('"+u+"')"
							);
						break;
						
						case 2:
						o.onclick = new Function(
							"window.open('"
						  + RegExp.$1 + (RegExp.$2?("','"+RegExp.$2):"")
						  + "')"
							);
						break;
						
						case 3:
						o.onclick = new Function(
							"_.go('"
						  + RegExp.$1.replace(/data\//,"")
						  + "')"
							);
						break;
					}
					
					o.href = 'javascript:;';
					break
				}
			}
		}
	},
	
	
	/*
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
	*/
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	Plore : mixed get(subject: String[, input: Mixed])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	get : function(k,v)
	{
		switch (k.toLowerCase())
		{
			case 'appname':
			var a = (v||this.PG_NAME).split(""),
				i = 0,
				l = a.length;
			
			do
			{
				if (!/\w/.test(a[i]))
				{
					a[i++] = "";
					a[i] = a[i].toUpperCase();
				}
			}
			while (++i < l);
			
			return $_.str.util.ucfirst(a.join(""))+'Application';
			break;
		}
		
		return
	}
};


/*
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
*/


/*
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
PLORE : NET
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
*/
Plore.net =
{
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	PLORE : NET : object XMLHttpRequest()
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	XMLHttpRequest : function()
	{
		if (window.XMLHttpRequest) return new XMLHttpRequest();
		else
		{
			var m = [
					function(){return new ActiveXObject('Msxml3.XMLHTTP')},
					function(){return new ActiveXObject('Msxml2.XMLHTTP')},
					function(){return new ActiveXObject('Microsoft.XMLHTTP')}
					],
				i = 0,
				l = m.length,
				o;
			
			do
			{
				try
				{
					o = m[i]()
				}
				catch (e)
				{
					continue
				}
				
				break
			}
			while (++i < l);
			
			return o
		}
	},
	
	
	/*
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	PLORE : NET : object load(file: Url[, callback: Function])
	IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
	*/
	load : function(u,callback)
	{
		var r = this.XMLHttpRequest();
		
		if (r)
		{
			r.onreadystatechange = function()
			{
				if (
				  r.readyState == 4
				  && r.status == 200
				  && r.responseText != null)
				{
					var tag			= document.createElement('script');
						tag.type	= 'text/javascript';
						tag.defer	= true;
						tag.text	= r.responseText;
					
					document
					  .getElementsByTagName('head')[0]
					  .appendChild(tag);
					
					if (callback) callback();
				}
			};
			
			r.open('GET',u,true);
			r.send();
			
			return r
		}
		
		return
	}
};


/*
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
*/


/*
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
PLORE : ready([onload: Function])
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
*/
Plore.ready = function(onload)
{
	if (
	  onload
	  && typeof onload === 'function')
	{
		this._onload = onload
	}
	
	this._beforeonload = function()
	{
		_ = new Plore;
		
		if (
		  __.USE_DYNAMIC_SCROLL
		  && window.iScroll
		  && typeof _.PG_MIDDLE === 'object')
		{
			$SCROLLER = new iScroll(
				_.PG_MIDDLE,
				{
					vScrollbar : !isIE
				}
				);
		}
		
		if (Plore._onload) Plore._onload(_)
	};
	
	if (
	  typeof __.INSTALL === 'object'
	  && __.INSTALL.length)
	{
		this.net.load(
			'js/'+__.INSTALL.shift(),
			this._callback = function()
			{
				__.INSTALL.length
				  ? Plore.net.load('js/'+__.INSTALL.shift(),Plore._callback)
				  : Plore._beforeonload()
			}
			);
	}
	else this._beforeonload();
};

