﻿$(document).ready(function() {

    $(".LoginLinkButton").click(function() { $(".LoginLinkButton").hide(); $(".LoginControl").fadeIn("slow"); });

    $(".ErrorLoginDiv img").click(function() { $(".ErrorLoginDiv").fadeOut("slow"); });

    $(".LoginTextBox").focus(function() { $(".LoginTextBox2").show(); $("#mockpass").hide(); });

    $(".LoginLinkButton").addClass("LinkPointer");
    $(".ErrorLoginDiv img").addClass("LinkPointer");

    $("#mockpass").focus(function() { $(".LoginTextBox2").show(); $(".LoginTextBox2").focus(); });

    $('body').addClass('JS');
    $TradeMarkSlideshow.init();
});

$.fn.ContactForm = {
    feedback: function() {
        var c = $("#feedback-message");
        c.show().overlay({
            api: true,
            expose: {
                color: "#333",
                loadSpeed: 50,
                opacity: 0.75
            },
            top: "25%",
            onClose: function() {
                c.remove();
            }
        }).load();
    },
    init: function(validatorOptions) {
        var c = $("#contact-form");

        // Hide phone or mail
        var medium = c.find("fieldset.medium");
        var mailpanel = medium.find("div.mail");
        var phonepanel = medium.find("div.phone");

        function resetValidationClasses() {
            c.find("label.error").remove();
            c.find(".valid").removeClass("valid");
            c.find(".error").removeClass("error");
        }

        function reset() {
            c.find("input:text, textarea").val("");
            c.find("input:radio").removeAttr("checked");
            c.find("input:radio[value=ContactByMail]").attr("checked", "checked");
            resetValidationClasses();
        }

        function changeMedium(reset) {
            var selectedValue = medium.find("input:radio:checked").val();
            if (reset) {
                mailpanel.hide();
                phonepanel.hide();
                $.fn.contactFormValidator.resetForm();
                resetValidationClasses();
            }
            else if (selectedValue === "ContactByMail") {
                phonepanel.hide();
                mailpanel.show();
            }
            else if (selectedValue === "ContactByPhone") {
                mailpanel.hide();
                phonepanel.show();
            }
            else {
                mailpanel.hide();
                phonepanel.hide();
            }

            $.fn.ContactForm.byMail = selectedValue === "ContactByMail";
        }

        medium.find("input:radio").click(function() { changeMedium(false); });
        c.find("input:reset").click(function() { changeMedium(true); });

        $("a[rel=#contact-form]").overlay({
            top: "10%",
            expose: {
                color: "#333",
                loadSpeed: 50,
                opacity: 0.75
            },
            onBeforeLoad: function(event) {
                reset();
                jQuery.validator.messages.required = "";
                jQuery.validator.messages.email = "Adressen är felaktig.";
                jQuery.validator.messages.number = "Skriv endast in siffror";
                jQuery.validator.messages.minlength = jQuery.validator.format("Minst {0} tecken krävs");

                $.fn.contactFormValidator = $("#aspnetForm").validate(validatorOptions);
                c.find("dd").text(
                        this.getTrigger().find("input:first").val()
                        );
                c.find("> fieldset > input[type=hidden]").val(
                        this.getTrigger().find("input:last").val()
                        );
                changeMedium(false);
            }
        });

        // Close btn
        var closeDiv = c.find("div.close");
        closeDiv.html("Stäng <span>X</span>");
        var closeBtn = closeDiv.find("span");
        function btnHover() { closeBtn.toggleClass("hover"); }
        closeDiv.hover(btnHover, btnHover);
    },
    contactByMail: function(element) { return $.fn.ContactForm.byMail; },
    contactByPhone: function(element) { return !$.fn.ContactForm.byMail; }
};

$TradeMarkSlideshow = {
    context: false,
    tabs: false,
    timeout: 8000,
    slideSpeed: 2000,
    tabSpeed: 300,
    fx: 'fade',

    init: function() {
        this.context = $('#TradeMarkSlideshow');
        this.tabs = $('ul.Slides-nav li', this.context);
        this.tabs.remove();
        this.prepareSlideshow();
        this.context.find(".Slides > ul").width("696px").css("position", "relative");
    },

    prepareSlideshow: function() {

        $("div.Slides > ul", $TradeMarkSlideshow.context).cycle({
            fx: $TradeMarkSlideshow.fx,
            timeout: $TradeMarkSlideshow.timeout,
            speed: $TradeMarkSlideshow.slideSpeed,
            fastOnEvent: $TradeMarkSlideshow.tabSpeed,
            pager: $("ul.Slides-nav", $TradeMarkSlideshow.context),
            pagerAnchorBuilder: $TradeMarkSlideshow.prepareTabs,
            before: $TradeMarkSlideshow.activateTab,
            pauseOnPagerHover: true,
            pause: true,
            height: '205px',
            fit: true
        });
    },

    prepareTabs: function(i, slide) {

        return $TradeMarkSlideshow.tabs.eq(i);
    },

    activateTab: function(currentSlide, nextSlide) {
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $TradeMarkSlideshow.context);
        if (activeTab.length) {
            $TradeMarkSlideshow.tabs.removeClass('on');

            activeTab.parent().addClass('on');
        }
    }
};