﻿$(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() {
		var url = window.location.toString();

		if (url.indexOf("/es/") != -1) {
			updateTips("¡Gracias! Tu nominación ha sido procesada.");
		} else {
			updateTips("Thank You! Your nomination has been submitted.");
		}

        name.val("");
        nominee.val("");
        distid.val("");
        comments.val("");
        tips.removeClass('ui-state-highlight');
        setTimeout(closeAfterSuccess, 3000);
    };

    function closeAfterSuccess() {
        $('#nominationForm').dialog('close');
        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 checkLength_ES(o, n, min, max) {
    	if (o.val().length > max || o.val().length < min) {
    		o.addClass('ui-state-error');
    		updateTips(n + " debe ser de " + min + " a " + 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;
        }
    }


    $(".nominationButton-en").click(function () {
    	$('.nominationForm-en').dialog('open');
        return false;
    });

    $(".nominationButton-es").click(function () {
        $('.nominationForm-es').dialog('open');
        return false;
    });

    $('.nominationForm-en').dialog({
    	title: "At the Heart of It Award 2012",
        autoOpen: false,
        height:650,
        width:800,
        modal: true,
        resizable: false,
        draggable: false,
        buttons: {
            '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 () { $(this).dialog('close'); }
        },
        close: function () {
            allFields.val('').removeClass('ui-state-error');
            updateTips("All form fields are required.");
        }
    });

    $('.nominationForm-es').dialog({
    	title: "Premio Desde el Corazón 2012",
        autoOpen: false,
        height: 570,
        width: 800,
        modal: true,
        resizable: false,
        draggable: false,
        buttons: {
            'Hacer una Nominación': function () {
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && checkLength_ES(name, "El nombre", 1, 45);
                bValid = bValid && checkLength_ES(nominee, "El nombre de la persona nominada", 1, 45);
                bValid = bValid && checkLength_ES(distid, "El número de distribuidor", 1, 7);
                bValid = bValid && checkLength_ES(comments, "Los comentarios", 1, 1000);

                if (bValid) {
                    $.emailSubmit();
                }
            },
            'Cerrar': function () { $(this).dialog('close'); }
        },
        close: function () {
            allFields.val('').removeClass('ui-state-error');
            updateTips("Se requiere llenar todos los campos.");
        }
    });

    jQuery.emailSubmit = function (options) {
        var defaults = {
            url: "../en/submit-nominee.aspx"
        };

        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();
	            }
	        }
        );

    };

});            // end of $(document).ready
