var galleryScrollHeight = 350;

function checkScrollUp() {
    var top = $('.gallery-scroll').scrollTop();
    if (top <= 0) {
        $('.arrow-up').css('opacity', 0.4);
    } else {
        $('.arrow-up').css('opacity', 1);
    }
}

function checkScrollDown() {
    var top = $('.gallery-scroll').scrollTop();
    if (top >= $('.gallery-scroll-content').height() - galleryScrollHeight) {
        $('.arrow-down').css('opacity', 0.4);
    } else {
        $('.arrow-down').css('opacity', 1);
    }
}
var openTimeout = null;

function openMenu(elm) {
    var hasVisible = false;
    $(elm).find("ul").each(function() {
        if ($(this).is(":visible")) {
            hasVisible = true;
        }
    });
    if (hasVisible) {
        return;
    }

    $(">ul", elm).css({top:0});

    var parents = $(elm).parents("ul");


    var w = (parents.size() - 1) * 220;
    if ($(elm).find("ul").size() == 0) {
        w = (parents.size() - 2) * 220;
    }
    var tw = $(elm).parents(".program-sitemap-submenu").data("tw");
    if (tw > w) {
        w = tw;
    } else {
        $(elm).parents(".program-sitemap-submenu").data("tw", w);
    }

    $(elm).parents("ul").eq(0).find("ul").stop().clearQueue().hide();


    var h = $(">ul", elm).data("h");
    if (typeof h == "undefined") {
        h = 0;

    }
    //$(elm).parents("ul").eq(0).find("a").removeClass("active-menu");
    //$(">a", elm).addClass("active-menu");

    $(elm).parents("ul").each(function(i) {
        $(this).stop().clearQueue();
        if (typeof $(this).data("h") != "undefined") {
            if ($(this).data("h") > h) {
                h = $(this).data("h");
            }
        }

    });
    var l = $(".container").offset().left + $(".container").outerWidth(false) -
        ($(elm).parents(".program-sitemap-submenu").offsetParent().offset().left + w + 220);

    if (l > 0) {
        l = 0;
    }

    $(elm).parents(".program-sitemap-submenu").animate({paddingRight:w + "px", height:h + "px", left: l + "px"}, function() {

    });
    $(">ul", elm).css({left:(220) + "px"}).show();


}

function initMenu() {
    var timeout = null;
    $(".program-menu-root ul").each(
        function() {
            $(this).data("h", $(this).innerHeight(true));
        }).hide();
    $(".program-sitemap-submenu").append('<div class="left-shadow"/><div class="right-shadow"/>');

    $(".program-sitemap-submenu li").each(function() {
        if ($("ul", this).size() > 0) {
            $(this).addClass("has-elements");
        }
    });


    $(".program-menu-root").mouseenter(
        function() {
            $(".program-menu-root ul").css({paddingRight:0, left:0}).hide().each(function() {
                $(this).css({height:$(this).data("h") + "px"});
            });
            $(">ul", this).show();
            if (timeout != null) {
                clearTimeout(timeout);
            }
            $(".program-menu-root a").removeClass("active-menu");
        }).mouseleave(function() {
            timeout = setTimeout(function() {
                $(".program-menu-root ul").stop().clearQueue().hide();
                $(".program-sitemap-submenu").data("tw", 0);
            }, 500);
        });

    $(".program-menu-root li").mousemove(function(event) {
        event.stopPropagation();
        $(".active-menu").removeClass("active-menu");
        $(">a", this).addClass("active-menu");
        $(this).parents("li").each(function(){
            $(">a", this).addClass("active-menu");
        });
    });

    $(".program-menu-root li").mouseenter(function(event) {
        event.stopPropagation();
        if (openTimeout != null){
            clearTimeout(openTimeout);
            openTimeout = null;
        }
        if (openTimeout == null){
            var elm = this;

            openTimeout = setTimeout(function(){
                openMenu(elm);
            }, 300);
        }


    });
    $(".program-sitemap-submenu").mouseleave(function() {
        //$(this).animate({paddingRight:0 + "px", height:$(this).data("h") + "px", left:0});
        //$("ul", this).hide();
        //$(".program-menu-root a").removeClass("active-menu");
//        console.log("leave submenu ");
    });
    $(".category-title").mouseenter(
        function() {
            $(".program-menu-root").each(function() {
                $(".program-menu-root > ul").css({paddingRight:0, left:0}).show().each(function() {
                    $(this).css({height:$(this).data("h") + "px"});
                });

                if (timeout != null) {
                    clearTimeout(timeout);
                }
                $(".program-menu-root a").removeClass("active-menu");
            });
        }).mouseleave(function() {
            timeout = setTimeout(function() {
                $(".program-menu-root ul").stop().clearQueue().hide();
            }, 500);
        });
    $(".home-slide").css({"visibility":"visible"});
}


