var setZip = (typeof Class == "object") ? Class.create() : {};
setZip = {
	
	region: '',
	zipCode: '',
	locCode: '',
	stateOrCountry: '',
	intl: false,
	city: '',
	
	config: {
		requestUrl: 'http://weather.cnn.com/weather/citySearch',
		divId: 'profile_customize_weather_overlay',
		submitId: 'customize_weather_submit',
		inputId: 'cnnConnectWeatherInput',
		defaultInputText: 'Enter a U.S./Intl city or ZIP code',
		responseId: 'profile_setZip_response'
	},

	init: function(){
		if(typeof showOverlay === 'function'){
			showOverlay(this.config.divId);
		}
	},
	
	validate: function(){

		var zipInput = $(setZip.config.inputId);
		var zipValue = zipInput.value
		if(zipInput && zipValue != setZip.config.defaultInputText){
			zipValue = zipValue.replace(/<[^>]*?>/g,'');
			zipValue = zipValue.toUpperCase();
			setZip.zipCode = zipValue;
			this.requestZipChange(setZip.zipCode);
		} else {
			var errorString = 'Please input a valid zip code and then hit go.';
			setZip.outputResponse(errorString);
		};
	},
	
	requestZipChange: function(){
		$(setZip.config.responseId).innerHTML += '';
		var requestUrl = this.config.requestUrl;
		var requestArgs = '&search_term=' + setZip.zipCode + '&mode=json_html&filter=true';
		var callObj = {url: requestUrl, args: requestArgs, domId: false, funcObj: setZip.requestHandler, breakCache: false};
		CSIManager.getInstance().callObject(callObj, 'requestHandler');
	},
	
	requestHandler: function(json){
		if(typeof json[0] != 'undefined'){
				setZip.region = json[0].region;
				setZip.zipCode = json[0].zip;
				setZip.locCode = json[0].locCode;
				setZip.stateOrCountry = json[0].stateOrCountry;
				setZip.intl = json[0].intl;
				setZip.city = json[0].city;
				
				var newVal = setZip.locCode + '~' + setZip.zipCode;
				var weatherCookie = '';
				if (typeof lwpCookie !== 'undefined') {
					var locationArr = unescape(lwpCookie).split('|');
					locationArr.unshift(newVal);
					for (var i = 0; i < locationArr.length; i++) {
						if (locationArr[i] != newVal && (weatherCookie.indexOf(locationArr[i]) == -1) || i === 0) {
							weatherCookie += locationArr[i];
							if (i < (locationArr.length - 1)) {
								weatherCookie += "|";
							}
						}
					}
				} else {
					weatherCookie = newVal;
				}
				//var html = '';
				/*html += 'Your location has been changed to ' + setZip.city + ', ' + setZip.stateOrCountry + '.';
				setZip.outputResponse(html);*/
				CNN_setCookie('lwp.weather', weatherCookie, 24 * 30 * 12, '/', document.domain);
				window.msReload();
		} else {
			var html = '';
			html += 'We didn\'t find results for: <b>' + setZip.zipCode +'</b>';
			html += '<ul style="padding-left:15px"><li>Check your spelling of the city name</li>';
			html += '<li>Make sure the U.S. ZIP code is accurate</li>';
			html += '<li>Use the <a href="http://www.usps.com/" title="">USPS</a> for U.S. zip codes / city names</li></ul>';
			html += '<div class="cnn_clear"></div>';
			setZip.outputResponse(html)
		};
	},
	
	outputResponse: function(errorString){
		$('profile_setZip_response').innerHTML = '';
		$('profile_setZip_response').innerHTML += errorString;
	},
	
	urlEncode: function(str){
		str=trimWS(str);
		str=str.replace(/st\./i,'saint');
		str=str.replace(/mt\./i,'mount');
		str=escape(str);
		return str;
	}	
	
}
$(document).observe('dom:loaded', function(){
	var el = $('profile_changeZip');
	if(el){
		$('profile_changeZip').observe('keypress', function(event){
			if(event.keyCode == Event.KEY_RETURN) {
				setZip.validate();
				Event.stop(event);
			}
		});
	};
});