﻿var wrapper;
var tip;
var spaceUrl;
var spaceUrl="";
var ism=false;
var spaceID=0;
var uid=0;
var isi=false;
var suid=0;
function blogInit(){
    wrapper=$("#modContainer");
    tip=$("#ajaxTip");
    //ajax事件交互提示
    tip.ajaxStart(function(){
        $(this).show();
    }).ajaxStop(function(){
        $(this).html("").hide();
    });
    //选择对话框标题框事件
    $(".dialog dt").toggle(function(){
        $(this).addClass("open");
        $(this).parent().find("dd").show();
        
    },function(){
        $(this).removeClass("open");
        $(this).parent().find("dd").hide();
    });
    
    tip.html("正在加载页面布局...");
    $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"getlayout",t:"s"},function(m){
        wrapper.append(m);
        blogGetMod();
    });
}

function blogGetMod(){
    var preload=$("#preload");
    var footer=$("#innerFooter");
    tip.html("正在加载用户模块...");
    $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"getmod"},function(m){

        $.each(m,function(i){
            var mod=AddModuleFrame(m[i].modID,m[i].modType,m[i].title,m[i].panel,m[i].ctler);
            GetModuleData(mod,m[i].ctler,m[i].config);
        });
        modEventInit();
        if($().lcSortable)
        {
            startDrop();
        }
        footer.show();
        preload.hide();
    },"json");
}
function AddModuleFrame(id,type,title,panelID,ctler){      
    var modStr="";
    modStr+="<div id=\"mod_"+id+"\" class=\"mod dropMod\" tid=\""+type+"\" ctler=\""+ctler+"\">";
    modStr+="    <div class=\"mheader lt\"><div class=\"mheader rt\"><div class=\"mheader t dropItem \">";
    modStr+="    <div class=\"mcaption modico_"+type+"\">"+title+"</div>";
    if(ism){
        modStr+="<span class='del'></span>";
    }
    else{
//        if(uid!=0){
//            modStr+="<b><a href='javascript:;' onclick=\"addModToMyBlog("+type+")\">添加到我的博客</a></b>";
//        }
    }
    modStr+="</div></div></div>";
    modStr+="    <div class=\"mbody\">";
    modStr+="        请稍候，正在加载内容...";
    modStr+="    </div>";
    modStr+="    <div class=\"mfooter lb\"><div class=\"mfooter rb\"><div class=\"mfooter b\"></div></div></div>";
    modStr+="</div>";
    var panel=$("#"+panelID);
    if(panel.length<1){
        var pid=Number(panelID.replace("panel",""));
        pid=pid>1?pid-1:pid;
        panel=$("#panel"+pid);
    }
    modStr=$(modStr);
    panel.append(modStr);
    return modStr;
}
function GetModuleData(mod,ctler,config){
    tip.html("正在读取数据...");
    $.post("/space/module/"+ctler+"?rnd="+Math.random(),{uid:suid,config:config},function(m){
        mod.find(".mbody").html(m);
    });
}

function startDrop(){
    $().lcSortable({
        submitUrl:spaceUrl+"/spaceajaxpage.aspx",
        submitPras:"f=cmp"
    },function(modID,modIDList,panelID){
        tip.html("正在保存模块位置...");
        $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"cmp",pi:panelID,mi:modID,midl:modIDList},function(m){
            if(m.err){
                alert(m.msg);
            }
        },"json");
    });
}
function modEventInit(){
    if(ism){
    $(".dropMod .dropItem").unbind("mouseover").mouseover(function(){
        $(this).find("span").show();
    }).unbind("mouseout").mouseout(function(){
        $(this).find("span").hide();
    });
    }else{
    $(".dropMod .dropItem").unbind("mouseover").mouseover(function(){
        $(this).find("b").show();
    }).unbind("mouseout").mouseout(function(){
        $(this).find("b").hide();
    });
    }
    $(".dropMod .del").css("cursor","pointer").unbind("click").click(function(){
        modid=$(this).parents(".dropMod").attr("id");
        if(confirm("你确实要删除此模块吗？\r\n如果要再次添加此模块请点击导航栏中的添加模块！")){
            delModule(modid);
        }
    });
}
//显示对话框
function showDialog(ctl,sender){
    $(".dialog").hide();
    var dialog=$("#"+ctl);
    sender=$(sender);
    var window = $(document.body);
    var height=document.documentElement.clientHeight;
    var width=document.documentElement.clientWidth;
    dialog.css({left:(width-dialog.width())/2,top:sender.offset().top+30});
    dialog.show();
}
function closeDialog(sender){
    $(sender).parents(".dialog").hide();
}
function closeDialogByID(ctl){
    $("#"+ctl).hide();
}

