﻿function OnPageLoad()
{
    GetUserLoginInfo();
}
function ChkSo(ob)
{
    if(ob.k.value=="")
    {
        alert("請輸入要搜索的關鍵詞!");
        return false; 
    }
}
function GetUserLoginInfo()
{
    var Url = IISPath+"ajaxget/CurrentUser.ashx?"+Math.random();
    run_ajax_async(Url,"",OutTopLoginInfo);
}
function OutTopLoginInfo(msg)
{
    var sHtml = "";
    sHtml += "<ul><li><a href=\"" + IISPath + "reg.aspx\" target=_blank'u'><div>加入會員</div></a></li>";
    sHtml += "<li><a href=\"" + IISPath + "login.aspx\" target=_blank'u' ><div>登入</div></a></li></ul>";
    if(msg!=""&&msg!="no")
    {
        sHtml = "";
        sHtml += "<ul><li>歡迎您, <a href='" + IISPath + "u/index.aspx' target=_blank'u' style=\" color:Red\" >" + msg + "</a></li>";
        sHtml += "<li><a href=\"" + IISPath + "u/index.aspx\" target=_blank'u'><div>管理</div></a></li>";
        sHtml += "<li><a href=\"" + IISPath + "u/LogOut.aspx\" target=_blank'u'><div>退出</div></a></li></ul>";
    }
    $("#tright").html(sHtml);
}
//收藏內容
function FavContent(ContentID)
{
    window.open(IISPath+"u/favorite.aspx?t=0&id="+ContentID);
}
//收藏分類
function FavClass(ContentID) {
    window.open(IISPath + "u/favorite.aspx?t=1&id=" + ContentID);
}
window.onload = OnPageLoad;

////////檢測是否有新短信/////////////////
var g_blinkswitch = 0;
var g_blinktitle = document.title;
var IsHaveNewMsgTimer = 0;
var TimeoutShow = 0;
var MsgTip = "";
function blinkNewMsg() {
    document.title = g_blinkswitch % 2 == 0 ? "【】 - " + g_blinktitle : "【" + MsgTip + "】 - " + g_blinktitle;
    g_blinkswitch % 2 == 0 ? $("#msgtip").hide() : $("#msgtip").show();
    g_blinkswitch++;
    TimeoutShow = setTimeout(blinkNewMsg, 1000);
}

function IsHaveNewMsg() {
    var Url = IISPath + "ajaxget/IsNewsMsg.ashx?" + Math.random();
    run_ajax_async(Url, "", Comp_IsHaveNewMsg);
}

function Comp_IsHaveNewMsg(msg) {
    if (msg != undefined && msg != '') {
        var aMsg = msg.split("#\n#");
        var iType = aMsg[0]; //iType為1表示聊天，為2表示短信
        var sMsg = aMsg[1]; //聊天返回請求的用戶，短信返回短信條數
        if (sMsg!=""&&iType == 1) { //聊天
            var sUser = sMsg;
            //$("#tright").append("<li ><a  href=\"" + IISPath + "u/chatonline.aspx?suid=" + sUser + "\" target=_blank'u'><div >【<font id='msgtip'>聊天</font>】</div></a></li>");
            //MsgTip = "有人想與你聊天，點擊右上角的聊天即可展開聊天";
            //setTimeout(blinkNewMsg, 1000);
            clearInterval(IsHaveNewMsgTimer);
            var sHtmls = "<a onclick=\"CloseTipsToCenter()\"  href=\"" + IISPath + "u/chatonline.aspx?suid=" + sUser + "\" target=_blank'u'>【接受進入聊天】</a>"
            sHtmls += " | <a  href=\"javascript:CloseTipsToCenter()\" >【拒絕】</a>";
            OpenTipsToCenter("有人想與你聊天", sHtmls, 300, 100, "#EB8C05", "#E5F5C9");
            PlayCallSound();
        }
        else if (sMsg != "" && iType == 2) { //短信
            var iCount = parseInt(sMsg);
            if (iCount > 0) {
                $("#tright").append("<li ><a  href=\"" + IISPath + "u/msglist.aspx\" target=_blank'u'><div >【<font id='msgtip'>短信</font>】</div></a></li>");
                setTimeout(blinkNewMsg, 1000);
                MsgTip = "你有新的短信，點擊右上角的短信查收";
                clearInterval(IsHaveNewMsgTimer);
            }       
        } 
    }
}

function PlayCallSound() {
    var obMsgSound = $("#MsgSound");
    if (obMsgSound.id == undefined) {
        $("body").append(" <bgsound id=\"MsgSound\" src=\"/sound/call.wav\"  loop=\"1\"> ");
    }
}

