var Thumb = function(photo,link) {
	this.photo = photo;
	this.link = link;
}

var SlideShowManager = {

	timeOut: 700,

	player: null,
        currentMusicIndex: 0,
        totalSongsInPlaylist: 0,
        playing: false,
	container: null,

	createPlayer: function(func) {
		
		SlideShowManager.player = flowplayer("player", "/plugins/flowplayer/flowplayer-3.1.3.swf", { 
		    plugins: { 
		        audio: { 
		            url: '/plugins/flowplayer/flowplayer.audio-3.1.2.swf' 
		        }, 
		        rtmp: { 
		        	url: '/plugins/flowplayer/flowplayer.rtmp-3.1.3.swf'	              
        		}
		    },
                    
                    onLoad: function(){
                        func();
                    },
                    
                    onBeforeFinish:function(clip) {
                       if (SlideShowManager.player.getPlaylist().length - 1 == clip.index)
                       {
                           SlideShowManager.player.stop();
                           SlideShowManager.player.play(0);
                       } else 
	   		   SlideShowManager.playNext();		 
                    },

                    onBegin: function(clip) {
                        SlideShowManager.currentMusicIndex = clip.index;
                    }
		})
	},

        preloadImages: function()
        {
            try {
            var img = new Image(); 
            img.src = '/images/template/sound/left_temn.gif';
            var img1 = new Image();
            img1.src = '/images/template/sound/mus_temn.gif';
            var img2 = new Image();
            img2.src = '/images/template/sound/right_temn.gif';
            var img3 = new Image();
            img3.src = '/images/template/sound/left_svetl.gif';
            var img4 = new Image();
            img4.src = '/images/template/sound/mus_svetl.gif';
            var img5 = new Image();
            img5.src = '/images/template/sound/right_svetl.gif';
            var img6 = new Image();
            img6.src = '/images/template/loader.gif';
            } catch (err)
            {}

        },
	
	init : function ()
	{
                               
                $('#playPrev').click(function(){
                    SlideShowManager.playPrev();
                    return false;
                });
                $('#playNext').click(function(){
                    SlideShowManager.playNext();
                    return false;
                });
                $('#playStop').click(function(){
                    SlideShowManager.playStop();
                    return false;
                });

                SlideShowManager.playing = false;
                $('#playStop').addClass('playStopPressed');

	},

        playStop: function(){
            if(SlideShowManager.playing){
                SlideShowManager.player.stop();
                SlideShowManager.playing = false;
                $('#playStop').addClass('playStopPressed');

            } else {
                SlideShowManager.playing = true;
                SlideShowManager.player.play(SlideShowManager.currentMusicIndex);
                $('#playStop').removeClass('playStopPressed');
            }
            
        },

	playMusic: function(playlist)
	{
            SlideShowManager.totalSongsInPlaylist = playlist.length;
            SlideShowManager.createPlayer(function(){
                if (SlideShowManager.totalSongsInPlaylist > 0)
                {
                    SlideShowManager.player.setPlaylist(playlist);
                    SlideShowManager.player.setVolume(100);
                    SlideShowManager.playStop();
                }
            })

  
	},

        playNext: function()
        {
            SlideShowManager.player.stop();
            if (SlideShowManager.currentMusicIndex == SlideShowManager.totalSongsInPlaylist-1)
            {
                SlideShowManager.player.play(0);
            }
            else
            {
                SlideShowManager.player.play(SlideShowManager.currentMusicIndex + 1);
            }
            $('#playStop').removeClass('playStopPressed');
            SlideShowManager.playing = true;
        },

        playPrev: function()
        {
            SlideShowManager.player.stop();
            if (SlideShowManager.currentMusicIndex == 0)
            {
                SlideShowManager.player.play(SlideShowManager.totalSongsInPlaylist-1);
            } else
                {
                    SlideShowManager.player.play(SlideShowManager.currentMusicIndex-1);
                }
            $('#playStop').removeClass('playStopPressed');
            SlideShowManager.playing = true;
        }
	
}