$(document).ready(function() {
    $("a[href^='http']").each(function() {
        if ($(this).attr('href').substr(0, 17) != 'http://www.brinox') {
            $(this).attr('target', '_blank');
        }
    });

    $('ul.left-menu > li').find('ul,hr').hide();
    $('ul.left-menu .current').parent().find('ul,hr').show();


    $("#eumap area").click(function() {
        $(".map").hide();

        if ($(this).attr("href").substring(0, 1) == "#") {
            $(".map-" + $(this).attr("href").substring(1)).show();
        } else {
            return true;
        }
        return false;
    });
    $(".map").hide();


    /*
     $('.category .category-title').click(function(){
     $('.sitemap-menu-ul', this).hide();
     $('.sitemap-menu').addClass('hover');
     $('.sitemap-menu-ul', this).fadeIn(1500);
     return false;
     });
     */


    $('.poll-holder form').submit(function() {
        var data = $(this).serialize();
        var pollObj = $(this);
        if (data.length > 0) {
            $.cookie('poll' + $(pollObj).attr('action'), true, {
                expires:2,
                path: '/'
            });
        }

        $('.poll-button input', this).attr('disabled', true);
        $.post($(this).attr('action'), data, function(result) {
            $('.poll-button', pollObj).remove();

            var pollResult = eval(result);
            $('.poll-row', pollObj).each(function(i) {
                $(this).empty().append('<div class="option-name">' + pollResult[i][2] + ' (' + pollResult[i][1] + '%)' + '</div>');
                $(this).append('<div class="option-bar">');
                $('.option-bar', this).css({
                    'background-color':'red',
                    'width':0
                }).animate({
                        width:pollResult[i][1] + '%'
                    });
            });
        });
        return false;
    });
    var pollAction = 'poll' + $('.poll-holder form').attr('action');
    if ($.cookie(pollAction) != null) {
        $('.poll-holder form').trigger('submit');
    }
    /*
     $('.gallery').jcarousel({
     visible:4,
     vertical: true
     });
     */

    $('.arrow-down').click(function() {
        var top = $('.gallery-scroll').scrollTop() + galleryScrollHeight;
        $('.gallery-scroll').animate({
            scrollTop: top
        }, {
            duration: 550,
            complete:function() {
                checkScrollUp();
                checkScrollDown();
            }
        });
    });

    $('.arrow-up').click(function() {
        var top = $('.gallery-scroll').scrollTop() - galleryScrollHeight;
        $('.gallery-scroll').animate({
            scrollTop: top
        }, {
            duration: 550,
            complete:function() {
                checkScrollUp();
                checkScrollDown();
            }
        });
    });

    //$('.gallery-scroll .image-thumb a').prettyPhoto({slideshow:5000, autoplay_slideshow:false});
    $("a.video").prettyPhoto();
    //$(".prettyPhoto-hidden a").prettyPhoto({slideshow:5000, autoplay_slideshow:false});


    //$('.image-thumb a, a.video').prettyPhoto();

    $('.gallery-scroll').scrollTop(0);
    checkScrollUp();
    checkScrollDown();

    $('ul.left-menu .folder').click(function() {
        if ($(this).parent().find('hr,ul').is(':visible')) {
            return;
        }
        $('ul.left-menu > li').find('ul,hr').slideUp();

        $(this).parent().find('hr,ul').each(function() {
            if ($(this).is(':visible')) {
                $(this).slideUp();
            } else {
                $(this).slideDown();
            }
        });
    });

    $(".ad-gallery").adGallery({
        loader_image: 'images/loader.gif',
        width: 600,
        height: 400,
        display_next_and_prev: true,
        display_back_and_forward: true
    });

    $(".ad-holder").css({left: (($(window).width() - 660) / 2) + "px", top: $(window).scrollTop() + "px"});
    $('<div class="ad-holder-overlay-background"></div>').insertBefore(".ad-holder");
    $(".ad-holder-overlay-background").css("opacity", 0.8).add(".ad-gallery .close-button").click(function() {
        $(".ad-holder-overlay-background").fadeOut();

        $(".ad-holder").css({visibility: "hidden", "z-index": -9999});
    });
    $(".ad-holder-overlay-background").hide();
    $(".image-holder a").click(function() {
        $(".ad-holder").css({left: (($(window).width() - 660) / 2) + "px", top: ($(window).scrollTop() + 100) + "px"});
        $(".ad-holder-overlay-background").show();
        $(".ad-holder").css({visibility: "visible", "z-index": 9999});
        $(".ad-thumb-list a").eq($(".image-holder a").index(this)).click();
        return false;
    });
//    initMenu();
    var slidemenu = $(".program-sitemap-menu").slidemenu();

    $(".category-title").mouseenter(
        function (event) {
            event.stopPropagation();
            slidemenu.trigger("openAll");
        }).mouseleave(function (event) {
            event.stopPropagation();
            slidemenu.trigger("closeAll");
        });


//    $(".ad-image").click(function(){
//        var img = this;
//        alert("click");
//        $(".ad-thumb-list a").each(function(i){
//            if ($(img).attr("src") == $(this).attr("href")){
//                window.open($(this).attr("rel"),'_blank');
//            }
//        });
//
//    });
});