function msgevent(iType, Recipient) {
    var Url = IISPath + "ajaxget/msgevent.ashx?t=" + iType + "&u=" + Recipient;
    run_ajax_async(Url, "", null);
    alert("消息已經發送！");
}
//////////////////////////////聊天處理////////////////////////////////////
function CurrentSendMsg(ob) {
    var msg = ob.value;
    if (ToSayUserName == "") {
        alert("請選擇好友！");
        return;
    }
    if (msg == "") {
        alert("不能發送空信息！");
        return;
    }
    var aMsg = [];
    var CurrentDateTime = new Date();
    aMsg.push(CurrentUserName);
    aMsg.push(CurrentNiName);
    aMsg.push(CurrentDateTime.toLocaleString());
    aMsg.push(msg);
    document.getElementById("MsgSound").src = "/sound/send.wav";
    var newmsg = aMsg.join("{\n}");
    AddOneMsgToChatList(newmsg);
    SaveMsg(ToSayUserName, UserOnlineID, msg);
    ob.value = "";
}
//保存一條信息到服務器
function SaveMsg(Recipient,OlineID, msg) {
    var Url = IISPath + "ajaxget/chatevent.ashx?t=1&u=" + Recipient + "&ol=" + OlineID + "&m=" + msg; 
    run_ajax_async(Url, "", null);
}
//向服務器獲取一條信息
function GetMsg(Sender) {
    var Url = IISPath + "ajaxget/chatevent.ashx?t=2&u=" + Sender  + "&i=" + Math.random();
    run_ajax_async(Url, "", Comp_GetMsg);
}
//多條信息用{*\n*} 格式 {#UserName#}{\n}{#NiName#}{\n}#DateTime#}{\n}{#Msg#}{*\n*} .....
function Comp_GetMsg(msg) {
    var aMsg = msg.split("{*\n*}");
    if (aMsg == "") return;
    for (var i = 0; i < aMsg.length; i++) {
        var sMsg = aMsg[i];
        if (sMsg != "") {
            AddOneMsgToChatList(sMsg);
        }
    }
    document.getElementById("MsgSound").src = "/sound/msg.wav";
}
var LoadMsgTimer = 0;
//載入某個人的最新信息
function LoadMsg(Sender) {
    LoadMsgTimer = setInterval('GetMsg("' + Sender + '")', 10000);
}

//格式 {#UserName#}{\n}{#NiName#}{\n}#DateTime#}{\n}{#Msg#}
function AddOneMsgToChatList(msg) {
    var msgTem = '<div class="msglist" ><span class="msgtitle"><a target=_blank href="/u/index.aspx?uid={#UserName#}">{#NiName#}</a> {#DateTime#}</span><br /><span class="msgcontent">{#Msg#}</span></div> ';
    var aMsg = msg.split("{\n}");
    var msghtml = msgTem.replace("{#UserName#}", aMsg[0]).replace("{#NiName#}", aMsg[1]).replace("{#DateTime#}", aMsg[2]).replace("{#Msg#}", aMsg[3]);
    var obScroll = document.getElementById("ChatList");
    $("#ChatList").append(msghtml);
    obScroll.scrollTop = obScroll.scrollHeight;
}
//檢測當前用戶是否有新短信或有人邀請聊天
IsHaveNewMsgTimer = setInterval(IsHaveNewMsg, 10000);

////////////////////////////////////內容頁的圖片瀏覽//////////////////

function ImgObj() {
    this.caption = "";
    this.url = "";
//    if (typeof CustomTags._initialized == "undefined") {  
//    }
//    CustomTags._initialized = true;
}
var GB_ROOT_DIR = "/js/openbox/";
function ContentImgShow(obContentName,sWidth,sHeigth) {
    var image_set = [];
    var SelectOb = "#" + obContentName;
    if(obContentName==null)
    {
        SelectOb = "body";
    }
    $(SelectOb).find("img").each(
		function(i) {
		    var obImg = new ImgObj();
		    obImg.caption = "圖片瀏覽";
		    obImg.url = this.src;
		    image_set.push(obImg);
		    if(sWidth!="")this.style.width = sWidth;
		    if(sHeigth!="")this.style.height = sHeigth;
		    this.style.cursor = "pointer"
		    this.alt = "點擊一下放大圖片";
		    this.onclick = function() { ShowImg(image_set, i) };
		}
	);
	if (image_set.length > 0) {
		document.writeln('<script type="text/javascript" src="/js/openbox/AJS.js"></script>');
		document.writeln('<script type="text/javascript" src="/js/openbox/AJS_fx.js"></script>');
		document.writeln('<script type="text/javascript" src="/js/openbox/gb_scripts.js"></script>');
		document.writeln('<link href="/js/openbox/gb_styles.css" rel="stylesheet" type="text/css" media="all" />');
	}
}
function ShowImg(aImage_set, Index) {
    GB_showImageSet(aImage_set, Index+1);
}