/* Weather Box */

Sppc.Landing.WeatherWeatherComBox$Class = {
	constructor: function(data) {
		Sppc.Core.extendSuper(this, Sppc.Landing.AbstractBox, Sppc.Landing.WeatherWeatherComBox$Class, data);
	},

	_settingsLoaded: function() {
		this.__super._settingsLoaded();
		
		var thisObj = this;
		jQuery('#weatherComLocation_'+this._id).change(function(){
			//jQuery('#searchWeatherComLocation_'+thisObj._id).val('');
		});

		jQuery('#weatherComLocation_'+this._id).keyup(function(){
			codeValidation();
		});

		var codeValidation = function () {
			var field = jQuery('#weatherComLocation_'+thisObj._id);
			jQuery('#boxSettingsSaveButton_'+thisObj._id).attr('disabled', true);
		    var valid = /^[A-Za-z]{4}\d{4}$/.test(field.val());
		    if (valid) {
			   field.css('border-color', '#000000');
			   field.css('color', '#000000');
			   $.post(site_url + 'box/weather_weathercom/search_locality_name/', {code: field.val(), id: thisObj._id},
					  function(data){
				         if (data.status && data.status == 'done') {
				        	 jQuery('#searchWeatherComLocation_'+thisObj._id).val(data.name);
				        	 jQuery('#weatherComLocationError_'+thisObj._id).hide();
				        	 jQuery('#weatherComLocationError_'+thisObj._id).val('');
				        	 jQuery('#boxSettingsSaveButton_'+thisObj._id).attr('disabled', false);
				        	 jQuery('#displayTitle_'+thisObj._id).val(data.name);
				         } else {
				        	 jQuery('#searchWeatherComLocation_'+thisObj._id).val('');
				        	 field.css('border-color', '#ff0000');
							 field.css('color', '#ff0000');
							 jQuery('#weatherComLocationError_'+thisObj._id).show();
				        	 jQuery('#weatherComLocationError_'+thisObj._id).text(data.error);
				         }
				      }, 'json'
			   );
			} else {
				jQuery('#searchWeatherComLocation_'+thisObj._id).val('');
				field.css('border-color', '#ff0000');
				field.css('color', '#ff0000');
		    }
		}
		
		var autocompleater = jQuery('#searchWeatherComLocation_'+this._id).autocomplete(site_url+"box/weather_weathercom/search", {
			autoFill: false,
			width: 320,
			max: 4,
			highlight: false,
			scroll: true,
			scrollHeight: 300,
			extraParams: { 'id': this._id },			
			formatItem: function(data, i, n, value) {
				return value.split(";")[1];
			},
			formatResult: function(data, value) {
				return value.split(";")[1];
			}
			
		});
		autocompleater.result(function(event, data, formatted) {
			var code = formatted.split(";")[0]; 
			var title = formatted.split(";")[1]; 
			jQuery('#displayTitle_'+thisObj._id).val(title);
			jQuery('#weatherComLocation_'+thisObj._id).val(code);
			jQuery('#weatherComLocation_'+thisObj._id).css('border-color', '#000000');
			jQuery('#weatherComLocation_'+thisObj._id).css('color', '#000000');
			jQuery('#weatherComLocationError_'+thisObj._id).hide();
       	 	jQuery('#weatherComLocationError_'+thisObj._id).val('');
			jQuery('#boxSettingsSaveButton_'+thisObj._id).attr('disabled', false);
			return code; 
		});

	}
};
Sppc.Landing.WeatherWeatherComBox = Sppc.Landing.WeatherWeatherComBox$Class.constructor;
