﻿
(function ($, undefined) {
    "use strict";

    $.widget("ui.SignUpToWin", {
        options: {
            onClickSelector: "img.free",
            signUpToWinURL: "/Widget/Submit/SignUpToWin",
            siteid: ""
        },

        _create: function _create() {
            var self = this;
            var element = self.element;
            var options = self.options;
            if (self._getCookie("ESDNSignUpToWin") === "entered") {
                $(options.onClickSelector).remove();
                //var entered = $("<div class='tabBouncerDoneArea'/>");
                //element.empty().append(entered).show();
                //entered.show();
                element.hide();
                $(".SignUpToWin").hide();
                return;
            }

            element.TabBouncer({ onClickSelector: options.onClickSelector });

            $("#sweepstakesRules").ModalPopup({ onClickSelector: ".sweepsRules" });

            $(".enterToWin .emailInput input", element).bind("keypress", { self: self }, self._inputKeyPress);

            $(document).bind("click", { self: self }, self._click);
        },

        submitEmail: function submitEmail() {
            var self = this;
            var element = self.element;
            var options = self.options;

            var success = function (data) {
                if (data.success) {
                    element.ModalPopup("close");
                    $(".enterToWin .emailInput input", element).val("");
                    $("#enterToWinError").empty().hide();
                    $("#SuccessError .label").empty().append(data.message);
                    //$("#SuccessError").show("slide", { direction: "up" });
                    //setTimeout(function () {
                    //    $("#SuccessError").hide("slide", { direction: "up" });
                    //}, 2000);
                    element.TabBouncer("done");
                    self._setCookie("ESDNSignUpToWin", "entered");
                    setTimeout(function () {
                        $(".tabBouncerDoneArea").hide("fade", function () { $(".SignUpToWin").hide(); });
                    }, 3000);
                } else {
                    $("#enterToWinError").empty().append(data.message).show("fade");
                    setTimeout(function () {
                        $("#enterToWinError").hide("fade");
                    }, 5000);
                }
            };

            var email = $(".enterToWin .emailInput input", element).val();
            var sendEmail = { email: email };
            if (options.siteid) {
                sendEmail.siteid = options.siteid;
                $.getJSON(options.signUpToWinURL + "?callback=?",
                    sendEmail,
                    success
                );
            } else {
                $.ajax({
                    type: 'post',
                    url: options.signUpToWinURL,
                    dataType: 'json',
                    data: sendEmail,
                    success: success
                });
            }
        },

        _setCookie: function _setCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            } else {
                var expires = "";
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        },

        _getCookie: function _getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1, c.length);
                }
                if (c.indexOf(nameEQ) == 0)
                    return c.substring(nameEQ.length, c.length);
            }
            return null;
        },

        _click: function _click(event) {
            var self = event.data.self;
            var element = self.element;
            var options = self.options;

            var target = $(event.target);

            if (target.is(".enterToWin button") || target.parents(".enterToWin button").length) {
                self.submitEmail();
            }
        },

        _inputKeyPress: function _inputKeyPress(event) {
            var self = event.data.self;
            var element = self.element;
            var options = self.options;

            var target = $(event.target);

            if (event.which == 13) {
                self.submitEmail();
            }
        }
    });
})($);

