var Tools = {
    Images: {
        /**
         * úadowanie kolejno zdjŕŠ
         */ 
        SequentialLoader: function() {
            var fnLoader = arguments.callee;
            var $el = $(this);
            var i = new Image();
            i.onload = function(){
                $el.find('img').attr('src', i.src);
                $el.next('li').each(fnLoader);
            };
            i.src = $el.find('img').attr('data-src');
        }
        
    }
};

var myJsp;

/* DOM Ready Functions */
jQuery(function($){
    
    // SzerokoťŠ ca-ego kontenera z elementami
    // *************************************************************************
    var setUlWidth = function(h){
        var w = 0;
        $('#itemList img').each(function(){
            var $el = $(this);
            
            var ow = $el.attr('data-owidth');
            var oh = $el.attr('data-oheight');
            
            var nh = h;
            var nw = ow * (nh/oh);
            w += nw;
            $el.css({
                width: nw,
                height: nh
            });
            
        });
        
        // +1 for firefox to prevent last image from breaking to next line
        var shim = 1; // $.browser.mozilla ? 1 : 0; 
        $('#itemList').width(w+shim).height(h);      
        try { 
            myJsp.reinitialise({
                contentWidth: w
            });
        } catch(e) {}
    };
    
    // Zmiana rozmiaru kontenera z kontentem podczas zmiany wielkoťci okna
    // *************************************************************************
    var resizeContent = function(){
    
        var sidebarWidth = 180;
        var minHeight = 440;
        var maxHeight = 1200;
        
        // max height dla iPad wynosi 670
		    if (isMobile || navigator.platform.indexOf('iPad') != -1) {
			     maxHeight = 670; 
		    }

        $('#content').css('width', $(document).width() - sidebarWidth); 
        
        var h = $(window).height() -30 ; // dodajemy 10px pod scrollbarerm 
        
        if(h > maxHeight) h = maxHeight;
        else if(h < minHeight) h = minHeight; 
        
        //$('.jspPane,#itemList').height(h);
                
        $('.jspContainer,.scroll-pane').height(h + 20);
        if(myJsp){
            var tmp = myJsp.getPercentScrolledX();
        }
        
        setUlWidth(h);
        positionFooter();
        
        if(myJsp){
            myJsp.scrollToPercentX(tmp, false);
        }
    };
    
    // wypozycjonowanie stopki 
    // *************************************************************************
    var positionFooter = function(){
            $('#contact, #biography').height($('#content').height() - 120); 
    	var offset = 85;
      $('#footer').css({
          position: 'absolute',
          top: $('#content').height() - offset
      });
    };
    
    // Inicjalizacja jScrollPane
    // *************************************************************************    

  		myJsp = $('.scroll-pane').jScrollPane({
  			      //autoReinitialise: true,
              //autoReinitialiseDelay: 10,
              //mouseWheelSpeed: 1000
              //animateEase: 'linear',
              animateDuration: 450
  		}).data('jsp');

   
    // bind to window resize event 
    // *************************************************************************
    var isMobile = typeof(isMobile) != 'undefined' ? isMobile : false;
    if(!isMobile) {
        $(window).unbind('resize', resizeContent).bind('resize', resizeContent).trigger('resize');
    }
      
    // Ladowanie zdjec
    // *************************************************************************
    $('#itemList li:eq(0)').each(Tools.Images.SequentialLoader);
     
    // Content
    // *************************************************************************
    
    var toggleContent =  function(name) {
     if ( $(name).is(':visible') ) {
          $(name).fadeOut();
       } else {
          $(name).fadeIn();
    }
    }
  
    $('#buttonContact').click( function(event) {
        if ( $('#biography').is(':visible') ) $('#biography').fadeOut();
        toggleContent('#contact');
        event.stopPropagation();
     } );           
       
    $('#buttonBiography').click( function(event) {
        if ( $('#contact').is(':visible') ) $('#contact').fadeOut();
        toggleContent('#biography');
        event.stopPropagation();
     } );         
});

