
(function(){'use strict';var Url={_keyStr:'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',_pattern:/^(?:([^:]+:)?\/\/(?:(?:(.+?):(.+?)@)?([^\/:]+)(?::(\d+))?))?(\/?[^\?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i,_parts:{href:function(_,$1,$2,$3,$4,$5,$6,$7,$8){var r=[];if($4){if($1){r.push($1);}
r.push('//');if($2){r.push($2+':'+$3+'@');}
r.push($4);if($5){r.push(':'+$5);}}
if($6){r.push($6);}
if($7){r.push('?'+$7);}
if($8){r.push('#'+$8);}
return r.join('')},protocol:'$1',user:'$2',pass:'$3',userpass:function(_,$1,$2,$3,$4,$5,$6,$7,$8){var r=[];if($2){r.push($2+':'+$3+'@');}
return r.join('')},hostname:'$4',port:'$5',host:function(_,$1,$2,$3,$4,$5,$6,$7,$8){var r=[];if($4){r.push($4);if($5){r.push(':'+$5);}}
return r.join('')},pathname:'$6',search:'$7',hash:'$8',pushState:function(_,$1,$2,$3,$4,$5,$6,$7,$8){var r=[];if($6){r.push($6);}
if($7){r.push('?'+$7);}
if($8){r.push('#'+$8);}
return r.join('')}},getUrl:function(){return location.href;},getSearch:function(url){return this._getPart(url,'search');},getSearchObj:function(url){return this._getPart(url,'search',true);},putSearch:function(search,url){return this._setPart(search,url,'search');},postSearch:function(search,url){return this._setPart(search,url,'search','post');},deleteSearch:function(url){return this._deletePart(url,'search');},getHash:function(url){return this._getPart(url,'hash');},getHashObj:function(url){return this._getPart(url,'hash',true);},putHash:function(hash,url){return this._setPart(hash,url,'hash');},postHash:function(hash,url){return this._setPart(hash,url,'hash','post');},deleteHash:function(url){return this._deletePart(url,'hash');},getPath:function(url){return this._getPart(url,'pathname');},getPathObj:function(url){return this._getPart(url,'pathname',true);},putPath:function(path,url){return this._setPart(path,url,'pathname');},postPath:function(path,url){return this._setPart(path,url,'pathname','post');},deletePath:function(url){return this._deletePart(url,'pathname');},getHost:function(url){return this._getPart(url,'hostname');},getHostObj:function(url){return this._getPart(url,'hostname',true);},putHost:function(host,url){return this._setPart(host,url,'hostname');},postHost:function(host,url){return this._setPart(host,url,'hostname','post');},deleteHost:function(url){return this._deletePart(url,'hostname');},getPort:function(url){return this._getPart(url,'port');},putPort:function(port,url){return this._setPart(port,url,'port');},deletePort:function(url){return this._deletePart(url,'port');},getProtocol:function(url){return this._getPart(url,'protocol');},putProtocol:function(protocol,url){return this._setPart(protocol,url,'protocol');},deleteProtocol:function(url){return this._deletePart(url,'protocol');},getUser:function(url){return this._getPart(url,'user');},getPass:function(url){return this._getPart(url,'pass');},getUserpass:function(url){return this._getPart(url,'userpass');},getUserpassObj:function(url){return this._getPart(url,'userpass',true);},putUserpass:function(userpass,url){return this._setPart(userpass,url,'userpass');},deleteUserpass:function(url){return this._deletePart(url,'userpass');},parseUrl:function(url){url=this._getUrl(url);if(typeof url!=='string'){return url;}
var oURL={};url.replace(this._pattern,function(_,$1,$2,$3,$4,$5,$6,$7,$8){oURL.protocol=$1||false;oURL.user=$2||false;oURL.pass=$3||false;oURL.hostname=$4!==undefined?this._getPartObj($4,'hostname'):false;oURL.port=$5||false;oURL.pathname=$6!==undefined?this._getPartObj($6,'pathname'):false;oURL.search=$7!==undefined?this._getPartObj($7,'search'):false;oURL.hash=$8!==undefined?this._getPartObj($8,'hash'):false;}.bindObj(this));return oURL;},composeUrl:function(oURL){if(typeof oURL==='undefined'){return'';}
else if(typeof oURL==='string'){return oURL;}
var url=[];if(oURL.hostname){if(oURL.protocol){url.push(oURL.protocol);}
url.push('//');if(oURL.user&&oURL.pass){url.push(oURL.user+':'+oURL.pass+'@');}
url.push(this._setPartObj(oURL.hostname,'hostname'));if(oURL.port){url.push(':'+oURL.port);}}
if(oURL.pathname){url.push(this._setPartObj(oURL.pathname,'pathname'));}
if(oURL.search){url.push('?'+this._setPartObj(oURL.search,'search'));}
if(oURL.hash){url.push('#'+this._setPartObj(oURL.hash,'hash'));}
return url.join('');},currentScriptElement:function(match){var aScripts=document.getElementsByTagName('script');if(typeof(match)==='undefined'){if(aScripts.length>0){return aScripts[(aScripts.length-1)];}else{return false;}}else{var curScript=false;var re=new RegExp(""+match+"","i");for(var i=0,total=aScripts.length;i<total;i++){curScript=aScripts[i];if(re.test(curScript.src)){return curScript;}}
return false;}},base64Encode:function(string){if(!SAPO.Utility.String||typeof(SAPO.Utility.String)==='undefined'){throw"SAPO.Utility.Url.base64Encode depends of SAPO.Utility.String, which has not been referred.";}
var output="";var chr1,chr2,chr3,enc1,enc2,enc3,enc4;var i=0;var input=SAPO.Utility.String.utf8Encode(string);while(i<input.length){chr1=input.charCodeAt(i++);chr2=input.charCodeAt(i++);chr3=input.charCodeAt(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64;}else if(isNaN(chr3)){enc4=64;}
output=output+
this._keyStr.charAt(enc1)+this._keyStr.charAt(enc2)+
this._keyStr.charAt(enc3)+this._keyStr.charAt(enc4);}
return output;},base64Decode:function(string){if(!SAPO.Utility.String||typeof(SAPO.Utility.String)==='undefined'){throw"SAPO.Utility.Url.base64Decode depends of SAPO.Utility.String, which has not been referred.";}
var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;var input=string.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i<input.length){enc1=this._keyStr.indexOf(input.charAt(i++));enc2=this._keyStr.indexOf(input.charAt(i++));enc3=this._keyStr.indexOf(input.charAt(i++));enc4=this._keyStr.indexOf(input.charAt(i++));chr1=(enc1<<2)|(enc2>>4);chr2=((enc2&15)<<4)|(enc3>>2);chr3=((enc3&3)<<6)|enc4;output=output+String.fromCharCode(chr1);if(enc3!==64){output=output+String.fromCharCode(chr2);}
if(enc4!==64){output=output+String.fromCharCode(chr3);}}
output=SAPO.Utility.String.utf8Decode(output);return output;},_getUrl:function(url){return typeof url!=='undefined'?url:this.getUrl();},_getPart:function(url,part,obj){url=this._getUrl(url);var uPart=url.replace(this._pattern,this._parts[part]||part||'');return obj?this._getPartObj(uPart,part):uPart;},_getPartObj:function(sPart,part){var sep;var oPart;switch(part){case'search':case'hash':oPart={};if(typeof sPart==='object'){oPart=sPart;}else if(!/[;&=]/i.test(sPart)){oPart=decodeURIComponent(sPart);}else if(sPart){var pArray=sPart.split(/[;&]/);for(var l=pArray.length,i=l-1;i>=0;i--){var pairVar=pArray[i].split('=');oPart[decodeURIComponent(pairVar[0])]=pairVar.length>1?decodeURIComponent(pairVar[1]):false;}}
break;case'userpass':case'host':sep=':';case'hostname':sep=sep||'.';case'pathname':sep=sep||'/';oPart=[];if(typeof sPart==='object'){oPart=sPart;}else if(sPart){oPart=sPart.split(sep);}
break;case'href':oPart=this.parseUrl(sPart);break;default:oPart=sPart;}
return oPart;},_setPart:function(oPart,url,part,type){oPart=this._getPartObj(oPart,part);url=this.parseUrl(url);if(url[part]&&type==='post'){oPart=this._concatPart(url[part],oPart,part);}else if(part==='userpass'){url['user']=oPart[0]||'';part='pass';oPart=oPart[1]||'';}
url[part]=oPart;return this.composeUrl(url);},_setPartObj:function(oPart,part){var sep;var sPart;switch(part){case'search':case'hash':if(typeof oPart==='string'){sPart=encodeURIComponent(oPart);}else{var aString=[];for(var key in oPart){if(oPart.hasOwnProperty(key)){var sKey=encodeURIComponent(key);var val=oPart[key];var sVal=val===false?'':'='+encodeURIComponent(val);aString.push(sKey+sVal);}}
sPart=aString.sort().join('&');}
break;case'userpass':case'host':sep=':';case'hostname':sep=sep||'.';case'pathname':sep=sep||'/';if(typeof oPart==='string'){sPart=oPart;}else if(oPart){sPart=oPart.join(sep);}
break;case'href':sPart=this.composeUrl(oPart);break;default:sPart=oPart;}
return sPart;},_concatPart:function(iPart,xPart,part){var oPart;switch(part){case'search':case'hash':var oPart={}
var auxI={};var auxX={};if(typeof iPart==='string'){auxI[iPart]=false;}
else{auxI=iPart;}
if(typeof xPart==='string'){auxX[xPart]=false;}
else{auxX=xPart;}
SAPO.extendObj(oPart,auxI);SAPO.extendObj(oPart,auxX||{});break;case'hostname':oPart=[];oPart=xPart?xPart.concat(iPart):oPart.concat(iPart);break;case'pathname':var idx=iPart.length-1;xPart=xPart&&xPart[0]===''?xPart:[''].concat(xPart||[]);oPart=iPart.slice(0,idx).concat(xPart.slice(1));break;default:oPart=xPart;}
return oPart;},_deletePart:function(url,part){var oURL=this.parseUrl(url);if(part==='userpass'){oURL['user']=false;part='pass';}
oURL[part]=false;return this.composeUrl(oURL);}};var handlerWindow=function(window,options){this._options={onHashChange:false,onSearchChange:false,onPathChange:false,pushState:true};SAPO.extendObj(this._options,options||{});this._init(window);}
handlerWindow.prototype={getHash:function(){return this._oldHash;},getHashObj:function(){return Url._getPartObj(this._oldHash,'hash');},putHash:function(oPart){this._updatePart(oPart,'hash');},postHash:function(oPart){this._updatePart(oPart,'hash','post');},deleteHash:function(){this._updateUrl('','hash');},getSearch:function(){return this._oldSearch;},getSearchObj:function(){return Url._getPartObj(this._oldSearch,'search');},putSearch:function(oPart){this._updatePart(oPart,'search');},postSearch:function(oPart){this._updatePart(oPart,'search','post');},deleteSearch:function(){this._updateUrl('','search');},getPath:function(){return this._oldPath;},getPathObj:function(){return Url._getPartObj(this._oldPath,'pathname');},putPath:function(oPart){this._updatePart(oPart,'pathname');},postPath:function(oPart){this._updatePart(oPart,'pathname','post');},deletePath:function(){this._updateUrl('/','pathname');},_init:function(window){this._window=window;this._location=window.location;this._history=window.history;this._document=window.document;this._pushState=this._history.pushState||function(state,title,path){this._location.replace(path);};this._updateInfo(true)
if(typeof this._options.onHashChange==='function'){SAPO.Dom.Event.observe(this._window,'hashchange',this._onhashchange.bindObjEvent(this));if(this._window.onhashchange===undefined){this._notOnhashchange();}}
if(this._options.pushState&&typeof this._options.onSearchChange==='function'){SAPO.Dom.Event.observe(this._window,'popstate',this._onsearchchange.bindObjEvent(this));}
if(this._options.pushState&&typeof this._options.onPathChange==='function'){SAPO.Dom.Event.observe(this._window,'popstate',this._onpathchange.bindObjEvent(this));}},_notOnhashchange:function(){setInterval(function(){this._newHash=Url.getHash(this._location.href);if(this._newHash!==this._oldHash){SAPO.Dom.Event.fire(this._window,'hashchange');}}.bindObj(this),100);},_onhashchange:function(e){SAPO.Dom.Event.stop(e);this._updateInfo()
if(this._newHash!==this._oldHash){this._oldHash=this._newHash;this._options.onHashChange();}},_onsearchchange:function(e){SAPO.Dom.Event.stop(e);this._updateInfo()
if(this._newSearch!==this._oldSearch){this._oldSearch=this._newSearch;this._options.onSearchChange();}},_onpathchange:function(e){SAPO.Dom.Event.stop(e);this._updateInfo()
if(this._newPath!==this._oldPath){this._oldPath=this._newPath;this._options.onPathChange();}},_updateInfo:function(updateOld){this._url=this._location.href
this._urlObj=Url.parseUrl(this._url);this._newHash=Url.getHash(this._url);this._newSearch=Url.getSearch(this._url);this._newPath=Url.getPath(this._url);if(updateOld){this._oldHash=this._newHash;this._oldSearch=this._newSearch;this._oldPath=this._newPath;}},_updatePart:function(oPart,part,type){oPart=Url._getPartObj(oPart,part);if(this._urlObj[part]&&type==='post'){oPart=Url._concatPart(this._urlObj[part],oPart,part);}
this._updateUrl(oPart,part);},_updateUrl:function(oPart,part){switch(part){case'hash':if(oPart===''){var scrollV=this._document.body.scrollTop;var scrollH=this._document.body.scrollLeft;}
this._oldHash=Url._setPartObj(oPart,part);this._location.hash=this._oldHash;if(oPart===''){this._document.body.scrollTop=scrollV;this._document.body.scrollLeft=scrollH;}
break;case'search':case'pathname':this._urlObj[part]=oPart;var newUrl=Url._getPart(Url.composeUrl(this._urlObj),'pushState');if(this._options.pushState){this._pushState(part,'',newUrl);this._updateInfo(true);}
else{this._location.replace(newUrl);}
break;}}}
SAPO.namespace('Utility');SAPO.Utility.Url=Url;SAPO.Utility.Url.HandlerWindow=handlerWindow;})();