 
/* -------------------------------------------------------------------------- */
/* mame.napilno.site.js
/* - zakladni JS soubor pro stranky mame.napilno
/* - !! vyzaduje jQuery
/* (c) Filip Novak, mame.napilno.cz, filip.novak@napilno.cz
/* -------------------------------------------------------------------------- */

// zabraneni konfliktu s Mootools v QuickEdit
// po te je nutno misto aliasu "$" pouzit "jQuery"
jQuery.noConflict();


/* navigace presunuta do horni casti stranky - start ------------------------ */

    /* inicializace menu - start -------------------------------------------- */
    jQuery(document).ready(function () {
        
        // 1. level
        jQuery('#navigationContainer > ul > li').addClass('firstLevel');

        // 2. level
        jQuery('#navigationContainer li.firstLevel > ul > li').addClass('secondLevel');

        // 3. level
        jQuery('#navigationContainer li.secondLevel > ul > li').addClass('thirdLevel');

        // vsechna prvni li (posledni "last" pridava Wayfinder v zakladu sam)
        jQuery('#navigationContainer ul li:first-child').addClass('first');

        // akce pro 1. level - start
        jQuery('#navigationContainer li.firstLevel > a').hover(
            function () {
                mn_menu_active = this;

                jQuery('#navigationContainer li.firstLevel > ul').slideUp('speed',
                    function (active_menu) {
                        window.setTimeout(
                        function () {
                            jQuery(window.mn_menu_active).parent().children('ul').slideDown('slow');
                        }, 150);
                    }
                );
            }
        );
        // akce pro 1. level - end

        // akce pro 2. level - start
        jQuery('#navigationContainer li.secondLevel > a').each( function () {
            if (jQuery(this).parent().children('ul').size()) { // jen pokud obsahuje submenu 3. urovne
                jQuery(this).hover(function () {
                    if (jQuery(this).parent().children('ul').css('display') == 'block') {
                        jQuery(this).parent().children('ul').slideUp('slow');
                    }
                    else {
                        jQuery(this).parent().children('ul').slideDown('slow');
                    }
                });
            }
        });
        // akce pro 2. level - end

        // akce pro click - start
        jQuery('#navigationContainer a').click(function (e) {

            e.preventDefault();
            window.menuClickTarget = this.href;

            // zhasnuti menu 3. urovne
            jQuery('#navigationContainer li.thirdLevel').parent().slideUp('slow',
                function () {
                    window.setTimeout(
                        function () {
                           jQuery('#navigationContainer li.secondLevel').parent().slideUp('slow');
                        }, 150
                    );
                }
            );

            window.setTimeout(getAjaxContent, 500);

        });
        // akce pro click - end

        // prida se trida "toTop" a tim se na zaklade CSS menu zmeni a premisti
        jQuery('#navigationContainer > ul').addClass('toTop');

        // zhasnuti submenu (2. uroven)
        jQuery('#navigationContainer li.firstLevel > ul').css('display', 'none');

        // zhasnuti submenu (3. uroven)
        jQuery('#navigationContainer li.secondLevel > ul').css('display', 'none');
        
    });
    /* inicializace menu - end ---------------------------------------------- */

/* navigace presunuta do horni casti stranky - end -------------------------- */



/* vytvoreni okna pro AJAX obsah - end --------------------------------------- */

function ajaxContainer() {

    window.maxActualZIndex = maxZIndex();

    // zesvetleni stranky - start
    jQuery('#pageContainer').after('<div id="pageMasker"></div>');
    jQuery('#pageMasker').css('display', 'none');
    jQuery('#pageMasker').css('width', jQuery('body').width());
    jQuery('#pageMasker').css('height', jQuery('body').height());
    jQuery('#pageMasker').css('position', 'absolute');
    jQuery('#pageMasker').css('left', 0);
    jQuery('#pageMasker').css('top', 0);
    jQuery('#pageMasker').css('margin', 0);
    jQuery('#pageMasker').css('padding', 0);
    jQuery('#pageMasker').css('z-index', window.maxActualZIndex+1000);
    jQuery('#pageMasker').css('opacity', 0.75);
    jQuery('#pageMasker').fadeIn('slow', function () {

        // prostor pro obsah - start
        var a_width  = 858;
        var a_height = jQuery(document).height() - 220 - 50;
        var a_left   = parseInt((parseInt(jQuery('body').width()) - a_width) / 2);
        var a_top    = 220;

        var ajaxMiddle_height = a_height - 34 - 13;

        var html = '<div id="ajaxContentFrame"><div id="ajaxTop"></div><div id="ajaxMiddle"></div><div id="ajaxBottom"></div></div>';
        
        jQuery('#pageMasker').after(html);
        jQuery('#ajaxContentFrame').css('width', a_width + 'px');
        jQuery('#ajaxContentFrame').css('height', a_height + 'px');
        jQuery('#ajaxContentFrame').css('position', 'absolute');
        jQuery('#ajaxContentFrame').css('left', a_left + 'px');
        jQuery('#ajaxContentFrame').css('top', a_top + 'px');
        jQuery('#ajaxContentFrame').css('z-index', window.maxActualZIndex+2000);

        jQuery('#ajaxMiddle').css('height', ajaxMiddle_height + 'px');
        return;

    });

    // zesvetleni stranky - end

}
/* vytvoreni okna pro AJX obsah - end --------------------------------------- */



