/* Author: 
    Tobias Norén
    Bacill Produktion AB
*/


/*
    External links
*/

$('a.ext').click(function () { window.open($(this).attr("href")); return false; });

/*
    Navigation
*/

var mainMenuDown = false;

$('header #topNavigation ul.dropdown li a').mouseover(function () {
    $(this).stop().animate({ backgroundColor: '#153b63' });
})
.mouseout(function () {
    $(this).stop().animate({ backgroundColor: '#1b528c' });
});

$('#subNavigation ul li a').mouseover(function () {
    $(this).stop().animate({ backgroundColor: '#f0f0f0' });
})
.mouseout(function () {
    $(this).stop().animate({ backgroundColor: '#fff' });
});

function mainmenu() {
    $("header #topNavigation li").hover(function () {

        mainMenuDown = true;
        var menuTop = $(this).offset().top - $(this).scrollTop() + 32;
        //alert(menuTop);

        if (!$(this).hasClass("current"))
            $(this).find("a.top").stop().animate({ backgroundPosition: '0 33px' });
        $(this).find('ul:first').css({ visibility: "visible", display: "none", position: "absolute", top: menuTop + "px" }).slideDown(300);

    }, function () {
        if (!$(this).hasClass("current")) {
            $(this).find("a.top").stop().animate({ backgroundPosition: '0px 0px' });
        }
        $(this).find('ul:first').slideUp(100, function () {
            $(this).css({ visibility: "visible", display: "none" })
            mainMenuDown = false;
        });
    });
}

/*
    News list
*/
var slideIndex = 0;
var slideAmount = $(".news").find(".slide").length;

if (slideAmount > 1)
    $('.news .arrow-right').show();

function newsSetArrows() {
    if (slideIndex == 0) {
        $('.news .arrow-left').animate({ backgroundPosition: '-40px 0px' }, function () { $('.news .arrow-left').hide(); });
    } else {
        $('.news .arrow-left').animate({ backgroundPosition: '0px 0px' });
        $('.news .arrow-left').show();
    }

    if (slideIndex == slideAmount - 1) {
        $('.news .arrow-right').animate({ backgroundPosition: '40px 0px' }, function () { $('.news .arrow-right').hide(); });
    } else {
        $('.news .arrow-right').show();
        $('.news .arrow-right').animate({ backgroundPosition: '0px 0px' });
    }
}

$('.news .arrow-left').live('click', function (e) {
    if (slideIndex > 0) slideIndex--;
    newsSetArrows();
    $('.news .wrapper').animate({ left: -(slideIndex * 300) }, { queue: false });
});

$('.news .arrow-right').live('click', function (e) {
    if (slideIndex < slideAmount) slideIndex++;
    newsSetArrows();
    $('.news .wrapper').animate({ left: -(slideIndex * 300) }, { queue: false });
});


/*
    Contact form
*/

var crosstag = "<img src=\"/images/cross.gif\" class=\"validation-cross\">";
var validator = $("#contactForm").validate({
    rules: {
        contact_namn: "required",
        contact_telefon: "required",
        contact_epost: { required: false, email: true }
    },
    messages: {
        contact_namn: crosstag,
        contact_telefon: crosstag,
        contact_epost: crosstag
    },
    submitHandler: function () {
        $("#form_submit").attr("disabled", "disabled");
        $("#form_submit").val("Skickar...");
        $.ajax({
            url: "/ajax/SendBigContactForm.aspx",
            type: "POST",
            cache: false,
            data: "pageId=" + $("#contactForm .page-id").val() + "&namn=" + $("#contact_namn").val() + "&telefon=" + $("#contact_telefon").val() + "&epost=" + $("#contact_epost").val() + "&meddelande=" + $("#contact_meddelande").val(),
            success: function (result) {
                if (result == "1") {
                    $("#contactForm").hide();
                    $("#contactThanks").show();
                } else {
                    $("#contact_submit").removeAttr("disabled");
                    $("#contact_submit").text("Skicka meddelande");
                    alert("Ett tekniskt fel uppstod.\n\nVänligen försök igen eller kontakta oss på telefon.");
                }
            }
        });
    },
    // set this class to error-labels to indicate valid fields 
    success: function (label) {
        label.html(" ");
    }
});

$("#contact_submit").removeAttr("disabled");
//$("#form_submit").text("Skicka bokningsförfrågan");   // Crashes IE ??!


/*
    Booking form
*/

var validator = $("#bookingForm").validate({
    rules: {
        booking_namn: "required",
        booking_telefon: "required",
        booking_epost: { required: true, email: true }
    },
    messages: {
        booking_namn: crosstag,
        booking_telefon: crosstag,
        booking_epost: crosstag
    },
    submitHandler: function () {
        $("#form_submit").attr("disabled", "disabled");
        $("#form_submit").val("Skickar...");
        $.ajax({
            url: "/ajax/SendBookingForm.aspx",
            type: "POST",
            cache: false,
            data: "pageId=" + $("#bookingForm .page-id").val() + "&bilmodell=" + $("#booking_bilmodell").val() + "&arsmodell=" + $("#booking_arsmodell").val() + "&antalmil=" + $("#booking_antalmil").val() + "&reg=" + $("#booking_reg").val() + "&namn=" + $("#booking_namn").val() + "&telefon=" + $("#booking_telefon").val() + "&epost=" + $("#booking_epost").val() + "&meddelande=" + $("#booking_meddelande").val(),
            success: function (result) {
                if (result == "1") {
                    $("#bookingForm").fadeOut();
                    $("#bookingThanks").fadeIn();
                } else {
                    $("#booking_submit").removeAttr("disabled");
                    $("#booking_submit").text("Skicka bokningsförfrågan");
                    alert("Ett tekniskt fel uppstod.\n\nVänligen försök igen eller kontakta oss på telefon.");
                }
            }
        });
    },
    // set this class to error-labels to indicate valid fields 
    success: function (label) {
        label.html(" ");
    }
});

$("#booking_submit").removeAttr("disabled");
//$("#form_submit").text("Skicka bokningsförfrågan");   // Crashes IE ??!


/*
    Contact form
*/

$('.contactForm .submit').click(function () {

    var pageName = $('.contactForm .page-name').val();
    var pageId = $('.contactForm .page-id').val();
    var name = $('.contactForm .name').val();
    var contact = $('.contactForm .contact').val();
    var isValid = true;

    $('.contactForm .name-err').css({ "display": "none" });
    $('.contactForm .contact-err').css({ "display": "none" });

    if (name == "" || name == "Ditt namn") {
        $('.contactForm .name-err').css({ "display": "inline" });
        isValid = false;
    }
    if (contact == "" || contact == "Telefon / E-post") {
        $('.contactForm .contact-err').css({ "display": "inline" });
        isValid = false;
    }

    if (isValid) {
        $(this).attr("disabled", "disabled");
        $.ajax({
            url: "/ajax/SendContactForm.aspx",
            type: "POST",
            cache: false,
            data: "pageName=" + pageName + "&pageId=" + pageId + "&name=" + name + "&contact=" + contact,
            success: function (result) {
                $('.contactForm .submit').removeAttr("disabled");
                $('.contactForm .sent').css({ "display": "block" });
                $('.contactForm .name').val("Ditt namn");
                $('.contactForm .contact').val("Telefon / E-post");
            }
        });
    }
});
 
 $(document).ready(function(){
     mainmenu();
});
