var onSlider = false;
var onPicture = false;
var loader = $('#st_loading');
//the ul element
var list		= $('#slider');
//the current image being shown
var currImage 	= $('#st_main #img-box').children('img:first');

$(function() {

    loader = $('#st_loading');
    //the ul element
    list		= $('#slider');
    //the current image being shown
    currImage 	= $('#st_main #img-box').children('img:first');
    if ($('#showSpash').length > 0) {
        showSplash();
    } else {
        loadpage();
    }

   
});
var slideCount = 0
var currentSlide = 0;
function startSlideshow() {
    setTimeout(nextSlide, 8000)

}

function nextSlide(){ 

    $('#st_main #img-box img.st_preview:eq('+currentSlide+')').fadeOut(800, function(){
  
        if ((currentSlide+1) == slideCount) {
            currentSlide = 0;
        } else {
            currentSlide=currentSlide+1;
        }
        $('#st_main #img-box img.st_preview:eq('+currentSlide+')').fadeIn(800, startSlideshow);
    });
}
function iniSlider () {
    $('.st_preview').mouseenter(function(){
        onPicture = true;
        $('#slider').show();

    }).mouseleave(function(){
        onPicture = false;
        if (!onSlider) $('#slider').hide();
    });
    $('#slider').find('.st_thumbs').bind('mouseenter',function(){

        onSlider = true;
        $(this).stop().animate({
            'opacity':'1'
        });
    });
    $('#slider').find('.st_thumbs').bind('mouseleave',function(){


        $(this).stop().animate({
            'opacity':'0'
        }, function(){
            onSlider = false;
            if (!onPicture) {
                $('#slider').hide();

            }
        });
    });
}



$(window).load(function(){
if ($('#showSpash').length == 0) {
    imgCorect();}
    if ($('.st_thumbs img').length > 1) {
	}
});

