jQuery(document).ready(function ($) {
    
    var homepage = {
        
        handleSubscription: function () {
            
            var $links   = $(".message.subscribe nav"),
            $form    = $("#subscription"),
            timer;
            
            // Fades out a first element, and then fades in a second element
            // we can also pass it an optional function to run after it is done.
            function toggleElements(first, second, func) {
                first.fadeOut(500, function () {
                    second.fadeIn();
                    
                    if (func) {
                        func();
                    }
                }); 
            };
            
            function validateForm() {
                if ($("#3977").attr("value").match(/[^\s]*@[a-z0-9.\-]*/i)) {
                    return true;
                } else {                  
                    // Remove all existing error messages
                    $("#email-required").fadeOut(function () {
                        $(this).remove();
                    });
                    
                    // Append form with form validation message
                    $("#subscription").append('<div id="email-required" class="validation-advice" style="">This is a required field</div>').focus();
                    $("#email-required").fadeOut(0).animate({ marginBottom: "10px"}, 400).fadeIn(400); 
                    
                    return false;
                }
            };
            
            // On click, show form
            $("a.icon_email").click(function (e) {
                
                e.preventDefault();
                
                toggleElements($links, $form);
                $(".message.subscribe").children("p").first().html("Sign up to receive the latest special offers, promotions, &amp; more!");
            });
            
            
            // When the mouse leaves the form, we want to fade it out unless the text block has focus
            $("#subscription").mouseleave(function () {
                if ($("#subscription input.text:focus").length === 0) {
                    timer = setTimeout(function () {
                        toggleElements($form, $links);
                    }, 1500);
                }
            });
            
            
            // When the mouse enters the form, prevent it from accidentally fading out
            $("#subscription").mouseenter(function () {
                clearTimeout(timer);
            });
            

            // Handles odd niche cases where submitting form doesn't cause validation labels to appear
            $("#subscription input[type='submit']").click(function (e) {
                
                if (validateForm()) {
                    
                } else {
                    e.preventDefault();
                }
                
            });
            
            $("#subscription").submit(function (e) {
                
                e.preventDefault();
                
                // If the given email address truly is in the correct format:
                if (validateForm()) {       
                    
                    
                    // Step 1: Remove all existing error messages
                    $("#email-required").fadeOut(500, function () {
                        $(this).remove();
                    });
                    
                    // Step 2: Fade out all elements
                    $("#subscription input").fadeOut();
                    
                    // Step 3: Add loader GIF            
                    $("#subscription").addClass("submitted loader");
                    
                    
                    // Step 4: Create the query string to post to Bronto
                    var dataString = "";
                    
                    $("#subscription input").each(function (index) {
                        
                        // for the first entry
                        dataString += (dataString === "") ? "" : "&";
                        
                        dataString += $(this).attr("name") + "=" + $(this).val();  
                    });
                    
                    // Step 5: Submit the request
                    $.ajax({  
                        type         : "POST",
                        url          : "//store.moissanite.com/public/webform/process",  // The current Bronto address
                        data         : dataString, 
                        crossDomain  : true,
                        beforeSend   : function () {                    
                            
                            setTimeout(function () {
                                $(".message.subscribe").children("p").first().html("Thank you! You should receive an email from Moissanite.com shortly.").siblings("form").fadeOut(400, function () {
                                    
                                    // Reset email field
                                    $(".message.subscribe form input.text").attr("value", "");
                                    
                                    // Toggle the links and form, then clean up the form for next time
                                    toggleElements($form, $links, function () {
                                        
                                        // Clean up subscription form
                                        $("#subscription").removeClass("loader").attr("style", "");
                                        
                                        // Clean up children elements
                                        $("#subscription input").attr("style", "");
                                    });
                                    
                                });
                            }, 3500);
                        },
                        error: function (e) {
                            console.log(e);
                        }
                    });  
                }
            });
            
        },
        
        activateSlider: function () {
            $('.slider').nivoSlider({  
                effect    : 'fade', 
                animSpeed : 500, 
                pauseTime : 7200
            });
        }
    };

    homepage.handleSubscription();
	  homepage.activateSlider();
  
});

