﻿$(document).ready(function(){
        $.ajaxWebService = function(url, dataMap, fnSuccess){
          $.ajax({
            type: "Post",
            contentType: "application/json; charset=utf-8",
            url:url,
            data:dataMap,
            dataType:"json",
            async:false,
            cache:false, 
            success: fnSuccess,
            error: function(XMLHttpRequest, textStatus, errorThrown){
                //alert(XMLHttpRequest.status);
                //alert(XMLHttpRequest.readyState);
                //alert(textStatus);
                //隐藏天气预报
                $("#div_weather1").hide();
                $("#div_weather2").hide();
            }
          });
        }
        //-----初始化---------开始-------------
        $("#div_weather2").hide();
        //显示城市的天气预报
        bindWeather();
        
        //-----初始化---------结束-------------
        
        
        //-----后续操纵---------开始-------------
        //点击切换城市时
        $("#changeCity").click(function(){
            $("#div_weather1").hide();
            $("#div_weather2").show();
            //绑定省份
            $.ajaxWebService("/webServer/WebService1.asmx/getProvince","{}",function(msgs){
                var data=eval("("+msgs.d+")");
                var provinces=data.provin;
                $("#select_province").empty();
                var x;
                for(x in provinces)
                {
                    $("#select_province").append("<option value='"+provinces[x].provin+"'>"+provinces[x].provin+"</option>");
                }
            });
            //绑定城市
            var provin= $("#select_province").find("option:selected").text();
            bindSelectCity(provin);
        });
        
        //省市改变时
        $("#select_province").change(function(){
            var provin= $("#select_province").find("option:selected").text();
            bindSelectCity(provin);
        });
        
        //点击取消时
        $("#btnCancle").click(function(){
            $("#div_weather1").show();
            $("#div_weather2").hide();
        });
        
        //点击定制时
        $("#btnChoice").click(function(){
            //获取城市值，并写入cookies
        var city=$("#select_city").find("option:selected").text();
        $.ajaxWebService("/webServer/WebService1.asmx/writeCookies","{'str':'"+city+"'}",function(msgs){});
        $("#div_weather1").show();
        $("#div_weather2").hide();
        bindWeather();
        });
        
        //-----定义事件---------结束------------
        
        //---------自定义函数
        //(绑定城市)
        ///provin 省份
        function bindSelectCity(provin)
        {
            $("#select_city").empty();
            $.ajaxWebService("/webServer/WebService1.asmx/getCity","{'str':'"+provin+"'}",function(msgs){
                var data=eval("("+msgs.d+")");
                var citys=data.city;
                var x;
                for(x in citys)
                {
                    $("#select_city").append("<option value='"+citys[x].city+"'>"+citys[x].city+"</option>");
                }
            });
        }
        //(取出天气预报信息)
        function bindWeather()
        {
            $.ajaxWebService("/webServer/WebService1.asmx/bindWeather","{}",function(msgs){
                var data=eval("("+msgs.d+")");
                var weather=data.weather;
                $("#cityName").html(weather[0].weather);
                $("#imgTodayStar").attr("src",weather[3].weather);
                $("#imgTodayEnd").attr("src",weather[4].weather);
                $("#imgTomStar").attr("src",weather[7].weather);
                $("#imgTomEnd").attr("src",weather[8].weather);
                $("#today1").html(weather[1].weather);
                $("#today2").html(weather[2].weather);
                $("#tomorry1").html(weather[5].weather);
                $("#tomorry2").html(weather[6].weather);
            });
        }
    });
