/************** Slideshow **************/ 
var Slideshow = { 
    nTop : 0,
    nLeft: 0,
    nLimit : 0,
    nHeight : 0,
    nWidth : 0,
    //you have to add the bottom padding/margin
    nMaskHeight : 290,
    nMaskWidth : 465,
    nImagesCount: 0,
    nCurrentIndex: 0,
    pInterval: {},
    
    init: function() 
    {
        $('.gboxmove').width($('.gboxmove img').size() * 93);
        $('.gbox1 img').eq(0).bind('click', function(){Slideshow.zoom($(this));}); 

        Slideshow.nLeft = parseInt($('.gboxmove').position().left);
        Slideshow.nWidth = parseInt($('.gboxmove').width());
        Slideshow.nLimit = (Slideshow.nWidth - Slideshow.nMaskWidth) * (-1);

//        $('.gslideup').click(function(){Slideshow.slideThumbs('down')});
//        $('.gslidedown').click(function(){Slideshow.slideThumbs('up')});
        
        $('.gslideup').hover(
            function () { Slideshow.pInterval = setInterval("Slideshow.moveThumbs(1)",10); $('.gslidedown').stop(true,true).fadeIn('fast'); },
            function () { clearInterval(Slideshow.pInterval); }
        );
        
        $('.gslidedown').hover(
            function () { Slideshow.pInterval = setInterval("Slideshow.moveThumbs(-1)",10); $('.gslideup').stop(true,true).fadeIn('fast'); },
            function () { clearInterval(Slideshow.pInterval); }
        );
        
        $('.gbox2').hover(
            function() 
            {
                var nCurrentPos = $('.gbox2 .gboxmove').position().left;
                
                if(nCurrentPos < 0)
                {
                    $('.gslideup').stop(true,true).fadeIn('fast');
                }
                
                if(nCurrentPos > Slideshow.nLimit)
                {
                    $('.gslidedown').stop(true,true).fadeIn('fast');
                }
            },
            function() 
            {
                $('.gslideup, .gslidedown').stop(true,true).fadeOut('fast');
            }
        );

        //Thumbs click events
        $('.gboxmove img').each(function(){
           var nImgIndex = $(this).index();

           $(this).click(function () {
               //Slideshow.fade(nImgIndex);
               Slideshow.zoom($('.gbox1 img').eq(nImgIndex));
           });
        });
    },
        
    //bThumbOrArrow -> Podavame true ako sme natisnali thumb / false ako e natisnata strelka
    fade: function (nCurrent)
    {     
        var pPrev = $('.gbox1 img:visible');
        var pNext = $('.gbox1 img').eq(nCurrent);
        
        pPrev.fadeOut('normal');
        
        pPrev.unbind('click');
        
        pNext.fadeIn('normal');
        
        pNext.bind('click', function(){Slideshow.zoom($(this));});  
        
    }, 
      
    zoom: function(pPhoto)
    {
        Slideshow.nImagesCount = pPhoto.parent().find('img').size() - 1;
        Slideshow.nCurrentIndex = pPhoto.index();
                
        var strUrl = pPhoto.attr('rel');
        var nIndex = pPhoto.index();
        
        $('body').append('<div id="zoombg" class="zoombgwrapper"></div>');
        $('#zoombg').height($(document).height()).width($(document).width()).fadeTo('normal','0.5');
        $('body').append('<div id="zoomimage" class="zoomimagewrapper"><img src="'+strRootPath+'images/ajax-loader.gif" /></div>');
        $('<div id="zoomimagecontrols" class="zoomimagecontrolswrapper"><div id="zoomimageleft"><img src="'+strRootPath+'images/left.png" /></div><div id="zoomimageright"><img src="'+strRootPath+'images/right.png" /></div></div>').appendTo('body');
        
        if(document.getElementById('zoomimagecontrols') != undefined)
        {
            $('#zoomimagecontrols').center().click(function(){Slideshow.closeSlideShow()});
            
            $('#zoomimageleft').click(function(e){e.stopPropagation(); Slideshow.changeZoomImage(-1);});
            $('#zoomimageright').click(function(e){e.stopPropagation(); Slideshow.changeZoomImage(1);})
        }
        
        Slideshow.loadImage(strUrl);
    },
    
    changeZoomImage: function(nDirection)
    {
        var nNextIndex = Slideshow.nCurrentIndex + nDirection;
        
        if(nNextIndex > Slideshow.nImagesCount)
        {
            Slideshow.nCurrentIndex = 0;
            Slideshow.loadImage($('.gbox1 img').eq(0).attr('rel'));
        }
        else if(nNextIndex <= 0)
        {
            Slideshow.nCurrentIndex = Slideshow.nImagesCount;
            Slideshow.loadImage($('.gbox1 img').eq(Slideshow.nCurrentIndex).attr('rel'));
        }
        else
        {
            Slideshow.nCurrentIndex = nNextIndex;
            Slideshow.loadImage($('.gbox1 img').eq(nNextIndex).attr('rel'));
        }
    },
    
    loadImage: function(strUrl)
    {        
        $('#zoomimage').css('opacity','0').html('<img src="'+strRootPath+'images/ajax-loader.gif" />').width('auto').height('auto').center().animate({'opacity':'1'},500);
        
        var pImg = new Image();
        pImg.src = strUrl;
        
        pImg.onload = function() {
            $('#zoomimage').css('opacity','0').html('<img src="'+strUrl+'" />').center();
            
            var nHeight = $('#zoomimage img').height();
            var nWidth = $('#zoomimage img').width();
            var nHeightWindow = $(window).height();
            var nWidthWindow = $(window).width();
            var nNewOfset = 0;
            
            
            var pZoomSet = $('#zoomimage').add('#zoomimage img');
            
            var strPoint = '';
            
            if(nWidth > nHeight)
            {
                if(nWidth/nHeight > nWidthWindow/nHeightWindow)
                {
                    pZoomSet.width(nWidthWindow-40).animate({'opacity':'1'},'normal');
                    pZoomSet.height('auto');
                    strPoint = 'left';
                    nNewOfset = 10;
                }
                else
                {
                    pZoomSet.height(nHeightWindow-40).animate({'opacity':'1'},'normal');
                    pZoomSet.width('auto');
                    strPoint = 'top';
                    nNewOfset = $(window).scrollTop() + 10;
                }
            }
            else
            {
                if(nWidth/nHeight > nWidthWindow/nHeightWindow)
                {
                    pZoomSet.width(nWidthWindow-40).animate({'opacity':'1'},'normal');
                    pZoomSet.height('auto');
                    strPoint = 'left';
                    nNewOfset = 10;
                }
                else
                {
                    pZoomSet.height(nHeightWindow-40).animate({'opacity':'1'},'normal');
                    pZoomSet.width('auto');
                    strPoint = 'top';
                    nNewOfset = $(window).scrollTop() + 10;
                }
            }
            
            $('#zoomimage').center().css(strPoint, nNewOfset).add('#zoombg').click(function() {Slideshow.closeSlideShow()});
            
            if(strPoint == 'top')
            {
                var nNewLeft = $('#zoomimage').css('left').replace('px','')-10;
                $('#zoomimage').css({'left': nNewLeft+'px'});
            }
        }   
    },
    
    slideThumbs: function(strDirection)
    {
        //alert(Slideshow.nLimit+" : "+Slideshow.nTop);
        var nAddLeft = 0;
        
        if(strDirection == 'down')
        {               
            nAddLeft = (Slideshow.nLeft > -Slideshow.nMaskWidth) ? Slideshow.nLeft*(-1) : Slideshow.nMaskWidth;
            
            if(Slideshow.nLeft < 0)
            {
                $('.gslidedown').stop(true,true).fadeIn('fast');
                $('.gboxmove').not(':animated').animate({left: '+='+nAddLeft},
                        function()
                        {
                            Slideshow.nLeft = parseInt($('.gboxmove').position().left);
                            
                            if(Slideshow.nLeft == 0)
                            {
                                $('.gslideup').stop(true,true).fadeOut('fast');
                            }
                        });
            }
        }
        else if(strDirection == 'up')
        {
            nAddLeft = ((Slideshow.nLimit - Slideshow.nLeft) < -Slideshow.nMaskWidth) ? Slideshow.nMaskWidth : (Slideshow.nLimit - Slideshow.nLeft)*(-1);
            
            if(Slideshow.nLeft > Slideshow.nLimit)
            {
                $('.gslideup').stop(true,true).fadeIn('fast');
                
                $('.gboxmove').not(':animated').animate({left: '-='+nAddLeft},
                        function()
                        {
                            Slideshow.nLeft = parseInt($('.gboxmove').position().left);
                            if(Slideshow.nLeft == Slideshow.nLimit)
                            {
                                $('.gslidedown').stop(true,true).fadeOut('fast');
                            }
                        });
            }
        }
    },
    
    moveThumbs: function (nDirc) 
    {
        var nLeft = $('.gboxmove').position().left + (3 * nDirc);
       
        if(nLeft <= 0 && nLeft >= Slideshow.nLimit)
        {
            $('.gboxmove').not(':animated').css({left:nLeft});
        }
        else if(nLeft >= 0) 
        {
             $('.gslideup').stop(true,true).fadeOut('fast');
        }
        else
        {
             $('.gslidedown').stop(true,true).fadeOut('fast');
        }
    },
    
    closeSlideShow: function()
    {
        $('#zoomimage, #zoomimagecontrols').fadeOut('normal',function(){$(this).remove();$('#zoombg').fadeOut('fast',function(){$(this).remove();})});
    }
}; 

$(document).ready(function(){if($('.slideshowwrapper').length) {Slideshow.init()}});
    
