$(document).ready(function() {
    function preload(arrayOfImages, loaded) {
        if (!loaded) {
            $(arrayOfImages).each(function() {
                $('<img/>')[0].src = this;
            });
        }
    }

    var ss_load = false;
    /* Preload Screenshots */
    $('.four-steps').mouseenter(function() {
        preload([
			'/Content/img/screens/screenshot-1-large.jpg',
			'/Content/img/screens/screenshot-2-large.jpg',
			'/Content/img/screens/screenshot-3-large.jpg',
			'/Content/img/screens/screenshot-4-large.jpg'
				], ss_load);
        ss_load = true;
    });


    /* Hover Controls */
    $('.four-steps a').hover(
  		function() {
  		    $(this).append($("<div class='l-overlay fmedium'> <span>View</span> </div>"));
  		    $('.l-overlay').animate({
  		        opacity: 1
  		    }, 150);
  		},
		  function() {
		      $('.l-overlay').animate({
		          opacity: 0
		      }, 150, function() {
		          $('.l-overlay').remove();
		      });
		  }
		);

    /* Sign In Form */

    $('#forgot-password, #js-forgot-password-success').css('opacity', '0');

    /* Show the Forgot Password Slide */
    $('#js-forgot-password-link').click(function(e) {
        $('#sign-in').animate({
            opacity: 0
        }, 200, function() {
            $(this).hide();
            $('#forgot-password').show().animate({
                opacity: 1
            }, 200);
        });
        e.preventDefault();
    });

    /* Toggle back from Forgot Pasword / Forgot Password Success to Sign In */
    $('#js-sign-in-link, #js-forgot-password-success a').click(function(e) {
        $('#forgot-password, #js-forgot-password-success').animate({
            opacity: 0
        }, 200, function() {
            $(this).hide();
            $('#sign-in').show().animate({
                opacity: 1
            }, 200);
        });
        e.preventDefault();
    });

    /* Display the Sign In Container */
    $('#js-sign-in').click(function(e) {
        $('.sign-in-center').css('display', 'block');
        $('.topbar').animate({
            height: '460'
        }, 300, function() {
            $('#sign-in #username').focus();
        });
        e.preventDefault();
    });

    /* Close Everything and reset the form */
    $('.close.sign-in').click(function(e) {
        $('.topbar').animate({
            height: '80'
        }, 300, function() {
            $('#forgot-password, #js-forgot-password-success').hide().css('opacity', '0');
            $('#sign-in').show().css('opacity', '1');
            $('.sign-in-container form input').not("input[name='timezone_offset']").val('');
        });
        e.preventDefault();
    });

    /* Run Validation on Sign In Form Submit */
    $("form#sign-in").submit(function(e) {

        $("form#sign-in .form-error").html("");
        $.post($(this).attr("action"), $(this).serialize(), function(data) {

            if (data.error) {
                _gaq.push(['_trackEvent', 'primary-nav', 'sign-in-error']);
                $("form#sign-in .form-error").html("<p>" + data.error + "</p>");
               
            }
            else if (data.success) {
                _gaq.push(['_trackEvent', 'primary-nav', 'sign-in-success']);
                location.href = data.success;
            }
        }, "json");

        e.preventDefault();
    });

    /* Run Validation on Forgot Password Sign In Form Submit */
    $("form#forgot-password").submit(function(e) {
        $("form#forgot-password .form-error").html("");
        var this_elem = $(this);
        $.post($(this).attr("action"), $(this).serialize(), function(data) {
            
            if (data.error) {
                $("form#forgot-password .form-error").html("<p>" + data.error + "</p>");
                _gaq.push(['_trackEvent', 'primary-nav', 'sign-in-pwreq-error']);
            }
            else if (data.success) {
                $(this_elem).animate({
                    opacity: 0
                }, 200, function() {
                    _gaq.push(['_trackEvent', 'primary-nav', 'sign-in-pwreq-success']);
                    $(this_elem).hide();
                    $('#js-forgot-password-success').show().css('visibility', 'visible').animate({ opacity: 1 });
                    $('#forgot-password input#email').val('');
                });
            }
        }, "json");
        e.preventDefault();
    });

});