function imgCorect() {
    var extrapace = 250;
    var windowHeight = $(window).height()-75;
    var windowWidth = $(window).width()-extrapace;

    $('.st_preview').each(function(){
       w = $(this).width();
       h = $(this).height();
$(this).height( windowHeight);
 $(this).width(Math.round((w/h)*windowHeight));

//        if (windowHeight/$(this).height() > windowWidth/$(this).width()) {
//            h = $(this).height();
//             w = $(this).width();
//            $(this).height( windowHeight);
//            $(this).width(Math.round((w/$(this).height())*windowHeight));
//        } else {
//            w = $(this).width();
//            $(this).width(Math.round((w/$(this).height())*windowHeight));
//            $(this).height(windowHeight);
//        }
          $('#img-box').width($('.st_preview').width()).height($('.st_preview').height());
    //    $('#st_main').width($(this).width()+250);
       $('#st_main').width($('.st_preview:first').width()+260);
       initArrows ();
    });


}
function imgCorectFirst() {
    var extrapace = 250;
    var windowHeight = $(window).height()-75;
    var windowWidth = $(window).width()-extrapace;


	$('.caption_container').css({'top' : $(window).height() - 45 + 'px', 'left' : '239px'});

    $('.st_preview:first').css('opacity',0);


 h = $('.st_preview:first').height();
 w = $('.st_preview:first').width();
 //console.log('imgCorectFirst ');
 
 //console.log($('.st_preview:first').find('img'));
 
    $('.st_preview:first').height(windowHeight);
    $('.st_preview:first').width(Math.round((w/h)*windowHeight));
   
	if($('#img-box').length > 0)   {
		$('#st_main').width($('.st_preview:first').width()+260);
	//    if (windowHeight/$('.st_preview:first').height() > windowWidth/$('.st_preview:first').width()) {
	//
	//        w = $('.st_preview:first').width();
	//        $('.st_preview:first').height( Math.round((h/$('.st_preview:first').width()) * windowWidth));
	//        $('.st_preview:first').width(windowWidth);
	//    } else {
	//
	//        $('.st_preview:first').width(Math.round((w/$('.st_preview:first').height())*windowHeight));
	//        $('.st_preview:first').height(windowHeight);
	//    }
	    $('#img-box').width($('.st_preview:first').width()).height($('.st_preview:first').height());
	    $('.st_preview').animate({
	        'opacity':'1'
	    });
	}

	if($('#film-box').length > 0)	{
		
		$('#st_loading').fadeOut(function()	{$('#st_loading').remove();});	
	}

initArrows ();

}
/*

function imgCorect() {
    var extrapace = 250;
    var windowHeight = $(window).height()-40;
    var windowWidth = $(window).width()-extrapace;

    $('.st_preview').each(function(){
   
        if (windowHeight/$(this).height() > windowWidth/$(this).width()) {
            h = $(this).height();
            $(this).height( Math.round((h/$(this).width()) * windowWidth));
            $(this).width(windowWidth);
        } else {
            w = $(this).width();
            $(this).width(Math.round((w/$(this).height())*windowHeight));
            $(this).height(windowHeight);
        }
        //  $('#img-box').width($('.st_preview').width()).height($('.st_preview').height());
        initArrows ();
    });


}
function imgCorectFirst() {
    var extrapace = 250;
    var windowHeight = $(window).height()-40;
    var windowWidth = $(window).width()-extrapace;

    $('.st_preview:first').css('opacity',0);

    if (windowHeight/$('.st_preview:first').height() > windowWidth/$('.st_preview:first').width()) {
        h = $(this).height();
        $('.st_preview:first').height( Math.round((h/$('.st_preview:first').width()) * windowWidth));
        $('.st_preview:first').width(windowWidth);
    } else {
        w = $('.st_preview:first').width();
        $('.st_preview:first').width(Math.round((w/$('.st_preview:first').height())*windowHeight));
        $('.st_preview:first').height(windowHeight);
    }
    $('#img-box').width($('.st_preview:first').width()).height($('.st_preview:first').height());
    $('.st_preview').animate({
        'opacity':'1'
    });



}
*/
function initArrows (){
    if ($('#arrows').length != 0) $('#arrows').remove();
    if ($('.st_thumbs img').length > 1) {

	    if(!$('html').hasClass('film')) {

		    $('<div id="arrows"><div id="arr-left"></div><div id="arr-right"></div></div>').css('opacity',0).appendTo('body').css({
		        'top' : ($(window).height()-45)+'px' ,
		        'left': '190px'
		    }).css('opacity',1); //.animate({'opacity': '1'},3000);
	    }
	    
	    $('#arr-left').click(function(){
	        var nextImg = null;
	        var nextTitle = null;
	        var currentImg = $('#img-box').children('img:first').attr('src');
	        if ($('.st_thumbs img[alt='+currentImg+']').prev('img').attr('alt') != null) {
	            nextImg = $('.st_thumbs img[alt='+currentImg+']').prev('img').attr('alt');
	        	nextTitle = $('.st_thumbs img[alt='+currentImg+']').prev('img').attr('title');
	        } else {
	            nextImg = $('.st_thumbs img:last').attr('alt');
	            nextTitle = $('.st_thumbs img:last').title('title'); 
	        }
	        replaceMaine(nextImg, nextTitle);
	    });
	    $('#arr-right').click(function(){
	        var nextImg = null;
	        var nextTitle = null;
	        var currentImg = $('#img-box').children('img:first').attr('src');
	        if ($('.st_thumbs img[alt='+currentImg+']').next('img').attr('alt') != null ) {
	            nextImg = $('.st_thumbs img[alt='+currentImg+']').next('img').attr('alt');
				nextTitle = $('.st_thumbs img[alt='+currentImg+']').next('img').attr('title'); 
	        } else {
	            nextImg = $('.st_thumbs img:first').attr('alt');
	        	nextTitle = $('.st_thumbs img:first').attr('title');
	        }
	        replaceMaine(nextImg, nextTitle);
	    });
    }
}
/*
function replaceMaine(src){
    if (src != null) {
        $('<img class="st_preview"/>').attr('src',src).load(function(){
           // var $this = $(this);
            var currImage = $('#st_main #img-box').children('img:first');
            $(this).hide();
            $(this).insertBefore(currImage);
            loader.hide();
            imgCorect();
            currImage.fadeOut(800,function(){
                console.log($(this));
                $(this).remove();
                $(this).fadeIn(400, function(){
                    iniSlider ();
                });
            });
        });
    }
     
} */
function replaceMaine(src, title){
    if (src != null) {
    
    	$('.caption_container').fadeOut(400, function() {
    		$(this).remove();
    	});
    
        $('<img class="st_preview"/>').attr('src',src).load(function(){
            var $this = $(this);
			var $currImage = $('#st_main #img-box').children('img:first');
            $this.hide();
            $this.insertBefore($currImage);
            $('<div class="caption_container" style="top : '+($(window).height() - 45) + 'px">'+title+'</div>').insertBefore($currImage).hide();

            loader.hide();
            imgCorectFirst();
            $currImage.fadeOut(800,function(){
                $(this).remove();
            	$('div.caption_container').fadeIn();    
                $this.fadeIn(400, function(){
                    iniSlider ();
                });
            });
        });
    }
 
}
function buildThumbs(){
    list.children('.album').each(function(){
        var elem 			= $(this);
        var thumbs_wrapper = elem.find('.st_thumbs_wrapper');
        var thumbs 		= thumbs_wrapper.children(':first');
        //each thumb has 180px and we add 3 of margin
        var finalW 			= thumbs.find('img').length * 183;
        thumbs.css('width',finalW + 'px');

        //make this element scrollable
        makeScrollable(thumbs_wrapper,thumbs);
    });
    $('#slider').css('top', $(window).height()/2 -103);
}


