﻿/// <reference path="../extjs_intellisense.js" />
/// <reference path="../jBox/jBox.js" />
//下面两个变量在主人动态中用到
var uin = 0;
var cid = 0;      //当前的选择id
var globalSpaceWebRoot = ".";
var globalSpaceSkinUrl = ".";

var _initContent = function(el) {
    //column那个容器
    if (typeof UtilContent == "undefined") {
        UtilContent = {};
    }
    UtilContent.rootElement = el;
    //这个tbody的行
    UtilContent._rows = UtilContent.rootElement.tBodies[0].rows[0];
    //列，google是3列，其实也可以更多 
    UtilContent.column = UtilContent._rows.cells;
    //var userId = $('#userid').val();
    var userId = $('input:last').val();
    var counter = 0;
    for (var i = 0; i < UtilContent.column.length; i++) {
        var ele = UtilContent.column[i];
        for (var j = 0; j < ele.childNodes.length; j++) {
            var ele1 = ele.childNodes[j];
            if (ele1.tagName == "DIV" && ele1.id.substr(0, 2) != "no") {
                //alert(ele1.id);
                readContent(ele1.id.split('_')[1], userId);
            }
        }
    }
};

$(document).ready(function() {
   
    //页面加载后读取模块内容
    //_initContent(document.getElementById('bodyTable'));
    $('a.control').click(function() {
        $(this).blur();
    });
    //GetAboutMe($("#ctl00_ContentPlaceHolderCenter_userid").val());
    GetAboutMe($("#userId").val());
    if ($("#editSetNow").val() == "1") {
        GetModule();
    }

    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }

    var url = document.location.href;
    //给uin赋值
    uin = $("#userId").val();

    if (document.getElementById("module_2")) {
        $.post('/' + globalSpaceWebRoot + '/h/MasterDynamicHandler.ashx?uin=' + uin,
            { 'id': 0 },
            function(data) {
                $("#mdc").html(data);
            }
        );
    }

    //好友新鲜事获取
    if (document.getElementById("module_3")) {
        $.post('/' + globalSpaceWebRoot + '/h/FriendRecentHandler.ashx?uin=' + uin,
            { 'id': 0 },
            function(data) {
                $("#frc").html(data);
            }
        );
    }

    var tip = $("#homeTips").val();
    if (tip != "") {
        showHomeTip(tip);
    }

    var fpr = $("#vistiorfpr").val();
    if (fpr == "0") {
        $("#nav a").attr('href', 'javascript:void(0)');
    }

    if ($("#visitorBlackList").val() == "1") {
        $("#nav a").attr('href', 'javascript:void(0)');
    }
});

function changeCategory(uin, cateId){
	document.location.href = "/"+uin+"/Album&cateId="+cateId;
}

function readContent(id, uin) {
    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    $.post('/' + globalSpaceWebRoot + '/m/ajax?action=read', { 'id': id, 'uin': uin }, function(data) {
        //$('#module_' + id + '_content').html(data);
    });
}
function myToggle(id) {
    //显示或者隐藏模块
    if ($('#module_' + id + '_content').is(':visible')) {
        $('#module_' + id + '_content').hide();
        $('#toggle_' + id).html('+');
    }
    else {
        $('#module_' + id + '_content').show();
        $('#toggle_' + id).html('-');
    }
}

