var weather = {
	options: {
		id: "ctn_", 
		c: "", 
		lat: "",
		lng: "",
//		serviceURL: "http://localhost:50002/comun/scripts/sb_response_json.asp?q=weatherObservation&",
		serviceURL: "http://ws.geonames.org/findNearByWeatherJSON?",
		site_from: "http://www.caribbeantravelnetwork.com"
//		site_from: "http://localhost:50002"
	
	},
	imgs:{
		clouds:{"n/a":"/i/weather/t.gif", 
			"clear sky":"/i/weather/clear.jpg",
			"few clouds":"/i/weather/few.jpg",
			"scattered clouds":"/i/weather/scattered.jpg",
			"broken clouds":"/i/weather/broken.jpg",
			"overcast":"/i/weather/overcast.jpg",
			"vertical visibility":"/i/weather/t.gif"},
		
		weatherCondition:{"n/a":"/i/weather/t.gif", 
			"drizzle":"/i/weather/drizzle.jpg",
			"rain":"/i/weather/rain.jpg",
			"snow":"/i/weather/snow.jpg",
			"snow grains":"/i/weather/grains.jpg",
			"ice crystals":"/i/weather/snow.jpg",
			"ice pellets":"/i/weather/pellets.jpg",
			"hail":"/i/weather/hail.jpg",
			"small hail/snow pellets":"/i/weather/small.jpg",
			"unknown precipitation":"/i/weather/t.gif",
			"mist":"/i/weather/t.gif",
			"fog":"/i/weather/t.gif",
			"smoke":"/i/weather/t.gif",
			"volcanic ash":"/i/weather/t.gif",
			"sand":"/i/weather/t.gif",
			"haze":"/i/weather/t.gif",
			"spray":"/i/weather/t.gif",
			"widespread dust":"/i/weather/t.gif",
			"squall":"/i/weather/squall.jpg",
			"sandstorm":"/i/weather/t.gif",
			"duststorm":"/i/weather/t.gif",
			"well developed dust/sand whirls":"/i/weather/t.gif",
			"funnel cloud":"/i/weather/t.gif",
			"tornado/waterspout":"/i/weather/t.gif",
			"light":"/i/weather/t.gif",
			"heavy":"/i/weather/t.gif",
			"in vicinity:":"/i/weather/t.gif",
			"shallow":"/i/weather/t.gif",
			"patches":"/i/weather/t.gif",
			"showers":"/i/weather/showers.jpg",
			"partial":"/i/weather/partial.jpg",
			"thunderstorm":"/i/weather/thunderstorm.jpg",
			"blowing":"/i/weather/blowing.jpg",
			"drifting":"/i/weather/t.gif",
			"freezing":"/i/weather/t.gif"}
 	},
	elements:[ 
		 {id:"weather", txt:"", tag:"DIV", c:"", attr:{"class":"c"}},
		 {id:"wth_datetime", txt:"Current contidions as of ", tag:"P", c:"weather", attr:{"class":"p"}},
		 {id:"wth_datetime_label", txt:"", tag:"LABEL", c:"wth_datetime", attr:{"class":"l"}},
		 {id:"wth_title", txt:"", tag:"H3", c:"weather", attr:{"class":"t"}},
		 {id:"wth_clouds", txt:"", tag:"IMG", c:"weather", attr:{"class":"i"}},
		 {id:"wth_weatherCondition", txt:"", tag:"IMG", c:"weather", attr:{"class":"i"}},
		 {id:"wth_temperature", txt:"", tag:"P", c:"weather", attr:{"class":"p"}},
		 {id:"wth_temperature_label", txt:"", tag:"LABEL", c:"wth_temperature", attr:{"class":"l"}},
		 {id:"wth_temperature_span", txt:"°C", tag:"SPAN", c:"wth_temperature", attr:{"class":"s"}},
		 {id:"wth_humidity", txt:"", tag:"P", c:"weather", attr:{"class":"p"}},
		 {id:"wth_humidity_label", txt:"", tag:"LABEL", c:"wth_humidity", attr:{"class":"l"}},
		 {id:"wth_humidity_span", txt:"%", tag:"SPAN", c:"wth_humidity", attr:{"class":"s"}},
		 {id:"wth_observation", txt:"", tag:"P", c:"weather", attr:{"class":"p"}},
		 {id:"wth_observation_label", txt:"", tag:"LABEL", c:"wth_observation", attr:{"class":"l"}}
	],
	styles: [{id:"weather", style:{"display": "none"}}],
	texts: {},
	init: function(){
		if (this.options.c != ""){
			var cD = get_componentDHTML(this.options.id, this.options.c, this.elements, this.styles);
			cD.make();
		}
		
		var _self = this;
		$("#" + this.options.id + " img.i").attr("src", _self.options.site_from + "/i/weather/n_a.jpg");
		this.refresh_data()
		
	},
	refresh_data: function (){
		var _self = this;
		$.getJSON(this.options.serviceURL + "lat=" + _self.options.lat + "&lng=" + _self.options.lng + "&callback=?", function(data, textStatus){
			var clouds_img = $(_self.imgs.clouds).attr(data.weatherObservation.clouds)
			if (typeof clouds_img != "undefined") {
				$("#" + _self.options.id + "wth_clouds").attr("src",_self.options.site_from + "" + (clouds_img) + "");
			}
			var weatherCondition_img = $(_self.imgs.clouds).attr(data.weatherObservation.weatherCondition)
			if (typeof weatherCondition_img != "undefined") {
				$("#" + _self.options.id + "wth_weatherCondition").attr("src",_self.options.site_from + "" + (weatherCondition_img) + "");
			}
			$("#" + _self.options.id + "wth_clouds").attr("alt", data.weatherObservation.clouds).attr("title", data.weatherObservation.clouds);
			$("#" + _self.options.id + "wth_weatherCondition").attr("alt", data.weatherObservation.weatherCondition).attr("title", data.weatherObservation.weatherCondition);
	
			$("#" + _self.options.id + "wth_title").attr("title", data.weatherObservation.stationName);
	
			$("#" + _self.options.id + "wth_temperature_label").text(data.weatherObservation.temperature);
			$("#" + _self.options.id + "wth_humidity_label").text(data.weatherObservation.humidity);
			$("#" + _self.options.id + "wth_observation_label").text(data.weatherObservation.observation);
			$("#" + _self.options.id + "wth_datetime_label").text(data.weatherObservation.datetime);

			$("#" + _self.options.id + "weather").fadeIn();
			
		});
	},
	setStyle: function(style){
		var found = false;
		for (var i = 0; i < this.styles.length; i++)
			if (style.id == this.styles[i].id) {
				var empty = {}
				this.styles[i].style = $.extend(empty, this.styles[i].style, style.style);
				found = true; break;
			}
		if (!found) this.styles = $.merge(this.styles, [style])
	},
	setStyles: function(styles){
		for (var i = 0; i < styles.length; i++)
			this.setStyle(styles[i]);
	},
	setElement: function(element){
		var found = false;
		for (var i = 0; i < this.elements.length; i++)
			if (element.id == this.elements[i].id) {
				if (typeof element.txt != "undefined") this.elements[i].txt = element.txt;
				if (typeof element.tag != "undefined") this.elements[i].tag = element.tag;
				if (typeof element.c != "undefined") this.elements[i].c  = element.c;
				var empty = {}
				this.elements[i].attr = $.extend(empty, this.elements[i].attr, element.attr);
				found = true; break;
			}
		if (!found) this.elements = $.merge(this.elements, [element])
	},
	setElements: function(elements){
		for (var i = 0; i < elements.length; i++)
			this.setElement(elements[i]);
	}
};