﻿$(document).ready(function () {

    $("body").traceBox();
    //$("div.news p").truncate();

    /* 'tabs' click event (used in both 'building a difference' and 'lights 4life'
    ------------------------------------------------------------------------------ */
    $(".container").each(function () {

        $(this).find("a.tab:first").addClass("active");
        $(this).find("div.tab:first").show();

        $("a.tab").click(function () {
            var i = $("a.tab").index(this);

            $(this).parent().find("a.active").removeClass("active");
            $(this).addClass("active");

            $(this).parent().find("div.tab").hide();
            $("div.tab").eq(i).show();

            return false;
        });
    }); // end of tabs


    /* 'toggle' click event (used in the media center)
    ------------------------------------------------------------------------------ */
    $(".toggle a.tab").click(function () {

        if ($(this).parent().parent().find("div.tab").hasClass("active")) {
            $(this).parent().parent().find("div.tab").removeClass("active")
        } else {
            $(this).parent().parent().find("div.tab").addClass("active")
        };

        return false;
    }); // end of toggle


    /* 'lights 4life' sliding galleries
    ------------------------------------------------------------------------------ */
    $('div.tab ul#beacon-of-hope').ninjaGallery({ spacerWidth: 2,
        smallSize: 124,
        largeSize: 323,
        wrapperWidth: 896,
        //wrapperWidth: 446,
        extraContentId: '.additional-content',
        offset: 'dynamic'
    });

    $('div.tab ul#ray-of-sunshine').ninjaGallery({ spacerWidth: 2,
        smallSize: 124,
        largeSize: 323,
        wrapperWidth: 896,
        extraContentId: '.additional-content',
        offset: 'dynamic'
    });

    /* 'At the Heart of It" award nomination dialog
    ------------------------------------------------------------------------------ */
    var name = $("#ctl00_MainContent_txtName");
    var nominee = $("#ctl00_MainContent_txtNominee");
    var distid = $("#ctl00_MainContent_txtDistID");
    var comments = $("#txtComments");
    var allFields = $([]).add(name).add(nominee).add(distid).add(comments);
    var tips = $(".validateTips");

    //    function submitSuccess() {
    //        updateTips("Thank You! Your nomination has been submitted.");
    //        name.val("");
    //        nominee.val("");
    //        distid.val("");
    //        comments.val("");
    //        setTimeout(closeAfterSuccess, 2000);
    //    };

    function submitSuccess(msg) {
        updateTips(msg);
        name.val("");
        nominee.val("");
        distid.val("");
        comments.val("");
        setTimeout(closeAfterSuccess, 2000);
    };

    function closeAfterSuccess() {
        $('.nominationForm').dialog('close');
        tips.removeClass('ui-state-highlight');
        tips.html("");
    }

    function updateTips(t) {
        tips
		    .text(t)
			.addClass('ui-state-highlight');
        //        setTimeout(function() {
        //            tips.removeClass('ui-state-highlight', 1500);
        //        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between " + min + " and " + max + " characters.");
            return false;
        } else {
            return true;
        }
    }

    function checkLengthEs(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("El tamaño de " + n + " debe ser entre " + min + " y " + max + " caracteres.");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#nominationButton").click(function () {
        $('.nominationForm').dialog('open');
        return false;
    });

    $(".nominationButton-es").click(function () {
        $('#nominationForm-es').dialog('open');
        return false;
    });

    $('#nominationForm-en').dialog({
        title: "Get to the heart of it!",
        autoOpen: false,
        height: 720,
        width: 705,
        modal: true,
        resizable: false,
        draggable: false,
        buttons: {
            'Close': function () { $(this).dialog('close'); },
            'Submit Nomination': function () {
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && checkLength(name, "the Name", 1, 45);
                bValid = bValid && checkLength(nominee, "the Nominee", 1, 45);
                bValid = bValid && checkLength(distid, "the Distributor ID", 1, 7);
                bValid = bValid && checkLength(comments, "the Comments", 1, 1000);

                if (bValid) {
                    $.emailSubmit();
                }
            }
        },
        close: function () {
            allFields.val('').removeClass('ui-state-error');
            updateTips("All form fields are required.");
        }
    });

    $('#nominationForm-es').dialog({
        title: "¡Tómalo desde el corazón!",
        autoOpen: false,
        height: 720,
        width: 760,
        modal: true,
        resizable: false,
        draggable: false,
        buttons: {
            'Cerrar': function () { $(this).dialog('close'); },
            'Enviar nominación': function () {
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && checkLengthEs(name, "Nombre", 1, 45);
                bValid = bValid && checkLengthEs(nominee, "Persona nominada", 1, 45);
                bValid = bValid && checkLengthEs(distid, "Número de distribuidor", 1, 7);
                bValid = bValid && checkLengthEs(comments, "Comentarios", 1, 1000);

                if (bValid) {
                    $.emailSubmit({successMessage: "¡Gracias! Tu nominación ha sido registrada."});
                }
            }
        },
        close: function () {
            allFields.val('').removeClass('ui-state-error');
            updateTips("Se requiere llenar todos los espacios en el formulario.");
        }
    });

    jQuery.emailSubmit = function (options) {
        var defaults = {
            url: "submit-nominee.aspx",
            successMessage: "Thank You! Your nomination has been submitted."
        };

        var d = $.extend(defaults, options);

        emailForm = {
            name: $(name).val(),
            nominee: $(nominee).val(),
            distid: $(distid).val(),
            comments: $(comments).val()
        };

        $.post(d.url, emailForm,
	        function (data) {
	            //alert(data);
	            if (data == "Success") {
	                submitSuccess(d.successMessage);
	            }
	        }
        );

    };

});           // end of $(document).ready