/* ziskani obsahu stranky pres AJAX - start --------------------------------- */
function getAjaxContent() {

    if (typeof(window.menuClickTarget) == 'undefined') return;

    //ajaxContainer();
    //jQuery('#ajaxMiddle').html('<p>ano</p>');
    //jQuery('#ajaxContentFrame').load(window.menuClickTarget);
    //alert(window.menuClickTarget);

    // docasna funkce presmeruje na puvodni cil linku
    
    window.setTimeout(function () {
        window.location = window.menuClickTarget;
    }, 300);
    
    

}
/* ziskani obsahu stranky pres AJAX - end ----------------------------------- */


/* vraci nejvetsi pouzity z-index - start ----------------------------------- */
function maxZIndex() {
    zIndexMax = 0;
    jQuery('*').each(function () {
        var zIndex = parseInt(jQuery(this).css('zIndex'));
        if (zIndex != NaN && zIndex > zIndexMax) {
            zIndexMax = zIndex;
        }
    });
    return zIndexMax;
}
/* vraci nejvetsi pouzity z-index - end ------------------------------------- */




/* PORTFOLIO - start -------------------------------------------------------- */


    /* inicializace - start ------------------------------------------------- */
    jQuery(document).ready(function () {

        // jedna se o galerii?
        if (jQuery('h2.porfolioExhibits + ul li').size() == 0) return;

        // vytvoreni prostoru pro velkou fotku
        jQuery('h1').after('<p id="portfolioBigImage" class="preloader"></p>');

        // nastaveni akce pro seznam obrazku
        jQuery('h2.porfolioExhibits + ul li a').each( function () {
            
            // click na link
            jQuery(this).click(function (e) {

                e.preventDefault();
                var hires_link = this;

                // pokud existoval jiz zobrazeny hires > zhasne ho
                if (jQuery('#portfolioBigImage img').size() != 0) {
                    jQuery('#portfolioBigImage img').fadeOut('slow', function () {
                        jQuery('#portfolioBigImage').empty();
                        loadHiresImage(hires_link);
                    });
                }

                // pokud ne > rovnou nacita hires
                else {
                    loadHiresImage(hires_link);
                }
            });

        });
        
        // prvni zavolani zobrazeni hiresu
        jQuery('h2.porfolioExhibits + ul li:first a').trigger('click');     

    });
    /* inicializace - start ------------------------------------------------- */



    /* getImageHeight - start ----------------------------------------------- */
    // vraci vysku predaneho obrazku pro zmenu velikosti na predanou sirku
    function getImageHeight(img_obj, target_width) {
        return target_width * (img_obj.height / img_obj.width);
    }
    /* getImageHeight - end ------------------------------------------------- */



    /* getImageWidth - start ------------------------------------------------ */
    // vraci vysku predaneho obrazku pro zmenu velikosti na predanou sirku
    function getImageWidth(img_obj, target_height) {
        return target_height * (img_obj.width / img_obj.height);
    }
    /* getImageWidth - end -------------------------------------------------- */


    /* loadHiresImage - start ----------------------------------------------- */
    // nacte novy hires obrazek do urceneho prostoru
    function loadHiresImage(hires_image_link) {

        // novy objekt hires obrazku
        var hires = new Image;
        hires.src = hires_image_link;

        jQuery(hires).load(function () {

            var hires_width = jQuery('#portfolioBigImage').width();
            var hires_height = getImageHeight(hires, hires_width);

            jQuery(hires).width(hires_width + 'px');
            jQuery(hires).height(hires_height + 'px');
            jQuery(hires).attr('title', jQuery(hires_image_link).children('img').attr('title'));
            
            jQuery('#portfolioBigImage').animate({
                height: hires_height + 'px'
            }, 'slow', 'linear', 
                function () {
                    jQuery('#portfolioBigImage').append(hires);
                    jQuery('#portfolioBigImage').children('img').css('display', 'none');
                    jQuery('#portfolioBigImage').children('img').fadeIn('slow', function () {
                        return;
                    });
                }
            );

        });

    }
    /* loadHiresImage - end ------------------------------------------------- */

/* PORTFOLIO - end ---------------------------------------------------------- */
