function DefaultLoadSifr() {
    $.sifrSettings({ path:'/swf/' });
    $(".SifredText").sifr({font: 'hlvtcond'});
}

function enter_DoFSCommand(command, arguments) {
    window[command](arguments);
}

function userAvatarConstructor_DoFSCommand(command, arguments) {
    console.log(command + " " + arguments);
    window[command](arguments);
}

function EndSaveAvatar(str) {
    console.log(typeof str);
    $.post('/signup', $('form#registerPopup').serialize()+"&avatar="+encodeURIComponent(Base64.encode(str)), function(data) {
        window.location.reload();
    });
}

function getFlashMovieObject(movieName){
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName];
    } else {
        return document.getElementById(movieName);
    }
}

function getAvatarSerializedData() {
    var flashMovie = getFlashMovieObject("userAvatarConstructor");
    flashMovie.BeginSaveAvatar();
}

function showAuthForm() {
    $("#divCommonTooltipContainer").hide();
    $("#overlay").height($(document).height());
    $("#overlay").show();
    $('#auth .login-section').show();
    $("#auth").fadeIn();
    $('#divMessage').html('');
    $("#auth #inpLoginFormUserName").focus();
    //$("select").hide();

    return false;
}

function showSignUp() {
    $("#divCommonTooltipContainer").hide();
    $("#overlay").height($(document).height());
    $("#overlay").show();
    $('#registerPopup').css("top", "30%");
    $('#registerPopup').show();
    $("#registerPopup").fadeIn();
    $('#divMessage').html('');
    $("#registerPopup").focus();
}

function CreateLoginRequest (login, password, saveCookie) {
    return '<login>' + '<uid>' + login + '</uid><pwd>' + MD5(password) + '</pwd>' + '<cookie>' + ((saveCookie == true) ? 'enable' : '') + '</cookie>' + '</login>';
}

$(document).ready(
    function() {
        $(".close a").click(function(){
            $("#overlay, #popup, .popup, .to-be-hide, .preloader").fadeOut();
            $("select").show();
            return false;
        });
        $("#auth .close, #overlay").click(function(){
            $("#overlay").fadeOut();
            $("#auth").fadeOut();
            $("select").show();
            return false;
        });
        $("#EmailConfirmationForm .close, #overlay").click(function(){
            $("#overlay").fadeOut();
            $("#EmailConfirmationForm").fadeOut();
            $("select").show();
            return false;
        });

        $("#registerPopup .close, #overlay").click(function(){
            $("#overlay").fadeOut();
            $("#registerPopup").fadeOut();
            $("select").show();
            return false;
        });

        $("#regButton").click(function() {
            getAvatarSerializedData();
        });


        $('#btnLogin').click(function() {
            $('#divMessage').html('');
            $('#auth .login-section').hide();
            $('#auth .loader').show();
            $('#auth .wait').show();

            $.ajax({
                url: "/login",
                data: $('form#auth').serialize(),
                method: "POST",
                statusCode: {
                    403: function() {
                         $("#overlay").hide();
                         $("#auth").fadeOut();
                         window.location.href = '/main';
                    },
                    302: function() {
                         $("#overlay").hide();
                         $("#auth").fadeOut();
                         window.location.href = '/main';
                    }
                }
            }).done(function(data) {
                 if(data.indexOf('<span>login_failed</span>') != -1) {
                     $('#divMessage').html('Неверное имя пользователя или пароль.');
                     $('#auth .login-section').show();
                     $('#divMessage').show();
                 } else {
                     $("#overlay").hide();
                     $("#auth").fadeOut();
                     window.location.href = '/main'; // GameIndex.aspx
                 }
                 $('#auth .loader').hide();
                 $('#auth .wait').hide();
            });

            return false;
        });
		
		if( !isFlashInstalled ) {
			$("#enter").css("display", "none");
			showAuthForm();
		}
    }
);

function OnStartGameClick() {
    showAuthForm();
}

function OnRulesClick() {
    $("#rulesPopup .flash").html("");
    $("#rulesPopup .flash").html('<embed name="rules" id="rules" align="middle" allowFullScreen="false" src="/file/pub/rules.swf" wmode="transparent" width="750" height="473" quality="high" bgcolor="#FFFFFF" allowScriptAccess="sameDomain"></embed>');
    $("#overlay").height($(document).height());
    $("#overlay").show();
    $(".popup:not(#rulesPopup)").fadeOut(500, function(){
        $('#rulesPopup').fadeIn();

    });
}

function OnGetShararamCardClick(event) {
    $(".ShowRegistrationFormLink").click(function () {

    })
}

function closeRules() {
    $("#rulesPopup .flash").html("");

    $('#rulesPopup').fadeOut(500);
    $('.popup .close').trigger('click');
    $("#overlay").fadeOut();
    $("#overlay").hide();
}

function SecurityPopup(){
    $(".popup:not(#securityPopup)").fadeOut(500, function(){
        $('#securityPopup').show();
    });

    initRollovers();
}

function OnExcursionClick(){
    $('.excursion a').trigger("click");
}
