﻿var submitLogin = function () {
    StopAnimation = false;
    $('.Overlay, .LoaderImage').fadeIn(300, LoadingFadeOut);
    $("#form-login").unbind("submit", submitLogin);

    //$("#login_box").block({ message: null, overlayCSS: { backgroundColor: '#ffffff', opacity: 0} });

    $.post($("#form-login").attr("action"), $("#form-login").serialize() + "&r=" + Math.random(), function (response) {

        if (!response.success) {

            if ($("#login-error-text").length > 0)
                $("#login-error-text").remove();

            $("<p class=\"error_text\" id=\"login-error-text\" style=\"padding: 10px 0 3px;\">" + response.errorMessage + "</p>").prependTo("#email-Label");
            $("#login-email, #login-password").addClass("error");

            $("#form-login").bind("submit", submitLogin);

        }
        else {
            var secondaryNav = document.getElementById("secondaryNav");
            $("#login-holder").fadeOut(200, function () {
                $("#login-holder").html($(response.html));
                if (secondaryNav.getAttribute("rel") == "EN") {
                    secondaryNav.style.width = "515px";
                }
                $("#login-holder").fadeIn(400);
            });
            $("#comment-action").html($(response.commentHtml));
            $(".btn_press_login").unbind("click", showLoginBox)

        }
        StopAnimation = true;
        $('.Overlay, .LoaderImage').fadeOut(200);
        //$("#login_box").unblock();

    }, "json");

    return false;
}


var StopAnimation = false;
var LoadingFadeIn = function () { if (!StopAnimation) { $('.LoaderImage').fadeIn(500, LoadingFadeOut); } }
var LoadingFadeOut = function () { if (!StopAnimation) { $('.LoaderImage').fadeOut(500, LoadingFadeIn); } }

var submitPassword = function () {

    $("#form-password").unbind("submit", submitPassword);

    $("#login_box").block({ message: null, overlayCSS: { backgroundColor: '#ffffff', opacity: 0} });

    $.post($("#form-password").attr("action"), $("#form-password").serialize(), function (response) {

        if (!response.success) {

            if ($("#password-error-text").length == 0)
                $("<p class=\"error_text\" id=\"password-error-text\">" + response.errorMessage + "</p>").prependTo("#form-password");
            else
                $("#password-error-text").html(response.errorMessage);
            $("#reemail").addClass("error");

            $("#form-password").bind("submit", submitPassword);

        }
        else {

            if ($("#password-error-text").length == 0)
                $("<p class=\"error_text\" id=\"password-error-text\">" + response.successMessage + "</p>").prependTo("#form-password");
            else
                $("#password-error-text").html(response.successMessage);

            $("#reemail").removeClass("error");

        }

        $("#login_box").unblock();

    }, "json");

    return false;
}

var submitComment = function () {


    $("#form-comment").unbind("submit", submitComment);

    $.post($("#form-comment").attr("action"), $("#form-comment").serialize(), function (response) {

        $("#form-comment").bind("submit", submitComment);

        if (!response.success) {

            if (response.errorType == "login") {
                $("#comment-write").hide();
                $("#comment-login").show();
            }
            else if (response.errorType == "val") {
                var obj = $("#comment-label");
                if ($.trim(obj.attr("title")) == "")
                    obj.attr("title", obj.html());
                obj.html("");
                obj.html(response.errorMessage);
                obj.css("color", "red");

            }
        }
        else {
            var obj = $("#comment-label");
            if ($.trim(obj.attr("title")) != "") {

                obj.html(obj.attr("title"));
                obj.attr("title", "");
                obj.css("color", "#2E2E2E");
            }

            $("#Comment_Message").val("");
            if (response.login == "true") {
                $("#login-holder").html($(response.html));
                $("#comment-action").html($(response.commentHtml));
                $(".btn_press_login").unbind("click", showLoginBox)
            }
            if (response.comment == "true") {
                jQuery.prompt(response.successMsg);
            }

        }


    }, "json");

    return false;

}

var openPlayer = function (eventID, contentID, starttype) {

    window.open("/player/index.aspx?eventID=" + eventID + "&contentID=" + contentID + "&starttype=" + starttype, "BabylonMediaCenter", "status=0,menubar=0,toolbar=0,width=810,height=600,resizable=0,location=0");

}

var openTVPlayer = function () {

    window.open("/player/tv.aspx", "BabylonMediaCenter", "status=0,menubar=0,toolbar=0,width=810,height=600,resizable=0,location=0");

}