//makes the thumbs div scrollable
//on mouse move the div scrolls automatically
function makeScrollable(outer, inner){
    var extra 			= 10;
    //Get menu width
    var divWidth = outer.width();
    //Remove scrollbars
    outer.css({
        overflow: 'hidden'
    });
    //Find last image in container
    var lastElem = inner.find('img:last');
    outer.scrollLeft(0);
    //When user move mouse over menu
    outer.unbind('mousemove').bind('mousemove',function(e){
        var containerWidth = lastElem[0].offsetLeft + lastElem.outerWidth() + 2*extra;
        var left = (e.pageX - outer.offset().left) * (containerWidth-divWidth) / divWidth - extra;
        outer.scrollLeft(left);
    });
}
function loadpage () {
    if ($('#disable-menu-anim').length == 0) {
        $('.menu').find('li').css('opacity',0).showmenu();
    }
    $('#pdf-form').submit(function(){
        $.ajax({
            'url': 'pdf-result.php',
            'data':  $('#pdf-form').serialize(),
            'success': function(data) {
                if(data.trim() != '') {
                    $('#pdf-form').html(data);
                }
            },
            'type': 'POST',
            'dataType': 'html',
            'beforeSend': function() {
                $('#pdf-form').css('opacity', '0.3');
            },
            'complete': function() {
                $('#pdf-form').css('opacity', '1');
            }
        });
        return false;
    });
if ( !$.browser.opera ) {
    imgCorectFirst();
}
    $("a.overlay[rel]").each (function () {

        var el =$(this).attr('rel')
        iniTxtPages();

//        if ($(el).length != 0) $(this).overlay({
//            mask: 'gray' ,
//            left : '240px' ,
//            top : 20
//        });
    });


   
    //let's load the current image
    //and just then display the navigation menu
    $('<img>').load(function(){
        loader.hide();
        currImage.fadeIn(800);
        //slide out the menu
        setTimeout(function(){
            list.animate({
                'left':'0px'
            },500);
        },
        1000);
    }).attr('src',currImage.attr('src'));

    //calculates the width of the div element
    //where the thumbs are going to be displayed
    if ($('.st_thumbs img').length > 0) {
        buildThumbs();
        initArrows ();

    }



    //clicking on a thumb, replaces the large image
    list.find('.st_thumbs img').bind('click',function(){
        //  var $this = $(this);
        
        var caption_string = $(this).attr('title');

        $('.caption_container:last').remove();

        
        loader.show();
        $('<img class="st_preview loaded_item"/>').load(function(data){

            var $currImage = $('#st_main #img-box').children('img:first');
            
            // Insert Contents
            $(this).insertBefore($currImage);
            $('<div class="caption_container" style="top : ' + ($(window).height()-45) + 'px">' + caption_string + '</div>').insertBefore($currImage);

            loader.hide();
            imgCorectFirst();
            $currImage.fadeOut(400,function(){
                $(this).remove();
            });

        }).attr('src',$(this).attr('alt'));
    
    
    }).bind('mouseenter',function(){
        $(this).stop().animate({
            'opacity':'1'
        });
    }).bind('mouseleave',function(){
        $(this).stop().animate({
            'opacity':'1'
        });
        iniSlider ()
    });

    $('#slider').hide();
    // function to show and hide the thumbnail scroller
    iniSlider ()

    // homepage slideshow
    slideCount = $('#st_main #img-box img.st_preview').length;

    if (slideCount > 1) {
        startSlideshow();
    }
    if ( $('a.artist-name').length != 0) {
        $('a.artist-name').imgPreview({
            containerID: 'imgPreviewWithStyles',
            srcAttr: 'rel',

            // When container is shown:
            onShow: function(link){
                // Animate link:
                // $(link).stop().animate({opacity:0.4});
                // Reset image:
                if (!$.browser.msie) {

                    $('img', this).stop().css({
                        opacity:0
                    });
                }

            },
            // When image has loaded:
            onLoad: function(){
                // Animate image
                $(this).animate({
                    opacity:1
                }, 600);
            },
            // When container hides:
            onHide: function(link){
                // Animate link:
                $(link).stop().animate({
                    opacity:1
                });
            }
        });
    }
}

