
if(!SAPO.Component||typeof(SAPO.Component)=='undefined'){SAPO.namespace('Component');}
(function(){var Css=SAPO.Dom.Css,Event=SAPO.Dom.Event,Dimensions=SAPO.Utility.Dimensions,Fade=false;if('Effects'in SAPO){if('Fade'in SAPO.Effects){Fade=SAPO.Effects.Fade;}}
function dce(type){return document.createElement(type);}
function isTag(el,tag){if(el&&typeof el.nodeType!=='undefined'){if(el.nodeType===1){if(el.tagName.toLowerCase()===tag){return true;}}}
return false;}
function getThumb(node){if(node){var i,li,j,lj,children=node.childNodes,img,c,props=['src','alt','title','className'];for(i=0,li=children.length;i<li;i++){c=children[i];if(isTag(c,'img')){img=dce('img');for(j=0,lj=props.length;j<lj;j++){if(typeof c[props[j]]!=='undefined'){img[props[j]]=c[props[j]];}}
return img;}}}
return false;}
function getCaption(el){var curr=el.nextSibling,capCount=0;while(curr){capCount++;if(isTag(curr,'div')){if(Css.hasClassName(curr,'caption')){return curr.innerHTML;}}
if(capCount>=2){return'';}
curr=curr.nextSibling;}
return'';}
function getHref(curr){var i,l;if(isTag(curr,'li')){for(i=0,l=curr.childNodes.length;i<l;i++){if(isTag(curr.childNodes[i],'a')){return curr.childNodes[i];}}}
return false;}
function getSpan(el){for(var i=0,l=el.childNodes.length;i<l;i++){if(isTag(el.childNodes[i],'span')){return el.childNodes[i];}}}
SAPO.Component.Gallery=function(options){this.init(options);};SAPO.Component.Gallery.version='0.1';SAPO.Component.Gallery.prototype={_isTag:isTag,_container:false,_image:false,_imageLoader:false,_galleryStage:false,_loaderWidth:false,_loaderHeight:false,_caption:false,_list:false,_next:false,_prev:false,_first:false,_last:false,_firstNode:false,_lastNode:false,_hprev:false,_hnext:false,_pagePrev:false,_pagePrevBound:false,_pageNext:false,_pageNextBound:false,_current:false,_navLock:false,_loadLock:false,_firstImage:false,_hoverResetTimeout:200,init:function(){this._options=SAPO.extendObj({container:false,list:false,hoverControls:true,hoverThumbs:false,navLinks:false,endsLinks:false,navThumbs:false,pageThumbs:false,prevLabel:'Anterior',nextLabel:'Pr&oacute;xima',firstLabel:'Primeira',lastLabel:'&Uacute;ltima',prevPageLabel:'&lt;&lt;',nextPageLabel:'&gt;&gt;',showList:true,showCaption:true,width:false,height:false,onChange:false,slidebox:false,fadeInTime:1000,fadeOutTime:1000,startAtIndex:undefined},arguments[0]||{});if(typeof this._options.maxSize==='string'){try{this._options.maxSize=this._options.maxSize.split(',');this._options.maxSize[0]=parseInt(this._options.maxSize[0],10);this._options.maxSize[1]=parseInt(this._options.maxSize[1],10);}catch(ex){throw'Problem parsing option maxSize!';}}
if(!(this._options.maxSize instanceof Array)){this._options.maxSize=[Dimensions.viewportWidth()-60,Dimensions.viewportHeight()-110];}
if(this._options.container){this._container=this._options.container=s$(this._options.container);}
if(this._options.slidebox){this._hoverResetTimeout=600;}
if(!this._container){this._makeMarkup();}
this._setupMarkup();this._setEvents();},_makeMarkup:function(){var html,div,prevImg,nextImg,prev=false,next=false,first=false,last=false,opt=this._options;html=dce('div');Css.addClassName(html,'SAPOGallery');if(opt.endsLinks){first=dce('a');first.innerHTML=opt.firstLabel;Css.addClassName(first,'first');html.appendChild(first);}
if(opt.navLinks){prev=dce('a');prev.href='#';next=dce('a');next.href='#';if(opt.navThumbs){prevImg=dce('img');nextImg=dce('img');prev.appendChild(prevImg);next.appendChild(nextImg);}
else{prev.innerHTML=opt.prevLabel;next.innerHTML=opt.nextLabel;}
Css.addClassName(prev,'previous');Css.addClassName(next,'next');html.appendChild(prev);html.appendChild(next);}
this._prev=prev;this._next=next;if(opt.endsLinks){last=dce('a');last.innerHTML=opt.lastLabel;Css.addClassName(last,'last');html.appendChild(last);}
this._first=first;this._last=last;this._list=this.makeThumbList((opt.slidebox===true)?false:true);html.appendChild(this._list);this._container=html;},makeThumbList:function(paging){var i,l,li,a,img,caption,hideCounter=0,ul=dce('ul'),list=this._options.list;Css.addClassName(ul,'thumbs');for(i=0,l=list.length;i<l;i++){li=dce('li');a=dce('a');img=dce('img');caption=dce('div');a.href=list[i].href;img.alt=a.title=list[i].title||'';img.src=list[i].thumb;a.appendChild(img);li.appendChild(a);if(typeof list[i].caption!=='undefined'){caption.innerHTML=list[i].caption;Css.addClassName(caption,'caption');li.appendChild(caption);}
if(this._options.pageThumbs&&paging){hideCounter=this._hidePaged(hideCounter,li);}
ul.appendChild(li);}
return ul;},setSlidebox:function(atag,onChange){this._options.slidebox=atag;this._options.onChange=onChange;this._loadImage(atag);},_hidePaged:function(counter,li){if(!this._pagePrevBound){this._pagePrevBound=li;}
if(counter>=this._options.pageThumbs){if(!this._pageNextBound){this._pageNextBound=li;}
li.style.display='none';}
counter++;return counter;},_setupMarkup:function(){var list,node,child,i,l,imgDiv,caption,pagePrev,pageNext,prevHover,nextHover,container=this._container,opt=this._options,prev=false,next=false,first=false,last=false,hideCounter=0;if(!this._list||!this._prev&&!this._next){for(i=0,l=container.childNodes.length;i<l;i++){node=container.childNodes[i];if(isTag(node,'ul')&&!this._list){list=this._list=node;}
if(isTag(node,'a')&&!this._prev||!this._next){if(Css.hasClassName(node,'previous')){prev=node;}
if(Css.hasClassName(node,'next')){next=node;}
if(Css.hasClassName(node,'first')){first=node;}
if(Css.hasClassName(node,'last')){last=node;}}}}
if(prev&&next){this._prev=prev;this._next=next;}
if(first&&last){this._first=first;this._last=last;}
if(opt.navThumbs){this._next.innerHTML='';this._prev.innerHTML='';}
imgDiv=dce('div');Css.addClassName(imgDiv,'SAPOGalleryStage');this._image=dce('img');if(this._options.width){this._image.width=opt.width;}
if(this._options.height){this._image.height=opt.height;}
imgDiv.appendChild(this._image);if(opt.showCaption&&!opt.slidebox){this._caption=dce('div');imgDiv.appendChild(this._caption);}
if(!opt.showList){this._list.style.display='none';}
if(opt.pageThumbs){pagePrev=dce('a');Css.addClassName(pagePrev,'disabled');pagePrev.innerHTML=opt.prevPageLabel;pageNext=dce('a');pagePrev.href=pageNext.href='#';pageNext.innerHTML=opt.nextPageLabel;list=this._list;Css.addClassName(pagePrev,'paginationPrevious');Css.addClassName(pagePrev,'disabled');Css.addClassName(pageNext,'paginationNext');list.parentNode.insertBefore(pagePrev,list);list.parentNode.appendChild(pageNext);this._pagePrev=pagePrev;this._pageNext=pageNext;}
first=last=false;list=this._list.childNodes;for(i=0,l=list.length;i<l;i++){if(isTag(list[i],'li')&&!first){first=list[i];}
if(isTag(list[i],'li')){last=list[i];if(opt.pageThumbs&&(opt.slidebox!==true||opt.slidebox===true&&!opt.list)){hideCounter=this._hidePaged(hideCounter,list[i]);}}}
if(hideCounter<=opt.pageThumbs&&pageNext){Css.addClassName(pageNext,'disabled');}
if(this._first&&this._last){this._first.href=this._firstNode=getHref(first);this._last.href=this._lastNode=getHref(last);}
if(opt.hoverControls){next=dce('a');prev=dce('a');next.href=prev.href='#';Css.addClassName(next,'next');Css.addClassName(prev,'previous');next.style.visibility=prev.style.visibility='visible';next.style.position=prev.style.position='absolute';next.style.outline=prev.style.outline='none';prev.style.top=prev.style.left=next.style.top='0';prevHover=dce('span');nextHover=dce('span');if(!opt.hoverThumbs){prevHover.innerHTML=opt.prevLabel;nextHover.innerHTML=opt.nextLabel;}
next.appendChild(nextHover);prev.appendChild(prevHover);imgDiv.appendChild(next);imgDiv.appendChild(prev);this._hnext=next;this._hprev=prev;}
this._galleryStage=imgDiv;if(!opt.slidebox){if(this._pagePrev){container.insertBefore(this._galleryStage,this._pagePrev);}
else{container.insertBefore(this._galleryStage,this._list);}}},_setImageLoader:function(){this._imageLoader=dce('img');this._imageLoader.style.position='absolute';this._imageLoader.style.left='-10000px';this._imageLoader.style.visibility='hidden';},_unsetImageLoader:function(){this._imageLoader.parentNode.removeChild(this._imageLoader);this._imageLoader=false;},_setEvents:function(){var i,l,hprev,hnext,atag=false,navHandler=this._navHandler,navOverHandler=this._navMouseoverHandler,navOutHandler=this._navMouseoutHandler,prev=this._prev,next=this._next,first=this._first,last=this._last,opt=this._options,list=this._list;if(opt.showList&&!opt.slidebox){Event.observe(list,'click',this._thumbsHandler.bindObjEvent(this));}
if(prev&&next){Event.observe(prev,'click',navHandler.bindObjEvent(this,'previous'));Event.observe(next,'click',navHandler.bindObjEvent(this,'next'));}
if(first&&last){Event.observe(first,'click',this._firstHandler.bindObjEvent(this));Event.observe(last,'click',this._lastHandler.bindObjEvent(this));}
if(opt.hoverControls){hprev=this._hprev;hnext=this._hnext;Event.observe(hprev,'click',navHandler.bindObjEvent(this,'previous'));Event.observe(hprev,'mouseover',navOverHandler);Event.observe(hprev,'mouseout',navOutHandler);Event.observe(hnext,'click',navHandler.bindObjEvent(this,'next'));Event.observe(hnext,'mouseover',navOverHandler);Event.observe(hnext,'mouseout',navOutHandler);}
if(opt.pageThumbs){Event.observe(this._pagePrev,'click',this._pagePrevHandler.bindObjEvent(this));Event.observe(this._pageNext,'click',this._pageNextHandler.bindObjEvent(this));}
if(!opt.slidebox){var childLiEls=list.getElementsByTagName('li');var idx=(typeof opt.startAtIndex==='number')?opt.startAtIndex:0;atag=getHref(childLiEls[idx]);}
if(atag){this._loadImage(atag);}},_loadImage:function(el){var opt=this._options,desc='',resetHnext=false,resetHprev=false,next,prev,nextLink,prevLink,href,prevSpan,nextSpan;this._navLock=true;if(opt.showCaption&&opt.slidebox&&!this._caption){this._caption=dce('div');Css.addClassName(this._caption,'caption');this._caption.style.visibility='hidden';this._galleryStage.parentNode.appendChild(this._caption);}
if(this._hnext){prevSpan=getSpan(this._hnext);if(prevSpan.style.visibility==='visible'){resetHprev=true;}
prevSpan.style.visibility='hidden';}
if(this._hprev){nextSpan=getSpan(this._hprev);if(nextSpan.style.visibility==='visible'){resetHnext=true;}
nextSpan.style.visibility='hidden';}
if(this._caption){desc=getCaption(el);}
this._setImageLoader();this._imageLoader.onload=function(){this._image.src=this._imageLoader.src;var width=Dimensions.elementWidth(this._imageLoader);var height=Dimensions.elementHeight(this._imageLoader);if(opt.width){height=parseInt(height*opt.width/width,10);width=opt.width;}
else
if(opt.height){width=parseInt(width*opt.height/height,10);height=opt.height;}
else
if(opt.maxSize){var maxSz=opt.maxSize;if(width>maxSz[0]||height>maxSz[1]){var arImg=width/height;var arMax=maxSz[0]/maxSz[1];var scl;if(arImg>arMax){scl=maxSz[0]/width;}
else{scl=maxSz[1]/height;}
width=parseInt(width*scl,10);height=parseInt(height*scl,10);}}
this._image.setAttribute('width',width);this._image.setAttribute('height',height);this._loaderWidth=width;this._loaderHeight=height;if(opt.hoverControls){var halfWidth=parseInt(width/2,10)+'px';this._hprev.style.width=halfWidth;this._hnext.style.width=halfWidth;var imagePadding=parseInt(this._image.offsetLeft,10)||0;this._hprev.style.left=imagePadding+'px';this._hnext.style.left=parseInt(width/2,10)+imagePadding+'px';this._hprev.style.height=this._hnext.style.height=height+'px';}
if(opt.onChange){opt.onChange(this);}
if(desc!==''){if(!opt.slidebox){var caption=dce("div");caption.innerHTML=desc;this._caption.parentNode.replaceChild(caption,this._caption);this._caption=caption;}else{this._caption.innerHTML=desc;}}
this._navLock=false;nextLink=this._navHandler('return','next');next=getThumb(nextLink);prevLink=this._navHandler('return','previous');prev=getThumb(prevLink);if(next){href=nextLink.href;if(this._next){this._next.href=href;}
if(this._hnext){this._hnext.href=href;this._hnext.style.visibility='visible';if(resetHnext){setTimeout(function(){nextSpan.style.visibility='visible';},this._hoverResetTimeout);}}}
else{if(this._hnext){this._hnext.style.visibility='hidden';}}
if(prev){href=prevLink.href;if(this._prev){this._prev.href=href;}
if(this._hprev){this._hprev.href=href;this._hprev.style.visibility='visible';if(resetHprev){setTimeout(function(){prevSpan.style.visibility='visible';},this._hoverResetTimeout);}}}
else{if(this._hprev){this._hprev.style.visibility='hidden';}}
this._setThumbs('navThumbs',next,prev);this._unsetImageLoader();}.bindObj(this);document.body.appendChild(this._imageLoader);this._current=el;if(opt.slidebox){Fade.hide(this._image,{dur:this._options.fadeOutTime,after:function(){if(this._imageLoader.src===el.href){this._imageLoader.src='';}
this._imageLoader.src=el.href;}.bindObj(this)});}
else{this._imageLoader.src=el.href;}},_setThumbs:function(type,next,prev){var nextLink,prevLink,nextPar,prevPar;if(this._options[type]){if(type==='hoverThumbs'){nextLink=getSpan(this._hnext);prevLink=getSpan(this._hprev);}
else{nextLink=this._next;prevLink=this._prev;}
nextLink.innerHTML='';prevLink.innerHTML='';nextPar=nextLink.parentNode;prevPar=prevLink.parentNode;if(next){nextLink.appendChild(next);}
if(prev){prevLink.appendChild(prev);}}
else{nextPar=this._next;prevPar=this._prev;}
if(next){Css.removeClassName(nextPar,'disabled');}
else{Css.addClassName(nextPar,'disabled');}
if(prev){Css.removeClassName(prevPar,'disabled');}
else{Css.addClassName(prevPar,'disabled');}
if(type!=='hoverThumbs'){this._setThumbs('hoverThumbs',getThumb(nextLink),getThumb(prevLink));}},_thumbsHandler:function(ev){Event.stop(ev);var el=Event.findElement(ev,'a');if(isTag(el,'a')){this._loadImage(el);}},_navHandler:function(ev,direction){if(typeof ev==='object'&&ev!==null){Event.stop(ev);}
if(this._navLock&&typeof ev!=='string'){return false;}
var el=this._current.parentNode,curr=false,atag=false;direction=direction+'Sibling';curr=el[direction];while(curr){atag=getHref(curr);if(atag){break;}
else{curr=curr[direction];}}
if(atag){if(typeof ev==='string'){if(ev==='return'){return atag;}}
this._loadImage(atag);}
return false;},_navMouseoverHandler:function(ev){var span=getSpan(Event.findElement(ev,'a'));if(span){if(span.innerHTML!==''){span.style.visibility='visible';}}},_navMouseoutHandler:function(ev){var span=getSpan(Event.findElement(ev,'a'));if(span){span.style.visibility='hidden';}},_firstHandler:function(ev){Event.stop(ev);this._loadImage(this._firstNode);},_lastHandler:function(ev){Event.stop(ev);this._loadImage(this._lastNode);},_swapVisibleThumbs:function(bound,direction,state,dry){var c=0,d,prevBound=bound,willBreak=false,istag;d=direction+'Sibling';if(direction==='previous'){bound=bound[d];}
while(true){if(c>=this._options.pageThumbs){willBreak=true;if(direction==='previous'&&state=='inline'){return prevBound;}}
if(bound){istag=isTag(bound,'li');if(istag){if(!willBreak&&!dry){bound.style.display=state;}
c++;prevBound=bound;}
if(willBreak&&istag){break;}
bound=bound[d];}
else{return false;}}
return prevBound;},_pagePrevHandler:function(ev){Event.stop(ev);var swap=this._swapVisibleThumbs.bindObj(this);var bound=swap(this._pagePrevBound,'previous','inline');if(bound){swap(this._pagePrevBound,'next','none');this._pageNextBound=this._pagePrevBound;this._pagePrevBound=bound;Css.removeClassName(this._pageNext,'disabled');bound=swap(this._pagePrevBound,'previous','inline',true);if(!bound){Css.addClassName(this._pagePrev,'disabled');}}},_pageNextHandler:function(ev){Event.stop(ev);if(this._pageNextBound){var swap=this._swapVisibleThumbs.bindObj(this);var bound=swap(this._pageNextBound,'next','inline');swap(this._pageNextBound,'previous','none');this._pagePrevBound=this._pageNextBound;this._pageNextBound=bound;Css.removeClassName(this._pagePrev,'disabled');if(!bound){Css.addClassName(this._pageNext,'disabled');}}},getGallery:function(){return this._container;},goToNext:function(){this._navHandler(null,'next');},goToPrevious:function(){this._navHandler(null,'previous');}};}());
