/// <reference path="jquery-1.3.2.min-vsdoc.js" />
var weekMenuWork = false;

$(function() {
    $('.weekmenubutton').click(function(event) {
        event.preventDefault();
        if (weekMenuWork) {
            return;
        }

        weekMenuWork = true;

        $(document).unbind('click.weekmenu');
        if ($('.den').index($('.den.show')) == -1) {
            ShowWeekMenu(event);
        }
        else {
            HideWeekMenu(event);
        }
    });

    $('.den').click(function(event) {
        event.stopPropagation();
    });

    $('.denin').css('opacity', 0.94);

    var start = $.cookie('jcarousel_akcninabidka');
    if (start == null) {
        start = 1;
    }
    start = Number(start);
    $('.panel-odsazeni').removeClass('panel-odsazeni');
    $('#galleri .nodisplay').removeClass('nodisplay');
    $('#galleri').jcarousel({
        // Configuration goes here
        start: start,
        vertical: false,
        scroll: 1,
        auto: 5,
        wrap: 'both',
        initCallback: jcarousel_initCallback,
        itemFirstOutCallback: jcarousel_itemFirstOutCallback,
        itemFirstInCallback: jcarousel_itemFirstInCallback
    });
});

function HideWeekMenu(event) {
    var zobrazeny = $('.den.show');
    if (zobrazeny.lenght < 1) {
        $('.weekmenubutton').removeClass('active');
        weekMenuWork = false;
        return;
    }

    zobrazeny.removeClass('show');
    zobrazeny.slideUp(300, function() {

        if ($(event.target).hasClass('active')) {
            $('.weekmenubutton').removeClass('active');
            weekMenuWork = false;
        }
        else {
            $('.weekmenubutton').removeClass('active');
            ShowWeekMenu(event);
        }
    });
};

function ShowWeekMenu(event) {
    $('.den').hide();
    $('.weekmenubutton').removeClass('active');
    var selected = $('.weekmenubutton').index(event.target);
    $(event.target).addClass('active');
    $('#den' + selected).addClass('show');
    
    $('#den' + selected).slideDown(450, function() {
        weekMenuWork = false;
    });

    setTimeout(function() {
        $(document).bind('click.weekmenu', null, function(event) {
            HideWeekMenu(event);
        });
    }, 0);
};

function jcarousel_initCallback(carousel) {
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.container.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function jcarousel_itemFirstOutCallback(carousel, panel, index, d) {
    //if (d == 'next') {
    //	$.cookie('jcarousel_akcninabidka', index + 1);
    ///}
}

function jcarousel_itemFirstInCallback(carousel, panel, index, d) {
    //if (d == "prev") {
    $.cookie('jcarousel_akcninabidka', index, { path: '/' });
    //}
}
