﻿// JavaScript Document
// ==UserScript==
// @name                首页的“设为首页”和“加入收藏夹”的js功能
// @description         “设为首页”和“加入收藏夹”
// @created date        2009/10/20   14:14
// @created author      Tiger(tigershi0507@gmail.com)
// @version             
// @version owner       Copyright (C) 2009-2010 keerr.com All rights reserved.
// ==/UserScript==

/*************************************************
verified time 	:	2009/10/20   15:30
verified author	:	Tiger
verified purpose:	添加了主的“添加首页”的程序段
**************************************************/

/// <summary>
/// 加入收藏夹
/// </summary>
/// <param name="title">页面名称</param>
/// <param name="url"></param>
function bookmarksite(title, url) {
    if (document.all)
        window.external.AddFavorite(url, title);
    else if (window.sidebar)
        window.sidebar.addPanel(title, url, "");
}

/// <summary>
/// 设为首页
/// </summary>
/// <param name="url">首页地址</param>
function sethomepage(obj) {
    if (document.all) {
        obj.style.behavior = 'url(#default#homepage)';
        obj.setHomePage('http://' + document.location.host);
    }
    else {
        setHomePage('http://' + document.location.host);
    }
}

function setHomePage(url) {
    if (!window.confirm("是否设置http://" + document.location.host + "为首页?")) return false;
    if (window.sidebar) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        }
        catch (e) {
            alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage', url);

    }
}


/// <summary>
/// 全站head的“设置”下拉框的js
/// </summary>
/// <param name="handleSel">点击控制处</param>
/// <param name="dropItemSel">下拉框</param>
/// <param name="parentSel">父元素，为了处理取消click事件的向下传递</param>
/// <param name="afterClickFun">点击后的处理函数（可选）</param>
$.dropdown = function(handleSel, dropItemSel, parentSel, afterClickFun) {
    //var $_drop = $(this).next("#sz_xbox");
    $(handleSel).click(function() {

        //        if ($_drop.css("display") === "none") {
        //            $_drop.hide();
        //        } else {
        //            $_drop.show();
        //        }
        $(dropItemSel).toggle();
    });
    $(parentSel).click(function(event) {
        event.stopPropagation();
    });
    $(document).click(function() {
        $(dropItemSel).hide();
    });
    if ($.isFunction(afterClickFun)) {
        afterClickFun();
    }
};


function ChangerMessageTitle(mcount) {
    //    var _canChangerMessageTitle = $.cookie("CanChangerMessageTitle");
    if (mcount > 0) {
        ChangerTitle(0, document.title);
    }
}
function ChangerTitle(step, _title) {
    step++;
    if (step > 100) { document.title = _title; return; }
    if (step % 2 == 1) { document.title = '【你有新通知】-' + _title; }
    else { document.title = '【　　　　　】-' + _title; }
    setTimeout("ChangerTitle(" + step + ",'" + _title + "')", 380);
}
/// <summary>
/// 获取通知数
/// </summary>
/// <param name=""></param>
/// <param name=""></param>
$.tongzhiNum = function() {
    $.ajax({
        url: '/ajax/getjson.aspx',
        type: 'POST',
        dataType: 'text',
        timeout: 3000,
        data: 'type=NoticesNum',
        cache: false,
        error: function() {
            $(".sh_loading").hide();
            $.popMsgbox("服务器忙碌，请刷新重试", { score: "+0", lc: "-3" }, { top: $(document).scrollTop() + 150 });
        },
        success: function(data) {
            if (data === "0") {
                $("#bubble-notice").hide();
            }
            else if (parseInt(data) > 0) {
                //$("#bubble-notice .navBubbleInt").text(data);
                $("#bubble-notice").show().find(".navBubbleInt").text(data);
                if (parseInt(data) !== _MessageCount) {
                    ChangerMessageTitle(parseInt(data));
                    _MessageCount = parseInt(data);
                }
            }
            setTimeout('$.tongzhiNum();', 20000);
        }
    });
};
$.urltype = {
    getType: function() {
        return $.evalJSON($.cookie("UrlType"));
    },
    setType: function(_typestr) {
        $.cookie("UrlType", _typestr);
    }
};
/********************************************************************
verified time 	:	2010/3/31   18:09
verified author	:	Tiger
verified purpose:	url跳转函数
*********************************************************************/
$.redirect = function(opt) {
    switch (opt["type"]) {
        case 1:     // 直接跳转
            window.location.href = opt["url"];
            break;
        case 2:     // 新窗口跳转
            window.open(opt["url"]);
            break;
        default:
    }
};
$.logredirect = function(_url) {
    $.login.loginFloatbox({
        nextInit: function(params) {
            $.redirect({ type: 1, url: _url });
        },
        params: {}
    });
};

$(function() {
    // 处理下拉的入口函数
    $.dropdown("#sz_main", "#sz_xbox", "#sz_");
    $.dropdown("#head_tz_nav>a", "#head_tz_nav .nav_content", "#head_tz_nav");
    // 获取通知数
    //$.tongzhiNum();
});
