﻿$(function () {
    var campaignWidth, campaignHeight, campaignTop, campaignLeft;
    var validCss = { "border": "1px solid #fff" },
        invalidCss = { "border": "1px solid red" };
    var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    var initActie1 = false, initActie2 = false;
    var formContent;
    campaignWidth = $("#lelie_campaign").width();
    campaignHeight = $("#lelie_campaign").height();
    campaignTop = $("#lelie_campaign").offset().top;
    campaignLeft = $("#lelie_campaign").offset().left;
    $("div.actie2-wrapper").offset({ top: campaignTop + 10, left: campaignLeft + 10 });
    $(document).bind('cbox_complete', function () {
        Cufon.refresh();
        if ($("#actie-box").height() > 0 && !initActie1) {
            $('#actie-box div').jScrollPane({ scrollbarWidth: 15, showArrows: true, arrowSize: 5 });
            initActie1 = true;
        }

    });
    $(document).bind('cbox_closed', function () {

        $("#lelie_campaign .right a.redBtn").data('disabled', false);
    });

    $('div.home_commingsoon a:not(.selected)').live("click", function () {
        $.colorbox.close();
        var index = $('div.home_commingsoon a').index($(this));
        $("div.home_commingsoon a.selected").removeClass("selected");
        $(this).addClass("selected");
        $('#slider1').anythingSlider(2 - index);
        return false;
    })

    if ($.browser.msie) {

    }
    else {
        //$("#colorbox").css('cssText', 'top:' + campaignTop + 'px !important;' + 'left:' + campaignLeft + 'px !important;');
    }
    $("#colorbox").css('cssText', 'top:' + campaignTop + 'px !important;' + 'left:' + campaignLeft + 'px !important;');
    //var boxLeft = campaignLeft + (campaignHeight - 500) / 2;
    var activeH2 = $('#actie-box h2.active');
    var option = {
        //inline: true,
        width: '600px',
        left: campaignLeft + 'px',
        top: campaignTop + 'px',
        opacity: 0.9,
        close: "FENSTER SCHLIESSEN",
        onOpen: function () {
            $("#cboxOverlay").offset({ top: campaignTop, left: campaignLeft });
            $("#cboxOverlay").width(campaignWidth);
            $("#cboxOverlay").height(campaignHeight);

            $("#colorbox").css("z-index", '999999');
        },
        OnClosed: function () {
            $("#colorbox").css("z-index", '-1');
            //            $('#cboxContent h2.active').remove()
        },
        onComplete: function () {
            //            Cufon.replace('.colorbox h2', { fontFamily: 'Gotham HTF' }); //black

            //            if ($('#cboxContent #actie-box').length) {
            //                window.h2 = $('#actie-box h2.active');
            //                $('#cboxContent').append(activeH2);
            //            }
            //$('#not-winner-box ul').jScrollPane();
        }
    };
    $('#campaign_form a.agreement').live('click', function () {
        $('div.actie2-wrapper').show();
        if (!initActie2) {
            Cufon.refresh();
            $('div#actie-box2 > div').jScrollPane({ scrollbarWidth: 15, showArrows: true, arrowSize: 5 });
            initActie2 = true;
        }

        return false;
    })
    $('#actie-box2 a.close').live('click', function () {
        $('div.actie2-wrapper').hide();
        return false;

    })

    $("#lelie_campaign a.actie").click(function () {
        var opClone = option;
        opClone.href = "#actie-box";
        opClone.inline = true;

        $.colorbox(opClone);
        return false;
    })
    $("#lelie_campaign a.kaarten").click(function () {
        var opClone = option;
        opClone.href = "#kaarten-box";
        opClone.inline = true;
        $.colorbox(opClone);
        return false;
    })
    $("#lelie_campaign a.reis").click(function () {
        var opClone = option;
        opClone.href = "#reis-box";
        opClone.inline = true;
        $.colorbox(opClone);
        return false;
    })

    $("#lelie_campaign a.winners").click(function () {
        var opClone = option;

        $.get('/campaignWinners.aspx', function (data) {
            opClone.inline = false;
            opClone.href = null;
            opClone.html = data;
            $.colorbox(opClone);
        });

        return false;
    });


    (function ($) {
        var currentIndex = 0;
        function slide() {
            if (currentIndex <= 0) {
                $(".karts a.prev").hide();
            }
            else {
                $(".karts a.prev").show();
            }
            if (currentIndex >= 4) {
                $(".karts a.next").hide();
            }
            else {
                $(".karts a.next").show();
            }
            if (currentIndex <= 4 && currentIndex >= 0) {
                $(".karts div.middle div").animate({
                    left: -(currentIndex * 207)
                })

                $(".karts .right div p:visible").fadeOut(function () {
                    $(".karts .right div p:eq(" + currentIndex + ")").fadeIn();
                })
            }

        }
        $(".karts a.prev").live("click", function () {
            currentIndex--;
            slide();
            return false;
        });
        $(".karts a.next").live("click", function () {
            currentIndex++;
            slide();
            return false;
        });
    })(jQuery);


    $("#lelie_campaign .right a.redBtn").click(function () {
        if ($(this).data('disabled')) {
            return false;
        }

        var $codeInput = $("input#code");
        if ($codeInput && $.trim($codeInput.val()) == "" || $codeInput.val().length < 12) {
            $codeInput.css("border-color", "red");
            return false;
        }
        else {
            $codeInput.css("border-color", "correct");
        }
        $(this).data("disabled", true);

        if (formContent) {
            option.inline = false;
            option.href = null;
            option.html = formContent;
            $.colorbox(option);
        }
        else {
            $.post('/campaign_form.aspx', { code: $codeInput.val() }, function (data) {
                option.inline = false;
                option.href = null;
                formContent = option.html = data;
                $.colorbox(option);
            });
        }
        

        return false;
    })
    $("#campaign_form a.greenBtn").live("click", function () {
        if ($(this).data('disabled')) {
            return false;
        }
        var data = {},
            valid = true;

        data["code"] = $("#code").val();

        $("input[type=text]", "#campaign_form").each(function (i, e) {
            var value = $.trim($(e).val());
            if (value == "" || ($(e).attr("name") == "email" && !emailReg.test(value))) {
                $(e).parent().css(invalidCss);
                valid = false;
            }
            else {
                $(e).parent().css(validCss);
                data[$(e).attr("name")] = value;
            }
        })
        if ($('input[name=gender]:checked', "#campaign_form").length) {
            $('div.gender').css(validCss);
            data['gender'] = $('input[name=gender]:checked', "#campaign_form").val();
        }
        else {
            $('div.gender').css(invalidCss);

            valid = false;
        }
        if ($("input[name=agreement]:checked", "#campaign_form").length) {
            $('input[name=agreement]').closest("tr").css(validCss);
        }
        else {
            $('input[name=agreement]').closest("tr").css(invalidCss);
            valid = false;
        }

        if ($('#hearfrom').val() == 'none') {
            $('div.heafrom').css(invalidCss);
            valid = false;
        }
        else {
            $('div.heafrom').css(validCss);
            data['hearfrom'] = $('#hearfrom').val();
        }


        if (!valid) {
            $("label.msg").css('opacity', '1');
            return false;
        }
        else {
            $("label.msg").css('opacity', '0');
        }
        data['newsletter'] = $('input[name=newsletter]:checked', "#campaign_form").length > 0;
        //        console.log(data);
        //        return false;


        $(this).data("disabled", true);

        $.post("/Campaign.aspx",
                data,
                function (result) {
                    $('#cboxLoadedContent').html(result);
                    Cufon.refresh();
                    //                    if (result.indexOf('id="winner-box"') >= 0) {
                    //                        $('#cboxClose').hide();
                    //                    }
                    //                    $.colorbox.close();
                    //                    opClone.inline = false;
                    //                    opClone.href = null;
                    //                    opClone.html = result;
                    //                    $.colorbox(opClone);
                });

        return false;
    })


    $("#winner-box #winnerForm span button").live("click", function () {

        var data = {};
        var valid = true;


        $("input[type=text]", "#winner-box").each(function (i, e) {
            var value = $.trim($(e).val());
            data[$(e).attr("name")] = value;
            if (value == "" || ($(e).attr("name") == "email" && !emailReg.test(value))) {
                $(e).css("border", "2px solid red");
                valid = false;
            }
            else {
                $(e).css("border", "2px solid #fff");
            }
        })

        if (!valid) {
            $('#winner-box  p.msg').css('opacity', '1');
            return false;
        }
        $('#winner-box  p.msg').css('opacity', '0');
        $(this).attr("disabled", "true");

        $.post("/handler/WinnerInfo.ashx",
                data,
                function (result) {
                    if (result == "true") {
                        $("#winner-box .formcontent").hide();
                        $("#winner-box #winnerSubmitted").show();
                        Cufon.refresh();
                    }
                    else {
                        alert("failed");
                    }
                });

        return false;
    })

})
