
var hhtDynamicHeader = new Class ({

  init:function(header){
  
  	this.header_container = header;
    this.currentHeader = 0;
    this.blendfx = null;
    this.parseOptions($('tx_hhtfadeheader_pi1_container').getProperty('hht:info'));
    this.images = eval($('tx_hhtfadeheader_pi1_pic').getProperty('hht:images'));
    
    //this.addImagesEl($('tx_hhtfadeheader_pi1_pic'));
          
    if(this.headerCount > 1) {
    	this.timer = this.nextHeader.bind(this).periodical(this.interval);
    }
    
    var nextButton = $('tx_hhtfadeheader_pi1_control_next');
    var prevButton = $('tx_hhtfadeheader_pi1_control_prev');
    
    if(nextButton)
    {
    	nextButton.addEvent('click',this.nextHeaderClick.bindWithEvent(this));
    }
    
    if(prevButton)
    {
    	prevButton.addEvent('click',this.lastHeaderClick.bindWithEvent(this));
    }
  },
	
	
	addImagesEl:function(el){
	
		for (var i=1; i<this.headerCount; i++) {

			var e = $("tx_hhtfadeheader_pi1_pic_"+i);
			
			/*var elImg =*/ new Asset.image(this.images[i].url, {'class': 'tx_hhtfadeheader_pi1_img', 'alt': this.images[i].title, 'title': this.images[i].title}).injectInside(e);
			//elImg.injectInside(e);
		}
	},
	

	parseOptions:function(value){
		var temp = eval(value)[0];
		
		this.headerCount = temp.numberOfHeader;
		this.interval = temp.interval;
		this.duration = temp.duration;	
	},
  
  
  nextHeader:function(){
  
		var i = this.currentHeader;
	
		
	
    if ((this.currentHeader + 1) == this.headerCount) {
    	this.currentHeader = 0;
    	
    } else {
    	this.currentHeader++;
    }
        
    var j = this.currentHeader;

    this.headerBlend(i,j);
  },
  
  
  lastHeader:function(){
  
 		var i = this.currentHeader;
  	
    if ((this.currentHeader) == 0) {
    	this.currentHeader = this.headerCount - 1;
    	
   	} else {
    	this.currentHeader--;
    }
    
    var j = this.currentHeader;

    this.headerBlend(i,j);
  },
  
  
  nextHeaderGroup:function(){
  
  	var i = this.currentHeader;

		var guid_current = $("tx_hhtfadeheader_pi1_pic_"+i).getProperty("hht:guid");	                
		var guid;
		
		for (k=this.currentHeader; k<this.headerCount; k++) {
		
			guid = $("tx_hhtfadeheader_pi1_pic_"+k).getProperty("hht:guid");
			
			if (guid_current != guid) {
				this.currentHeader = k;
				break;
			}
		}
			
		if (this.currentHeader == i) {
    	this.currentHeader = 0;
    }
    
    this.headerBlend(i,this.currentHeader);
  },
  
  
  lastHeaderGroup:function(){
  
  	var i = this.currentHeader;
  	
		var guid_current = $("tx_hhtfadeheader_pi1_pic_"+i).getProperty("hht:guid");
		var guid;
		
			// find previous group
		for (k=this.currentHeader; k>=0; k--) {
		
			guid = $("tx_hhtfadeheader_pi1_pic_"+k).getProperty("hht:guid");
			
			if (guid_current != guid) {
				this.currentHeader = k;
				break;
			}
		}
		
		if (this.currentHeader == i) {
    	guid = $("tx_hhtfadeheader_pi1_pic_"+(this.headerCount - 1)).getProperty("hht:guid");;
    }

			// find first occurrence of the group
		for (k=0; k<this.headerCount; k++) {
		
			if ($("tx_hhtfadeheader_pi1_pic_"+k).getProperty("hht:guid") == guid) {
				this.currentHeader = k;
				break;
			}
		}
		
    this.headerBlend(i,this.currentHeader);
  },
  
  
  pauseHeaderClick:function(){
  
  	if (this.timer) {
  		$clear(this.timer);
			this.timer = null;
			
  	} else {
  		this.nextHeader();
			this.timer = this.nextHeader.bind(this).periodical(this.interval);
  	}
  },
  
  
  nextHeaderClick:function(){
    
    this.headerStop();
		$clear(this.timer);
		this.timer = null;
		// headerNext();
		this.nextHeaderGroup();
		this.timer = this.nextHeader.bind(this).periodical(this.interval);
  },
  
  
  lastHeaderClick:function(){
  
  	this.headerStop();			
		$clear(this.timer);
		this.timer = null;
		// headerPrev();
		this.lastHeaderGroup();
		this.timer = this.nextHeader.bind(this).periodical(this.interval);
	},
  
  headerBlend:function(curHeader,newHeader){
  
  	if($("tx_hhtfadeheader_pi1_pic_"+newHeader).getElement('img') == null){
    	new Asset.image(this.images[newHeader].url, {'class': 'tx_hhtfadeheader_pi1_img', 'alt': this.images[newHeader].title, 'title': this.images[newHeader].title}).injectInside($("tx_hhtfadeheader_pi1_pic_"+newHeader));
    };
  
  	this.headerStop();

		var e1 = $("tx_hhtfadeheader_pi1_pic_"+curHeader);
		var e2 = $("tx_hhtfadeheader_pi1_pic_"+newHeader);
    var to1 = 0;
    var to2 = 1;

    this.blendfx = new Fx.Styles(e1, {duration: this.duration, wait: true, fps:16 ,transition: Fx.Transitions.linear});
    
		if (curHeader == 0) {
			e1.setStyle("z-index",200);
		}
		
		$$(".tx_hhtfadeheader_pi1_pic").setStyle("opacity",0);
		e2.setStyle("z-index",(e1.getStyle("z-index")-1));
		e1.setStyle("opacity",to2);
		e2.setStyle("opacity",to2);
        
		this.blendfx.start({"opacity":to1});
  },
  
  headerStop:function(){
  
  	if(this.blendfx){
  		this.blendfx.stop();
  	}
  }
});

window.addEvent('load', function()    {

   var header = $('tx_hhtfadeheader_pi1_container')
   header.hht_dynamic_header = new hhtDynamicHeader();
   header.hht_dynamic_header.init.bind( header.hht_dynamic_header, header ).delay(1000);   
});