function  showSplash() {
   
                
    $('#st_main').hide();
 
    $('<div id="splasher" ></div><div id="splash" />').appendTo('body').hide();
    $('#splasher').css('background-color', '#000');
    $('#splash').hide();
    splasherCorrect();
    $(window).resize(splasherCorrect);
    $('#splasher').fadeIn(100, function(){
        $('#splash').fadeIn(2000, function (){
            $(this).delay(100).fadeOut(1000, function(){
               
              $('#splash').css({'background-position' : 'center 0' }).fadeIn(1000);
            });
           
            $('#splasher').delay(1000).animate({'opacity': 0},1000, function(){
                 
                  $(this).delay(1).css({'background-color': '#FFF' , 'opacity' : 1})

                    $('#splash').delay(1000).fadeOut(2000,function(){
                          $(this).remove();});
                      $('#splasher').delay(1000).fadeOut(2000,function(){
                          $(this).remove();
                          $('#st_main').delay(1000).show();
                          loadpage();
                           imgCorect();
                      });

              })
             
        });
    });

       $('body').show();
}
function splasherCorrect() {
    $('#splasher').css('padding-top', ($(window).height()/2-40)+'px').width($(window).width()).height($(window).height()-($(window).height()/2-40));
    $('#splash').css('top', ($(window).height()/2-40)+'px').css('left', ($(window).width()/2-250)+'px');
}




function iniTxtPages() {
   
    $('.overlay').click(function(){
         $('.simple_overlay').each( function(){ $(this).hide(); });
        var ob = $(this);

		if($('#img-box').length > 0)	{
	    
	        $('#img-box').fadeOut(400,function(){
	           $(this).hide();
	           $('#st_main').css('width','auto');
	           $(ob.attr('rel')).show()
	        });
		}
		else if ($('#film-box').length > 0) {

		    $('#film-box').fadeOut(400,function(){
	           $(this).hide();
	           $('#st_main').css('width','auto');
	           $(ob.attr('rel')).show()
	        });
		}

        $('#arrows').fadeOut(400,function(){
           $(this).hide() ;
        });
        return false;
    });
}


(function($){
    $.fn.showmenu = function(){
        var menu = new Array();
        $(this).each(function (){
            
            menu[menu.length ]=$(this);
        });


        var i = 0;
        show(menu);
        function show(menu){
            if (i < menu.length) {
                menu[i].css('line-height' , '10px').animate({
                    'opacity':'0.1'
                },50,function(){
                    i++;
                    show(menu);
                }).animate({
                    'line-height':'16px' ,
                    'opacity':'1'
                },400 );
            }
        }



    };
    
    

})(jQuery);
