var Profile = {
    init: function(){
        tinyMCE.init({
            mode : "none",
            theme : "advanced",

            // Theme options
            theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,outdent,indent,|,link,unlink,cleanup",
            theme_advanced_buttons2 : "",
            theme_advanced_buttons3 : "",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            convert_urls : false
        });
    }
}
Profile.init();

Profile.showLogin = function(){
    $.modal.close();
    html = $("#login_block_div").html();
    $("#login_block_div").empty();
    
    $.modal(html, {
        overlayClose:true,
        onClose: function(){
            Profile.close("login_block_div");
        }
    });
    return false;
}

Profile.showReg = function(){
    $.modal.close();
    html = $("#registration_block_div").html();
    $("#registration_block_div").empty();

    $.modal(html, {
        overlayClose:true,
        onClose: function(){
            Profile.close("registration_block_div");
        }
    });
    return false;
}
Profile.showForgot = function(){
    $.modal.close();
    html = $("#forgot_password_block_div").html();
    $("#forgot_password_block_div").empty();

    $.modal(html, {
        overlayClose:true,
        onClose: function(){
            Profile.close("forgot_password_block_div");
        }
    });
    return false;
}

Profile.close = function (id){
    $("#" + id).html ( $('#simplemodal-data').html() );
    $('#' + id).hide();
    $.modal.close();
}

Profile.manageFriens = function (){
    var html = '<div id="ov_fr"><table cellpadding="0" cellspacing="8" width="500" class="friends_list" border=0>';
    html += "<tr><th width='25'></th>"
    html += "<th></th>"
    html += "<th width='50'></th>"
    var col = true;
    link = $('#div_friend a').each(function(){
        tmp = {};
        tmp.link = $(this).attr('href');
        tmp.id = tmp.link.substring(9);
        tmp.id = tmp.id.substring(0, ( tmp.id.length - 1 ) );
        tmp.name = $(this).attr('title');
        tmp.avatar = $(this).children('img').attr('src');
        html +="<tr class='row_" + col + "' id='ov_user_id_" + tmp.id + "'>";
        col = (col)?false:true;
        html +='<td align="center"><a href="' + tmp.link +'" title="' + tmp.name + '"><img src="' + tmp.avatar + '" width="20" height="20" alt="' + tmp.name + '"></a></td>';
        html +='<td><a href="' + tmp.link +'" title="' + tmp.name + '">' + tmp.name + '</a></td>';
        html +='<td  align="center"><a href="#" onclick="return Profile.createPMMessage(\'' + tmp.name + '\');"><img src="/images/005.jpg" width="16" height="18"></a> ';
        html +='<a href="javascript: deleteFriendFromList(' + tmp.id + ');"><img src="/images/delete_s.jpg" width="8" height="8"></a></td>';
        html +="</tr>";
    });
    html += "</table></div>";
    $.modal(html, {
        overlayClose:true,
        position: [200]
        });
    return false;
}

Profile.createPMMessage = function ( userName, block ){
    if( typeof userName == "undefined" ){
        userName = "";
    }
    $.modal.close();

    if( block === true){
        document.getElementById('pm_friend_list').style.display = 'none';
        document.getElementById('to_nick_name').readOnly = true;
        document.getElementById('parent_content').style.display = 'block';
    } else {
        document.getElementById('to_nick_name').readOnly = false;
        document.getElementById('pm_friend_list').style.display = 'block';
        document.getElementById('parent_content').style.display = 'none';
    }

    html = $("#div_create_pm").html();
    $("#div_create_pm").empty();

    $.modal(html, {
        overlayClose:true,
        //        position: [50],
        //        minHeight:550,
        onShow: function(){
            tinyMCE.execCommand('mceAddControl', false, 'pm_content');
            document.getElementById('to_nick_name').value = userName;
            validPMField('to_nick_name');
            loadPMFriendList();
        },
        onClose: function(){
            tinyMCE.execCommand('mceRemoveControl', false, 'pm_content');
            Profile.close("div_create_pm");
        }
    });
    return false;
}

Profile.changePassword = function (){
    $.modal.close();
    html = $("#change_password_div").html();
    $("#change_password_div").empty();

    $.modal(html, {
        overlayClose:true,
        onClose: function(){
            Profile.close("change_password_div");
        }
    });
    return false;
}

Profile.statusInit = function (){
    obj = $("#input_user_status");
    obj.textPlaceholder('#cccccc');
}

Profile.changeStatus = function (){
    status = $("#input_user_status").attr('value');
    if( status == $("#input_user_status").attr('placeholder') ){
        status = "";
    }

    $("#enter_icon").hide();
    AjaxLoading('enter_icon');

    $.ajax( {
        url:"/users/saveStatus/0/json/",
        data: {status: encodeURIComponent( status )},
        dataType:'json',
        type: 'POST',
        complete: function (){
            AjaxLoading('enter_icon');
            $("#enter_icon").show();
        }
    });
    return false;
}
//Profile.setLoginData = function ( data ){
//    if( data.s == 1){
//
//        $("#us_log .profile").attr("href", $("#us_log .profile").attr("href") + data.d.user_id + "/" );
//        $("#us_log .comments").attr('href', $("#us_log .profile").attr("href") + "new_comments/");
//        AV_path = "/img/avatar/";
//
//        img = AV_path + ( ( data.d.avatar_image == null) ? "res_20_20_no.jpg": "resize_20_20/" + data.d.avatar_image );
//        $("#us_log .profile").css('background-image', "url('" + img  + "')");
//
//        $("#us_log .messages").html( data.d.new_messages + "/" + data.d.total_messages );
//        if( data.d.new_messages != "0"){
//            $("#us_log .messages").addClass('new');
//        }
//
//        if( data.d.new_comments != "0"){
//            $("#us_log .comments").html( $("#us_log .comments").html() + " (" + data.d.new_comments + ")" );
//            $("#us_log .comments").addClass('new');
//        }
//
//        $("#us_log").removeClass('display_none');
//        $("#us_nolog").addClass('display_none');
//    } else {
//        $("#us_log").addClass('display_none');
//        $("#us_nolog").removeClass('display_none');
//    }
//}