/*------------------------------------------------------------------------------
	$Id: Ticker.js,v 1.3 2010/04/12 02:53:39 mok Exp $
	Class: 	adobe.Ticker
	Author:	mok
------------------------------------------------------------------------------*/
adobe.link("lib/animator.js");
adobe.Ticker = Class.create((function() {

	return {
	
		initialize : function(container,options) {
		
			if(!$(container)) return;
			
			this.ticker = $(container);		
			this.tickerItems = this.ticker.select(".ticker-item");		
			this.ticks=[];		
			this.tickerSize = this.tickerItems.length;
			
			this.options = Object.extend({
				duration: 4000,
				transition: Animator.tx.easeInOut,
				timeout: 100000000000,
				tickerclass : "ticker",
				rotations: 5
			});			
			
			this.revolved = 0;
			this.limit = this.tickerSize*this.options.rotations;
			
			this.tickerItems.each(function(ti,index){ 
				 this.ticks[index] = new adobe.TickerItem(ti) ;
				 ti.select('a[rel=modal]').invoke("observe","click",this.pauseAnimation.bindAsEventListener(this))
			}.bind(this))
			
			this.ticker.className= this.options.tickerclass;
			
			this.visible = 0;
			
			this.animatorOut = new Animator({
				duration: this.options.duration,
				transition: this.options.transition,
				onComplete: (function() {	
					this.animateIn();
				}).bind(this)
			})
			
			this.animatorIn = new Animator({
				duration: this.options.duration,
				transition: this.options.transition,
				onComplete: (function(){									  
					this.animateOut();		
				}).bind(this)
			})
			
			$(this.ticks[this.visible].id).style.display='block';
			this.animateOut();
						
			document.observe("pane:close",function() {
				this.animatorOut.options.duration = this.options.duration;
				this.animatorIn.options.duration = this.options.duration;
				if(this.revolved < this.limit) {
					this.animatorOut.options.duration=this.options.duration;
					this.animatorIn.options.duration =this.options.duration;
				}
			}.bind(this))
		},		
		
		getVisibleItem : function(element) {
			return $(this.ticks[this.visible].id)
		},
		
		pauseAnimation : function() {
			this.animatorOut.options.duration=this.options.timeout;
			this.animatorIn.options.duration =this.options.timeout;
		},
		
		animateOut : function() {
						
			if(this.revolved == this.limit) return;
			
			element = this.getVisibleItem();
			this.animatorOut.addSubject(new NumericalStyleSubject(element,"opacity",1,0))
			this.animatorOut.play();
						
			this.revolved++;
			
		},
		animateIn : function() {
			
			this.getVisibleItem().style.display = "none";
			this.visible = (this.visible == this.tickerSize-1) ? 0 : this.visible+1;
			this.getVisibleItem().style.display = "block";
			
			element = this.getVisibleItem();
			
			this.animatorIn.addSubject(new NumericalStyleSubject(element,"opacity",0,1))
			this.animatorIn.play();
		
		}				
	}

})());
adobe.TickerItem = Class.create({
	initialize: function(el) {
		this.tickerItem = el
		this.tickerItem.wrap("div",{ "style": "-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)';opacity: 0; _filter: alpha(opacity=0); display:none"});
		this.id = this.tickerItem.up().identify();
	}						
});
