﻿// 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:	添加了主的“添加首页”的程序段
**************************************************/
/********************************************************************
verified time 	:	2010/4/20   15:06
verified author	:	Tiger
verified purpose:	完成了天气方面的改进
*********************************************************************/

/// <summary>
/// 加入收藏夹
/// </summary>
/// <param name="title">页面名称</param>
/// <param name="url"></param>
var bookmarksite = function (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>
var sethomepage = function (obj) {
    if (document.all) {
        obj.style.behavior = 'url(#default#homepage)';
        obj.setHomePage('http://' + document.location.host);
    }
    else {
        setHomePage('http://' + document.location.host);
    }
};

var setHomePage = function (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() {
//    $.dropdown("#fb_drop_handler", "#sz_xbox", "#fb_drop");

//});

function topHead() {
    $("body").css({ "background": "#FFFFFF url(/images/hd/bgimg_bg.jpg) repeat-x scroll center top" }).append("<div class='bgimg'></div>");
}


/// <summary>
/// 头部天气
/// </summary>
var headWeather = function () {
    var cNumber = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
    var d = new Date(),
        t = d.getDay();
    var getDayArray = function () {
        var newStr = "";
        $.each(cNumber, function (i, v) {
            if (Math.floor((i + t) / 8) === 0 && (t + 3) > 7) {
                newStr += v + ",";
            } else if (i >= t - 1) {
                newStr += v + ",";
            };
        });
        return newStr;
    };
    var nextDayStr = getDayArray();

    $.ajax({
        url: "/ajax/getJson.aspx",
        type: "post",
        data: { type: "getWeather" },
        dataType: "text",
        cache: false,
        success: function (data) {
            $.ajax({
                url: data,
                dataType: "xml",
                cache: false,
                success: function (xmlDom) {
                    var $_data = $(xmlDom);
                    var curImgUrl = $_data.find("current_conditions icon").attr("data"),
                        curTmp = $_data.find("current_conditions temp_c").attr("data");
                    var n = 0;


                    $("#curImg").attr("src", "http://www.google.com" + curImgUrl);
                    $("#curTmp").text(curTmp);

                    //                    $("#nextWeather li").each(function(i) {
                    //                        var $_item = $_data.find("forecast_conditions").eq(i),
                    //                            date = $_item.find("day_of_week").attr("data"),
                    //                            low = $_item.find("low").attr("data"),
                    //                            high = $_item.find("high").attr("data"),
                    //                            imgUrl = "http://www.google.com" + $_item.find("icon").attr("data"),
                    //                            condition = $_item.find("condition").attr("data");
                    //                        var $_this = $(this);
                    //                        $_this.find(".tqimg").attr("src", imgUrl);
                    //                        $_this.find("b").text("宁波" + date);
                    //                        $_this.find(".blue").text(condition);
                    //                        $_this.find(".temp").text("最高/最低气温：" + high + "°C-" + low + "°C");
                    //                    });

                    //                    $_data.find("forecast_conditions").each(function(i) {
                    //                        var $_this = $(this),
                    //                            date = $_this.find("day_of_week").attr("data"),
                    //                            low = $_this.find("low").attr("data"),
                    //                            high = $_this.find("high").attr("data"),
                    //                            imgUrl = "http://www.google.com" + $_this.find("icon").attr("data"),
                    //                            condition = $_this.find("condition").attr("data");
                    //                        var $_item = $("#nextWeather li");
                    //                        if (nextDayStr.indexOf(date) !== -1) {
                    //                            var $_li = $_item.eq(n);
                    //                            $_li.find(".tqimg").attr("src", imgUrl);
                    //                            $_li.find("b").text("宁波" + date);
                    //                            $_li.find(".blue").text(condition);
                    //                            $_li.find(".temp").text("最高/最低气温：" + high + "°C-" + low + "°C");
                    //                            n++;
                    //                        }
                    //                    });
                    var $_item = $("#nextWeather ul").empty();
                    $_data.find("forecast_conditions").each(function (i) {
                        var $_this = $(this),
                            date = $_this.find("day_of_week").attr("data"),
                            low = $_this.find("low").attr("data"),
                            high = $_this.find("high").attr("data"),
                            imgUrl = "http://www.google.com" + $_this.find("icon").attr("data"),
                            condition = $_this.find("condition").attr("data");

                        if (nextDayStr.indexOf(date) !== -1) {
                            var $_li = '<li><img class="left" width="40" height="40" src="' + imgUrl + '"><b>宁波' + date + '</b>：<span class="blue">' + condition + '</span><br><span>最高/最低气温：' + high + '°C-' + low + '°C</span><br></li>';
                            $_item.append($_li);
                        }
                    });
                },
                error: function () {
                }
            });



        },
        error: function () {
        }
    });
};

/// <summary>
/// 头部天气
/// </summary>
var headWeatherByPHP = function () {
    var cNumber = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    var d = new Date(),
        t = d.getDay();
    var getDayArray = function () {
        var newStr = "";
        $.each(cNumber, function (i, v) {
            if (Math.floor((i + t) / 8) === 0 && (t + 3) > 7) {
                newStr += v + ",";
            } else if (i >= t - 1) {
                newStr += v + ",";
            };
        });
        return newStr;
    };
    var nextDayStr = getDayArray();

    $.ajax({
        url: "/static/weather/getRealWeatherData.php",
        type: "post",
        data: "",
        dataType: "text",
        cache: false,
        success: function (data) {
            var json = eval("(" + data + ")"),
                weather = json["weatherinfo"];
            var curTmp = weather["temp"];

            $("#curTmp").text(curTmp);
        }
    });

    $.ajax({
        url: "/static/weather/getWeatherData.php",
        type: "post",
        data: "",
        dataType: "text",
        cache: false,
        success: function (data) {
            var json = eval("(" + data + ")"),
                weather = json["weatherinfo"];
            var curImgUrl = "http://www.weather.com.cn/m2/i/icon_weather/29x20/d" + weather["img_single"] + ".gif";
            var n = 0;

            $("#curImg").attr("src", curImgUrl);

            var $_item = $("#nextWeather ul").empty();
            for (var i = 1; i < 6; i++) {
                var temp = weather["temp" + i],
                    imgUrl = "http://www.weather.com.cn/m2/i/icon_weather/29x20/d" + weather["img" + (i * 2 - 1)] + ".gif",
                    condition = weather["weather" + i];
                var $_li = '<li><img class="left" width="40" height="40" src="' + imgUrl + '"><b>' + cNumber[(t + i - 1) % 7] + '</b>：<span class="blue">' + condition + '</span><br><span>最高/最低气温：' + temp + '</span><br></li>';
                $_item.append($_li);
            }
        }
    });
};

$(function () {
    //headWeather();
    headWeatherByPHP();
});