﻿function SwapBtnImages(objImg){
    var arrSrc = new Array();
    arrSrc = objImg.src.split('.');
    strImgType = arrSrc[arrSrc.length-1];
    strSrc = objImg.src.replace('.' + strImgType, '');
    objImg.src = (strSrc.substring(strSrc.length-1, strSrc.length) == "r")? strSrc.substring(0, strSrc.length-1) + "." + strImgType : strSrc + "r." + strImgType;
}
var baseTime = 7*60*60*24;
function ChangeArea(val, obj){
document.WeatherImg.src = 'http://udn.com/WEATHER/IMAGES/'+val+'icon.gif';
document.WeatherLow.src = 'http://udn.com/WEATHER/IMAGES/'+val+'lowtemp.gif';
document.WeatherHigh.src = 'http://udn.com/WEATHER/IMAGES/'+val+'hightemp.gif';
document.getElementById('div_weatherlist').style.display = "none";
document.getElementById('spn_weathercity').innerHTML = obj;
document.getElementById('spn_weatherarrow').innerHTML = "▼";
DelCookie("OMG_Weather");
SetCookie("OMG_Weather", val + "_" + obj, baseTime, "/", ".98173.com.tw");
}
function FirstLoadArea(){
    var area = GetCookie("OMG_Weather");
    if(area == null){
        ChangeArea("taipei", "台北");
    }else{
        arr_area = new Array();
        arr_area = area.split("_");
        ChangeArea(arr_area[0], arr_area[1]);
    }
}
function realPosX (oTarget) {
	try {
		var realX = oTarget.offsetLeft;
		if (oTarget.offsetParent != null && oTarget.offsetParent.tagName.toUpperCase() != "BODY") {
			realX += realPosX(oTarget.offsetParent); 
		}
		return realX;
	} catch (e) {
		alert("realPosX: "+e);
	} 
}
function realPosY (oTarget) {
	try {
		var realY = oTarget.offsetTop;
		if (oTarget.offsetParent != null && oTarget.offsetParent.tagName.toUpperCase() != "BODY") {
			realY += realPosY(oTarget.offsetParent);
		}
		return realY;
	} catch (e) { 
		alert("realPosY: "+e);
	}
}
function callWeatherList(obj){
    document.getElementById('div_weatherlist').innerHTML = BindWeatherList();
    objList = document.getElementById('div_weatherlist');
    if(objList.style.display == "inline"){
        objList.style.display = "none";
        document.getElementById('spn_weatherarrow').innerHTML = "▼";
    }else{
        document.getElementById('spn_weatherarrow').innerHTML = "▲";
        objList.style.display = "inline";
        y = realPosY(obj) + 0;
        x = realPosX(obj);
        objList.style.top = y.toString() + "px";
        objList.style.left = x.toString() + "px";
    } 
}
function inWeatherList(obj){
    objList = obj;
    if(objList.style.display == "inline"){
    }else{
        document.getElementById('spn_weatherarrow').innerHTML = "▲";
        objList.style.display = "inline";
    } 
}
function outWeatherList(obj){
    objList = obj;
    if(objList.style.display == "inline"){
        objList.style.display = "none";
        document.getElementById('spn_weatherarrow').innerHTML = "▼";   
    }
}
function BindWeatherList(){
    var objCounty = [{en:"taipei",tw:"台北"},{en:"kaoshiung",tw:"高雄"},{en:"jilung",tw:"基隆"},{en:"taoyuan",tw:"桃園"},{en:"xinju",tw:"新竹"},{en:"miaoli",tw:"苗栗"},{en:"taichung",tw:"台中"},{en:"zhanghua",tw:"彰化"},{en:"nantou",tw:"南投"},{en:"yunlin",tw:"雲林"},{en:"jiayi",tw:"嘉義"},{en:"tainan",tw:"台南"},{en:"pingtong",tw:"屏東"},{en:"hengchun",tw:"恆春"},{en:"yilan",tw:"宜蘭"},{en:"hualian",tw:"花蓮"},{en:"taitung",tw:"台東"},{en:"penghu",tw:"澎湖"},{en:"jinmeng",tw:"金門"},{en:"matsu",tw:"馬祖"}];
    objHtml = "<table width=\"200\" height=\"130\" align=\"center\" border=\"0\" id=\"tbweather\" cellspacing=\"2\" cellpadding=\"1\" style=\"color: #333;background-color:White;border:#AAA 1px solid;\"><tbody>";
    for(var i=0; i<objCounty.length; i++){
        if((i)%5 == 0){
            objHtml += "<tr>";
        }
        objHtml += "<td bordercolor=\"#FFFFFF\"><span onclick=\"ChangeArea('" + objCounty[i].en + "','" + objCounty[i].tw + "');\" style=\"cursor:pointer;text-decoration:none;color: #333;\">" + objCounty[i].tw + "</span></td>";
        if((i+1)%5 == 0){
            objHtml += "</tr>";
        }
    }
    return objHtml + "</tbody></table>";
}//-----------------------cookieset-------------------------
function GetCookieVal(offset){//取得Cookie解碼後的值
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
function SetCookie(name, value){//設定Cookie值
    var expdate = new Date();
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
    document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
    +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
    +((secure == true) ? "; secure" : "");
}
function DelCookie(name){//删除Cookie
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
function GetCookie(name){//取得Cookie的原始值
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen){
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return GetCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