var Subscribe = function () { return OpenPage("http://strongmail.babylon.com.tr/Default.aspx/Subscription/"); }
var ForgotPassword = function () { return OpenPage("http://strongmail.babylon.com.tr/Default.aspx/RemindPassword/"); }
var Profile = function (url) { return OpenPage(url); }

function OpenPage(url) {
    var width = 900;
    var height = 600;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var params = 'width=' + width + ', height=' + height;
    params += ', top=' + top + ', left=' + left;
    params += ', scrollbars=1';
    params += ', resizable=1';
    params += ', directories=0';
    params += ', location=0';
    params += ', menubar=0';
    params += ', toolbar=0';
    params += ', status=0';

    var ContentPopup = window.open(url, 'StrongMail', params);
    if (window.focus && ContentPopup != null) { ContentPopup.focus() }
    return false;
}

$(document).ready(function () {
    $("#form-login").bind("submit", submitLogin);

    $(".t_calendar").click(function () {
        $("#calendar_overlay").fadeIn(300);
        $(".calendar-overlay-close").fadeIn(300);
    });
    var closeCalendarOverlay = function () {
        $('#calendar_overlay').fadeOut(300);
        $(".calendar-overlay-close").fadeOut(300);
    }
    $(document).mouseup(function (e) {
        if ($(e.target).hasClass('t_calendar') == false && $(e.target)[0].tagName != "OBJECT" && $(e.target).hasClass('wrap') == false) {
            closeCalendarOverlay();
        }
    });
    $('.calendar-overlay-close').click(closeCalendarOverlay);


    function fixHeight() {
        var contentHeight = $('#content');
        var mainOuterHeight = contentHeight.eq(0).outerHeight();
        var sidebarOuterHeight = $('.event_sidebar').eq(0).outerHeight();

        var sidebarHeight = $('.event_sidebar').eq(0).height();
        var mainHeight = contentHeight.eq(0).height();

        // if sidebar is bigger
        if (mainOuterHeight < sidebarOuterHeight) {
            // calculate padding
            var padding = mainOuterHeight - mainHeight;
            // calculate new height
            var height = sidebarOuterHeight - padding;
            // set new height
            contentHeight.css('height', height);
        }

    }
    fixHeight();


    /***************/

    $("#form-password").bind("submit", submitPassword);

    $("#remember").click(function () {
        if (this.checked)
            setCookie("rememberState", "1", 365);
        else
            setCookie("rememberState", "0", 365);

        if ($("#c_remember")[0] != undefined)
            $("#c_remember")[0].checked = this.checked;
    });

    $("#c_remember").click(function () {
        if (this.checked)
            setCookie("rememberState", "1", 365);
        else
            setCookie("rememberState", "0", 365);

        if ($("#remember")[0] != undefined)
            $("#remember")[0].checked = this.checked;
    });

    if ($("#remember")[0] != undefined) {
        var state = getCookie("rememberState");
        if (state != null && state == "1")
            $("#remember")[0].checked = true;
        else
            $("#remember")[0].checked = false;
    }

    if ($("#c_remember")[0] != undefined) {
        var state = getCookie("rememberState");
        if (state != null && state == "1")
            $("#c_remember")[0].checked = true;
        else
            $("#c_remember")[0].checked = false;
    }
});

var submitLoginComment = function () {

    $("#form-login").unbind("submit", submitLogin);

    //$("#login_box").block({ message: null, overlayCSS: { backgroundColor: '#ffffff', opacity: 0} });

    $.post($("#form-login").attr("action"), $("#form-login").serialize(), function (response) {

        if (!response.success) {

            if ($("#login-error-text").length > 0)
                $("#login-error-text").remove();

            $("<p class=\"error_text\" id=\"login-error-text\">" + response.errorMessage + "</p>").prependTo("#form-login");
            $("#login-email, #login-password").addClass("error");

            $("#form-login").bind("submit", submitLogin);

        }
        else {
            var secondaryNav = document.getElementById("secondaryNav");
            if (secondaryNav.rel == 'EN') {
            }
            secondaryNav.style.width = 532;
            $("#login-holder").html($(response.html));
            $("#comment-action").html($(response.commentHtml));
            $(".btn_press_login").unbind("click", showLoginBox)

        }

        $("#login_box").unblock();

    }, "json");

    return false;
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}
