(function($){
    $.fn.jqRegionSelect = function()
    {
        $("#mapOfRegions").removeClass("hidden");
        $("#mapOfRegions").css("right",$(document).width()/2-495+"px");
        $("#mapOfRegions").toggle();
        $("#mapOfRegions .mor_content").fadeTo(0, 0);

        $(".mor_button").click(function(){

            var bg = $("<div/>")
            .addClass("bgOverlay")
            .height($(document).height())
            .width($(document).width())
            .click(function(){
                mapToggleOff();
                $(this).remove();

            });

            mapToggleOn();

            $("body").append(bg);

            return false;
        });

        $(document).keyup(function(event){
            if ($('.bgOverlay').hasClass("bgOverlay"))
            {
                mapToggleOff();
                $('.bgOverlay').remove();
            }
        });


        var mapToggleOn = function()
        {
            $("#mapOfRegions").toggle(500,function(){
                $("#mapOfRegions .mor_content").fadeTo(200, 1);
            });
        }

        var mapToggleOff = function()
        {
            $("#mapOfRegions .mor_content").fadeTo(200, 0,function(){
                $("#mapOfRegions").toggle(500);
            })
        }
    }
})(jQuery);

$(document).ready(function(){


    $("#tabs").find('.hidden').each(function(){
        $(this).removeClass('hidden');
    });


    // Zobrazi detailni boxik o firme
    $(".showMore").mouseover(function(){
        var item = $(this).find(".moreInfo");
        item.removeClass('hidden');
        item.addClass('open');

    });
    $(".showMore").mouseleave(function(){
        $(this).find(".moreInfo").addClass('hidden');
        $(this).find(".moreInfo").removeClass('open');

    });

    $('.eshop_order, .eshop_producer').change(function(){
        $("form[name=fiter]").submit();
    });


    var imgs = new Object();
    imgs.img_stin16x9_1_i = "169_nf_s1.gif";
    imgs.img_stin16x9_2_i = "169_nf_s2.gif";
    imgs.img_stin16x9_3_i = "169_nf_s3.gif";
    imgs.img_stin16x9_4_i = "169_nf_s4.gif";
    imgs.img_stin16x9_5_i = "169_nf_s5.gif";
    imgs.img_stin4x3_1_i = "43_nf_s1.gif";
    imgs.img_stin4x3_2_i = "43_nf_s2.gif";
    imgs.img_stin4x3_3_i = "43_nf_s3.gif";
    imgs.img_stin4x3_4_i = "43_nf_s4.gif";
    imgs.img_stin4x3_5_i = "43_nf_s5.gif";
    imgs.def = "43_nf_s5.gif";

    $('img').error(function() {
        //alert(imgs[$(this).parent().attr("class")].length);

        if (imgs.hasOwnProperty($(this).parent().attr("class")))
            $(this).attr("src","http://r.ceskestavby.cz/imgs/imgNotFound/"+imgs[$(this).parent().attr("class")]);
        else
            $(this).remove();
    //$(this).attr("src","http://r.ceskestavby.cz/imgs/imgNotFound/"+imgs.def);
    });

    $(".set_test").click(function(){
        if($.cookie("set_test") == 'true')
        {
            $.cookie("set_test",'false');
        }
        else
        {
            $.cookie("set_test",'true');
        }

    });

    $(".mainMenu a").mouseenter(function(){
        var menu = $(this).attr("class");

        $(this).data('timeout',setTimeout(function() {
            if (!$(".subMenuArea .subMenu."+menu).hasClass("active"))
            {
                $(".subMenuArea .subMenu.active").css("z-index",0).removeClass("active").delay(100).slideUp(0);
                $(".subMenuArea .subMenu."+menu).css("z-index",50).fadeIn(50).addClass("active");
            }
        },200));
    });

    $(".mainMenu a").mouseleave(function(){
        clearTimeout($(this).data('timeout'));
    });

    $("#rollUpMenu").mouseleave(function(){

        if (!$(".subMenuArea .subMenu.active").hasClass("menu0"))
        {
            $(".subMenuArea .subMenu.active").css("z-index",0).removeClass("active").delay(100).slideUp(0);
            $(".subMenuArea .subMenu.menu0").css("z-index",50).fadeIn(50).addClass("active");
        }
    });


});



// Zobrazovani a skryvani mapy
var openMap = function()
{
    var bg = $("<div/>")
    .addClass("bgOverlay")
    .height($(document).height())
    .width($(document).width())
    .click(function(){
        mapToggleOff();
        $(this).remove();

    });

    mapToggleOn();

    $("body").append(bg);
}

var mapToggleOn = function()
{
    $("#mapOfRegions").toggle(500,function(){
        $("#mapOfRegions .mor_content").fadeTo(200, 1);
    });
    return false;
}

var mapToggleOff = function()
{
    $("#mapOfRegions .mor_content").fadeTo(200, 0,function(){
        $("#mapOfRegions").toggle(500);
    });
    $(".bgOverlay").remove();
}


// Vyvola mapu pokud neni nastaven zadny region
$("a.regHref").click(function(){
    alert('asd');
    var region = $('.mpmenu').attr('id')
    if (region)
    {
        return true;
    }
    else
    {
        openMap();
        return false;
    }
});

var serveRedirect = function(region)
{
    var base = new Array();
    base[0] = ['http://www.ceskestavby.cz/'];
    base[1] = ['http://jiho.ceskestavby.cz/'];
    base[2] = ['http://stredo.ceskestavby.cz/'];
    base[3] = ['http://zapado.ceskestavby.cz/'];
    base[4] = ['http://severo.ceskestavby.cz/'];
    base[5] = ['http://vychodo.ceskestavby.cz/'];
    base[6] = ['http://jiho.moravskestavby.cz/'];
    base[7] = ['http://severo.moravskestavby.cz/'];

    window.location = base[region];
}
