﻿var spinn = spinn || {};

spinn = {
    vars: {},

    init: function () {
        spinn.vars.inputBorderTop = "#abadb3";
        spinn.vars.inputBorder = "#e3e9ef";
        spinn.vars.inputLength = 1000;
        spinn.handleInputs();
        spinn.handleTextArea();
        spinn.handleConfirm();
        spinn.handleValidation();
        spinn.twitter.init();
    },

    handleInputs: function () {
        $('input:text, textarea').each(function () {
            $(this).bind('focus', function (e) {
                if (!$(this).hasClass('notDone')) {
                    $(this).css({ 'border': '1px solid #b9a166' });
                }
            });
            $(this).bind('blur', function (e) {
                if (!$(this).hasClass('notDone')) {
                    $(this).css({ 'border': '1px solid ' + spinn.vars.inputBorder, 'border-top': '1px solid ' + spinn.vars.inputBorderTop });
                }
            });
        });

        // remove border around image inputs...
        $('input:checkbox').css('border', '0');
        $('input:image').css('border', '0');
    },

    handleTextArea: function () {
        // First entry on page (or after reload)
        $('textarea:first').each(function () {
            spinn.vars.currentLength = $(this).val().length;
            if (spinn.vars.currentLength <= spinn.vars.inputLength) {
                $('span.counter').text((spinn.vars.inputLength - spinn.vars.currentLength) + ' tecken kvar').css('color', '#999999');
            } else {
                $('span.counter').text((spinn.vars.currentLength - spinn.vars.inputLength) + ' tecken för mycket').css('color', '#e81919');
            }
        });

        $('textarea:first').bind('keyup', function () {
            spinn.vars.currentLength = $(this).val().length;
            if (spinn.vars.currentLength <= spinn.vars.inputLength) {
                $('span.counter').text((spinn.vars.inputLength - spinn.vars.currentLength) + ' tecken kvar').css('color', '#999999');
            } else {
                $('span.counter').text((spinn.vars.currentLength - spinn.vars.inputLength) + ' tecken för mycket').css('color', '#e81919');
            }
        });
    },
    twitter: {
        init: function () {
            getTwitters('twitter', {
                id: 'spinn11',
                count: 4,
                enableLinks: true,
                ignoreReplies: true,
                clearContents: true,
                template: '%text%<span class="twitter-nav"><span>%time%</span> <a href="http://twitter.com/%user_screen_name%/statuses/%id_str%/"></a></span>'
            });
        }
    },
    handleConfirm: function () {
        // First entry on page (if saved with state Checked)
        $("span.confirmCheck input").each(function () {
            spinn.vars.confirm = (this.checked) ? '/UI/Frontend/Images/btnSend.gif' : '/UI/Frontend/Images/btnSendInactive.gif';
            $("input.finalizeReg").attr('src', spinn.vars.confirm);
        });

        $("span.confirmCheck input:checkbox").bind('click', function () {
            spinn.vars.confirm = (this.checked) ? '/UI/Frontend/Images/btnSend.gif' : '/UI/Frontend/Images/btnSendInactive.gif';
            $("input.finalizeReg").attr('src', spinn.vars.confirm);
        });

        // The click on "Review the registration"
        $('a.reviewLink').bind('click', function (e) {
            e.preventDefault();
            window.open($(this).attr('href'), 'reviewReg', 'status=0, toolbar=0, location=0,scrollbars=1,width=500, height=600');
        });
    },

    handleValidation: function (e) {
        $("span.validation-error")
                .bind("DOMAttrModified propertychange", function (e) {
                    // Exit early if IE because it throws this event lots more
                    if (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid") return;

                    var controlToValidate = $("#" + this.controltovalidate);
                    var validators = controlToValidate.attr("Validators");
                    if (validators == null) return;

                    var isValid = true;
                    $(validators).each(function () {
                        if (this.isvalid !== true) {
                            isValid = false;
                        }
                    });

                    if (isValid) {
                        controlToValidate.removeClass("notDone");
                    } else {
                        controlToValidate.addClass("notDone");
                    }
                });
    }
}

$(document).ready(function(){
    spinn.init();
});
