$(document).ready(function(){
    
    var current_slide = "";
    var animation_duration = 750;
    var animating = false;
    
    if(window.location) {

    }
    
    var color_settings = {
        vilytter: {
            bgcolor: "#003974",
            header: "#EC008C",
            textcolor: "#FFFFFF",
            logo: "magenta",
            active: "#EC008C",
            weight: 1,
            title: "Vi lytter | "
        },
        vistoler: {
            bgcolor: "#FAA61A",
            header: "#EC008C",
            textcolor: "#FFFFFF",
            logo: "magenta",
            active: "#EC008C",
            weight: 2,
            title: "Vi stoler | "
        },
        vitror: {
            bgcolor: "#FFD400",
            header: "#EC008C",
            textcolor: "#333333",
            logo: "magenta",
            active: "#EC008C",
            weight: 3,
            title: "Vi tror | "
        },
        visamarbejder: {
            bgcolor: "#7A1315",
            header: "#EC008C",
            textcolor: "#FFFFFF",
            logo: "magenta",
            active: "#EC008C",
            weight: 4,
            title: "Vi samarbejder | "
        },
        tagansvar: {
            bgcolor: "#EC008C",
            header: "#FFFFFF",
            textcolor: "#FFFFFF",
            logo: "blue",
            active: "#003974",
            weight: 5,
            title: ""
        }
    };
    
    $(".iframe_link").click(function(){
        $(".iframe_div").hide();
        $(".iframe_link.active").removeClass('active');
        var el = $(this).attr('id');
        el = $("#"+el.replace("_button", ""));
        el.show();
        $(this).addClass('active');
        return false;
    });
    
    $("#navigation li a").click(function(){
        if(animating)
        {
            return false;
        }
    });
    
    $(".big_buttons a, .small_buttons a").click(function(){
        $("#overlay").fadeIn(250);
        $(".iframe_div").hide();
        $(".iframe_link.active").removeClass('active');
        var el = $(this).parent().attr('class');
        el = $("#"+el.replace("_button", ""));
        el.show();
        $("#"+$(this).parent().attr('class')+"_button").addClass('active');
        setTimeout(function(){
            $("#overlay_content").fadeIn(250);
        }, animation_duration);
        return false;
    });
    
    $(".close_overlay a").click(function(){
        close_overlay(250);
        return false;
    });
    
    $(".read_more").click(function(){
        var el = $(this).parent().parent().find("h4");
        if(el.css('display') == "block")
        {
            el.slideUp(250);
            $(this).find('a').html('Læs mere');
        }
        else
        {
            el.slideDown(250);
            $(this).find('a').html('Læs mindre');
        }
        return false;
    });
    
    $.address.change(function(event) {
        
        if(!animating)
        {
            $(".content_wrapper").css('overflow', 'hidden');
            setTimeout(function(){
                $(".content_wrapper").css('overflow', 'auto');
            }, animation_duration);
            animating = true;
            close_overlay(250);
            var window_width = $(window).width();
            var val = event.value.replace("/", "");

            if(val == "")
            {
                val = "vi-lytter";
            }
            
            var color_settings_key = val.replace("-", "");
            var bgcolor = color_settings[color_settings_key]['bgcolor'];
            var header = color_settings[color_settings_key]['header'];
            var textcolor = color_settings[color_settings_key]['textcolor'];
            var logo = color_settings[color_settings_key]["logo"];
            var active = color_settings[color_settings_key]["active"];
            var title = color_settings[color_settings_key]["title"];
            setTimeout(function(){
                window.document.title = title+"Tag ansvar - Radikale Venstre";
                var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

                $.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
                    try {
                        var pageTracker = _gat._getTracker("UA-17939766-1");
                        pageTracker._trackPageview();
                    } catch(err) {}
                    
                    pageTracker._trackEvent('Menu', 'Click', title);

                });
            }, animation_duration+1000);

            var weight = color_settings[color_settings_key]["weight"];

            if(current_slide == "") { current_slide = weight; }



            if(current_slide < weight)
            {
                // Slide to the left
                var hide_sliding_animation = "-="+$(window).width();
                var show_starting_position = window_width+"px";
                var show_sliding_animation = "-="+$(window).width();
            }
            else
            {
                // Slide to the right
                var hide_sliding_animation = "+="+$(window).width();
                var show_starting_position = window_width-(window_width*2)+"px";
                var show_sliding_animation = "+="+$(window).width();
            }

            current_slide = weight;

            $(".content_wrapper").each(function(){
                if($(this).css('display') == "block")
                {
                    $(this).animate({left: hide_sliding_animation}, animation_duration);
                    var el = $(this);
                    setTimeout(function(){
                        el.css({
                            left: window_width+"px",
                            display: "none"
                        }).find(".video_container div").hide();
                    }, animation_duration);
                }
            });
            
            
            $("#"+val).css({
                display: "block",
                left: show_starting_position
            }).animate({left: show_sliding_animation}, animation_duration);
            setTimeout(function(){
                $("#"+val+" .video_container div").fadeIn(500);
            }, animation_duration);

            $('.content_overlay').hide();
            setTimeout(function(){
                $('.content_overlay').show();
                var content_div_margin = $("#"+val+" .content_div").css('marginTop');
                //alert(content_div_margin);
            }, animation_duration);
            
            $("#navigation li.active").removeClass('active');
            $("#"+val+"_nav").addClass('active');
            changeColor(val, bgcolor, header, textcolor, logo, active);
            setTimeout(function(){
                animating = false;
            }, animation_duration)
        }
    });
    
    function changeColor(d, b, h, t, l, a) {
        $("#"+d+", body").css('background', b);
        $("#"+d+" .left h1").css('color', h);
        $("#"+d+" .left h4, .left h2").css('color', t);
        $("#header h1 a").css('background', 'url(images/logo_'+l+'.png) no-repeat top left');
        $("#navigation li a").css('color', "#fff");
        $("#navigation li.active a").css('color', a);
    };

});

function close_overlay(animation_duration)
{
    $("#overlay_content").fadeOut(animation_duration);
    setTimeout(function(){
        $("#overlay").fadeOut(animation_duration);
    }, animation_duration);
    return false;
}
