/**
 * Hlavni javascriptovy soubor, obsahujici validaci formularu pomoci jquery
 * @author Krystof Honsu (honsukry@fel.cvut.cz)
 */

$(function () {

    // colorbox gallery
    $('a.gallery').colorbox();

    $(".datepicker").each(function () {
        $(this).datepicker();
    });

    // CSS images preloader
    $.preloadCssImages();

    // reference - loga
    $(".reference_logo").each(function () {
        var src = $(this).attr("src");
        src = src.replace(".jpg","-active.jpg")
        $(this).attr("src", src);

        src = $(this).attr("src");
        src = src.replace("-active.jpg",".jpg")
        $(this).attr("src", src);
    })
    $(".reference_logo").mouseover(function () {
        var src = $(this).attr("src");
        src = src.replace(".jpg","-active.jpg")
        $(this).attr("src", src);
    });
    $(".reference_logo").mouseout(function () {
        var src = $(this).attr("src");
        src = src.replace("-active.jpg",".jpg")
        $(this).attr("src", src);
    });

    /**
     * Order form
     */

    // ovladani zobrazeni policka, pokud se jedna o free tour
    $("#guideInput").hide();
    $("#freeToggle").click(function () {
        $("#guideInput").toggle();
    })
    // ovladani zobrazeni policka pro cas, pokud jde o private tour
    $("#timeInput").hide();
    $("#timeToggle").click(function () {
        $("#timeInput").toggle();
    })

    // a.blank jako target blank
    $("a.blank").each(function () {
        $(this).click(function () {
            window.open(this.href);
            return false;
        });
    });

    $("#sendMail").validate({
        rules: {
            name: "required",
            mail: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "Zadejte Vaše jméno!",
            mail: "Zadejte platný e-mail!"
        }
    });
    
    // submenu
    $('#menu_items > li').bind('mouseover', jsddm_open);
    $('#menu_items > li').bind('mouseout',  jsddm_timer);

    /**
     * We use the initCallback callback
     * to assign functionality to the controls
     */
    function mycarousel_initCallback(carousel) {

        jQuery('.jcarousel-scroll select').bind('change', function() {
            carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
            return false;
        });

        jQuery('#mycarousel-next').bind('click', function() {
            carousel.next();
            return false;
        });

        jQuery('#mycarousel-prev').bind('click', function() {
            carousel.prev();
            return false;
        });

        // Disable autoscrolling if the user clicks the prev or next button.
        jQuery('#mycarousel-next').bind('click', function() {
            carousel.startAuto(0);
        });

        jQuery('#mycarousel-prev').bind('click', function() {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        jQuery("#mycarousel").hover(function() {
            carousel.stopAuto();
        }, function() {
            carousel.startAuto();
        });

    };

    // Ride the carousel...
    jQuery(document).ready(function() {
        jQuery("#mycarousel").jcarousel({
            scroll: 1,
            auto: 8,
            //size: 5,
            wrap: 'last',
            initCallback: mycarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    });

    

    /**
     * We use the initCallback callback
     * to assign functionality to the controls
     */
    function footercarousel_initCallback(carousel) {

        jQuery('.jcarousel-scroll select').bind('change', function() {
            carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
            return false;
        });

        jQuery('#footerbox-next').bind('click', function() {
            carousel.next();
            return false;
        });

        jQuery('#footerbox-prev').bind('click', function() {
            carousel.prev();
            return false;
        });

        // Disable autoscrolling if the user clicks the prev or next button.
        jQuery('#footerbox-next').bind('click', function() {
            carousel.startAuto(0);
        });

        jQuery('#footerbox-prev').bind('click', function() {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        jQuery("#footerbox").hover(function() {
            carousel.stopAuto();
        }, function() {
            carousel.startAuto();
        });

    };

    // Ride the carousel...
    jQuery(document).ready(function() {
        jQuery("#footerbox").jcarousel({
            scroll: 5,
            //auto: 2,
            wrap: 'last',
            initCallback: footercarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    });

});


var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
    if(closetimer)

    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = jsddm_close;



