
	/**
	 * mayaFish.modules.picturePlayer
	 * 
	 * @author krish
	 */

	if (!mayaFish.modules.picturePlayer)
	{
		mayaFish.modules.picturePlayer = 
		{
            initPlayer: function(initParams)
            {
                dojo.require("dojo.fx");

                var boxWidth  =  mayaFish.$id('pp'+initParams.uniqueId).offsetWidth;
                
                var box = this._getBoxById(initParams.uniqueId,true);
                box.init(initParams);
                
                var sendParams = {'playerId': initParams.playerId, 'uniqueId':initParams. uniqueId,'boxWidth': boxWidth }
                
                if( initParams.embedOptions ){ sendParams.embedOptions = initParams.embedOptions }
                
                mayaFish.httpRequest(
                {
                    'url':      mayaFish.consts.SITE_URL+'/picturePlayer/getPlayer',
                    'params':   sendParams,
                    'onFail':    function()
                    {
                        
                    },
                    'onSuccess': function(response) 
                    {
                           mayaFish.modules.picturePlayer.renderPlayer(initParams.uniqueId,response);
                    }
                });  
            },
            
            renderPlayer: function(boxId, html)
            {
                mayaFish.$id('pp'+boxId).innerHTML = html;
                mayaFish.modules.picturePlayer._getBoxById(boxId).thumbnailsInit();
                mayaFish.modules.picturePlayer.playerStep(boxId,1);
                
            },
            
            imageClick: function( boxId, imageIndex )
            {
                return mayaFish.modules.picturePlayer._getBoxById(boxId).imageClick(imageIndex);
            },
        
            magnifyImage: function( boxId )
            {
                return mayaFish.modules.picturePlayer._getBoxById(boxId).magnifyImage();
            },    
        
			enableAnimation: true,
			boxes: [],

            playerPause: function(boxId,pause)
            {
                if( arguments.length<2 ){pause = true;}
                this._getBoxById(boxId).setAutoplay(pause);
            },
			playerShowContent: function(boxId,contentIdx,pauseAutoplay)
			{				
                var direction = this._getBoxById(boxId).currentItemIdx < contentIdx ? 'next' : 'prev'; 
				this._getBoxById(boxId).showSlide(contentIdx,pauseAutoplay,direction);
			},
			playerStep: function(boxId,step,pauseAutoplay)
			{
				var box = this._getBoxById(boxId);
                
                if( arguments.length<3 ){pauseAutoplay = false;}
				
				//ha csak egy content van ne csináljon semmit
				if( box.params.itemCount == 1 && box.currentItemIdx == 0 ){return;}
				
				var nextIdx = step + box.currentItemIdx;
                var lastIdx = -1 +  box.params.itemCount;
				if( nextIdx < 0 ){nextIdx = box.params.itemCount-1;}
				if( nextIdx > lastIdx ){nextIdx = 0;}
                
                var direction = step > 0 ? 'next' : 'prev';

				box.showSlide(nextIdx,pauseAutoplay,direction);
			},
			_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 picturePlayer(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].params.autoplay = false;
					this.boxes[i].setAutoplay();
				}
                return 'picturePlayer freeze boxes: done!';
			},
			_getPlayers: function()
			{
				for( var i=0; i<this.boxes.length; i++ )
				{
					console.log(this.boxes[i]);
				}
                return 'picturePlayer getPlayers: done!';
			}
		};
	};




function picturePlayer(boxId)
{ 
    this.id = boxId;
}

