/// <reference path="jquery/jquery126.js" />
/// <reference path="jquery/JQuery.Intellisense.js" />


$(function() {
    $("#login .content").hide();

    $("#weathertown li a").each(function() {
        var _this = this;
        $(this).click(function() {
            loadWeather($(_this).attr("name"), $(_this).text(), $("#weathertown li a").index(_this));
        });
    });
    

    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(); });

	window.setTimeout(function(){
    showAlarm.json("Weather/GetAlarmTop/top=1");
	},200);
	window.setTimeout(function(){
    showVideo.json("Video/GetVideoTop/kindId=8&top=3");
	},200);
	window.setTimeout(function(){
    showImagenews.json("Article/GetArticleTop", "kindId=4&top=5&connumber=5&conlen=220&image=1&child=1");
	},200);

    window.lastNewstabIndex = 0;
    $("#news .newsnva li a").hover(function() {
        $(this).addClass("selected");
        $("#news .newsnva li a").eq(window.lastNewstabIndex).removeClass("selected");
        var index = $("#news .newsnva li a").index($(this));
        window.lastNewstabIndex = index;
        $("#news .list .newslist").each(function(i, item) {
            if (i != index) { $(item).hide(); }
        });
        $("#news .list .newslist").eq(index).show();
        if ($("#news .list .newslist").eq(index).find("li").size() == 0) {
            $("#news .list .newslist").eq(index).html('<img src="images/wait.gif" />');
            loadTxtNews($(this).attr("name"), index);
        }
    }, function() {
        if (window.lastNewstabIndex != $("#news .newsnva li a").index($(this))) {
            $(this).removeClass("selected");
        }
    });
    $("#news .newsnva li a").eq(0).addClass("selected");
	window.setTimeout(function(){
    loadTxtNews($("#news .newsnva li a").eq(0).attr("name"), 0);
	},200);
	window.setTimeout(function(){
    showLinks.json("Article/GetLinkArticles/kindId=77&top=50", "", "link1");
	},200);
	window.setTimeout(function(){
    showLinks.json("Article/GetLinkArticles/kindId=78&top=50", "", "link2");
	},200);
	window.setTimeout(function(){
    checklogin.json("User/CheckLogin");
	},200);

});


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('');
    if (!window.oldtopvalue) { window.oldtopvalue = $("#weathersk .wdvalue").css("top"); }
    $("#weathersk .wdvalue").css("top", window.oldtopvalue);
    window.setTimeout(function() {
        var qname = "";
        if (name != "市区") qname = name;
        showSk.json("Weather/GetLastSk", "code=" + code + "&cityname=" + escape(qname), name);
        showWeather.json("Weather/GetWeathers", "code=" + code + "&cityname=" + escape(qname), name);
    }, 300);
    $("#todyWeather .town li a").eq(index).addClass("selected");
    $("#todyWeather .town li a").eq(window.lastwIndex).removeClass("selected");
    window.lastwIndex = index;
}
function showSk(json, cityname) {
    //alert(json.wd);
    var wd = parseFloat(json.wd);
    var top = parseFloat($("#weathersk .wdvalue").css("top"));
    var tmp = -1.8;
    if (json.wd < 0) { tmp = -2.6; }
    wd = json.wd - top - (wd / 10.0) * (tmp);
    $("#weathersk .wdvalue").css("top", -wd);
    if (json.wd.substring(0, 1) == ".") json.wd = "0" + json.wd;
    if (json.wd.substring(0, 2) == "-.") json.wd = json.wd.replace(".", "0.");
    $("#weathersk .shiduwendu .wendu").html('温度 <div class="value">' + json.wd + '℃</div>');
    $("#weathersk .shiduwendu .shidu").html('湿度 <div class="value">' + json.shidu + '</div>');

    if (json.fx.substring(0, 1) == ".") json.fx = "0" + json.fx;
    $("#weathersk .fx .image").html(json.fx + "m/s");
    $("#weathersk .fx .image").css("background-image", "url(images/weatherangle/" + json.fxcode + ".gif)");
    $("#weathersk .fx .info").html(json.fsname + ' 小于<span class="value">' + json.fs + '</span> 级');
    $("#weathersk .jyl").html('1小时降雨量 <span class="value">' + json.yl + '</span> 毫米');
    $("#weathersk .updateinfo").html(cityname + " " + json.sj + " 实况");
}
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.col19 != "") {
        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 showAlarm(json) {
    var showgg = false;
    var height = "80px";
    if (json.length == 0)
        showgg = true;
    else
        $("#ggyj .gg").hide();
    if (json.length > 0) {
        var html = '<ul>';
        $.each(json, function(i, item) {
            html += '<li><div class="title"><img src="images/gonggao.gif" /> <a href="alarm.shtml?Id=' + item.AlarmID + '" target="_blank">' + item.Title + '：</a></div>';
            html += '<div class="image">';
            var icos = item.ImgUrl;
            if (icos.indexOf("|") > 0) {
                var arr = icos.split("|");
                for (var i = 0; i < arr.length; i++) {
                    html += '<a href="alarm.shtml?Id=' + item.AlarmID + '" target="_blank"><img src="' + arr[i] + '" /></a>';
                }
            }
            else {
                html += '<a href="alarm.shtml?Id=' + item.AlarmID + '" target="_blank"><img src="' + icos + '" /></a>';
            }
            html += '</div>';
            //html += '<li><div class="image"><img src="' + item.ImgUrl + '" /></div>';
            //item.Content = item.Content.replace("<br />", "");
            html += '<div class="yjcontent"><a href="alarm.shtml?Id=' + item.AlarmID + '" target="_blank">' + item.Content + "... &gt;&gt;查看详细情况</a></div>";
            //html += '<div class="detail"><a href="alarm.shtml?Id=' + item.AlarmID + '" target="_blank">&gt;&gt;查看详细情况</a></div>';
            html += '</li>';
        });
        html += '</ul>';
        $("#ggyj .yj").html(html);

        var maxheight = $("#ggyj .yj")[0].scrollHeight;
        if (maxheight > 185) {
            html = '<marquee direction="up" scrollamount="3" onmouseover="this.stop()" onmouseout="this.start()" style="height:190px;">' + html;
            html += '</marquee>';
            $("#ggyj .yj").html(html);
        }
    } else {
        showgg = true;
        $("#ggyj .gg").css("background-image", "none");
        height = "180px";
    }
    if (showgg) {
        //showyjcontent.json("Weather/GetAlarmTopOne");
        showGonggao.json("Article/GetArticleTop", "kindId=66&top=5", height);
    }
}

//公告
function showGonggao(json, height) {
    var html = '<ul>';
    $.each(json, function(i, item) {
		if (item.TitleColor != "")
			item.Title='<span class="title" style="color:'+item.TitleColor+'">'+item.Title+'</span>';
		else
			item.Title='<span class="title">'+item.Title+'</span>';
        html += '<li><a target="_blank" href="article.shtml?Id=' + item.ArticleID + '">' + item.Title + '</a> <div class="date">[' + item.AddDate + ']</div></li>';
    });
    html += '</ul>';
    $("#ggyj .gg").html(html);

    var maxheight = $("#ggyj .gg")[0].scrollHeight;
    if (maxheight > parseInt(height)) {
        html = '<marquee direction="up" scrollamount="2" onmouseover="this.stop()" onmouseout="this.start()" style="height:' + height + ';">' + html;
        html += '</marquee>';
        $("#ggyj .gg").html(html);
    }
}

//视频
function showVideo(json) {
    if (json.length == 0) return;
    window.videoJson = json;
    playVideo(0);


    var html = '<ul>';
    $.each(json, function(i, item) {
        if (item.ImgUrl == "") { item.ImgUrl = "images/nopic.jpg" }
        html += '<li><a href="javascript:;" title="' + item.Title + '" onclick="playVideo(' + i + ',true)" ><img src="' + item.ImgUrl + '" /></a></li>';
    });
    html += '</ul>';
    $("#video .morevideo").html(html);
}
function playVideo(i, toplay) {
    var json = window.videoJson[i];
    var url = document.location.href;
    if (json.FileUrl.toLowerCase().indexOf("http://") == -1)
        url = url.substring(0, url.lastIndexOf("/") + 1) + json.FileUrl;
    else
        url = json.FileUrl;
    var pra = "file=" + url;
    pra += "&image=" + json.ImgUrl;
    pra += "&allowfullscreen=true&stretching=fill";
    if (toplay) { pra += "&autostart=true" }

    var flashVar = '<param name="allowscriptaccess" value="always">';
    flashVar += '<param name="allowfullscreen" value="true">';
    flashVar += '<param name="flashvars" value="' + pra + '">';

    var html = '<div>' + getFlash("images/player.swf", "226", "170", flashVar) + '</div>';
    html += '<div class="title">' + json.Title + '</div>';
    $("#video .playerContainer").html(html);
}

//链接
function showLinks(json, id) {
    $.each(json, function(i, item) {
        $("#" + id).append('<option value="' + item.Http + '">' + item.Title + '</option>');
    });

}

//图片新闻
function showImagenews(json) {
    if (json.length == 0) return;
    window.clips = json;
    var pics = "", links = "", texts = "";
    $.each(json, function(i, item) {
        if (pics == "") {
            pics = item.ImgUrl;
            links = item.ArticleID;
            texts = item.Title.subString(32);
        }
        else {
            pics += "||" + item.ImgUrl;
            links += "||" + item.ArticleID;
            texts += "||" + item.Title.subString(32);
        }
    });
    var flashUrl = "images/focus.swf";
    var flashVars = "<param name='FlashVars' value='picurl=" + pics + "&piclink=" + links + "&pictext=" + texts;
    flashVars += "&borderwidth=220&borderheight=165&textheight=22&borderw=false&callback=ViewArticle&callbackOnChange=onClipChange'>";

    $("#news .imgnews .clip").html(getFlash(flashUrl, "220", "165", flashVars, false, "#cccccc"));

    showArticleContent(json[0]);
}
function ViewArticle(articleId) {
    window.setTimeout(function() {
        document.location.href = "article.shtml?id=" + articleId;
    }
	, 20);
}
function onClipChange(index) {
    var json = window.clips;
    showArticleContent(json[index]);
}
function showArticleContent(json) {
    var html = '<div class="title">' + json.Title.subString(32) + '...</div>';
    html += '<div class="detail">' + json.Content + '  <span class="view">&gt;&gt;<a href="javascript:;" onclick="ViewArticle(' + json.ArticleID + ')">详细了解</a></span></div>';
    $("#news .imgnews .detailcontent").html(html);
}

/*文本新闻*/
function loadTxtNews(kindId, index) {
    showTxtNews.json("Article/GetArticleTop", "kindId=" + kindId + "&top=4", index);

}
function showTxtNews(json, index) {
    var html = "<ul>";
    $.each(json, function(i, item) {
        if (item.Http == "") { item.Http = "article.shtml?Id=" + item.ArticleID }
        item.Title = item.Title.subString(54, true);
        if (item.ImgUrl != "") item.Title = '<span style="color:#ff0000">[图]</span>' + item.Title;
        html += '<li><a target="_blank" href="' + item.Http + '">';
        if (item.TitleColor != "")
        { html += '<span style="color:' + item.TitleColor + '">' + item.Title + '</span>' }
        else
        { html += item.Title }
        //var d = item.AddDate.getDatePart();
        //d = d.getFullYear() + "-" + item.AddDate.getDatePart() + "-" + d.getDay();
        html += '</a><span class="date">' + item.AddDate.getDatePart() + '</span>';
        html += '</li>';
    });
    html += '</ul>';

    $("#news .list .newslist").eq(index).html(html);
}

function userlogin() {
    var uname = $("#uname").val();
    var upwd = $("#upwd").val();
    if (uname == "" || upwd == "") {
        alert("请填写用户名和密码！");
        return;
    }
    $("#form1").get(0).submit();
}

function logout() {
    logoutpost.json("User/GuestLogout");
}
function logoutpost(json) {
    window.setTimeout(function() {
        document.location.href = document.location.href;
    }, 300);
}
function checklogin(json) {
    if (json.Result == "1" && logined()) {
        $("#login .content").html('<div class="logined">您已登录，可以使用专业用户相关功能了；<a href="zyservice/">进入专业服务</a></div><div  class="logout"><a href="javascript:" onclick="logout()">退出登陆</a></div>');
    }
    $("#login .content").show();
}