﻿/// <reference path="jquery/jquery126.js" />
/// <reference path="jquery/JQuery.Intellisense.js" />

var _code = "";
var _cityname = "";

$(function() {

    $("#weathertown .mainsite li a").each(function() {
        var _this = this;
        $(this).click(function() {
            $("#weathertown li a").removeClass("selected");
            $(this).addClass("selected");
            loadWeather($(_this).attr("name"), $(_this).text(), $("#weathertown li a").index(_this));
        });
    });

    $("#weathertown .mainsite li a").eq(0).addClass("selected");


    loadWeather("", "市区", 0);
    window.lastwIndex = 0;

    $("#cityname").focus(function() {
        var val = $(this).val();
        if (val.indexOf("名称") > -1) { $(this).val("") }
    });
    $("#cityname").autocomplete('api/?mod=Weather&action=SeachWeatherCity&top=10');
    $("#cityname").result(function(event, data, formatted) { queryWeather(); });

    var code = $("#weathertown li a").eq(0).attr("name");
    _cityname = "市区"
    showssites.json("Weather/GetCitys");
});

function showssites(json) {
    var sb = new Array();
    $.each(json, function(i, item) {
        sb.push('<li><a href="javascript:;" name="' + item.Code + '">');
        sb.push(item.CityName + '</a></li>');
    });
    if (sb.length > 0) $("#seachweahter .ssite").html(sb.join(""));

    $("#weathertown .ssite li a").each(function() {
        var _this = this;
        $(this).click(function() {
            $("#weathertown li a").removeClass("selected");
            $(this).addClass("selected");
            loadWeather($(_this).attr("name"), $(_this).text(), $("#weathertown li a").index(_this));
        });
    });

}

function queryWeather() {
    var name = $("#cityname").val();
    if (name.indexOf("名称") == -1) {
        loadWeather("", name, -1);
    }
}

function loadWeather(code, name, index) {
    $("#todyWeather .weather1").html('<img src="images/wait.gif" />');
    $("#todyWeather .weather2").html('');
    $("#exp .content").html('<img src="images/wait.gif" />');

    var qname = "";
    if (name != "市区") qname = name;
    $("#todyWeather .caption dt").html(name + " 天气预报");
    $("#exp .caption dt").html(name + " 生活指数");

    window.setTimeout(function() {
        showWeather.json("Weather/GetWeathers", "code=" + code + "&cityname=" + escape(qname), name);
    }, 300);
    
    var expsites = "市区,江油,三台,安县,平武,北川,盐亭,梓潼";
    if (expsites.indexOf(name) > -1)
        showWeatherExp.json("Weather/GetWeatherExp", "code=" + code + "&cityname=" + escape(qname), name);
    else
        $("#exp .content").html('<p>无</p>');
   
    $("#todyWeather .town li a").eq(index).addClass("selected");
    $("#todyWeather .town li a").eq(window.lastwIndex).removeClass("selected");
    window.lastwIndex = index;
}
function showWeather(json, cityname) {
    var html = '';
    if (json.length == 0) {
        $("#todyWeather .weather1").empty();
        $("#todyWeather .wtitle").html("没有" + cityname + "的数据");
        return;
    }
    var w12 = json[0];
    var w24 = json[1];
    html = '<span class="cityname">' + cityname + '</span> 绵阳市气象台 ';
    html += w12.datetime + " 发布";
    $("#todyWeather .wtitle").html(html);

    var title = "";
    var hours = w12.datetime.getHour();


    if (hours >= 20 || hours < 8) //晚上
    {
        $("#todyWeather .weather2").html(getWeatherHtml(w24, w24, "明天白天"));
        $("#todyWeather .weather1").html(getWeatherHtml(w12, w24, "今天晚上"));
    }
    else//白天
    {
        $("#todyWeather .weather1").html(getWeatherHtml(w12, w24, "今天白天"));
        $("#todyWeather .weather2").html(getWeatherHtml(w24, w24, "今天晚上"));
    }

}
function getWeatherHtml(w12, w24, title) {
    var wname = "--";
    var img = "images/weatherflash/0.swf";
    w12.col21 = w12.col21.replace("≤", "小于");
    w12.col21 += "级";
    if (w12.col20 == "" || w12.col20 == "0")
        w12.col20 = "风速 ";
    else
        w12.col20 += "风 ";

    if (w12.co19 != "") {
        var arrw = w12.col19.split("_");
        var index = arrw[0];
        wname = arrw[1];
        img = "images/weatherflash/" + index + ".swf";
    }
    var color, wd = "";
    if (title.indexOf("晚上") > -1) {
        color = '';
        wd = '<span class="wdname">最低温度</span> ' + w24.col12;
    }
    else {
        wd = '<span class="wdname">最高温度</span> ' + w24.col11;
    }
    var flash = getFlash(img, "80", "80", '<param name="scale" value="exactfit">', false, color);
    html = '<div class="container">';
    html += '<div class="flash">' + flash + '</div>';
    //html += '<div class="title">' + title + '</div>';
    html += '<ul>';
    html += '<li class="title">' + title + '</li>';
    html += '<li><span class="header"></span><span class="data">' + wname + '</span></li>';
    html += '<li><span class="header"></span><span class="data">' + wd + '℃</span></li>';
    html += '<li><span class="header"></span><span class="data">' + w12.col20 + w12.col21 + '</span></li>';
    html += '</ul>';
    html += '</div>';
    return html;
}

function showWeatherExp(json, cityname) {
    var sb = new Array();
    sb.push('<table><tr><th>项目</th><th>等级</th><th>说明</th><tr>');
    $.each(json, function(i, item) {
        sb.push('<tr>');
        sb.push('<td>' + item.name + '</td>');
        sb.push('<td>' + item.level + '</td>');
        sb.push('<td>' + item.content + '</td>');
        sb.push('</tr>');
    });
    sb.push('</table>');
    $("#exp .content").html(sb.join(""));
    $("#exp table tr").hover(
	function() {
	    $(this).addClass("trover");
	},
	function() {
	    $(this).removeClass("trover");
	});
}
