dojo.declare("Gallery",null,{
  entry:'div.imageElement',
  titleSelector:'h3',
  subtitleSelector:'p',
  linkSelector:'a.open',
  imageSelector:'img.full',
  gal:this,
  height:480,
  width:640,
  titleHeight:54,
  duration:10000,
  titleClass:'title-gallery',
  textClass:'text-gallery',
  pauseIcon:'/img/pause.gif',
  playIcon:'/img/play.gif',
  changing:false,
  constructor:function(id,args){
    dojo.mixin(this,args);
    this.mainElem=dojo.byId(id);
    this.mainElem.style.height=this.height+'px';
    this.mainElem.style.width=this.width+'px';

    this.gallery=dojo.query('> div', this.mainElem);
    this.hasg=this.gallery?this.gallery.length && this.gallery.length>0 : false;
    if(this.hasg){
      for(var i=0; i<this.gallery.length;i++){
        this.gallery[i].style.display='none';
      }
    }

    this.allDiv=dojo.doc.createElement('div');
    this.allDiv.style.position='relative';
    this.allDiv.style.height=this.height+'px';
    this.allDiv.style.width=this.width+'px';
    this.allDiv.style.backgroundColor='black';
    this.allDiv.style.overflow='hidden';
    this.allDiv.style.backgroundPosition='center center';
    this.allDiv.style.backgroundRepeat='no-repeat';
    dojo.place(this.allDiv, this.mainElem, 'last');

    /* Div do Play/Pause */
    this.playing=true;
    this.ppdiv=dojo.doc.createElement('div');
    this.ppdiv.style.position='absolute';
    this.ppdiv.style.width='20px';
    this.ppdiv.style.height='20px';
    this.ppdiv.style.paddingTop='4px';
    this.ppdiv.style.paddingLeft='4px';
    this.ppdiv.style.right='0px';
    this.ppdiv.style.top='0px';
    this.ppdiv.style.display='none';
    this.ppdiv.style.backgroundColor='black';
    this.ppdiv.style.cursor='pointer';
    dojo.style(this.ppdiv,'opacity','0.8');
    this.ppdiv.style.zIndex=100;
    dojo.place(this.ppdiv, this.allDiv, 'last');
    this.ppdiv.img=dojo.doc.createElement('img');
    this.ppdiv.img.src=this.pauseIcon;
    this.ppdiv.img.alt="Pausar/Continuar";
    dojo.place(this.ppdiv.img, this.ppdiv, 'last');

    this.omover=dojo.connect(this.allDiv,'onmouseover',dojo.hitch(this,this.onMouseOver));
    this.omout=dojo.connect(this.allDiv,'onmouseout',dojo.hitch(this,this.onMouseOut));

    dojo.connect(this.ppdiv,'onclick',dojo.hitch(this,this.playOrPause));
    /* Div do Play/Pause */

    this.dontmove=dojo.doc.createElement('div');
    this.dontmove.style.position='relative';
    this.dontmove.style.height=this.height+'px';
    this.dontmove.style.width=this.width+'px';
    this.dontmove.style.top='0px';
    this.dontmove.style.left='0px';
    this.dontmove.style.overflow='hidden';
    dojo.place(this.dontmove, this.allDiv, 'last');

    this.textBox=dojo.doc.createElement('div');
    this.textBox.style.height=this.titleHeight+'px';
    this.textBox.style.width=this.width+'px';
    this.textBox.style.position='absolute';
    this.textBox.style.left='0px';
    this.textBox.style.top=this.height+'px';
    this.textBox.style.backgroundColor='#111';
    this.textBox.id="textbox_gallery_"+id;
    dojo.style(this.textBox,'opacity','0.8');
    dojo.place(this.textBox,this.dontmove,'last');

    this.cover=dojo.doc.createElement('div');
    this.cover.style.position='absolute';
    this.cover.style.height=this.height+'px';
    this.cover.style.width=this.width+'px';
    this.cover.style.backgroundColor='black';
    this.cover.id='cover_gallery_'+id;
    dojo.place(this.cover, this.allDiv, 'first');

    this.pos=0;
    this.init=true;
    if(this.duration<1500){this.duration=1500;}
    dojo.addOnLoad(dojo.hitch(this,this.next));
  },
  goTo:function(num){
    this.pos=num;
    this.next();
  },
  next:function(){
  	this.onStart(this.pos);
    clearTimeout(this.tm);
    if(!this.init){
      this.changing=true;
      var to=dojo.animateProperty({
        node:this.textBox,
        duration:200,
        properties:{
          top:{start:this.height-this.titleHeight,end:this.height}
        }
      });
      var co=dojo.animateProperty({
        node:this.cover,
        duration:200,
        properties:{
          opacity:{start:0,end:1}
        }
      });
      dojo.connect(co,'onEnd',dojo.hitch(this,this.goOn));
      to.play();co.play();
      this.init=true;
    } else {
      this.goOn();
    }
  },
  goOn:function(){
    var rg=/<[^>]*>/g;
    var tit=dojo.query('> '+this.titleSelector,this.gallery[this.pos]);
    tit=tit&&tit.length&&tit.length>0?tit[0].innerHTML.replace(rg,""):'';

    var txt=dojo.query('> '+this.subtitleSelector,this.gallery[this.pos]);
    txt=txt&&txt.length&&txt.length>0?txt[0].innerHTML.replace(rg,""):'';

    var img=dojo.query('> '+this.imageSelector,this.gallery[this.pos]);
    img=img&&img.length&&img.length>0?img[0]:false;

    this.allDiv.title=img.alt.replace(rg,"");
    if(img){this.allDiv.style.backgroundImage='url('+img.src+')';}else{this.allDiv.style.backgroundImage='';}

    var link=dojo.query('> '+this.linkSelector,this.gallery[this.pos]);
    link=link&&link.length&&link.length>0?link[0]:false;

    if(this.connect){dojo.disconnect(this.connect);}
    if(link.href && link.href.length>0 && link.href!='#'){
      this.connect=dojo.connect(this.dontmove,'onclick',function(){if(link.target=='_blank'){window.open(link.href);}else{window.location.href=link.href;}});
      this.cover.style.cursor='pointer';
    }else{
      this.cover.style.cursor='default';
    }

    this.textBox.innerHTML='<h3 class="'+this.titleClass+'">'+tit+'</h3><p class="'+this.textClass+'">'+txt+'</p>';
    var to=dojo.animateProperty({
      node:this.textBox,
      delay:200,
      duration:200,
      properties:{
        top:{start:this.height,end:this.height-this.titleHeight}
      }
    });
    var co=dojo.animateProperty({
      node:this.cover,
      delay:200,
      duration:200,
      properties:{
        opacity:{start:1,end:0}
      }
    });
    to.play();co.play();

	var p=this.pos;
    this.pos++;
    if(this.pos>=this.gallery.length){this.pos=0;}

	if(this.playing){this.tm=setTimeout(dojo.hitch(this,this.next),this.duration);}
    this.init=false;
    this.changing=false;
    this.onEnd(p);
  },
  onStart:function(){},
  onEnd:function(){},
  playOrPause:function(){
  	if(this.playing){
  	  this.ppdiv.img.src=this.playIcon;
  	  this.pause();
  	}else{
  	  this.ppdiv.img.src=this.pauseIcon;
  	  this.play();
  	}
  	this.playing=!this.playing;
  },
  pause:function(){
	dojo.disconnect(this.omout);
    clearTimeout(this.tm);
  },
  play:function(){
	this.omout=dojo.connect(this.allDiv,'onmouseout',dojo.hitch(this,this.onMouseOut));
    this.tm=setTimeout(dojo.hitch(this,this.next),this.duration);
  },
  onMouseOver:function(){
  	this.ppdiv.style.display='block';
  },
  onMouseOut:function(){
  	this.ppdiv.style.display='none';
  },
  isChanging:function(){return this.changing;}
});
