var bgImg = {
    width: 0,
    height: 0,
    pomer: 0
};

$(function() {
    $('#body-bg').load(function(){
        bgImg.width  = $(this).width();
        bgImg.height = $(this).height();
        bgImg.pomer  = $(this).width() / $(this).height();
        updateBgImageSize();
    });
    
    resizeBody(0);
    $(window).resize(function() {
        updateBgImageSize();
        resizeBody(0);
    });
    
    $("#toggleBtn").toggle(
        function(){ $(this).removeClass().addClass('plus'); $(this).parent().find("div.data-bg").slideUp(1200); }
       ,function(){ $(this).removeClass().addClass('minus'); $(this).parent().find("div.data-bg").slideDown(500); }
    ).trigger('click');
    
    $("#gallery a.gallery-item-url img").hover(
         function(){
             $.src = $(this).attr('src');
             $(this).attr('src',WEBROOT + '/public/themes/images/info.png').addClass('hover');
             
             var popup = $(this).parent().parent().find("div.gallery-item-desc");
             var posun = 0;
             
             var pom = $(window).height() - $("#kontakt").outerHeight() - 6 - $(this).offset().top;
             if(popup.outerHeight() > pom){
                 posun = popup.outerHeight() - pom;
             }
             
             popup.css({ 
                'top'  : ($(this).position().top - posun) + 'px'
               ,'left' : ($("#gallery").offset().left - $(this).offset().left - popup.outerWidth() - 10) + 'px'
             }).show();
         }
        ,function(){
             $(this).attr('src',$.src).removeClass('hover');
             $(this).parent().parent().find("div.gallery-item-desc").hide();
         }
    );
    
    $("#gallery a.gallery-item-url").click(function(){
         
         if($("#toggleBtn").hasClass('minus')){
             $("#toggleBtn").trigger('click');
         }
         
         var imgOverlay = $('<div>')
         .attr('id','imgOverlay')
         .css({
              'width' : $(window).width() + 'px'
             ,'height' : $(window).height() + 'px'
             ,'position' : 'absolute'
             ,'top' : '0px'
             ,'left' : '0px'
             ,'background' : '#000'
             ,'opacity' : 0.8
             ,'cursor'   : 'pointer'
         })
         .appendTo($("body"))
         .show()
         .click(function(){
             $('#imgOverlay').remove();
             $('#imgPreview').remove();
             $('#imgPreviewClose').remove();
         });
         
         var imgPreview = $('<img>')
         .attr('src',$(this).attr('href'))
         .attr('id','imgPreview')
         .hide()
         .appendTo($("body"))
         .load(function(){
             if($(window).width() / $(window).height() < $(this).width() / $(this).height()){
                 var width = ($(window).width() - 200);
                 var height = Math.floor(width * $(this).height() / $(this).width());
             } else {
                 var height = ($(window).height() - 60);
                 var width = Math.floor(height * $(this).width() / $(this).height());
             }
             
             $(this).css({
                'height'   : height + 'px'
               ,'width'    : width + 'px'
               ,'position' : 'absolute'
               ,'top'      : (($(window).height() - height) / 2) + 'px'
               ,'left'     : (($(window).width() - width) / 2) + 'px'
               ,'cursor'   : 'pointer'
             })
             .show()
             .click(function(){
                 $('#imgOverlay').remove();
                 $('#imgPreview').remove();
                 $('#imgPreviewClose').remove();
             });
             
             var imgPreviewClose = $('<img>')
             .attr('src',WEBROOT + '/public/themes/images/close.gif')
             .attr('id','imgPreviewClose')
             .css({
                'position' : 'absolute'
               ,'top'      : (($(window).height() - height) / 2) + 'px'
               ,'left'     : ((($(window).width() - width) / 2) + width + 15) + 'px'
               ,'cursor'   : 'pointer'
             })
             .appendTo($("body"))
             .click(function(){
                 $('#imgOverlay').remove();
                 $('#imgPreview').remove();
                 $('#imgPreviewClose').remove();
             });
         });
             
         /*
         var loading = $('<div>')
         .attr('id','loading')
         .css({
              'width' : $(window).width() + 'px'
             ,'height' : $(window).height() + 'px'
             ,'position' : 'absolute'
             ,'top' : '0px'
             ,'left' : '0px'
             ,'background' : 'url(' + WEBROOT + '/public/themes/images/ajax-loader.gif) no-repeat 400px 300px #000'
             ,'opacity' : 0.8
         })
         .appendTo($("body"))
         .fadeIn(500);
         
         $("#body-bg").remove();
         
         var bgImg = $('<img>').hide().attr('src',$(this).attr('href')).attr('id','body-bg').prependTo($("body"));
         updateBgImageSize();
         bgImg.show();
         loading.fadeOut(200,function(){
             resizeBody(0);
         });
         
         */
         
         return false;
    });
});


function updateBgImageSize(){
    var pomer = $(window).width() / $(window).height();
    if(pomer < bgImg.pomer){
        $('#body-bg').css({
            'height' : '100%'
           ,'width' : 'auto'
        });
    } else {
        $('#body-bg').css({
            'width' : '100%'
           ,'height' : 'auto'
        });
    }
}

function resizeBody(odsadenie){
    $("#body").css('height' , ($(window).height() - $("#kontakt").outerHeight() - 6) + 'px');    
    if($.browser.msie && $.browser.version.substr(0,1) < 8){
    	$("#body").css('width' , ($(body).width() - parseInt(odsadenie)) + 'px');
    }
}