picturePlayer.prototype.id             = null;
picturePlayer.prototype.timer          = null;
picturePlayer.prototype.currentItemIdx = -1;
picturePlayer.prototype.params        = {};
picturePlayer.prototype.lightbox;
picturePlayer.prototype.init = function(initParams)
{
    this.params = initParams;
    this.lightbox = mayaFish.ui.lightbox(initParams.lightboxData);
}
picturePlayer.prototype.imageClick = function(imageIndex)
{
    this.toolsSelectedImageIndex = imageIndex;

    if( this.params.enableImageSharing==1 )
    {
        this.toolsDisplay( !this.isToolsOpened() );
    }
    else if( this.params.enableImageSharing==0 && this.params.enableMagnifier==1 )
    {
        this.magnifyImage();
    }
    
    return false;
},
picturePlayer.prototype.magnifyImage = function()
{
    if( this.params.enableImageSharing==1 )
    {
        this.toolsDisplay( false );
    }
    this.lightbox.show(this.toolsSelectedImageIndex);
    return false;
},
picturePlayer.prototype.showSlide = function(itemIdx,pauseAutoplay, direction)
{
    if( arguments.length<2 ){pauseAutoplay = false;}
    if( arguments.length<3 ){direction = 'next';} 

    if( this.timer ){clearTimeout(this.timer);}

    if( this.currentItemIdx == itemIdx )
    {
        this.setAutoplay(true);
        return;
    }

    if( this.currentItemIdx > -1 )
    {
        var flags = {};
        
        if( itemIdx == 0 ){ flags.isFirst = true; }
        if( itemIdx == this.params.itemCount-1 ){ flags.isLast = true; }
        
        this.switchImage(   this.params.animationEffect,
                            mayaFish.$id('ppImage'+this.id+'_'+this.currentItemIdx),
                            mayaFish.$id('ppImage'+this.id+'_'+itemIdx),
                            direction,
                            flags
                         );
    }
    else
    {
            mayaFish.$id('ppImage'+this.id+'_'+itemIdx).style.zIndex = 1;
            mayaFish.$id('ppImage'+this.id+'_'+itemIdx).style.display = 'block';
    }

    this.setAutoplay(pauseAutoplay);

    this.updateSelector(itemIdx,this.currentItemIdx);

    this.currentItemIdx = itemIdx;
    
    
};
picturePlayer.prototype.switchImage = function(anim,eleOut,eleIn,direction,flags)
{
    
    switch( anim )
    {
        case 'slide':
            var ppImagesWidth = mayaFish.$id('ppImageContainer'+this.id).offsetWidth;
            var ppImagesHeight = mayaFish.$id('ppImageContainer'+this.id).offsetHeight;
            
            var inTop  = flags.isFirst || ( direction=='prev' && !flags.isLast) ? 0 : -ppImagesHeight;
            var outTop = flags.isFirst  || ( direction=='prev' && !flags.isLast) ? -ppImagesHeight : 0;

            var inLeftStart  = direction=='next' ? ppImagesWidth : -ppImagesWidth;
            var inLeftEnd    = 0
            var outLeftStart = 0;
            var outLeftEnd   = direction=='next' ? -ppImagesWidth : ppImagesWidth;
            

            eleOut.style.position = 'relative';
            eleOut.style.zIndex = 1;
            eleOut.style.display = 'block';
            eleOut.style.left = ''+outLeftStart+'px';
            eleOut.style.top =  ''+outTop+'px';  
            
            eleIn.style.position = 'relative';
            eleIn.style.zIndex = 0;
            eleIn.style.display = 'block';
            eleIn.style.left = ''+inLeftStart+'px';
            eleIn.style.top = ''+inTop+'px';

            var a = dojo.fx.slideTo({
                                        node: eleOut,
                                        top: outTop,
                                        left: outLeftEnd,
                                        unit: "px",
                                        duration: 300
                                    });
            var b = dojo.fx.slideTo({
                                        node: eleIn,
                                        top: inTop,
                                        left: inLeftEnd,
                                        unit: "px",
                                        duration: 300,
                                        onEnd: function(n)
                                        {
                                            eleOut.style.display = "none";
                                            eleIn.style.left = '0px';
                                            eleIn.style.top = '0px';
                                        }
                                    });
            var c = dojo.fx.combine([a,b]).play(); 
        break;
        
        case 'wipe':
            eleOut.style.zIndex = 1;
            eleIn.style.zIndex = 0;
            var a = dojo.fx.wipeOut({ 
                                    node: eleOut, 
                                    duration: 400
                                });
            var b = dojo.fx.wipeIn( {node: eleIn, duration:400} );
            var c = dojo.fx.combine([a,b]).play(); 
        break;
        
        case 'fade':
            eleOut.style.zIndex = 1;
            eleIn.zIndex = 0;
            eleIn.style.display = 'block';
            var a = dojo.fadeOut({ 
                                    node: eleOut, 
                                    duration: 400,
                                    onEnd: function(n){n.style.display = "none";} 
                                });
            var b = dojo.fadeIn( {node: eleIn, duration:400} );
            var c = dojo.fx.combine([a,b]).play();     
        break;
        
        default: //no anim
            eleIn.style.display  = 'block';
            eleOut.style.display = 'none';
        break;
    }   
};
picturePlayer.prototype.toolsSelectedImageIndex = null;
picturePlayer.prototype.toolsDisplay = function(visible)
{
    mayaFish.$id('ppToolsDropdown'+this.id).style.display = (visible) ? 'block' : 'none';
};
picturePlayer.prototype.isToolsOpened = function()
{
    return ( mayaFish.$id('ppToolsDropdown'+this.id).style.display == 'block' );
};

picturePlayer.prototype.setAutoplay = function(pauseAutoplay)
{
    var Me = this;

    if( Me.timer ){clearTimeout(Me.timer);}

    if( Me.params.autoplay == false ){return;}

    var time = pauseAutoplay ? Me.params.autoplayRestartTime : Me.params.delay;

    Me.timer = setTimeout( "mayaFish.modules.picturePlayer.playerStep('"+Me.id+"',1)", time);
};

picturePlayer.prototype.updateSelector = function(selectIdx, deselectIdx)
{
    if( this.params.selectorStyle == 'floatingbar' || this.params.selectorStyle == 'thumbnailsbar' )
    {
        if( deselectIdx>-1 )
        {
            mayaFish.removeClass( mayaFish.$id('ppSelectorButton'+this.id+'_'+deselectIdx), 'selected' );
        }
        mayaFish.addClass( mayaFish.$id('ppSelectorButton'+this.id+'_'+selectIdx), 'selected' );
        this.thumbnailSelect(selectIdx);
    }
}

picturePlayer.prototype._thumbnails = false
picturePlayer.prototype._thumbnailSlider;
picturePlayer.prototype.thumbnailsInit = function()
{
    this._thumbnails = ( this.params.selectorStyle == 'thumbnailsbar' );
    
    if( this._thumbnails )
    {
        this._thumbnailSlider = new picturePlayerCarouselWidget(
                mayaFish.$id('ppThumbnailsImagesContainer'+this.id),
                mayaFish.$id('ppThumbnailsImages'+this.id),
                mayaFish.$id('ppThumbnailsButtonLeft'+this.id),
                mayaFish.$id('ppThumbnailsButtonRight'+this.id)   
            );
    }
};
picturePlayer.prototype.thumbnailSelect = function(itemIdx)
{
    if( this._thumbnails )
    {
        this._thumbnailSlider.makeItemVisible( 
                        mayaFish.$id('ppSelectorButton'+this.id+'_'+itemIdx)
                        );
    }
}