//添加模块
function addModule(controler){
    closeDialogByID("modDialog");
    tip.html("正在添加模块...");
    $.post(spaceUrl+"/postmodule.aspx",{f:"add",mti:controler},function(m){
        mod=AddModuleFrame(m.msg.modID,m.msg.modType,m.msg.title,m.msg.panel);
        GetModuleData(mod,m.msg.ctler,m.msg.config);
        var a=$("#a_"+controler);
        var content="<span id='span_"+controler+"'>"+a.html()+"<b></b></span>";
        a.replaceWith(content);
        startDrop();
        modEventInit();
    },"json");
}
//删除模块
function delModule(modid){
    var id=modid.replace("mod_","");
    var typeid=$("#"+modid).attr("tid");
    tip.html("正在删除模块...");
    $.post(spaceUrl+"/postmodule.aspx",{f:"del",mi:id},function(e){
        if(e.err)
            alert(e.msg);
        else{
            var span=$("#span_"+typeid);
            span.find("b").remove();
            var con = "<a id='a_"+typeid+"' href='javascript:;' onclick='addModule("+typeid+");'>"+span.html()+"</a>";
            span.replaceWith(con);
            $("#"+modid).remove();
        }
    },"json");
}
//工具条显示/隐藏事件
$().ready(function(){
    $("#toolBarHide").click(function(){
        $("#innerToolBar").hide();
        $("#toolBarShow").show();
    });
    $("#toolBarShow").click(function(){
        $("#innerToolBar").show();
        $(this).hide();
    });
});

//设置主题
function setTheme(themeID,spaceType){
    tip.html("正在应用新主题...");
    $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"settheme",theme:themeID},function(){
        var cssFile="/templates/default/html/space/themes/"+spaceType+"/"+themeID+"/style.css";
        $("#themeCSS").attr("href",cssFile);
    });
}

//设置布局
function setLayout(layoutFile){
    tip.html("正在应用新布局...");
    $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"setlayout",layout:layoutFile},function(e){
        if(e.err){
            alert("修改布局时出错,请稍候再试...");
        }else{
            location.reload();
        }
    },"json");
}

//其它功能*************************************************************************************
//“给我留言”点击事件
function sendMessageInit(obj){
    $("#msgForm").toggle();
    $("#sendMessage").unbind("click").click(function(){
        var btn=$(this);
        var ctn = $("#msgCtn").val();
        var pri = $("#msgPri").attr("checked");
        var uname=$("#username").val();
        if(trim(ctn).length<1){
            alert("留言内容不能为空");
            return false;    
        }
        if($("#divUserName").css("display")!="none")
        {
            if(uname=="")
            {
                alert("称呼不能为空！");
                return false;
            }
        }
        if(isi)
        tip.html("正在保存留言...");
        $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"sendmessage",ctn:ctn,uname:uname,pri:pri},function(e){
            if(e.err){
                alert(e.msg);
            }
            else{
                if(isi){
                    var mod=$(btn).parents(".mod");
                    GetModuleData(mod,mod.attr("ctler"));
                }else{
                    location.reload();
                }
            }
        },"json");
    });
}
function delMessage(mid,obj){   
    if(confirm("是否要删除该留言？"))
    {
        if(isi)
            tip.html("正在删除留言...");
        $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"delmessage",mid:mid},function(e){
            if(e.err){
                alert(e.msg);
            }
            else{
                $(obj).parents("li").remove();
            }
        },"json");
    }
}
//添加到我的博客功能事件
function addModToMyBlog(type){
    tip.html("正在向您的空间添加模块...");
    $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"addmodtomyblog",mti:type},function(e){
        if(e.err)
            alert(e.msg);
        else{
            alert("该模块已成功添加到您的博客中");
        }
    },"json");
}

//添加空间好友
function addBlogFriend(fid){
    if(fid==0){
        alert("错误的参数提交，请刷新页面后重试");
        return false;
    }
    if(uid==0){
        alert("请登录后再执行此操作");
        return false;
    }
    
    tip.html("正在执行添加好友操作...");
    $.post(spaceUrl+"/spaceajaxpage.aspx",{f:"addfriend",fid:fid},function(e){
        if(e.err)
            alert(e.msg);
        else
            alert("已成功添加好友");
    },"json");
}
