﻿var submitLogin = 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)
                $("<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 {

            $("#login-holder").html($(response.html));
            $("#comment-action").html($(response.commentHtml));
            $(".btn_press_login").unbind("click", showLoginBox)

        }

        $("#login_box").unblock();

    }, "json");

    return false;
}

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(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_commentcontent").val("");
            //$(response.html).prependTo("#comments");

            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");

}

$().ready(function() {
    /*yekta*/
    $(".t_calendar").click(function() {
        $("#calendar_overlay").fadeIn(300);
        $(".calendar-overlay-close").fadeIn(300);
    });
    $("#calendar_overlay").mouseup(function() {
        return false
    });
    var closeCalendarOverlay = function() {
        $('#calendar_overlay').hide();
        $(".calendar-overlay-close").hide();
    }
    $(document).mouseup(function(e) {
        if ($(e.target).hasClass('t_calendar') == 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();


    /***************/

    /*şenol*/
    $("a.fpAction").click(function() {
        if ($("#boxLogin").is(":visible")) {
            $("#boxLogin").hide();
            $("#boxForgotPass").show();
            $("#login-email, #login-password").val("").removeClass("error");
            $("#login-error-text").remove();
        }
        else {
            $("#boxLogin").show();
            $("#boxForgotPass").hide();
            $("#reemail").val("").removeClass("error");
            $("#password-error-text").remove();
        }
    });
    /********/


    $("#form-login").bind("submit", submitLogin);
    $("#form-password").bind("submit", submitPassword);


});

