﻿// Globals ********************

    var closeFancybox = false;

// Initialize **************************************
    
    $(document).ready(function() {
        
        initializeFancybox();
        initializeTestimonialsForm();

    });

// Global Functions ****************

    function initializeFancybox()
    {
        $(".fancybox").fancybox({
            padding     : 5
        });

        $(".fancybox_NoClose").fancybox({
            padding             : 5,
            fitToView           : true,
            closeBtn            : false,
            closeClick          : false
        });

    }

    function showLoadingLightBox(text)
    {
        $("#siteLightBox_Loading_Text").html(text);
        $("#triggerSiteLightBox_Loading").click();
    }

    function showMessageLightBox(text)
    {
        $("#siteLightBox_Message_Text").html(text);
        $("#triggerSiteLightBox_Message").click();
    }

    function checkForEarlyClose()
    {
        if (closeFancybox)
        {
            $.fancybox.close();
        }

        closeFancybox = false;
    }


// Testimonials ******************
    
    var defaultTestimonialsText = "Enter Your Testimonial Here!";

    function initializeTestimonialsForm()
    {
        $("#testimonialsForm").submit(function(){
            
            clearOutTextarea();
            
            if ($(this).valid())
            {
                showTestimonialsLoadingBar();

                $.ajax({
                    url     : $("#testimonialsForm").attr("action"),
                    type    : "POST",
                    data    : $("#testimonialsForm").serialize(),
                    success : submitTestimonialCallback
                });
            }
            else
            {
               // Do Nothing
            }

            return false;

        });
    }

    function showTestimonialsLoadingBar()
    {
        $("#testimonialSubmitButton").hide();
        $("#testimonialSubmitLoadingBar").show();
    }

    function hideTestimonialsLoadingBar()
    {
        $("#testimonialSubmitLoadingBar").hide();
        $("#testimonialSubmitButton").show();
    }

    function submitTestimonialCallback(jsonData)
    {
        if (jsonData.success)
        {
            showMessageLightBox("Your testimonial was saved successfully. Thank you!");
        }
        else
        {
            showMessageLightBox("There was a problem saving your testimonial.  Please close this light box, try re-sending it.");
        }
    }

    function clearOutTextarea()
    {
        var textValue = $("#Text").val();
        
        textValue = textValue.replace(defaultTestimonialsText, "");
        
        $("#Text").val(textValue); 
    }

    function resetTestimonialsForm()
    {
        $("#Text").val(defaultTestimonialsText);
        $("#Name").val("");
        $("#City").val("");
        $("#State").val("CA");

        clearTestimonialErrors();
    }

    function clearTestimonialErrors()
    {
        $(".field-validation-error").html("");
    }