function AddFriend(uid) {
    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    var url = "/" + globalSpaceWebRoot + "/m/ajax?action=AddFriend";
    var param = { "uid": uid };
    $.post(url, param, function(arr) {
        var idx = arr.indexOf("{$}");
        if (idx > -1 && idx < 5) {
            spaceAlert({ msg: arr.split("{$}")[1] });
        }
        else {
            spaceAlert({ msg: "添加失败，请稍后再试。" });
        }
    });
}
function ZoomImg(url) {
    window.open(url, "相片");
}
function GetAboutMe(uin) {
//    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
//        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
//    }
//    var url = "/" + globalSpaceWebRoot + "/home.aspx?action=GetAboutMe&uin=" + uin;
//    //$("#AboutMeDiv").html(url);
//    var param = { "uin": uin };
//    $.post(url, param, GetAboutMeHandle);
//    function GetAboutMeHandle(msg) {
//        $("#AboutMeDiv").html(msg);
//    }
}
//function searchFriend(){
//    var key = $("#searchKey").val();
//    if(key == ""){
//        alert("用户昵称不能为空。");
//        $("#searchKey")[0].focus();
//        return;
//    }
//    var url = "/search?from=1&nc=" + escape(key);
//    window.open(url);
//}
function GetLayout() {
    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    var url = "/" + globalSpaceWebRoot + "/0/set?action=GetLayout";
    var param = {};
    $.post(url, param, GetLayoutHandle);
    function GetLayoutHandle(msg) {
        $("#editSetMenu").html(msg);
    }
}
function SetPageLayOut() {
    var id = $("input[@name=layoutTypeRadio][checked]").val();
    var col1 = "33%";
    var col2 = "66%";
    if (id == "2") {
        col1 = "66%";
        col2 = "33%";
    } else if (id == "3") {
        col1 = "49%";
        col2 = "49%";
    }
    $("#col_1").css("width", col1);
    $("#col_2").css("width", col2);
    $("#layoutIdNew").val(id);
}
function LayoutCancel() {
    var id = $("#defaultLayoutId").val();
    $("#layoutTypeRadio_" + id).attr("checked", "checked");
    SetPageLayOut();
}
function LayoutSave() {
    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    var id = $("input[@name=layoutTypeRadio][checked]").val();
    $.post('/' + globalSpaceWebRoot + '/m/ajax?action=UseLayout', { 'id': id }, function(data) {
        data = data.split('{$}');
        if (data[0] == '1') {
            spaceAlert({ msg: '成功修改布局，该布局将在5分钟内生效' });
        }
        else {
            spaceAlert({ msg: data });
        }
    });
}
function GetSkin(pageNo,uin) {    
    var url = "/"+uin+"/set?action=GetSkin";
    //var pageNo = $("#skinPageNo").val();
    var skinId = $("#layoutSkinNew").val();
    var param = { "pageNo": pageNo, "skinId": skinId };
    $.post(url, param, GetLayoutHandle);
    function GetLayoutHandle(msg) {
        $("#editSetMenu").html(msg);
        if ($("#moduleSet")) {
            $("#moduleSet").removeClass("active");
        }
        if ($("#menuSet")) {
            $("#menuSet").removeClass("active");
        }
        if ($("#skinSet")) {
            $("#skinSet").addClass("active");
        }
        $("#saveHomePageConfig").click(function() {
            UseSkin(); 
        });
    }
}

function UseSkin()
{
    var val = $("input[@name=skinTypeRadio][checked]").val();
    var skinId = val.split("{$}")[0];
    var folder = val.split("{$}")[1]; 
    $('#useSkin_' + skinId + '>input').attr('disabled','disabled');
    $.post('/m/ajax?action=UseSkin',{'folder':folder},function(data){
        data = data.split('{$}');
        if(data[0]=='1')
        {
            //jBox.iframe("http://www.baidu.com","baidu");
            //var boxAlert = jBox.alert('修改皮肤成功，该皮肤将在5分钟内生效');
            onHideFn = function(){ location.reload();}
            spaceAlert({msg:"修改皮肤成功，该皮肤将在5分钟内生效",onHideFn:onHideFn});
        }
        else
        {
            spaceAlert({msg:data});
        }
    });
}

function SetPageImg(id)
{
    if (globalSpaceSkinUrl == "." && $("#globalSpaceSkinUrl")) {
        globalSpaceSkinUrl = $("#globalSpaceSkinUrl").val();
    }    
    var val = $("#" + id).val();   
    $("#" + id).attr("checked","checked");
    var id = val.split("{$}")[0];
    var folder = val.split("{$}")[1];    
    var linkUrl0 = globalSpaceSkinUrl + "/skin/spacefg/" + folder + "/Base2.css";
    var linkUrl1 = globalSpaceSkinUrl + "/skin/spacefg/" + folder + "/Box2.css";
    var linkUrl2 = globalSpaceSkinUrl + "/skin/spacefg/" + folder + "/Layout2.css";
    
    //alert(folder);
    
    //location.href = linkUrl0;
    $("#layoutSkinNew").val(folder);  
    $("link")[0].href = linkUrl0;
    $("link")[1].href = linkUrl1;
    $("link")[2].href = linkUrl2;
    //$("link")[0].href = "/" + globalSpaceWebRoot + "/skin/" + folder + "/default.css";
    $("#ctl00_ContentPlaceHolderTop_control").removeClass("control").removeClass("saveTip").addClass("control").addClass("saveTip");
}

