YAHOO.namespace("lemon.cx.weather");
YAHOO.lemon.cx.weather = function(){
        var sHtml,sImgCol='images',sHost='',aDes=['Today','Today Degree',"Rain Rate","UVI Degree","Degree Converter"];
        var bShowToday=false;var sImgP='41';var bUVI=true;
        var bShowC2F=false;var nVer=0;
        return {
            isShowToday:function (is){if(is==undefined)return bShowToday;else bShowToday = is==true;},
            isShowC2F:function (is){if(is==undefined)return bShowC2F;else bShowC2F = is==true;},
			isUVI:function(is){if(is==undefined)return bUVI;else bUVI = is==true;},
			setSize:function(sP){sImgP=sP;},setVersion:function(nV){
				switch(nV){
					case 0:nVer=0;break;
					case 1:nVer=1;break;
				}
			},
			setImgCol:function(col){this.sImgCol=col;},	setHost:function(sH){this.sHost = sH;},
			setDesc:function (aDes){this.aDes=aDes;},
            init: function(sKey,sShowWeatherId,$dDT) {
                    //alert(sKey);
                    var handleSuccess = function(obj){
                            if(typeof(obj)=='object' ){
                                YAHOO.lemon.cx.weather.initWeatherToday(obj);
                                YAHOO.lemon.cx.weather.initForecast6Days(obj);
                                document.getElementById(sShowWeatherId).innerHTML = YAHOO.lemon.cx.weather.sHtml;
                                YAHOO.lemon.cx.weather.onload();
                            }else{
                                //alert('Connection fail!!');
                                document.getElementById('WeatherLoading').className='hideUL';
                                document.getElementById('WeatherFail').className='showUL';
                            }
                    }

                    var handleFailure = function(aW){if(typeof(aW)!='Array' || aW.length() == 0 ){
                                //alert('Connection fail!!');
                                document.getElementById('WeatherLoading').className='hideUL';
                                document.getElementById('WeatherFail').className='showUL';
                        }
                    }
                    if($dDT==undefined)$dDT=''+'&ti='+new Date().getTime();
                    var sUrl = 'http://taoyuan.fullon-hotels.com.cn/gate/gb/man.hihotel.com.tw/systems/Weather/getWeather.php?';
                    var sPar = 'k='+escape(sKey);
                    if($dDT)
                    sPar += '&dt='+$dDT;
                    // sPar+='&ti='+new Date().getTime();
                    var proxy = new ScriptTagProxy();
                    proxy.initialize();
                    proxy.loadUrl(sUrl, sPar, this,handleSuccess,handleFailure);

            },
            initWeatherToday:function(obj){
                    this.sHtml = '<div id="WeatherSkin" class="x-box-blue Today hideWT"><div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div><div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc">';
                    this.sHtml += '<div id="WeatherToday"><img src="'+this.sHost+'systems/Weather/'+this.sImgCol+'/'+obj[0]['WeatherNo']+'.gif" border="0" title="'+obj[0].WeatherDesc+'" width="'+sImgP+'" /><ul id="WeatherToday_desc"><li title="'+this.aDes[0]+'">'+obj[0]['Date']+'</li><li title="'+this.aDes[1]+'">'+obj[0]['LowDegree']+'&deg;c~'+obj[0]['HeightDegree']+'&deg;c'+(this.isUVI()?'</li><li title="'+this.aDes[2]+'">':' | ')+obj[0]['RainRate']+'%'+(this.isUVI()?'|<img src="'+this.sHost+'systems/Weather/'+this.sImgCol+'/uvi/u'+obj[0]['UVI']+'.gif" id="img_wea_uvi" alt="'+this.aDes[3]+'" />':'')+(this.isShowC2F()?'|<div id="degExch" title="'+this.aDes[3]+'">&deg;F</div>':'')+'</li></ul></div>';
                    this.sHtml += '</div></div></div><div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div></div>';
            },
            initForecast6Days:function(obj){
                    this.sHtml += '<div id="box" class="x-box-blue hideUL"><div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div><div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc">';
                    this.sHtml += '<table id="WeatherForecast" class="hideUl first-of-type"><tr>';
                    for(var iday in obj){if(iday==0||typeof(obj[iday])!='object')continue;else this.sHtml += '<td id="Forecast'+iday+'" '+(iday==1?'class="dailyitem first-of-type chgBak"':'class="dailyitem chgBak"')+'>'+obj[iday]['Date']+'<br /><img src="'+this.sHost+'systems/Weather/'+this.sImgCol+'/'+obj[iday]['WeatherNo']+'.gif" border="0" title="'+obj[iday]['WeatherDesc']+'" width="'+sImgP+'" /><br />'+obj[iday]['LowDegree']+'&deg;C~'+obj[iday]['HeightDegree']+'&deg;C</td>';}
                    this.sHtml += '</tr></table>';
                    this.sHtml += '</div></div></div><div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div></div>';
            },
            onload:function (){
                    for(var i=1;i<7;i++){
                            YAHOO.util.Event.addListener('Forecast'+i, 'mouseover', YAHOO.lemon.cx.weather.moo,{id:'Forecast'+i,className:'dailyitem chgCol'});
                            YAHOO.util.Event.addListener('Forecast'+i, 'mouseout', YAHOO.lemon.cx.weather.moo,{id:'Forecast'+i,className:'dailyitem chgBak'});
                    }
                    YAHOO.util.Event.addListener('box', 'mouseover', YAHOO.lemon.cx.weather.moo,{id:'box',className:'x-box-blue showUL'});
                    YAHOO.util.Event.addListener('box', 'mouseout', YAHOO.lemon.cx.weather.moo,{id:'box',className:'x-box-blue hideUL'});
                    YAHOO.util.Event.addListener('WeatherToday', 'mouseover', YAHOO.lemon.cx.weather.moo,{id:'box',className:'x-box-blue showUL'});
                    YAHOO.util.Event.addListener('WeatherToday', 'mouseout', YAHOO.lemon.cx.weather.moo,{id:'box',className:'x-box-blue hideUL'});
                    YAHOO.util.Event.addListener('WeatherToday', 'mouseover', YAHOO.lemon.cx.weather.moo,{id:'WeatherSkin',className:'x-box-blue Today showWT'});
                    YAHOO.util.Event.addListener('WeatherToday', 'mouseout', YAHOO.lemon.cx.weather.moo,{id:'WeatherSkin',className:'x-box-blue Today hideWT'});
                    YAHOO.util.Event.addListener('WeatherSkin', 'mouseover', YAHOO.lemon.cx.weather.moo,{id:'box',className:'x-box-blue showUL'});
                    YAHOO.util.Event.addListener('WeatherSkin', 'mouseout', YAHOO.lemon.cx.weather.moo,{id:'box',className:'x-box-blue hideUL'});
            },
            moo:function(evt,act){
                    switch(evt['type']){
                            case 'mouseover':case 'mouseout':
                                    document.getElementById(act.id).className = act.className;break;
                    }
            },
            convertDegree:function(type,value){
                    switch(type){case 'C2F':F=value * 9 / 5 + 32;return Math.round(F);break;
                            case 'F2C':C=(value -32) * 5 / 9;return Math.round(C);break;}
            }
        };
}();