document.domain = 'wundercard.dk';

$(document).ready(function () {
    // put all your jQuery goodness in here.
    $('.fpSection1').fadeRotationWithNav({ 'timeInSeconds': 2, 'onClick': true, 'onClickContinueFromHere': true });
    prepareFormFieldsLabelInside();
    prepareDiscountFilter();
    initIsicIframe();
    $(window).resize(function () {initIsicIframe();});

    $('.fpSection2 ul li:nth-child(even)').addClass('even');
    $('.fpSection2 ul li:nth-child(odd)').addClass('odd');
});

function initIsicIframe() {
    if ($('.isicIframe').size() > 0) {
        var minimumSize = 350;
        var isicIframePosTop = $('.isicIframe').position().top;
        var winHeight = $(window).height();
        var ifrmHeight = Math.floor(winHeight - isicIframePosTop);
        if (ifrmHeight < minimumSize) {
            ifrmHeight = minimumSize
        }
        $('.isicIframe').height(ifrmHeight);
    }
}


function prepareFormFieldsLabelInside()
{
    $('textarea[title]').focus(function(){showHideInputValue($(this), 'focus')}).blur(function(){showHideInputValue($(this), 'blur')}).each(function(){showHideInputValue($(this), 'blur')});
    $('input[type=text][title]').focus(function () { showHideInputValue($(this), 'focus') }).blur(function () { showHideInputValue($(this), 'blur') }).each(function () { showHideInputValue($(this), 'blur') });
}

function showHideInputValue(argObj, argStrType)
{
    if(argStrType == 'focus' && $(argObj).val()==$(argObj).attr('title'))
        $(argObj).val('')
    else if($(argObj).val()=="")
        $(argObj).val($(argObj).attr('title'))
}
function getUrlParameter(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
function prepareDiscountFilter() {
    if ($('.discountFilter').size() > 0) {
        var prechosenCategory = getUrlParameter('cat');
        if (prechosenCategory > 0) {
            $('.discountFilter input[type=checkbox]').each(function () {
                if ($(this).val() != 'discount_' + prechosenCategory) {
                    $(this).removeAttr('checked');
                }
                else {
                    $(this).attr('checked', 'checked');
                }
            });
        }
    }

    $('.discountFilter input[type=checkbox]').each(function(){
        if(!$(this).is(":checked"))
        {
            var category = $(this).val();
            $('.itemListCategoryAndImage li.'+category).hide();
        }
    }).click(function(){
        if($(this).is(":checked"))
        {
            var category = $(this).val();
            $('.itemListCategoryAndImage li.'+category).slideDown(200);
        }
        else
        {
            var category = $(this).val();
            $('.itemListCategoryAndImage li.'+category).slideUp(200);
        }
    });


    $('.toggleAll .selectAll').click(function(event){
        event.preventDefault();
        $('.itemListCategoryAndImage li').slideDown(200);
        $('.discountFilter input[type=checkbox]').attr('checked','checked');
    });
    $('.toggleAll .deselectAll').click(function(event){
        event.preventDefault();
        $('.itemListCategoryAndImage li').slideUp(200);
        $('.discountFilter input[type=checkbox]').removeAttr('checked');
    });
}

//--------------- NEWSLETTER signup functions ---------------------
function checkEmail(email) {
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = $("#" + email).val();
    return pattern.test(emailVal);
}

function submitNewsletterForm() {   
    $(".formNewsletter .btnSubmit ").addClass('submitting').text('Vent...');
    $("#newsletterSuccess").css('display', 'none');
    $("#newsletterInvalid").css('display', 'none');
    $("#newsletterError").css('display', 'none');
    $.ajax({
        type: "POST",
        url: "/services/WunderCardWebService.asmx/AddNewsletterSubscriber",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{"email":"' + $("#formNewsletterSubscribe input[name = mail]").val() + '"}',
        traditional: true,
        error: function (xhr, message) { //communication was not successfull
            $("#newsletterError").css('display', 'block');
        },
        success: function (json) { //communication is successfull, email may still be invalid
            if (json.d.indexOf("success") == 0) {
                $("#newsletterSuccess").css('display', 'block');
                $(".formNewsletter").css('display', 'none');
            } else if (json.d.indexOf("invalid email") != -1) {
                $("#newsletterInvalid").css('display', 'block');
                $(".formNewsletter .btnSubmit ").removeClass('submitting').text('Tilmeld');
            } else {
                $("#newsletterError").css('display', 'block');
                $(".formNewsletter .btnSubmit ").removeClass('submitting').text('Tilmeld');
            }
        }
    });
}

$(function () {
    $("#formNewsletterSubscribe input:button").click(function () { submitNewsletterForm() });
    $("#formNewsletterSubscribe input:text").keypress(function (e) {
        if (e.which == 13) {
            jQuery(this).blur();
            submitNewsletterForm();
        }
    });
});
