if (!window.JONGO) 
	var JONGO = {};
	
// if u mapping WeatherChannelServlet to a path like this,
// /javascripts/weather.js, than you have to put this file 
// in the folder /javascripts
// and calling it via:
// function onWeatherData(data) {
//   alert(data.weather.ver);
// }
// new JONGO.WeatherUtils('30066',10, onWeatherData);

// for the return data structure, refer to:
// http://www.weather.com/documentation/xml/weather.dtd
// for more information
JONGO.WeatherUtils = Class.create();
JONGO.WeatherUtils.__instances = [];
JONGO.WeatherUtils.CHINA = {
'BEIJING' : 'CHXX0008',
'SHANGHAI' : 'CHXX0116',
'GUANGZHOU' : 'CHXX0037',
'ANSHUN' : 'CHXX0005',
'BAODING' : 'CHXX0308',
'BAOSHAN' : 'CHXX0370',
'CHANGSHA' : 'CHXX0013',
'CHANGCHUN' : 'CHXX0010',
'CHANGZHOU' : 'CHXX0015',
'CHONGQING' : 'CHXX0017',
'CHIFENG' : 'CHXX0286',
'DALIAN' : 'CHXX0019',
'DALI' : 'CHXX0371',
'DATONG' : 'CHXX0251',
'FOSHAN' : 'CHXX0028',
'FUSHUN' : 'CHXX0029',
'FUZHOU' : 'CHXX0031',
'GAOXIONG' : 'TWXX0013',
'GUILIN' : 'CHXX0434',
'GUIYANG' : 'CHXX0039',
'HAERBING' : 'CHXX0046',
'HAIKOU' : 'CHXX0502',
'HANGZHOU' : 'CHXX0044',
'HUIZHOU' : 'CHXX0053',
'JILIN' : 'CHXX0063',
'JINAN' : 'CHXX0064',
'JIUJIANG' : 'CHXX0068',
'KAIFENG' : 'CHXX0072',
'KUNMING' : 'CHXX0076',
'LASA' : 'CHXX0080',
'LANZHOU' : 'CHXX0079',
'LUOYANG' : 'CHXX0086',
'LIUZHOU' : 'CHXX0479',
'NANJING' : 'CHXX0099',
'NANNING' : 'CHXX0100',
'NANTONG' : 'CHXX0101',
'JINYANG' : 'CHXX0351',
'MUDANJIANG' : 'CHXX0278',
'QINGDAO' : 'CHXX0110',
'QUANZHOU' : 'CHXX0114',
'SHAOXING' : 'CHXX0117',
'SHANTOU' : 'CHXX0493',
'SHENYANG' : 'CHXX0119',
'SHENZHEN' : 'CHXX0120',
'SHIJIAZHUANG' : 'CHXX0122',
'TAIYUAN' : 'CHXX0129',
'TAIBEI' : 'TWXX0021',
'TAIZHONG' : 'TWXX0019',
'TIANJIN' : 'CHXX0133',
'WENZHOU' : 'CHXX0462',
'WULUMUQI' : 'CHXX0135',
'XIAN' : 'CHXX0141',
'YINCHUAN' : 'CHXX0259',
'YUEYANG' : 'CHXX0411',
'ZHANGJIAKOU' : 'CHXX0300',
'ZHENGZHOU' : 'CHXX0165',
'XINING' : 'CHXX0236',
'XIAMEN' : 'CHXX0140',
'HONGKONG' : 'CHXX0049',
'XIANYANG' : 'CHXX0143',
'XIXIANG' : 'CHXX0148',
'XINZHU' : 'TWXX0009',
'XUZHOU' : 'CHXX0437',
'WUHAN' : 'CHXX0138',
'WUYISHAN' : 'CHXX0467',
'YANJI' : 'CHXX0291',
'YICHANG' : 'CHXX0407',
'YIBIN' : 'CHXX0362',
'YINING' : 'CHXX0203'
};

JONGO.WeatherUtils.prototype = {
	initialize:function(locid, days, callback, options) {
		this.instanceId = (new Date()).getTime();
		JONGO.WeatherUtils.__instances[this.instanceId] = this;
		
		this.locid = locid;
		this.days = days;
		
		this.options = Object.extend({
				metric:false,
				onLoading:function() {
					window.status = 'Loading weather data...';
				},
				onError:function(errType, errMsg) {
					window.status =	'Error requesting weather data\n'+
									'Error Type:'+errType+'\n'+
									'Message:'+errMsg;
				},				
				onComplete:function() {
					window.status = 'Completed loading weather data';
				}
			}, options || {});
			
		this.callback = function(data) {
			// guess response status
			if (!data) {
				this.options.onError.call(this,'Connection', 'Connection Timeout');
			}else{
				if (data.ver) {
					// succeed
					if (callback) {
						callback.call(this,data);
					}else{
						alert('Please provide customized callback function to handle weather data.');
					}
				}else if (data.error){
					// error?
					this.options.onError.call(this, data.error.err.type, data.error.err.content);
				}
			}
			this.options.onComplete.call(this);
			this.jsonRquest.removeScriptTag();
			// unregister self
			JONGO.WeatherUtils.__instances[this.instanceId] = null;
		};
		
		
		var end_point;
	    $A(document.getElementsByTagName("script")).findAll( function(s) {
	      return (s.src && s.src.match(/weatherutils\.js(\?.*)?$/))
	    }).each( function(s) {
	      var path = s.src.replace(/weatherutils\.js(\?.*)?$/,'');
	      if (!end_point)
		      end_point = path+'weather.js';
	    });	
	    this.end_point = end_point;
	    
		this.options.onLoading.call(this);
		var request = 'http://weather.jongo.com/javascripts/weather.js'+'?locid='+this.locid+'&days='+this.days+'&callback=JONGO.WeatherUtils.__instances[\''+this.instanceId+'\'].callback';
		
		if (this.options.metric) {
			request += '&m';
		}
		this.jsonRquest = new JSONscriptRequest(request);
		this.jsonRquest.buildScriptTag();
		this.jsonRquest.addScriptTag();	
	}
}

// jsr_class.js
//
// JSONscriptRequest -- a simple class for making HTTP requests
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//
//
// Sample Usage:
//
// <script type="text/javascript" src="jsr_class.js"></script>
// 
// function callbackfunc(jsonData) {
//      alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude + 
//            '  Longitude = ' + jsonData.ResultSet.Result[0].Longitude);
//      aObj.removeScriptTag();
// }
//
// request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&
//            output=json&callback=callbackfunc&location=78704';
// aObj = new JSONscriptRequest(request);
// aObj.buildScriptTag();
// aObj.addScriptTag();
//
//


// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

