/**
 * billboardManager
 *
 * @package    mivilagunk
 * @subpackage Manager
 * @author     Szijártó Tamás ( szicsu ) <szicsu@jquery.hu>
 * @version    SVN: $Id: $
 */
billboardManager = new function(){
	var self = this;
	
	this.data = {};
	this.containerObj;
	this.timer;
	this.timerTime;
	
	this.init = function( containerObj ){
		
		this.containerObj = containerObj;
		
		this.containerObj
			.find('.jsBillboardPagerLink').bind('click', function(){
				
				data = self.getItem( $(this).attr('rel') );
				
				$('.jsBillboardImg', self.containerObj).attr('src', data.img );
				$('.jsBillboardLink', self.containerObj)
					.attr('href', data.link )
					.attr('title', data.title )
					.text( data.title );
				$('.jsBillboardLead', self.containerObj).html( data.lead );

                                self.userList(data);

				$('.jsBillboardPagerLink.active').removeClass('active');
				$(this).addClass('active').trigger('blur');
				
				self.resetTimer();
				
				return false;
			});
	}

    this.userList = function(slideData) {

        if ( self.hasFans(slideData) ) {
            var sHTML = '';
            for ( var index in slideData.fans ) {
                var fan = slideData.fans[index];
                sHTML += '<li>'
                       + '<a href="'+fan.url+'" title="'+fan.name+'">'
					   + '<img src="'+fan.img+'" alt="'+fan.name+'" />'
					   + '</a>'
					   + '</li>';
            }
            $('#jsBillboardUserUl', self.containerObj).html(sHTML);
            $('#jsBillboardHeadline', self.containerObj).removeClass('wider');
            $('#jsBillboardUserList', self.containerObj).show();
            $('#jsBillboardHeadline', self.containerObj).html(data.theyLikesTxt);
        } else {
            $('#jsBillboardHeadline', self.containerObj).addClass('wider');
            $('#jsBillboardUserList', self.containerObj).hide();
            $('#jsBillboardHeadline', self.containerObj).html(data.nobodyLikesTxt);
        }

    }

    this.hasFans = function(slideData) {
        for ( var fan in slideData.fans ) {
            return true;
        }
        return false;
    }

	
	this.initTimer = function( time ){
		
		self.timerTime = time;
		
		self.timer = setInterval( function(){
		
			if( ( aItem = $('.jsBillboardPagerLink.active', self.containreObj ).parent().nextAll('li').find('.jsBillboardPagerLink')[0] ) ){
				aObj = $(aItem);
			}
			else{
				aObj = $('.jsBillboardPagerLink', self.containreObj ).eq(0);
			}
			
			aObj.trigger('click');
			
		}, self.timerTime);
		
	}
	
	this.resetTimer = function(){
		
		if( self.timer ){
			window.clearInterval( self.timer );
			self.initTimer( self.timerTime );
		}
	}
	
	/**
	 *
	 */
	this.addData = function( data ){
		this.data = data;
		this.preloadingImages();
	}
	
	/**
	 *
	 */
	this.preloadingImages = function(){
	
		for( var i in this.data ){
			Site.Helper.cacheImage( this.data[i].img );
		}
	}
	
	this.getItem = function( id ){
		
		return this.data[ id ] || false;
	}
}	