var PhotoSlider = {
	container : null,
	slideTimeout: 2333,
	currentPage: null,
        lastPage: null,
        photoID: null,
        containerWidth: 728,
        request:null,
        slideToLeftButton: null,
        slideToRightButton: null,
        loadedPages: [],
        containerLeft:0,
        slideInProgress: false,

	slideLeft: function() {
            PhotoSlider.containerLeft -= PhotoSlider.containerWidth;           
            PhotoSlider.container.animate(
                    {marginLeft: PhotoSlider.containerLeft +'px'},
                    PhotoSlider.slideTimeout, 'expoout', function() {
                        var nextPage = PhotoSlider.currentPage + 1;
                        if (nextPage <= PhotoSlider.lastPage)
                        {
                            PhotoSlider.getNextPageDate(nextPage, PhotoSlider.addPhotosFromRight);
                        }
                        PhotoSlider.slideInProgress = false;
                    });

            PhotoSlider.currentPage++ ;
            PhotoSlider.updateSlideButtons();
	},
   
	slideRight: function() {
            PhotoSlider.containerLeft += PhotoSlider.containerWidth;
            PhotoSlider.container.animate(
                     {marginLeft: PhotoSlider.containerLeft  +'px'},
                     PhotoSlider.slideTimeout, 'expoout',function() {
                         var nextPage = PhotoSlider.currentPage - 1;
                         if (nextPage > 0)
                         {
                            PhotoSlider.getNextPageDate(nextPage, PhotoSlider.addPhotosFromLeft);                            
                         }
                         PhotoSlider.slideInProgress = false;
                     });
                     
            PhotoSlider.currentPage -- ;
            PhotoSlider.updateSlideButtons();
	},


        updateSlideButtons: function()
        {
            if (PhotoSlider.currentPage < PhotoSlider.lastPage)
            {
                PhotoSlider.slideToLeftButton.show();
            } else {
                PhotoSlider.slideToLeftButton.hide();
            }

            if (PhotoSlider.currentPage > 1)
            {
                PhotoSlider.slideToRightButton.show();
            } else {
                 PhotoSlider.slideToRightButton.hide();
            }

        },

        addPhotosFromRight: function(data)
        {            
            PhotoSlider.container.append(data);
        },

        addPhotosFromLeft: function(data)
        {
           PhotoSlider.containerLeft -= PhotoSlider.containerWidth;

           PhotoSlider.container.prepend($(data).hide());
           var clonedContainer = PhotoSlider.container.clone(true);
           clonedContainer.find('li:hidden').show();
           clonedContainer.css('margin-left',PhotoSlider.containerLeft );
           PhotoSlider.container.parent().append(clonedContainer);
           PhotoSlider.container.replaceWith("");
           PhotoSlider.container = clonedContainer;
        },

	
	getNextPageDate: function(page, func){

            if (PhotoSlider.loadedPages[page] != 1)
            {
                PhotoSlider.request = $.get("/index.php/ajax/getCategoryPage/"+ PhotoSlider.photoID +"/" + page,
                            function(data)
                            {
                                PhotoSlider.loadedPages[page] = 1;
                                if(data)
                                {
                                    func(data);
                                }
                            },
                            'html'
                    );
             }
	},
	
        btnSlideRightClicked:function(){
                if (!PhotoSlider.slideInProgress)
            {
                PhotoSlider.slideInProgress = true;
                PhotoSlider.slideRight();

            }
            return false;
        },
        
        btnSlideLeftClicked: function(){
            if (!PhotoSlider.slideInProgress)
            {
                PhotoSlider.slideInProgress = true;
                PhotoSlider.slideLeft();
            }
            return false;
        },

	init:function()
	{

            PhotoSlider.currentPage = parseInt($("#currentPage").val());
            PhotoSlider.lastPage = parseInt($("#lastPage").val());
            PhotoSlider.photoID =  parseInt($("#photoID").val());
            PhotoSlider.container = $('#additionalPhotos');
            PhotoSlider.container.css('width',PhotoSlider.lastPage * PhotoSlider.containerWidth );



            PhotoSlider.loadedPages[PhotoSlider.currentPage] = 1; // already loaded

            if (PhotoSlider.currentPage < PhotoSlider.lastPage)
            {
                PhotoSlider.getNextPageDate(PhotoSlider.currentPage + 1,PhotoSlider.addPhotosFromRight);
            }
            if (PhotoSlider.currentPage != 1)
            {
                PhotoSlider.getNextPageDate(PhotoSlider.currentPage - 1,PhotoSlider.addPhotosFromLeft);
            }

            PhotoSlider.slideToRightButton = $("#prevplist");

            PhotoSlider.slideToRightButton.click(PhotoSlider.btnSlideRightClicked)
            PhotoSlider.slideToLeftButton = $("#nextplist");
            PhotoSlider.slideToLeftButton.click(PhotoSlider.btnSlideLeftClicked)

            PhotoSlider.container.mousewheel(function(event, delta) {
                if (delta < 0)
                {
                    if (PhotoSlider.currentPage < PhotoSlider.lastPage){
                        PhotoSlider.btnSlideLeftClicked();
                    }
                } else
                {
                    if (PhotoSlider.currentPage > 1) {
                        PhotoSlider.btnSlideRightClicked();
                    }
                }
                return false;
            });

            PhotoSlider.updateSlideButtons();

	}
}
