/*
|	Core Manager
|
|	Namespace, utility and global variables creation
|	Requires: None
|
*/
		
		// Namespace creation
		var flex = {};
		
		// Globals
		var oLoad=[],_nPageId=0;
		
		// Load
		{flex.load=function(){
			return {
				init:function(){
					//run through all load functions
					var nLength=oLoad.length;
					if (nLength > 0) {
						for(i=0;i<nLength;i++){
							oLoad[i].call(this);
						}
					};
				}
			}
		}()};
		
		// Utils
		(flex.utils=function(){
			return {
				get:function(){
					var oEA=new Array();
					for (var i=0;i<arguments.length;i++) {
						var oE = arguments[i];
						(typeof oE=='string')?oE=document.getElementById(oE):null;
						if(arguments.length==1) return oE;
						oEA.push(oE);
					}
					return oEA;
				},
				getWhere:function(oP,vW) {  
					var oEA=[];
					var oAll=oP.getElementsByTagName("*");
					for(var i=0;i<oAll.length;i++) {
						if(eval("oAll[i]."+vW)){
							oEA.push(oAll[i]);
						}
					}
					return oEA;
				},
				getPosition:function(oP){
					var nOT=0,nOL=0,nOB=0,nOR=0;
					while(oP.offsetParent!=null){
						nOT+=oP.offsetTop;
						nOL+=oP.offsetLeft;
						nOB+=(oP.offsetTop+oP.offsetHeight);
						nOR+=oP.offsetWidth;
						oP=oP.offsetParent;
					}
					// Set right and bottom based on viewport size
					var oVPSizes=flex.utils.getViewportSize();
					nOB=(nOB-oVPSizes[1]);
					nOR=(nOR-oVPSizes[0]);
					
					return [nOT,nOL,nOB,nOR];
				},
				rnd:function(){return Math.floor(Math.random()*1000000000);},
				addEvent:function(obj,type,fn){
					if (obj.addEventListener){obj.addEventListener(type,fn,false);}else if(obj.attachEvent){obj["e"+type+fn]=fn;obj[type+fn]=function(){obj["e"+type+fn](window.event);};obj.attachEvent("on"+type,obj[type+fn]);};
				},
				removeEvent:function(obj,type,fn){
					if (obj.removeEventListener){obj.removeEventListener(type,fn,false);}else if(obj.detachEvent){obj.detachEvent("on"+type,obj[type+fn]);obj[type+fn]=null;obj["e"+type+fn]=null;}
				},
				simpledate:function(nD){
					var oDate = new Date(nD), oNow = new Date(), nDiff = 0, vDate = "";
					if (isNaN(oDate))
					{
						oDate = new Date(nD.replace(/\-/g, '\/').replace(/[T|Z]/g, ' '));
					}
					oDate=(Date.UTC(oDate.getFullYear(),oDate.getMonth(),oDate.getDate(),0,0,0))/86400000;
					oNow=(Date.UTC(oNow.getFullYear(),oNow.getMonth(),oNow.getDate(),0,0,0))/86400000;
					nDiff=(oNow-oDate);
					if(nDiff<1){vDate="Today"}
					else if(nDiff==1){vDate="Yesterday"}
					else if(nDiff<7){vDate=nDiff+" days ago"}
					else if(nDiff<14){vDate="1 week ago"}
					else if(nDiff<31){vDate=Math.floor(nDiff/7)+" weeks ago"}
					else if(nDiff<62){vDate="1 month ago"}
					else if(nDiff<365){vDate=Math.floor(nDiff/31)+" months ago"}
					else if(nDiff<730){vDate="1 year ago"}
					else{vDate=Math.floor(nDiff/365)+" years ago"};
					return vDate;
				},
				getTime:function(nD){
					var oDate=new Date(nD),vTime='',nHours=oDate.getHours(),nMinutes=oDate.getMinutes(),nSeconds=oDate.getSeconds();
					nHours=(nHours<10)?'0'+nHours:nHours;
					nMinutes=(nMinutes<10)?'0'+nMinutes:nMinutes;
					nSeconds=(nSeconds<10)?'0'+nSeconds:nSeconds;
					return nHours+':'+nMinutes+':'+nSeconds;
				},
				getViewportSize:function(){
					var oReturnSize=[];
					oReturnSize.push(document.body.offsetWidth);// Width
					oReturnSize.push(((document.documentElement.clientHeight>document.body.offsetHeight)?document.documentElement.clientHeight:document.body.offsetHeight));// Height
					return oReturnSize;
				},
				maximise:function(oT){
				//	var oT=flex.utils.get(oT),oDimensions=flex.utils.getViewportSize();
				//	oT.style.width=oDimensions[0]+'px';
				//	oT.style.height=oDimensions[1]+'px';
				},
				center:function(){
					return {
						init:function(oT,bTop){
							clearInterval(oT.oPosInterval);
							oT.oPosInterval=setInterval(function(){
								flex.utils.center.setPos(oT,bTop);
							},30);
						},
						setPos:function(oT,bTop){
							var nHeight=document.documentElement.clientHeight,nTopPos=0,nLeftPos=0;
							nTopPos=Math.ceil((nHeight/2)-(oT.offsetHeight/2));
							nLeftPos=Math.ceil((document.body.offsetWidth/2)-(oT.offsetWidth/2));
							(!bTop)?oT.style.top=((nTopPos<20)?20:nTopPos)+'px':null;
							oT.style.left=((nLeftPos<20)?20:nLeftPos)+'px';
						},
						clear:function(oT){
							clearInterval(oT.oPosInterval);
						}
					}
				}(),
				cookie:function(){ //Modified from QuirksMode.org
					return {
						create:function(vName,vValue,nDays) {
							if (nDays) {
								var dDate = new Date();
								dDate.setTime(dDate.getTime()+(nDays*24*60*60*1000));
								var vExpires = "; expires="+dDate.toGMTString();
							}
							else var vExpires = "";
							document.cookie = vName+"="+vValue+vExpires+"; path=/";
						},
						read:function(vName) {
							var vCookieName = vName + "=";
							var aCookie = document.cookie.split(';');
							for(var i=0;i < aCookie.length;i++) {
								var oC = aCookie[i];
								while (oC.charAt(0)==' ') oC=oC.substring(1,oC.length);
								if (oC.indexOf(vCookieName) == 0) return oC.substring(vCookieName.length,oC.length);
							}
							return undefined;
						},
						erase:function(vName) {
							flex.utils.cookie.create(vName,"",-1);
						}
					}
				}(),
				create:function(vType,vContents,vTextWrapper){
					var oItem=(vType=='text')?document.createTextNode(vContents):document.createElement(vType);
					if(vTextWrapper){
						oWrapperItem=document.createElement(vTextWrapper);
						oWrapperItem.appendChild(oItem);
						return oWrapperItem;
					} else {
						return oItem;
					}
				}
			}
		}());
		


		// Protypes
			// Trim
			String.prototype.trim = function() {
				return this.replace(/^\s+|\s+$/g,"");
			}
		
			// Remove last full stop prototype
			String.prototype.trimFullStops = function() {
				return this.replace(/^\.+|\.+$/g,"");
			}
		
			// Remove spaces prototype
			String.prototype.removeSpace = function() {
				return this.replace(/\s/g,"");
			}
		
			// New line remove protoype
			String.prototype.replaceNewLine = function() {
				return this.replace(/(\r\n|\r|\n)/g, "<br />");
			}
		
			// Replace breaks remove protoype
			String.prototype.replaceBreaks = function() {
				return this.replace(/<br \/>|<br\/>/g, "\n");
			}
		
			// Remove BR protoype
			String.prototype.stripBreaks = function() {
				return this.replace(/<br \/>|<br\/>/g, " ");
			}		
		
			// HTML remove tags protoype
			String.prototype.stripTags = function() {
				return this.replace(/<\S[^>]*>/g, "");
			}
			
			// Convert RGB to Hex
			String.prototype.toHex = function() {
				var b="",x=this;if(x.substr(0,1)!="r"){return x.replace('#','');}x=x.substr(4,x.length-5).split(", ");
				for(var i=0;i<x.length;i++){b+=((x[i]<16)?"0":"")+(x[i]&0xff).toString(16);}return b;
			}
			
			// Check is valid HEX
			String.prototype.isHex=function(){
				return this.match(/^[0-9a-f]{3,6}$/i);
			}

	
		// Lazy replacement for encodeURIComponent
		var Encode=encodeURIComponent;
		
		// Add domload listener
		//document.observe("dom:loaded",flex.load.init);

		function ToggleInterest(sInterest) {
		    $('#' + sInterest).toggleClass('closed');
		    $('#' + sInterest).toggleClass('open'); //.hasClass('closed'));
		   }

		   function doPostBackAsync(eventName, eventArgs) {
		   	var prm = Sys.WebForms.PageRequestManager.getInstance();

		   	if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
		   		prm._asyncPostBackControlIDs.push(eventName);
		   	}

		   	if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
		   		prm._asyncPostBackControlClientIDs.push(eventName);
		   	}

		   	if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
		   		Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
		   	}

		   	__doPostBack(eventName, eventArgs);
		}
	
	    
