var dashboard = Class.create();
dashboard.prototype = {
	owner: null,
	initialize: function( options ) {
		options = options || {}
		this.owner = options.owner;
		this.choices = options.choices || 20;
	},	
	
	/*
	The form submit really needs to update the hidden input fields, not the text fields
	*/
	autocompleteAfterUpdateElement: function( element, selectedElement ) {
		id_name = element.id;
		input_id = id_name.substr( id_name.length - 1 );
		resort_id = selectedElement.id.substr( 6 );
		$('dashboardResortChoice' + input_id).value = resort_id;
	},
	
	/*
	Refresh the text fill-in field to the value reflected in the underlying
	hidden input fields
	*/
	autocompleteOnHide: function( element, update ) {
		id_name = element.id;
		input_id = id_name.substr( id_name.length - 1 );
		resort_id = $('dashboardResortChoice' + input_id).value;
		name = resort_ids_to_names[resort_id];
		element.value = name;
		new Effect.Fade(update,{duration:0.15});
	},
	
	/*
	Mostly stolen from Autocomplete.Local, but with modifications to give us
	fine-grained control over width
	*/
	autocompleteOnShow: function( element, update ) {
		if(!update.style.position || update.style.position=='absolute') {
			update.style.position = 'absolute';
			Position.clone(
				element, update,
				{setHeight: false, offsetTop: element.offsetHeight, setWidth: false}
			);
			update.style.width = ( element.offsetWidth - 8 ) + 'px';
		}
		Effect.Appear(update,{duration:0.15});
	},

	/*
	Mostly stolen from Autocomplete.Local, but with modifications to put
	resort_id into the <li> elements.
	*/
	autocompleteSelector: function( instance ) {
		var ret       = []; // Beginning matches
		var partial   = []; // Inside matches
		var entry     = instance.getToken();
		var count     = 0;
	
		for (var i = 0; i < instance.options.array.length &&  
			ret.length < instance.options.choices ; i++) { 
	
			var elem = instance.options.array[i];
			var foundPos = instance.options.ignoreCase ? 
				elem.toLowerCase().indexOf(entry.toLowerCase()) : 
				elem.indexOf(entry);
	
			while (foundPos != -1) {
				for ( var j = 0; j < instance.options.resort_ids.length; j++ ) {
					a_resort_id = resort_ids[j];
					if ( resort_ids_to_names[a_resort_id] == elem ) {
						resort_id = a_resort_id
					}
				}
				
				if (foundPos == 0 && elem.length != entry.length) { 
					ret.push("<li id=\"resort" + resort_id + "\"><strong>" + elem.substr(0, entry.length) + "</strong>" + 
						elem.substr(entry.length) + "</li>");
					break;
				} else if (entry.length >= instance.options.partialChars && 
					instance.options.partialSearch && foundPos != -1) {
					if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
						partial.push("<li id=\"resort" + resort_id + "\">" + elem.substr(0, foundPos) + "<strong>" +
							elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
							foundPos + entry.length) + "</li>");
						break;
					}
				}
	
				foundPos = instance.options.ignoreCase ? 
					elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 
					elem.indexOf(entry, foundPos + 1);
	
			}
		}
		if (partial.length)
			ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
		return "<ul>" + ret.join('') + "</ul>";
	},
	
	initAutocomplete: function( resort_ids, resort_ids_to_names ) {
		db_edit_link = $('dashboardEditLink');  
		db_edit_link.dashboard = this;  
		db_edit_link.onclick = function() { this.dashboard.toggleEdit(); }  
		resort_names = []
		for ( var i = 0; i < resort_ids.length; i++) {
			resort_id = resort_ids[i];
			resort_names.push( resort_ids_to_names[resort_id] );
		}
		for ( var i = 0; i < 4; i++) {
			inputId = 'dashboardResortInput' + ( i + 1 )
			ac = new Autocompleter.Local(
				inputId, 'dashboardAutocompletePalette', resort_names,
				{
					fullSearch: true,
					partialChars: 1,
					afterUpdateElement: this.autocompleteAfterUpdateElement,
					onHide: this.autocompleteOnHide,
					selector: this.autocompleteSelector,
					resort_ids: resort_ids,
					resort_ids_to_names: resort_ids_to_names,
					choices: this.choices,
					onShow: this.autocompleteOnShow
				}
			);
			/*
			We have to override the Autocompleter's default fixIEOverlapping function
			because it sets the new z-index too low.
			*/
			ac.fixIEOverlapping = function() {
				Position.clone(this.update, this.iefix);
				this.iefix.style.zIndex = 1;
				this.update.style.zIndex = 9000;
				Element.show(this.iefix);
			};
			input = $(inputId);
			input.onclick = function() { this.focus(); this.select(); }
		}
	},
	toggleEdit: function() {
		if($('dashboardEditOverlay').style.display=='none'){
			Effect.Appear($('dashboardEditOverlay'));
			Effect.Appear($('dashboardEditOverlayBackground'), { to: 0.9 });
		}else{
			Effect.Fade($('dashboardEditOverlay'));
			Effect.Fade($('dashboardEditOverlayBackground'));
			for ( var i = 1; i < 5; i++) {
				$('dashboardResortChoice' + i).value = $('dashboardResortOrigChoice' + i).value;
				$('dashboardResortInput' + i).value = $('dashboardResortOrigInput' + i).value;
			}
		}
	}
}
