﻿$().ready(function() {

    var options = {
        success: handleLoginResponse,
        dataType: 'json',
        beforeSubmit: blockPage
    };

    $('#form-login').ajaxForm(options);

    var options2 = {
        success: handlePasswordResponse,
        dataType: 'json',
        beforeSubmit: blockPage
    };

    $('#form-sendpassword').ajaxForm(options2);

    clearLoginForm();
    clearPasswordForm();

    $("#email").bind("focus", onEmailFocus);
    $("#pemail").bind("focus", onPEmailFocus);

});


var submitLoginForm = function() {

    var options = {
        success: handleLoginResponse,
        dataType: 'json',
        beforeSubmit: blockPage
        
    };

    $('#form-login').ajaxSubmit(options);

    return false;

}

var submitPasswordForm = function() {

    var options = {
        success: handlePasswordResponse,
        dataType: 'json',
        beforeSubmit: blockPage
    };

    $('#form-sendpassword').ajaxSubmit(options);

    return false;

}

var handleLoginResponse = function(response) {

    $.unblockUI();

    if (!response.success) {

        highlightLoginInput(response.message);
    }
    else {

        tb_remove();

        //login WTCookie
        if (readCookie('kullaniciDTarihi') == null) {
            createCookie('kullaniciDTarihi', response.kullaniciDTarihi, 10000);
        }
        if (readCookie('kullaniciSehir') == null) {
            createCookie('kullaniciSehir', response.kullaniciSehir, 10000);
        }
        /////////

        document.getElementById("topmovie").postLogin();

        //refreshPanel();

        if (typeof (response.backto) != "undefined" && response.backto != null) {
            window.location = response.backto;
            return;
        }

        if (typeof (response.callback) != "undefined" && response.callback != null && $.isFunction(response.callback)) {
            eval(response.callback)();
            return;
        }
    }

}

var refreshPanel = function() {

    $.get("/member/refreshpanel.aspx", {}, function(response) {

        $("#loginpanel").html($(response));
        bindPanelInboxLink();


    }, "html");

}

var handlePasswordResponse = function(response) {

    $.unblockUI();

    if (!response.success) {

        highlightPasswordInput(response.message);
    }
    else {

        tb_remove();

        clearPasswordForm();

        $(response.successHtml).appendTo("body");
            setTimeout("showPasswordSuccess()", 500);
    }

}

var highlightLoginInput = function(message) {

    $(".txtinput.blue", "#form-login").removeClass("blue").addClass("orange");
    $("#email").val(message);

}

var highlightPasswordInput = function(message) {

    $(".txtinput.blue", "#form-sendpassword").removeClass("blue").addClass("orange");
    $("#pemail").val(message);

}

var clearLoginForm = function() {    

    $("#email").val("");
    $("#password").val("");
    $("#remember").attr("checked", false);

    $(".txtinput.orange", "#form-login").removeClass("orange").addClass("blue");
}

var clearPasswordForm = function() {

    $("#pemail").val("");

    $(".txtinput.blue", "#form-sendpassword").removeClass("orange").addClass("blue");
}

var closeLoginModal = function() {

    closeModal();
    clearLoginForm();

}

var closePasswordModal = function() {

    closeModal();
    clearPasswordForm();

}

var showPasswordSuccess = function() {

    tb_show(null, "#TB_inline?height=400&width=501&inlineId=modal-holder-info&modal=true", false);
}


var onEmailFocus = function() {

    if ($(this).val() == "Wrong email/password!" || $(this).val() == "Yanlış email/şifre!")
        $(this).val("");

}

var onPEmailFocus = function() {

    if ($(this).val() == "Wrong email!" || $(this).val() == "Yanlış email!")
        $(this).val("");

}