function SetPageSkin() {
    if (globalSpaceSkinUrl == "." && $("#globalSpaceSkinUrl")) {
        globalSpaceSkinUrl = $("#globalSpaceSkinUrl").val();
    }    
    var val = $("input[@name=skinTypeRadio][checked]").val();    
    var id = val.split("{$}")[0];
    var folder = val.split("{$}")[1];    
    var linkUrl0 = globalSpaceSkinUrl + "/skin/spacefg/" + folder + "/Base2.css";
    var linkUrl1 = globalSpaceSkinUrl + "/skin/spacefg/" + folder + "/Box2.css";
    var linkUrl2 = globalSpaceSkinUrl + "/skin/spacefg/" + folder + "/Layout2.css";
    
    //alert(folder);
    
    //location.href = linkUrl0;
    $("#layoutSkinNew").val(folder);  
    $("link")[0].href = linkUrl0;
    $("link")[1].href = linkUrl1;
    $("link")[2].href = linkUrl2;
    //$("link")[0].href = "/" + globalSpaceWebRoot + "/skin/" + folder + "/default.css";
    $("#ctl00_ContentPlaceHolderTop_control").removeClass("control").removeClass("saveTip").addClass("control").addClass("saveTip");
    //alert($("#ctl00_ContentPlaceHolderTop_control").css("z-index"));
}
function GetModule() {
    var url = "/0/set?action=GetModule";
    var param = {};
    $.post(url, param, GetModuleHandle);
    function GetModuleHandle(msg) {
        $("#editSetMenu").html(msg);
    }
}
function AddModule(id) {
    if ($('#module_' + id).length > 0) {
        var html = "<img style=\"cursor:pointer\" src=\"" + $("#globalSpaceSkinUrl").val() + "/skin/images/delModule.gif\" id=\"addModule_" + id + "\" onclick=\"RemoveModule(" + id + ")\" title=\"移除\" />";
        $('#ModuleTd_' + id).html(html);
        $("#module_" + id).show();
    } else {
        var uin = $("#homeUserId").val();
        var url = "/" + uin + "/Read/";
        var param = { 'id': id, 'uin': uin };
        $.post(url, param, AddModuleHandle);
        function AddModuleHandle(msg) {
            var html = $("#col_1").html();
            html = msg + html;
            $("#col_1").html(html);
            html = "<img style=\"cursor:pointer\" src=\"" + $("#globalSpaceSkinUrl").val() + "/skin/images/delModule.gif\" id=\"addModule_" + id + "\" onclick=\"RemoveModule(" + id + ")\" title=\"移除\" />";
            $('#ModuleTd_' + id).html(html);
            _IG_initDrag(document.getElementById('bodyTable'));
        }
    }
}
function RemoveModule(id) {
    var html = "<img style=\"cursor:pointer\" src=\"" + $("#globalSpaceSkinUrl").val() + "/skin/images/addModule.gif\" id=\"addModule_" + id + "\" onclick=\"AddModule(" + id + ")\" title=\"添加\" />";
    $('#ModuleTd_' + id).html(html);
    $("#module_" + id).hide();
}

//主人动态部分的js
function getMsg(id) {
    if (id == cid)
        return;

    $('#mdh' + cid).removeClass('active');
    $('#mdh' + id).addClass('active');
    cid = id;

    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    $.post('/' + globalSpaceWebRoot + '/h/MasterDynamicHandler.ashx?uin=' + uin,
        { 'id': id },
        function(data) {
            document.getElementById("mdc").innerHTML = data;
        }
    );
}

//好友新鲜事部分的JS
function getFr(id) {
    if (id == cid)
        return;

    $('#fr' + cid).removeClass('active');
    $('#fr' + id).addClass('active');
    cid = id;

    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    $.post('/' + globalSpaceWebRoot + '/h/FriendRecentHandler.ashx?uin=' + uin,
        { 'id': id },
        function(data) {
            document.getElementById("frc").innerHTML = data;
        }
    );
}

function showorhide(pid) {
    var postbody = document.getElementById("pb" + pid);
    if (postbody.style.display != "none")
        postbody.style.display = "none";
    else
        postbody.style.display = "block";
}
//小图的的处理
function autoSize(obj, max_w, max_h) {
    MAX_WIDTH = max_h;
    MAX_HEIGHT = max_w;
    IMG_MARGIN = 7;

    var tImg = new Image();
    tImg.onload = function() {
        var w = this.width;
        var h = this.height;
        if (w < MAX_WIDTH && h < MAX_HEIGHT) {
            obj.width = w;
            obj.height = h;
            //obj.style.marginLeft=IMG_MARGIN+(MAX_WIDTH-w)/2+'px';
            //obj.style.marginTop=IMG_MARGIN+(MAX_HEIGHT-h)/2+'px';
        }
        else {
            if (w > h) {
                obj.width = MAX_WIDTH; obj.height = MAX_WIDTH * h / w;
            }
            else {
                obj.height = MAX_HEIGHT; obj.width = MAX_HEIGHT * w / h;
            }
        }
    }
    tImg.src = obj.src;
}

