// hover podmenu
function hovEff(el, swtch) {
    var $curr = $(el);
    var rel = $curr.attr('rel');
    var activemenu = 'item-' + indexMenu;

    if (rel == activemenu) {
        //return;
    }
    
    $curr.toggleClass("hover", swtch);
    if ($curr.hasClass("pre-active")) {
        if ($curr.hasClass("hover")) {
            $curr.removeClass("pre-active");
            $curr.addClass("pre-active-hover");
        }
    } else if ($curr.hasClass("pre-active-hover")) {
        $curr.removeClass("pre-active-hover");
        $curr.addClass("pre-active");
    }

    $pre = $curr.parent().prev().children();
    $pre.toggleClass("pre-hover", swtch);
    if ($pre.hasClass("active")) {
        if ($pre.hasClass("pre-hover")) {
            $pre.removeClass("active");
            $pre.addClass("active-pre-hover");
        }
    } else if ($pre.hasClass("active-pre-hover")) {
        $pre.removeClass("active-pre-hover");
        $pre.addClass("active");
    }
}

// automaticke stridany fotek
// index pro aktualni foto
var indexPhoto = 0;
// rychlost animace fotky
var speedAnimate = 3000;
// animace fotek je v behu = true
var photo_animate;
// aktivni hover na menu
var photo_hover = false;
// seznam fotek pro stridani
var images = new Array(
    '/media/index/01-investicni-vystavba.jpg',
    '/media/index/02-prumyslova-vystavba.jpg',
    '/media/index/03-bytove-stavby.jpg',
    '/media/index/04-vodohospodarske-stavby.jpg',
    '/media/index/05-dopravni-stavby.jpg',
    '/media/index/06-urbanismus.jpg',
    '/media/index/07-interiery.jpg'
);
// index pro aktualini polozku menu
var indexMenu = 1;
// index pro predchozi aktualini polozku menu
var indexPrevMenu = images.length;

function runAnimate()
{
    indexPhoto = indexPhoto + 1;
    indexPrevMenu = indexPhoto;

    if (indexPhoto >= images.length) {
        indexPhoto = 0;
    }

    indexMenu = indexPhoto + 1;

    $("#main_banner").fadeOut("slow", function () {
        $('#main_banner').css('background-image', 'url("' + images[indexPhoto] + '")');
        $("#main_banner").fadeIn("slow");
        $('#second_menu li a.item-' + indexMenu).addClass("active");
        $('#second_menu li a.item-' + indexMenu).parent().prev().children().addClass("pre-active");
        $('#second_menu li a.item-' + indexPrevMenu).removeClass("active");
        $('#second_menu li a.item-' + indexPrevMenu).parent().prev().children().removeClass("pre-active");

        if (!photo_hover) {
            photo_animate = setTimeout(function() { runAnimate(); }, speedAnimate);
        }
    });
}

$(document).ready(function()
{
    // automaticke stridany fotek
    photo_animate = setTimeout(function() { runAnimate(); }, speedAnimate);

    // stop / start animace fotky
    $("#second_menu").hover(
        function() {
            // zastaveni animace, pokud na ni uzivatel najel mysi
            clearTimeout(photo_animate);
            photo_hover = true;
        },
        function() {
            // znovuspusteni animace, pokud na ni uzivatel uz neni mysi
            //indexPhoto = indexPhoto - 1;
            photo_animate = setTimeout(function() { runAnimate(); }, speedAnimate);
            photo_hover = false;
        }
    );

    // stridani fotek po hoveru na podmenu
    $('#second_menu li a').mouseover(function() {
        $('#second_menu li a').removeClass("active");
        //$('#second_menu li a').removeClass("pre-hover");
        $('#second_menu li a').removeClass("active-pre-hover");
        $('#second_menu li a').parent().prev().children().removeClass("pre-active");
    });
    
    $('#second_menu li a.item-1').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[0] + '")');
    }).mouseout(function(){
        indexPhoto = 0;
    });

    $('#second_menu li a.item-2').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[1] + '")');
    }).mouseout(function(){
        indexPhoto = 1;
    });

    $('#second_menu li a.item-3').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[2] + '")');
    }).mouseout(function(){
        indexPhoto = 2;
    });

    $('#second_menu li a.item-4').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[3] + '")');
    }).mouseout(function(){
        indexPhoto = 3;
    });

    $('#second_menu li a.item-5').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[4] + '")');
    }).mouseout(function(){
        indexPhoto = 4;
    });

    $('#second_menu li a.item-6').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[5] + '")');
    }).mouseout(function(){
        indexPhoto = 5;
    });

    $('#second_menu li a.item-7').mouseover(function() {
        $('#main_banner').css('background-image', 'url("' + images[6] + '")');
    }).mouseout(function(){
        indexPhoto = 6;
    });
});

