Vudu.widget.module.Ticker = function(holder, config) {
    
    // base url
    this._rootUrl = '/mediaWebApp';
    //movie
    this._showMovies = 4;
    // datasource
    this._directorUrl = 'proxy';
    this._imgSoruce = '_IMG';
    this._fetchSize = 100;
    // timer
    this._timerMin = 0.5;
    this._timerMax = 5;
    this._animationSpeed = 1;
    
    if (config) {
    
        this._rootUrl = config.rootUrl ? config.rootUrl : this._rootUrl;
        this._showMovies = config.showMovies ? config.showMovies : this._showMovies;
        this._directorUrl = config.directorUrl ? config.directorUrl : this._directorUrl;
        this._imgSoruce = config.imgSoruce ? config.imgSoruce : this._imgSoruce;
        this._showTitle = config.showTitle ? config.showTitle : this._showTitle;
        this._fetchSize = config.fetchSize ? config.fetchSize : this._fetchSize;
        this._timerMin = config.timerMin ? config.timerMin : this._timerMin;
        this._timerMax = config.timerMax ? config.timerMax : this._timerMax;
        this._animationSpeed = config.animationSpeed ? config.animationSpeed : this._animationSpeed;
    }
	
    this._posterWidth = 97;
    this._posterSpacing = 8;
    this._posterBorder = 1;
	
	this._holder = YAHOO.util.Dom.get(holder);
    YAHOO.util.Dom.setStyle(this._holder, 'overflow', 'hidden');
    this.movies = new Array( this._showMovies + 1 );
    this.moviesTimer = 0;
	this._currentLastIndex = 0;
	
    var thisRef = this;
    this._ds = new Vudu.widget.module.Ticker.DataSource(
	   this._directorUrl, function() { thisRef.init();}, 
	   this._fetchSize);
    this._ds.loadData();
    
    
    this.init = function() {
        
        for (var i = 0; i < this.movies.length; i++) {
        
            this.movies[i] = new Vudu.widget.module.Ticker.Movie(this._rootUrl, this._animationSpeed, this._posterWidth, this._posterSpacing);
            this._holder.appendChild(this.movies[i].render());
            var left = ( this._posterWidth + this._posterSpacing + ( this._posterBorder * 2 ) ) * i;
            this.movies[i].position(this._ds.pop(), ''+left +'' );
        }
        
		this._currentLastIndex = this.movies.length - 1;
		
        this.moviesTimer = setTimeout(function() {
            thisRef.showMovie();
        }, this.getRandomTime());
    }
    
    this.showMovie = function() {
    
        var temp = this.movies[ this._currentLastIndex ];
        temp.position(this._ds.pop());
        
		this._currentLastIndex -= 1;
        if(this._currentLastIndex < 0 ) {
            this._currentLastIndex = this.movies.length - 1;
        }
		
        for (var i = 0; i < this.movies.length; i++) {
            this.movies[i].animate();
        }
		
		var thisRef = this;
		this.moviesTimer = setTimeout(function() {
            thisRef.showMovie();
        }, this.getRandomTime());
    }
    
    
    this.getRandomTime = function() {
    
        var randomTime = this._animationSpeed * 1000 + 
		                 this._timerMin * 1000 + 
						 Math.floor( Math.random() * (this._timerMax - this._timerMin) * 1000);
        
        return randomTime;
    }
};

Vudu.widget.module.Ticker.DataSource = function(director, onInit, dataFetchSize) {

    this._dataFetchSize = dataFetchSize;
    this._directorUrl = director;
    this._holder = new Array();
    this._loadDataBeforeCount = 5;
    this._loadDataAfterTime = 500;
    this._initiated = false;
    this._onInit = onInit;
    
    var thisRef = this;
    this.oCallback = {
        success: function(oResponse) {
        
            var results = Vudu.util.parseDirectorJSONResponse(oResponse.responseText);
            
            thisRef.append(results.watchedContent);
            if (!thisRef._initiated) {
                thisRef._initiated = true;
                thisRef._onInit();
            }
        },
        failure: function(oResponse) {
        }
    };
    
    this.loadData = function() {
    
        var queryString = "_type=watchedContentSearch&format=application%2Fjson";
        queryString += "&count=" + this._dataFetchSize;
        var url = this._directorUrl + '?' + queryString;
        YAHOO.util.Connect.asyncRequest('GET', url, this.oCallback);
    }
    
    this.append = function(newList) {
    
        var temp = newList.reverse();
        temp.concat(this._holder);
        this._holder = temp;
        
        /* if there is not enough data - use timeout to check for later */
        if (this._holder.length <= this._loadDataBeforeCount) {
        
            /* stop the .pop call for loadData */
            this._loadDataBeforeCount = -1;
            /* after N seconds - return the callbefore lock and check for new data */
            var thisRef = this;
            setTimeout(function() {
                thisRef._loadDataBeforeCount = 5;
                thisRef.caller.loadData();
            }, this._loadDataAfterTime);
        }
    };
    
    
    this.pop = function() {
    
        if (this._holder.length <= this._loadDataBeforeCount) {
            this.loadData();
        }
        return this._holder.pop();
    }
}