var gamecardTimeout;
function showgamecard1(cgin, gamenick, gamelevel, cgamearea, cgameserver, netid, e) {
    $("#divGameCard").html("");
    //    var dtls = "<div class='usercardshowname'>角色名:" + gamenick + "</div>";
    //    dtls +=    "<div class='usercardshowname'>游戏名:" + cgin + "</div>";
    //    dtls +=    "<div class='usercardshowname'>区域:" + cgamearea + "</div>";
    //    dtls +=    "<div class='usercardshowname'>服务器:" + cgameserver + "</div>";
    //    dtls +=    "<div class='usercardshowname'>等级:" + gamelevel + "</div>";
    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    var dtls = "<div class='gamePopDiv'>";
    dtls += "<table>"
    dtls += "<tr>"
    dtls += "<td class='tar'>角色名：</td>"
    dtls += "<td class=\"tal\">" + gamenick + "</td>"
    dtls += "</tr>"
    dtls += "<tr>"
    dtls += "<td class='tar'>游戏名：</td>"
    dtls += "<td class=\"tal\">" + cgin + "</td>"
    dtls += "</tr>"
    dtls += "<tr>"
    dtls += "<td class='tar'>区域：</td>"
    dtls += "<td class=\"tal\">" + cgamearea + "</td>"
    dtls += "</tr>"
    dtls += "<tr>"
    dtls += "<td class='tar'>服务器：</td>"
    dtls += "<td class=\"tal\"><a href='/" + globalSpaceWebRoot + "/m/NetDiscussMain.html?netid=" + netid + "&nettype=0'>" + cgameserver + "</a></td>"
    dtls += "</tr>"
    dtls += "<tr>"
    dtls += "<td class='tar'>等级：</td>"
    dtls += "<td class=\"tal\">" + gamelevel + "</td>"
    dtls += "</tr>"
    dtls += "</table></div>"
    $("#divGameCard").html(dtls);

    var x = GetElementLeft(e, 0);
    var y = GetElementTop(e, 0);

    $("#divGameCard").css("top", y).css("left", x);
    $("#divGameCard").show();

    $("#divGameCard").unbind();

    $("#divGameCard").bind("mouseenter", function() { if (gamecardTimeout != null) clearTimeout(gamecardTimeout); $("#divGameCard").show() });
    $("#divGameCard").bind("mouseleave", function() { $("#divGameCard").hide() });
}

function hidegamecard1() {
    //$("#divGameCard").html("");
    gamecardTimeout = setTimeout(function() { $("#divGameCard").hide(); }, 200);
}

function GetElementTop(obj, positionNum) {
    var y = obj.offsetTop;
    var theight = obj.offsetHeight / 3;
    //alert(1);
    while (obj = obj.offsetParent) y += obj.offsetTop;
    if (document.all) {
        if (typeof (positionNum) != 'undefined') {
            return y + positionNum + theight;
        } else {
            return y;
        }
    }
    else {
        if (typeof (positionNum) != 'undefined') {
            return (y + positionNum + theight) + "px";
        } else {
            return y + "px";
        }
    }
}
function GetElementLeft(obj, positionNum) {
    var x = obj.offsetLeft;
    var twidth = obj.offsetWidth;
    var cardwidth = parseInt($("#divGameCard").css("width").replace("px", ""), 10);
    while (obj = obj.offsetParent) x += obj.offsetLeft;
    if (document.all) {
        //alert(x-positionNum+twidth+cardwidth);
        if (x - positionNum + twidth + cardwidth > ContainGetElementLeft($("#container")[0])) {
            return x - positionNum - cardwidth - 20;
        }
        else {
            return x - positionNum + twidth - 20;
        }
    } else {
        if (x - positionNum + twidth + cardwidth > ContainGetElementLeft($("#container")[0])) {
            return (x - positionNum - cardwidth - 20) + "px";
        }
        else {
            return (x - positionNum + twidth - 20) + "px";
        }
    }
}

