
if(!SAPO.Component||typeof(SAPO.Component)=='undefined'){SAPO.namespace('Component');}
SAPO.Component.Dialog={_dialogs:{},_overlayLock:false,_makeNewDialog:function(){var id;var safety=0;var options;while(true){if(typeof arguments[0][0].id!="undefined"){if(typeof this._dialogs[arguments[0][0].id]!="undefined"){return false;}}
if(typeof this._dialogs[id]=="undefined"){options=this._setOptions(arguments[0]);id=options.id;this._dialogs[id]={options:options,overlay:false,component:false,targetElement:false,originalPosition:false,initialHeight:false,initialWidth:false};break;}else{id="SAPODialog_"+Math.round(10000*Math.random());}
if(safety>10){throw"You are creating too many dialogs";}else{safety++;}}
this._currentDialogId=id;var width=parseInt(options.width,10);var height=options.height;if(this._setTargetElement(options)===false){return false;}
var pageDims=this.getPageSize(id);var scrollDims=this.getPageScroll();var left=((Number(pageDims[2])/2)-(width/2));var top=0;options.message=options.message.replace(new RegExp("\\n","g"),'<br />');this.addLayer(options.className,pageDims);this._dialogs[id].component=this.createMessageComponent(options.message,width,height,top,left,options.className);return{pageDims:pageDims,scrollDims:scrollDims};},_setOptions:function(options){if(typeof options[0]==="string"){options[0]={message:options[0]};}
return Object.extend({id:"SAPODialog_"+Math.round(10000*Math.random()),width:250,height:false,onComplete:false,callBack:false,callBackCancel:false,className:false,targetElement:false,okLabel:'Ok',cancelLabel:'Cancelar',debug:false},options[0]||{});},_stoHideFlash:false,_intervalHideFlash:2000,_currentDialogId:false,addLayer:function(className,pageDims){var id=this._currentDialogId;var dialog=this._dialogs[id];var target=dialog.targetElement;var overlay=this._createOverlay(pageDims);this._dialogs[id].overlay=overlay;if(target){var pos=target.style.position;if(pos===''||pos==='static'){target.style.position='relative';dialog.originalPosition=Array();dialog.originalPosition[0]=target.style.top;dialog.originalPosition[1]=target.style.left;dialog.originalPosition[2]=target.style.bottom;dialog.originalPosition[3]=target.style.right;target.style.top=0;target.style.left=0;target.style.bottom=0;target.style.right=0;}
target.appendChild(overlay);}else{document.body.appendChild(overlay);}
if(this._stoHideFlash){clearInterval(this._stoHideFlash);this._stoHideFlash=false;}
this._hideAllFlash(className,this._currentDialogId);this._stoHideFlash=setInterval(function(cName,id){this._hideAllFlash(cName,id);}.bind(this,className,this._currentDialogId),this._intervalHideFlash);},_createOverlay:function(pageDims){var overlay=document.createElement('div');overlay.style.position='absolute';overlay.style.zIndex='100000';overlay.style.backgroundColor='#696969';overlay.style.cursor='not-allowed';overlay.className='sapo_widget_dialog_overlay';overlay.style.width=pageDims[0]+'px';overlay.style.height=pageDims[1]+'px';overlay.style.left='0px';overlay.style.top='0px';Element.setOpacity(overlay,0.5);return overlay;},removeLayer:function(id){if(typeof id=="undefined"){id=this._currentDialogId;}
var dialog=this._dialogs[id];if(dialog.component){dialog.component.parentNode.removeChild(dialog.component);}
if(dialog.overlay){dialog.overlay.parentNode.removeChild(dialog.overlay);}
if(dialog.targetElement){if(dialog.originalPosition===false){dialog.targetElement.style.top=dialog.originalPosition[0];dialog.targetElement.style.left=dialog.originalPosition[1];dialog.targetElement.style.bottom=dialog.originalPosition[2];dialog.targetElement.style.right=dialog.originalPosition[3];}}
this.showAllFlash(id);this._overlayLock=false;delete this._dialogs[id];},_addComponent:function(){var dialog=this._dialogs[this._currentDialogId];if(dialog.targetElement){dialog.targetElement.appendChild(dialog.component);dialog.targetElement=dialog.component.parentNode;}else{document.body.appendChild(dialog.component);}},_setupComponent:function(dialogInfo){var id=this._currentDialogId;var dialog=s$(id);var component=this._dialogs[id].component;this._dialogs[id].initialHeight=dialog.clientHeight;this._dialogs[id].initialWidth=dialog.clientWidth;var height=parseInt(Element.getHeight(component),10);var top=this._getTopPosition(dialogInfo.pageDims,dialogInfo.scrollDims,height,id);component.style.top=top+'px';if(this._dialogs[id].options.onComplete){this._dialogs[id].options.onComplete();}},_setTargetElement:function(options){var id=this._currentDialogId;var dialog=this._dialogs[id];if(options.targetElement){if(typeof options.targetElement==="string"){dialog.targetElement=document.getElementById(options.targetElement);}else{dialog.targetElement=options.targetElement;}
for(var i in this._dialogs){if(dialog.targetElement===this._dialogs[i].targetElement&&i!=id){delete this._dialogs[id];return false;}}}else{if(this._overlayLock===false){this._overlayLock=true;}else{return false;}}
return true;},_getTopPosition:function(pageDims,scrollDims,height,id){if(typeof id=="undefined"){id=this._currentDialogId;}
var position=Number((Number(pageDims[3])/2)-(height/2));if(position<0){position=5;}
if(this._dialogs[id].targetElement){return position;}else{return(position+Number(scrollDims[1]));}},_getLeftPosition:function(pageDims,scrollDims,width,id){if(typeof id=="undefined"){id=this._currentDialogId;}
var position=Number((Number(pageDims[2])/2)-(width/2));if(position<0){position=5;}
if(this._dialogs[id].targetElement){return position;}else{return(position+Number(scrollDims[0]));}},_makeOkButton:function(){var ok=document.createElement('INPUT');var options=this._dialogs[this._currentDialogId].options;if(options.image&&typeof(options.image)!='undefined'){ok.type='image';ok.src=options.image;}else{ok.type='button';ok.value=options.okLabel;}
ok.style.fontFamily='Arial, Verdana, sans-serif';ok.style.fontSize='12px';Event.observe(ok,'click',function(e,callBack,id){if(callBack){callBack(this._dialogs[id].options);}
SAPO.Component.Dialog.removeLayer(id);}.bindAsEventListener(this,options.callBack,this._currentDialogId));return ok;},_makeCancelButton:function(){var cancel=document.createElement('INPUT');var options=this._dialogs[this._currentDialogId].options;if(options.imageCancel&&typeof(options.imageCancel)!='undefined'){cancel.type='image';cancel.src=options.imageCancel;}else{cancel.type='button';cancel.value=options.cancelLabel;}
cancel.style.fontFamily='Arial, Verdana, sans-serif';cancel.style.fontSize='12px';Event.observe(cancel,'click',function(e,callBack,id){if(callBack){callBack(this._dialogs[id].options);}
SAPO.Component.Dialog.removeLayer(id);}.bindAsEventListener(this,options.callBackCancel,this._currentDialogId));return cancel;},alert:function(){var dialog=this._makeNewDialog(arguments);if(dialog===false){return false;}
var component=this._dialogs[this._currentDialogId].component;var ok=this._makeOkButton();var p=document.createElement('DIV');p.style.textAlign='center';p.style.padding='20px';p.appendChild(ok);component.appendChild(p);this._addComponent();this._setupComponent(dialog);},confirm:function(){var dialog=this._makeNewDialog(arguments);if(dialog===false){return false;}
var component=this._dialogs[this._currentDialogId].component;var ok=this._makeOkButton();var cancel=this._makeCancelButton();var p=document.createElement('DIV');p.style.textAlign='center';p.style.padding='20px';p.appendChild(ok);p.appendChild(document.createTextNode('   '));p.appendChild(cancel);component.appendChild(p);this._addComponent();this._setupComponent(dialog);},info:function(){var dialog=this._makeNewDialog(arguments);if(dialog===false){return false;}
this._addComponent();this._setupComponent(dialog);},createMessageComponent:function(message,width,height,top,left,className){var comp=document.createElement('div');comp.id=this._currentDialogId;comp.style.position='absolute';comp.style.zIndex='100001';comp.style.width=width+'px';if(height){comp.style.height=height+'px';}
comp.style.left=left+'px';comp.style.top=top+'px';comp.style.cursor='default';if(className&&typeof(className)!='undefined'&&className!==null){comp.className=className;}else{comp.style.padding='5px';comp.style.fontFamily='Arial, Verdana, sans-serif';comp.style.fontSize='12px';comp.style.textAlign='center';comp.style.backgroundColor='#FFF';comp.style.border='1px solid black';comp.className='sapo_widget_dialog_component';}
comp.innerHTML=message;return comp;},isInsideComponent:function(elm,className){while(elm.nodeName.toLowerCase()!='body'){if(elm.className===className){return true;}
elm=elm.parentNode;}
return false;},_hideAllFlash:function(className,id){var root,i,l;if(this._dialogs[id].targetElement){root=this._dialogs[id].targetElement;}else{root=document.body;}
var aEmbed=root.getElementsByTagName('EMBED');for(i=0,l=aEmbed.length;i<l;i++){if(!this.isInsideComponent(aEmbed[i],className)){aEmbed[i].style.visibility='hidden';}}
var aObject=root.getElementsByTagName('OBJECT');for(i=0,l=aObject.length;i<l;i++){if(!this.isInsideComponent(aObject[i],className)){aObject[i].style.visibility='hidden';}}
var aIframe=root.getElementsByTagName('IFRAME');for(i=0,l=aIframe.length;i<l;i++){if(!this.isInsideComponent(aIframe[i],className)){aIframe[i].style.visibility='hidden';}}
var aSelect=root.getElementsByTagName('SELECT');for(i=0,l=aSelect.length;i<l;i++){if(!this.isInsideComponent(aSelect[i],className)){aSelect[i].style.visibility='hidden';}}},showAllFlash:function(id){var root,i,l;if(this._stoHideFlash){clearInterval(this._stoHideFlash);this._stoHideFlash=false;}
if(this._dialogs[id].targetElement){root=this._dialogs[id].targetElement;}else{root=document.body;}
var aEmbed=root.getElementsByTagName('EMBED');for(i=0,l=aEmbed.length;i<l;i++){aEmbed[i].style.visibility='visible';}
var aObject=root.getElementsByTagName('OBJECT');for(i=0,l=aObject.length;i<l;i++){aObject[i].style.visibility='visible';}
var aIframe=root.getElementsByTagName('IFRAME');for(i=0,l=aIframe.length;i<l;i++){aIframe[i].style.visibility='visible';}
var aSelect=root.getElementsByTagName('SELECT');for(i=0,l=aSelect.length;i<l;i++){aSelect[i].style.visibility='visible';}},getPageScroll:function(){var arrayPageScroll=[SAPO.Utility.Dimensions.scrollWidth(),SAPO.Utility.Dimensions.scrollHeight()];return arrayPageScroll;},getPageSize:function(id){var arrayPageSize,target=false;if(typeof id!="undefined"){target=this._dialogs[id].targetElement;}
if(target){arrayPageSize=[SAPO.Utility.Dimensions.elementWidth(target),SAPO.Utility.Dimensions.elementHeight(target),SAPO.Utility.Dimensions.elementWidth(target),SAPO.Utility.Dimensions.elementHeight(target)];}else{arrayPageSize=[SAPO.Utility.Dimensions.pageWidth(),SAPO.Utility.Dimensions.pageHeight(),SAPO.Utility.Dimensions.viewportWidth(),SAPO.Utility.Dimensions.viewportHeight()];}
return arrayPageSize;},updatePosition:function(id){if(typeof id=="undefined"){id=this._currentDialogId;}
var dialog=s$(id);var newHeight=dialog.clientHeight;var newWidth=dialog.clientWidth;var pageDims=this.getPageSize(id);var scrollDims=this.getPageScroll();if(this._dialogs[id].initialWidth!=newWidth){dialog.style.left=this._getLeftPosition(pageDims,scrollDims,newWidth,id)+'px';this._dialogs[id].initialWidth=newWidth;}
if(this._dialogs[id].initialHeight!=newHeight){dialog.style.top=this._getTopPosition(pageDims,scrollDims,newHeight,id)+'px';this._dialogs[id].initialHeight=newHeight;}},updateSize:function(width,height,id){if(typeof id=="undefined"){id=this._currentDialogId;}
var dialog=s$(id);if(width!==false){dialog.style.width=width+'px';}
if(height!==false){dialog.style.height=height+'px';}
this.updatePosition(id);},debug:function(){}};