﻿var loginDown = false;
function clearLoginResults() { $('#loginResult').html(''); }
function showLogin() {
    $('#login').show();
    $('#username').focus();
    clearLoginResults();
    $('#forgotPw').hide();
}

function showForgotPw() {
    $('#login').hide();
    $('#email').attr('value', $('#username').val());
    $('#forgotPw').show();
}

function dropLogin() {
    if (!loginDown) {
        showLogin();
        $('#loginDrop').slideDown();
        $('#username').focus();
        loginDown = !loginDown;
        
    } else {
        $('#loginDrop').slideUp();
        loginDown = !loginDown;

    }
    event.preventDefault(); 
}
$(document).ready(function () {

    $("#resetPwBut").bind("mouseover", function () { this.src = "/content/i/resetPwOn.png"; });
    $("#resetPwBut").bind("mouseout", function () { this.src = "/content/i/resetPw.png"; });
    $("#loginBut").bind("mouseover", function () { this.src = "/content/i/headerLoginButOn.png"; });
    $("#loginBut").bind("mouseout", function () { this.src = "/content/i/headerLoginBut.png"; });

    $("#loginCancelBut").bind("mouseover", function () { this.src = "/content/i/headerLoginCancelOn.png"; });
    $("#loginCancelBut").bind("mouseout", function () { this.src = "/content/i/headerLoginCancel.png"; });

    $("#headerLogin").click(dropLogin);

    $('#loginCancel').click(function () {
        $('#loginDrop').slideUp();
        return false;
    });

    $("#cancelForgotPwLink").click(function () {
        showLogin();
        return false;
    });

    $('#forgotPwForm').bind('submit', function () {
        //$('#resetPwBut').attr('disabled', true);
        $.post('/password/userforgotpwjson', $('#forgotPwForm').serialize(), function (json) {
            var message = json.ReturnMessage;
            $('#forgotPwResult').html(json.ReturnMessage);
            if (json.ReturnCode != 0) {
                $('#forgotPwResult').toggleClass('error');
            } else {
                message += ' <a href="" id="forgotPwLoginLink">login</a> now...';
            }

            $('#forgotPwResult').html(message);
            $('#forgotPwLoginLink').click(function () {
                showLogin();
                return false;
            });
        }, 'json');
        return false;
    });

    $("form#nlSub1, form#nlSub2").bind('submit', function () {
        var form = 'form#' + this.id;
        subscribeNewsletter('SubscribeNewsletterJson', 'div.subNlResult', form);
        return false;
    });

    $('form#promoNlSub').bind('submit', function () {
        subscribeNewsletter('SubscribePromoJson', 'div.subPromoNlResult', 'form#promoNlSub');
        return false; 
    });

    $('#loginForm').bind('submit', function () {
        $('#loginBut').attr('disabled', true);
        clearLoginResults();
        $.post('/account/loginjson', $('#loginForm').serialize(), function (json) {
            if (json.ReturnCode == 0)
                location.href = _secureUrl + '/account';
            else {
                $('#loginResult').html('That login information wasn\'t quite right. Did you <a href="" id="forgotPwLink">forget your password</a>?');
                $('#loginBut').attr('disabled', false);
                $('#forgotPwLink').click(function () {
                    showForgotPw();
                    return false;
                });
            }
        }, 'json');
        return false;
    });
});

function subscribeNewsletter(action, resultDiv, form) {
    $.post('/home/' + action, $(form).serialize(), function (json) {
        $(resultDiv).html(json.ReturnMessage);
        if (json.ReturnCode != 0) {
            $(resultDiv).addClass('error');
        } else {
            $(resultDiv).removeClass('error');
        }
    }, 'json');
}