Vudu.widget.module.Ticker.Movie = function(rootUrl, animationSpeed, posterWidth, posterSpacing) {

    this._animationSpeed = animationSpeed;
    this._rootUrl = rootUrl;
    this._homePageUrl = null;
    
    this.container = null;
    this._posterIMG = null;
    this._textDiv = null;
    this._slideAnimation = null;
    
    this._posterSpacing = posterSpacing;
	this._borderWidth = 1;
	
    this._posterHeight = 138;
    this._textHeight = 30;
    this._posterWidth = posterWidth;
	this._textWidth = posterWidth;
	
    this.width = this._posterWidth + this._borderWidth; 
    this.height = this._posterHeight + this._textHeight;
    
    this.render = function() {
    
        this.container = document.createElement('div');
        YAHOO.util.Dom.setStyle(this.container, 'position', 'absolute');
        YAHOO.util.Dom.setStyle(this.container, 'cursor', 'pointer');
		YAHOO.util.Dom.setStyle(this.container, 'top', '0px');
        YAHOO.util.Dom.setStyle(this.container, 'background-color', '#000000');
        YAHOO.util.Dom.setStyle(this.container, 'width', this.width + 'px');
        YAHOO.util.Dom.setStyle(this.container, 'height', this.height + 'px');
        
        var thisRef = this;
        YAHOO.util.Event.addListener(this.container, "click", function() {
            if (thisRef._homePageUrl) document.location.href = thisRef._homePageUrl;
        });
        
        
        this._textDiv = document.createElement('div');
        YAHOO.util.Dom.setStyle(this._textDiv, 'position', 'absolute');
        YAHOO.util.Dom.setStyle(this._textDiv, 'overflow', 'hidden');
        YAHOO.util.Dom.setStyle(this._textDiv, 'top', ( this._posterHeight +  this._borderWidth ) + 'px');
        YAHOO.util.Dom.setStyle(this._textDiv, 'left', '1px');
		YAHOO.util.Dom.setStyle(this._textDiv, 'padding', '3px');
        YAHOO.util.Dom.setStyle(this._textDiv, 'width', this._textWidth + 'px');
        YAHOO.util.Dom.setStyle(this._textDiv, 'height', this._textHeight + 'px');
        this.container.appendChild(this._textDiv);
        
        
        this._posterIMG = document.createElement('img');
        this._posterIMG.src = this._imgSoruce + "/spacer.png";
        YAHOO.util.Dom.setStyle(this._posterIMG, 'position', 'absolute');
		YAHOO.util.Dom.setStyle(this._posterIMG, 'top', '1px');
		YAHOO.util.Dom.setStyle(this._posterIMG, 'left', '1px');
        YAHOO.util.Dom.setStyle(this._posterIMG, 'width', this._posterWidth + 'px');
        YAHOO.util.Dom.setStyle(this._posterIMG, 'height', this._posterHeight + 'px');
        this.container.appendChild(this._posterIMG);
        
        this._slideAnimation = new YAHOO.util.Anim(this.container);
        this._slideAnimation.duration = this._animationSpeed;
        this._slideAnimation.attributes = {
            left: {
                by: this._posterSpacing + this.width
            }
        };
        
        return this.container;
    };
    
    this.position = function(movie, leftPosition) {
    
        if (movie != null) {
        
            this._homePageUrl = Vudu.util.getMovieHomePage(this._rootUrl, movie.title[0], (movie.releaseTime) ? movie.releaseTime[0] : null);
            
            this._posterIMG.src = movie.posterUrl[0] + '-s';
            this._textDiv.innerHTML = 
			     '<span style="font-size:9px">' +
                    (movie.city ? (movie.city[0]+'') : '') +
                    (movie.state ? (', ' + movie.state[0]) : '') +
                '</span>';
        }
        if (!leftPosition && leftPosition != '0') {
            YAHOO.util.Dom.setStyle(this.container, 'left', '-' + (this.width + this._posterSpacing) + 'px');
        }
        else {
            YAHOO.util.Dom.setStyle(this.container, 'left', leftPosition + 'px');
        }
    };
    
    this.animate = function() {
        this._slideAnimation.animate();
    };
}