
(function(window,undefined){'use strict';SAPO.namespace('Utility');var logAsImg=function(host,msg){var i;var src=[host,'?',encodeURIComponent(msg)].join('');i=new Image();i.src=src;};var reqCmd=function(){var a=new SAPO.Communication.Ajax(SAPO.Utility.Debug._remoteInEndpoint,{method:'GET',cors:true,parameters:{_:~~(Math.random()*10000)},onSuccess:function(tmp,cmd){a._wentWell=true;SAPO.Utility.Debug.submitConsoleCommand(cmd);},onComplete:function(){if(!this._wentWell){throw new Error('Could not access in endpoint: '+SAPO.Utility.Debug._remoteInEndpoint+'!');}
reqCmd();}});};var typeof2=function(o){try{return(typeof o!=='object'||o===null)?typeof o:/(\w+)\(/.exec(o.constructor.toString())[1];}catch(ex){}
return'?';};var cache=[];var isNode=(typeof Node==='object')?function(o){return o instanceof Node;}:function(o){return o&&typeof o==='object'&&typeof o.nodeType==='number'&&typeof o.nodeName==='string';};var replacer=function(k,v){var to=typeof v;if(to==='object'&&v!==null){if(isNode(v)){return(v.nodeType===1)?v.nodeName:'NODE';}
else if(cache.indexOf(v)!==-1){return'CIR';}
cache.push(v);}
return v;};var jsonStringifyWithoutCirculars=function(o,sep){cache=[];var o2=JSON.stringify(o,replacer,sep);cache=null;return o2;};SAPO.Utility.Debug={_consoleSupported:!!window.console,_consoleEl:undefined,_consoleListener:undefined,_maxCmds:10,_maxLines:50,_chronos:{},_remoteOutEndpoint:undefined,_remoteInEndpoint:undefined,chronoStart:function(name){if(typeof name==='undefined'){name='_default_';}
this._chronos[name]=new Date().getTime();},chronoStop:function(name,lapMode){if(typeof name==='undefined'){name='_default_';}
var thisTime=new Date().getTime();var oldTime=this._chronos[name];if(typeof oldTime!=='number'){throw'Called chronoStop of name "'+name+'" prior to calling chronoStart?!';}
var res=thisTime-oldTime;if(!lapMode){this._chronos[name]=new Date().getTime();}
return res;},ptNum:0,rtNum:0,addPoint:function(parentEl,pos,color,side){if(color===undefined){color='#A0A';}
if(side===undefined){side=3;}
var d=(side-1)/2;var pointEl=document.createElement('div');pointEl.className='dbgPt';pointEl.id='dbgPt'+SAPO.Utility.Debug.ptNum;pointEl.style.position='absolute';pointEl.style.width=side+'px';pointEl.style.height=side+'px';pointEl.style.left=(pos[0]-d)+'px';pointEl.style.top=(pos[1]-d)+'px';pointEl.setAttribute('d',d);pointEl.style.backgroundColor=color;parentEl.appendChild(pointEl);++SAPO.Utility.Debug.ptNum;return pointEl;},movePoint:function(point,pos){var pointEl=s$(point);var d=parseInt(pointEl.getAttribute('d',d),10);pointEl.style.left=(pos[0]-d)+'px';pointEl.style.top=(pos[1]-d)+'px';},removePoints:function(parentEl){var el;for(var i=parentEl.childNodes.length-1;i>=0;--i){el=parentEl.childNodes[i];if(el.className==='dbgPt'){parentEl.removeChild(el);}}
SAPO.Utility.Debug.ptNum=0;},addRect:function(parentEl,pos,dims,color){if(color===undefined){color='rgba(0,200,200,0.5';}
var pointEl=document.createElement('div');pointEl.className='dbgRt';pointEl.id='dbgRt'+SAPO.Utility.Debug.rtNum;pointEl.style.position='absolute';pointEl.style.width=(dims[0]-2)+'px';pointEl.style.height=(dims[1]-2)+'px';pointEl.style.left=pos[0]+'px';pointEl.style.top=pos[1]+'px';pointEl.style.backgroundColor='transparent';pointEl.style.border='1px solid '+color;parentEl.appendChild(pointEl);++SAPO.Utility.Debug.rtNum;return pointEl;},updateRect:function(point,pos,dims){var pointEl=s$(point);if(pos){pointEl.style.left=pos[0]+'px';pointEl.style.top=pos[1]+'px';}
if(dims){pointEl.style.width=(dims[0]-2)+'px';pointEl.style.height=(dims[1]-2)+'px';}},removeRects:function(parentEl){var el;for(var i=parentEl.childNodes.length-1;i>=0;--i){el=parentEl.childNodes[i];if(el.className==='dbgRt'){parentEl.removeChild(el);}}
SAPO.Utility.Debug.rtNum=0;},log:function(msg){if(this._remoteOutEndpoint){this.remoteLog(msg);}
if(this._consoleSupported&&!this._consoleEl){return console.log(msg);}
var d=this._attachConsole();var els=this._consoleEl.getElementsByTagName('*');els=Array.prototype.slice.call(els);if(els.length>this._maxLines){this._consoleEl.removeChild(els.shift());this._consoleEl.removeChild(els.shift());}
d.innerHTML+='<br/>'+msg;d.scrollTop=d.scrollHeight;},_moveCursorToEnd:function(el){var l=el.value.length||0;if(!l){return;}
if('setSelectionRange'in el){el.setSelectionRange(l,l);}
else if('createTextRange'in el){var range=el.createTextRange();range.moveStart('character',l);range.select();}},_getDims:function(){return[window.innerWidth-53,Math.min(140,~~(window.innerHeight*0.3))];},_attachConsole:function(startsClosed){if(typeof this._consoleEl!=='undefined'){return this._consoleEl;}
var dims=SAPO.Utility.Debug._getDims();var dbgCmds=[];var dbgCmdsDelta=0;var s;var dbgDiv=document.createElement('div');var dbgInput=document.createElement('input');dbgInput.setAttribute('autocapitalize','off');dbgInput.setAttribute('spellcheck','false');dbgDiv.setAttribute('id','dbg');s=dbgDiv.style;s.position='fixed';s.bottom='22px';s.left=0;s.padding='1px 4px';s.zIndex=10000;s.overflow='hidden';s.overflowY='auto';s.border='1px solid #AAA';s.wordWrap='break-word';s.fontFamily='monospace';s.fontSize='13px';s.backgroundColor='#FFF';s.width=dims[0]+'px';s.height=dims[1]+'px';dbgInput.setAttribute('id','dbgLine');dbgInput.setAttribute('type','text');s=dbgInput.style;s.position='fixed';s.bottom=0;s.left=0;s.padding='1px 4px';s.zIndex=10000;s.border='1px solid #AAA';s.fontFamily='monospace';s.fontSize='13px';s.width=dims[0]+'px';s.height='16px';var x=dims[0]+12;var y=0;var w=40;var h=20;var bh=22;var dbgDismiss=document.createElement('button');dbgDismiss.innerHTML='DBG';s=dbgDismiss.style;s.position='fixed';s.bottom=y+'px';s.left=x+'px';s.width=w+'px';s.height=h+'px';s.padding=0;s.border=0;s.fontSize='11px';var onDismiss=function(){var disp=(dbgDiv.style.display==='none')?'':'none';dbgDiv.style.display=disp;dbgInput.style.display=disp;dbgClear.style.display=disp;dbgPrev.style.display=disp;dbgNext.style.display=disp;};SAPO.Dom.Event.observe(dbgDismiss,'click',onDismiss);y+=bh;var dbgClear=document.createElement('button');dbgClear.innerHTML='CLR';s=dbgClear.style;s.position='fixed';s.bottom=y+'px';s.left=x+'px';s.width=w+'px';s.height=h+'px';s.padding=0;s.border=0;s.fontSize='11px';SAPO.Dom.Event.observe(dbgClear,'click',function(){dbgCmds=[];SAPO.Utility.Debug.submitConsoleCommand('cls');dbgInput.value='';dbgInput.focus();});y+=bh;var onKey=function(event){if(event.keyCode===38||event.keyCode===40){try{SAPO.Dom.Event.stop(event);}catch(ex){}
var delta=(event.keyCode===40)?1:-1;var l=dbgCmds.length;var i=l+dbgCmdsDelta+delta;if(i<0){i=0;}
else if(i>l-1){i=l-1;}
var cmd=dbgCmds[i];if(!cmd){return dbgInput.focus();}
dbgInput.value=cmd;dbgCmdsDelta+=delta;dbgInput.focus();this._moveCursorToEnd(dbgInput);return false;}
if(event.keyCode!==13){return true;}
var dbgVal=dbgInput.value;dbgInput.value='';dbgInput.focus();SAPO.Utility.Debug.submitConsoleCommand(dbgVal);}.bindObjEvent(this);var dbgNext=document.createElement('button');dbgNext.innerHTML='\\/';s=dbgNext.style;s.position='fixed';s.bottom=y+'px';s.left=x+'px';s.width=w+'px';s.height=h+'px';s.padding=0;s.border=0;s.fontSize='11px';SAPO.Dom.Event.observe(dbgNext,'click',function(){onKey({keyCode:40});});y+=bh;var dbgPrev=document.createElement('button');dbgPrev.innerHTML='/\\';s=dbgPrev.style;s.position='fixed';s.bottom=y+'px';s.left=x+'px';s.width=w+'px';s.height=h+'px';s.padding=0;s.border=0;s.fontSize='11px';SAPO.Dom.Event.observe(dbgPrev,'click',function(){onKey({keyCode:38});});document.body.appendChild(dbgDiv);document.body.appendChild(dbgInput);document.body.appendChild(dbgDismiss);document.body.appendChild(dbgClear);document.body.appendChild(dbgPrev);document.body.appendChild(dbgNext);SAPO.Dom.Event.observe(window,'resize',function(){var dims=SAPO.Utility.Debug._getDims();var w=dims[0]+'px';var h=dims[1]+'px';var x=(dims[0]+12)+'px';dbgDiv.style.width=w;dbgDiv.style.height=h;dbgInput.style.width=w;dbgDismiss.style.left=x;dbgClear.style.left=x;dbgPrev.style.left=x;dbgNext.style.left=x;});SAPO.Utility.Debug.submitConsoleCommand=function(cmd){dbgCmdsDelta=0;dbgCmds.push(cmd);if(dbgCmds.length>this._maxCmds){dbgCmds.shift();}
if(cmd==='cls'){s$('dbg').innerHTML='';return;}
this.log(['IN:  ',cmd].join(''));var res=SAPO.Utility.Debug._runCmd(cmd);var out=res[2];var outType=res[3];var htmlRes=['OUT: (',outType,'): ',out].join('');this.log(htmlRes);};SAPO.Dom.Event.observe(dbgInput,'keydown',function(event){if(event.keyCode===38||event.keyCode===40){SAPO.Dom.Event.stop(event);var delta=(event.keyCode===40)?1:-1;var cmd=dbgCmds[dbgCmds.length+dbgCmdsDelta+delta];if(!cmd){return;}
dbgInput.value=cmd;dbgCmdsDelta+=delta;dbgInput.focus();this._moveCursorToEnd(dbgInput);return false;}
if(event.keyCode!==13){return true;}
var dbgVal=dbgInput.value;dbgInput.value='';dbgInput.focus();SAPO.Utility.Debug.submitConsoleCommand(dbgVal);}.bindObjEvent(this));this._consoleEl=dbgDiv;if(startsClosed){onDismiss();}
return dbgDiv;},_runCmd:function(cmd){try{var obj=eval(cmd);var type=typeof2(obj);obj=jsonStringifyWithoutCirculars(obj,'');return[false,cmd,obj,type];}catch(ex){return[true,cmd,ex.toString(),'Exception'];}},_setConsoleListener:function(f){this._consoleListener=f;},showConsoleAnyway:function(startsClosed){this._attachConsole(startsClosed);var origLog=console.log;var origErr=console.error;console.log=function(){var args=Array.prototype.slice.call(arguments);var s=args.join(', ');SAPO.Utility.Debug.log(s);origLog.apply(console,args);};console.error=function(){var args=Array.prototype.slice.call(arguments);var s=args.join('ERROR: '+args.join(', '));SAPO.Utility.Debug.log(s);origErr.apply(console,args);};this._origLog=origLog;this._origErr=origErr;this.origLog=function(){SAPO.Utility.Debug._origLog.apply(console,Array.prototype.slice.call(arguments));};this.origErr=function(){SAPO.Utility.Debug._origErr.apply(console,Array.prototype.slice.call(arguments));};},setRemoteLog:function(outEp,inEp){this._remoteOutEndpoint=outEp;this._remoteInEndpoint=inEp;if(inEp){if(!SAPO||!SAPO.Communication||!SAPO.Communication.Ajax){SAPO.require(['http://js.staging.sapo.pt/SAPO/Communication/Ajax/2.1/'],reqCmd);}
else{reqCmd();}}},remoteLog:function(){var args=Array.prototype.slice.call(arguments);logAsImg(this._remoteOutEndpoint,args.join(', '));}};})(window);