		
	/**
	 * mayaFish.modules.mediaLeadBox2
	 * 
	 * @author krish
	 */

	if (!mayaFish.modules.mediaLeadBox2)
	{
		mayaFish.modules.mediaLeadBox2 = 
		{
			enableAnimation: true,
			boxes: [],
			boxInit: function(mlbId,initParams)
			{
				//load dojo requirements if animation enabled
				if( mayaFish.modules.mediaLeadBox2.enableAnimation ){ dojo.require("dojo.fx"); }

                var boxWidth  =  mayaFish.$id('mediaLeadBoxContainer'+initParams.boxId).offsetWidth;
                var boxHeight =  mayaFish.$id('mediaLeadBoxContainer'+initParams.boxId).offsetHeight;
                
                var box = this._getBoxById(initParams.boxId,true);
                box.init(initParams);
                
                mayaFish.httpRequest(
                {
                    'url':      '/mediaLeadBox2/getBoxData',
                    'params':   {'mlbId': mlbId, 'uniqueId': initParams.boxId,'boxWidth': boxWidth, 'boxHeight': boxHeight },
                    'onFail':    function()
                    {
                        
                    },
                    'onSuccess': function(response) 
                    {
                           mayaFish.modules.mediaLeadBox2.boxLoaded(initParams.boxId,response);
                    }
                });
            },
            boxLoaded: function( boxId,html )
            {
                mayaFish.$id('mediaLeadBoxContainer'+boxId).innerHTML = html;
                this._getBoxById(boxId).onLoaded();
                mayaFish.modules.mediaLeadBox2.boxShowNext(boxId);
            },
            
			boxShowContent: function(boxId,contentIdx,stopAutoplay)
			{				
				this._getBoxById(boxId).showSlide(contentIdx,stopAutoplay);
			},
			boxShowNext: function(boxId)
			{
				var box = this._getBoxById(boxId);
				
				//ha csak egy content van ne csináljon semmit
				if( box.itemsCount == 1 && box.currentItemIdx == 0 ){ return; }
				
				var nextIdx = 1 + box.currentItemIdx;
                var lastIdx = -1 +  box.itemsCount;
				if( nextIdx < 0 ){ nextIdx = 0; }
				if( nextIdx > lastIdx ){ nextIdx = 0; }

				box.showSlide(nextIdx,false);
			},
			_getBoxById: function(boxId, forceCreate)
			{
				if( arguments.length<2 ){ forceCreate = false; }
				var box = null;
				for( var i=0; i<this.boxes.length; i++ )
				{
					if( this.boxes[i].id == boxId )
					{
						box = this.boxes[i];
						break;
					}
				}
				if( box == null && forceCreate == true)
				{
					this.boxes.push( new mediaLeadBox2Box(boxId) );
                    box = this.boxes[ this.boxes.length-1 ];
				}
				return box;
			},
			//developer
			_freezeBoxes: function()
			{
				for( var i=0; i<this.boxes.length; i++ )
				{
					this.boxes[i].options.autoplay = false;
					this.boxes[i].setAutoplay();
				}
			}
			
		};
		
	}

function mediaLeadBox2Box(boxId)
{ 
    this.id = boxId;
}
/**
 * ide töltődnek be plugin objektumok, egy box páldánynak csak egy pluginja lehet
 * @stastic
 */
mediaLeadBox2Box.plugins = [];   

mediaLeadBox2Box.prototype.id             = null;
mediaLeadBox2Box.prototype.timer          = null;
mediaLeadBox2Box.prototype.itemsCount     = 0;
mediaLeadBox2Box.prototype.currentItemIdx = -1;
mediaLeadBox2Box.prototype.options        = {};
mediaLeadBox2Box.prototype.plugin         = null;
mediaLeadBox2Box.prototype.init = function(initParams)
{

    this.options.autoplay            = true;
    this.options.delay               = parseInt(initParams.delay)>0 ? parseInt(initParams.delay) : 5000;
	this.options.autoplayRestartTime = 15000;
    this.itemsCount                  = parseInt(initParams.itemsCount);

    if( initParams.jsPlugin )
    {
        this.plugin = mediaLeadBox2Box.plugins[initParams.jsPlugin];
        this.plugin.init(this,initParams);
    }
}
mediaLeadBox2Box.prototype.onLoaded = function()
{
    if( this.plugin && typeof(this.plugin.onLoaded)=="function" )
    {
        this.plugin.onLoaded();
    }
}
mediaLeadBox2Box.prototype.showSlide = function(itemIdx,stopAutoplay)
{
    if( arguments.length<2 ){ stopAutoplay = false; }

    if( this.timer ){ clearTimeout(this.timer); }

    if( this.currentItemIdx == itemIdx )
    {
        this.setAutoplay(true);
        return;
    }

    if( this.currentItemIdx > -1 && mayaFish.modules.mediaLeadBox2.enableAnimation )
    {
        mayaFish.$id('mlbSlide'+this.id+'_'+this.currentItemIdx).style.zIndex = 1;
        mayaFish.$id('mlbSlide'+this.id+'_'+itemIdx).style.zIndex = 0;
        mayaFish.$id('mlbSlide'+this.id+'_'+itemIdx).style.display = 'block';

        var a = dojo.fadeOut({ 
                                node: mayaFish.$id('mlbSlide'+this.id+'_'+this.currentItemIdx), 
                                duration: 400,
                                onEnd: function(n){ n.style.display = "none"; } 
                            });
        var b = dojo.fadeIn( { node: mayaFish.$id('mlbSlide'+this.id+'_'+itemIdx), duration:400 } );
        var c = dojo.fx.combine([a,b]).play();

        mayaFish.$id('mlbButton'+this.id+'_'+this.currentItemIdx).className = 'mlbButton';	
        mayaFish.$id('mlbButton'+this.id+'_'+itemIdx).className = 'mlbButton active';
    }
    else
    {
        //hides current item, sets button state to inactive
        if( this.currentItemIdx > -1 )
        {
            mayaFish.$id('mlbSlide'+this.id+'_'+this.currentItemIdx).style.display = 'none';
            mayaFish.$id('mlbButton'+this.id+'_'+this.currentItemIdx).className = 'mlbButton';		
        }
        //displays new item, sets active button
        mayaFish.$id('mlbSlide'+this.id+'_'+itemIdx).style.display = 'block';
        mayaFish.$id('mlbButton'+this.id+'_'+itemIdx).className = 'mlbButton active';
    }

    this.setAutoplay(stopAutoplay);

    this.currentItemIdx = itemIdx;
}

mediaLeadBox2Box.prototype.setAutoplay = function(stopAutoplay)
{
    var Me = this;

    if( Me.timer ){ clearTimeout(Me.timer); }

    if( Me.options.autoplay == false ){ return; }

    var time = stopAutoplay ? Me.options.autoplayRestartTime : Me.options.delay;

    Me.timer = setTimeout( "mayaFish.modules.mediaLeadBox2.boxShowNext('"+Me.id+"')", time);
};
                    


                    