//  增加删除模块
//function changeModuleStatus(moduleId){
//    if($("#modlueListCheck_" + moduleId).attr("class") == "checked"){
//        $("#modlueListCheck_" + moduleId).removeClass("checked");
//        $("#module_" + moduleId).hide();
//    } else {
//        $("#modlueListCheck_" + moduleId).addClass("checked");
//        if($("#module_" + moduleId).length > 0){
//            $("#module_" + moduleId).show();
//        } else {
//            if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
//                globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
//            }
//            var uin = $("#homeUserId").val();
//            var url = "/"+globalSpaceWebRoot+"/" + uin + "/Read/";
//            var param = {"id":moduleId, "uin":uin};
//            $.post(url, param, AddModuleHandle);
//            function AddModuleHandle(msg){
//                var html = $("#col_1").html();
//                html = msg + html;
//                $("#col_1").html(html);
//                _IG_initDrag(document.getElementById('bodyTable'));
//                $("#module_" + moduleId).show();
//            }
//        }
//    }
//}
function changeModuleStatus(moduleId) {
    var checked = $("#moduleList_" + moduleId).attr("checked");
    if (checked) {                                              //  显示
        var modObj = $("#module_" + moduleId);
        if (modObj.length > 0) {
            $("#module_" + moduleId).show();
        } else {
            if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
                globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
            }
            var uin = $("#homeUserId").val();
            var url = "/" + uin + "/Read/";
            var action = "Read";
            var param = { "id": moduleId, "uin": uin };
            $.post(url, param, AddModuleHandle);
            function AddModuleHandle(msg) {
                var html = $("#col_1").html();
                html = msg + html;
                $("#col_1").html(html);
                _IG_initDrag(document.getElementById('bodyTable'));
                $("#module_" + moduleId).show();
            }
        }
    } else {                                                    //  隐藏
        myClose(moduleId);
    }
}

function changeMenuStatus(menuId) {
    var checked = $("#menuList_" + menuId).attr("checked");
    if (checked) {                                              //  显示
        var modObj = $("#menu_" + menuId);
        if (modObj.length > 0) {
            $("#menu_" + menuId).show();
        } else {
            var menus = document.getElementsByName('menuList');
            var value = 0;
            for (var i = 0; i < menus.length; i++) {
                if (menus[i].checked == true) {
                    value++;
                }
            }
            if (value >= 9) {
                spaceAlert({ msg: '最多只能显示8个菜单项' });
                return;
            }

            if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
                globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
            }
            var uin = $("#homeUserId").val();
            var url = "/" + uin + "/ReadMenu/";
            var action = "ReadMenu";
            var param = { "id": menuId, "uin": uin };
            $.post(url, param, AddModuleHandle);
            function AddModuleHandle(msg) {
                var html = $("#col_1").html();
                html = html + msg;
                $("#col_1").html(html);
                _IG_initDrag(document.getElementById('menuTable'));
                $("#menu_" + menuId).show();
            }
        }
    } else {                                                    //  隐藏
        menuClose(menuId);
    }
}

function opengSearch(p1, p2) {
    if (globalSpaceWebRoot == "." && $("#globalSpaceWebRoot")) {
        globalSpaceWebRoot = $("#globalSpaceWebRoot").val();
    }
    window.open('/' + globalSpaceWebRoot + '/gsearch?from=1&' + p1 + '=' + escape(p2));
}
function showHomeTip(msg) {
    //    var html = "<div style=\"padding-top:10px;\">" + msg + "</div>";
    //    var parm={
    //        w:"300",
    //        h:"150",
    //        type:"inline",
    //        model:"1",
    //        title:"",
    //        content:html
    //    } 
    //    var box = jBox.window(parm);
    //    setTimeout(function(){
    //        if(jBox.boxes[0]){
    //            jBox.close(jBox.boxes[0]);
    //        }
    //    }, 3000);

    var insertHtml = "";
    insertHtml += "<div class='warnInfo'>";
    insertHtml += "<div class=\"warnInfoLogo\"></div>";
    insertHtml += "<div class=\"warnInfoCon\">";
    insertHtml += msg;
    insertHtml += "</div>"; //warnInfoCon end
    insertHtml += "<div class=\"clear\"></div>";
    insertHtml += "</div>"; //warninfo end
    insertHtml += ""; //module end

    $(insertHtml).appendTo("#col_2");
}
