$(function () {
    //podpora zmeny velkosti bgbox-u
	var $bgbox = $('#bgbox');
	if ($bgbox.length) {
	    $bgbox.find('img.bg').fadeTo(0,0);
        function bgboxChangeView() {
            var h = $(window).height();
            //od vysky odpocitame vysku spodku a vrchneho menu
            $bgbox.height(h - $('#nav-panel').height() - $('#bottom-panel').height() - $('#textbox').height() - 28);
            //rozmery obrazku
            var $imgs = $bgbox.find('img.bg');
            $imgs.each(function () {
                var $img = $(this);
                if (this.complete) {
                    bgboxChangeBgView.call(this);
                } else {
                    $img.load(bgboxChangeBgView);
                }
            });
        }
        function bgboxChangeBgView() {
            var $img = $(this);
            var bgboxHeight = $bgbox.height();
            var originalImage = new Image();
            originalImage.src = $img.attr('src');
            var scaleFactor = originalImage.width / originalImage.height;
            var resizeW = Math.max(900, Math.max(originalImage.width,100));
            var resizeH = resizeW / scaleFactor;
            if (resizeH > bgboxHeight) {
                resizeH = bgboxHeight;
                resizeW = resizeH * scaleFactor;
            }
            $img.width(resizeW).height(resizeH);
            $img.css({
                position: 'absolute',
                top: 0,
                left: ($bgbox.width() - resizeW) / 2
            });
            if ($(this).index() == 0) $img.fadeTo('slow',1);
        }
        $bgbox.css('paddingBottom', 0);
        window.setTimeout(bgboxChangeView,100);
        //onwindow resize
        $(window).on('resize.bgbox', bgboxChangeView);
        //on orientation change - iPad
        if (window.addEventListener) {
            window.addEventListener('orientationchange', bgboxChangeView, true);
        }
        //iPhone = hide URL bar - nem funguje
        // window.setTimeout(function() { window.scrollTo(0, 1) }, 100);
        //bg image switcher
        $('#textbox div.switcher a.switch').click(function () {
            var $imageToDisplay = $bgbox.find('img.bg').eq($(this).index());
            var $currentImage = $bgbox.find('img.bg:visible');
            $currentImage.fadeOut('fast', function () {
                $imageToDisplay.fadeTo('slow', 1);
            });
        });
    }
    var $bgboxFull = $('#bgboxFull');
	if ($bgboxFull.length) {
	    $bgboxFull.find('img.bg').fadeTo(0,0);
        function bgboxFullChangeView() {
            var h = $(window).height();
            //od vysky odpocitame vysku spodku a vrchneho menu
            $bgboxFull.height(h - $('#nav-panel').height() - $('#bottom-panel').height());
            $bgboxFull.css('overflow', 'hidden');
            //rozmery obrazku
            var $imgs = $bgboxFull.find('img.bg');
            $imgs.each(function () {
                var $img = $(this);
                if ($img.prop('complete')) {
                    bgboxFullChangeBgView.call(this);
                } else {
                    $img.load(bgboxFullChangeBgView);
                }
            });
        }
        function bgboxFullChangeBgView() {
            var $img = $(this);
            var bgboxHeight = $bgboxFull.height();
            var originalImage = new Image();
            originalImage.src = $img.attr('src');
            var scaleFactor = originalImage.width / originalImage.height;
            var resizeW = $bgboxFull.width();
            var resizeH = resizeW / scaleFactor;
            $img.width(resizeW).height(resizeH);
            $img.css({
                position: 'absolute',
                top: 0,
                left: ($bgboxFull.width() - resizeW) / 2
            });
            if ($(this).index() == 0) $img.fadeTo('slow',1);
        }
        $bgboxFull.css('paddingBottom', 0);
        window.setTimeout(bgboxFullChangeView,100);
        //onwindow resize
        if (window.addEventListener) {
            $(window).on('resize.bgboxFull', bgboxFullChangeView);
        }
        //on orientation change - iPad
        window.addEventListener('orientationchange', bgboxFullChangeView, true);
    }
});
