
/**
 * Main class.
 */
HP.Modules.Weather = Class.create(HP.Module,{
	
	initialize: function($super, moduleid) {
		
		$super(moduleid);
		
		this.WEATHER_AJAX_ACTION_REORDER = "REORDER";
		this.WEATHER_AJAX_ACTION_DELETE = "DELETE";
		
		this.WEATHER_AJAX_RESPONSE_OK = "OK";
		this.WEATHER_AJAX_RESPONSE_DUPLICATE = "DUPLICATE";
		this.WEATHER_AJAX_RESPONSE_BAD_ACTION = "BAD_ACTION";
		this.WEATHER_AJAX_RESPONSE_BAD_ARGS = "BAD_ARGS";
		
		var dels = this.rootNode.select('a[action=remove]');
		for(var i = 0; i < dels.length; i++){
			var del = dels[i];
			del.observe('click', this.deleteLocation.bind(this, del.readAttribute('locale')));
		}
		
		this.popup = new HP.ModulePopups.Weather(moduleid);
		this.registerEvents('a[action=addNew]', this.popup.open.bindAsEventListener(this.popup, {popupid: 'ADD'}));
		
		this.weatherList = this.$GEBI('weatherList');
		
		// For drag-and-drop.
		this.createSortable();
		
	},
	
	createSortable: function() {
		
		if ( this.weatherList ) {
			Sortable.create( this.weatherList,
						{constraint:'vertical', onUpdate: this.saveOrder.bindAsEventListener(this) }
						);
		}
		
	},
	
	saveOrder: function ( list ) {

		var ids = [];
		
		for ( var i = 0; i < list.childNodes.length; i++ ) {
			var child = list.childNodes[i];
			ids.push( child.id );
		}
		
		this.doAJAX(this.WEATHER_AJAX_ACTION_REORDER, {payload: ids});						
	},
	
	deleteLocation: function ( cityCode ) {
		this.doAJAX(this.WEATHER_AJAX_ACTION_DELETE, {cityCode: cityCode},
						this.deleteLocationCallback.bind(this) );
	},
	
	deleteLocationCallback: function ( response ) {		
		if(this.popup.visible) return;
		var cityCode = response.responseText;
		this.weatherList.removeChild(this.weatherList.down('li[citycode=' + cityCode + ']'));
	}
	
});

HP.ModulePopups.Weather = Class.create(HP.ModulePopup, {
	initialize: function($super, moduleid){
		$super(moduleid);
		
		this.WEATHER_AJAX_ACTION_SEARCH = "SEARCH";
		this.WEATHER_AJAX_ACTION_ADD = "ADD";
	},
	onOpen: function(){
		if(!this.completer){
			this.locationBox = this.rootNode.down('input#location');
			this.serviceUrl = 'ajax/service.action?moduleid=' + this.moduleid + '&' +
								'action=' + this.WEATHER_AJAX_ACTION_SEARCH + '&';
			
			this.autocomplete_choices = this.rootNode.down('div#autocomplete_choices');
			
			this.completer = new Ajax.Autocompleter( this.locationBox,
									this.autocomplete_choices,
									this.serviceUrl,
									{minChars: 3, frequency: 0.5, afterUpdateElement: this.getWeatherLocationData} );
		}
	},
	onClose: function(){
		this.completer = null;
	},
	getWeatherLocationData: function ( element, li ) {
		
		var to_add = [];
		to_add.push( li.readAttribute('city') );
		to_add.push( li.readAttribute('state') );
		to_add.push( li.readAttribute('country') );
		to_add.push( li.readAttribute('zipcode') );
		to_add.push( li.readAttribute('citycode') );
		to_add.push( li.readAttribute('citytype') );
		
		element.locar = to_add;
		
	},
	prepForm: function(pars){
		pars['payload'] = this.locationBox.locar;
		return pars;
	},
	validate: function($super, pars){
		var errs = $super(pars);
		errs.location = !pars.payload;
		return errs;
	}
});