/*

	# ------------------------------------------------------------------------------------------------- #
	#																				#
	# DC.SearchTextfield.class.js															#
	#																				#
	# MySpinalCord.com																	#
	# Copyright 2010 Thommy Morgan														#
	# All Rights Reserved																#
	#																				#
	# ------------------------------------------------------------------------------------------------- #
	
			
*/


DC.SearchTextfield = function(id, script, maxNumOfTags) {
	
	this.superClass = DC.Textfield;
	this.superClass(id);
	
	this.field.addEventTo("focus", function() { 
		tryout(function() { 
			this.delegate.searchFieldDidFocus();
		});
	});
	this.field.addEventTo("blur", function() { 
		tryout(function() { 
			this.delegate.searchFieldDidBlur(); 
		});
	});
	this.field.addEventTo("keydown", function() { 
		tryout(function() { 
			this.delegate.searchFieldDidKeyDown(); 
		});
	});
	
	/*
	this.resourceIndex = DC.SearchTextfield.searchFields.length;
	
	// During each phase a script will be called
	// There are a total of three phases: focus, blur and keydown
	this.script 		= (script != null) ? script : null;
	this.maxNumOfTags 	= (maxNumOfTags != null) ? maxNumOfTags : 5; // Default is 5 tags

	this.keydownInterval = null;
	
	// Boolean Variables
	this.maxedTags		= false;
	this.alertedOfMax	= false;
	
	// Search Strings
	this.originalStringValue 	= "";
	this.actualStringValue 		= "";
	this.hiddenStringValue 		= " ";
	
	//
	this.currentTagValue 		= "";
	this.selectedSuggestion		= -1;
	
	// Data
	this.tagSuggestions 		= new Array();
	this.maxResults			= 5;
	
	// Objects
	this.tagSuggestionsDropBox 	= null;
	
	*/
	
	DC.SearchTextfield.searchFields.push(this);

}
DC.SearchTextfield.prototype = new DC.Textfield;


DC.SearchTextfield.searchFields = new Array();


/*
DC.SearchTextfield.prototype.focusSearch = function() {
	with(this) {
		keydownInterval = setInterval(function() { keydownListener(); }, 30);
		keydown();
		focusEvent();
		focus();
	}
}


DC.SearchTextfield.prototype.blurSearch = function() {
	with(this) {
		clearInterval(keydownInterval);
		if(tagSuggestionsDropBox != null) {
			try { 
				var timer;
				var targetEvent = blurEvent;
				var superEvent = blur;
				var isFocused = focused;
				function kill() {
					if(isFocused) {
						clearTimeout(timer);
						return;
					}
					targetEvent();
					superEvent();
					if(tagSuggestionsDropBox)
						tagSuggestionsDropBox.killself();
					clearTimeout(timer);
				}
				timer = setTimeout(kill, 200);
			}
			catch(err) {} // Otherwise forget about it
		}
	}
}


DC.SearchTextfield.prototype.setFocusEvent = function(evt) {
	this.focusEvent = evt;
}


DC.SearchTextfield.prototype.setBlurEvent = function(evt) {
	this.blurEvent = evt;
}


DC.SearchTextfield.prototype.setMaxNumOfTags = function(num) {
	this.maxNumOfTags = num;	
}


DC.SearchTextfield.prototype.deselectCurrent = function() {
	with(this) {
		
		if(
			tagSuggestions.length > 0 &&
			tagSuggestionsDropBox != null && 
			tagSuggestionsDropBox.attachedObject.children.length == ((tagSuggestions.length < maxResults) ? tagSuggestions.length : maxResults) &&
			selectedSuggestion > -1
		) {
			tagSuggestionsDropBox.attachedObject.children[selectedSuggestion].setClass("dropBoxSelectableChoice");
		}
		selectedSuggestion = -1;
			
	}
}


DC.SearchTextfield.prototype.selectChoice = function(choice) {
	with(this) {
		deselectCurrent();
		if(
			tagSuggestions.length > 0 &&
			tagSuggestionsDropBox != null && 
			tagSuggestionsDropBox.attachedObject.children.length == ((tagSuggestions.length < maxResults) ? tagSuggestions.length : maxResults)
		) {
			tagSuggestionsDropBox.attachedObject.children[choice].setClass("dropBoxSelectableChoice dropBoxSelectableChoiceSelected");
			selectedSuggestion = choice;
		}
	}
}


DC.SearchTextfield.prototype.submitChoice = function() {
	with(this) {
		clearInterval(keydownInterval);
		if(
			tagSuggestions.length > 0 &&
			tagSuggestionsDropBox != null &&
			tagSuggestionsDropBox.attachedObject.children.length == ((tagSuggestions.length < maxResults) ? tagSuggestions.length : maxResults) &&
			selectedSuggestion > -1
		) {
			field.value = actualStringValue.substring(0, actualStringValue.toLowerCase().lastIndexOf(currentTagValue.toLowerCase())) + tagSuggestions[selectedSuggestion] + " ";
		}
		focusToEnd();
		focusEvent();
	}
}


DC.SearchTextfield.prototype.textfieldChanged = function() {
	with(this) {
		
		var current_index 	= 1;
		var splitter 		= hiddenStringValue.indexOf(" ", current_index);                               

		if(!didEdit) return;
		
		currentTagValue = hiddenStringValue.substring(hiddenStringValue.lastIndexOf(" ") + 1);

		if(tagSuggestionsDropBox == null) {
			tagSuggestionsDropBox = new DC.Layer();
		}
		field.offsetParent.appendChild(tagSuggestionsDropBox.attachedObject);
		tagSuggestionsDropBox.setStyle("position", "absolute");
		tagSuggestionsDropBox.setStyle("width", field.getWidth() + "px");
		tagSuggestionsDropBox.setStyle("backgroundColor", "#ccc");
		tagSuggestionsDropBox.moveTo(field.getLeft(), field.getTop() + field.getHeight());
		
		requireScript(script + "?tag=" + escape(currentTagValue) + "&field=" + resourceIndex);
		
	}
}

DC.SearchTextfield.prototype.keydown = function() {
	with(this) {
		
		if(event != null) {

			var keynum = (event.keyCode) ? event.keyCode : event.which;
			
			if(keynum == 13) {
				submitChoice();
			}
			else
			if(
				(keynum == 38 || keynum == 40)
			) {

				if(
					tagSuggestions.length > 0 && 
					tagSuggestionsDropBox != null &&
					tagSuggestionsDropBox.attachedObject.children.length == ((tagSuggestions.length < maxResults) ? tagSuggestions.length : maxResults)
				) {

					if(selectedSuggestion > -1)
						tagSuggestionsDropBox.attachedObject.children[selectedSuggestion].setClass("dropBoxSelectableChoice"); 
					
					if(keynum == 38 && selectedSuggestion > 0)
						selectedSuggestion--;
					else	
					if(keynum == 40 && selectedSuggestion < tagSuggestionsDropBox.attachedObject.children.length - 1)
						selectedSuggestion++;
					else
					if(keynum == 38 && selectedSuggestion == 0)
						selectedSuggestion = tagSuggestionsDropBox.attachedObject.children.length - 1;
					else
					if(keynum == 40 && selectedSuggestion == tagSuggestionsDropBox.attachedObject.children.length - 1)
						selectedSuggestion = 0;
						
					selectChoice(selectedSuggestion);
				
				}
				
				return false;
				
			}
			else selectedSuggestion = -1;
			
			// Ignore changes if keynum is any other key
			if(!(keynum >= 48 && keynum <= 90) && keynum != 13) {
				return;
			}
		
		}
		
		if(!didEdit) return;
		
		originalStringValue 	= field.value;
		actualStringValue 		= field.value + ((!maxedTags) ? String.fromCharCode(keynum) : "");
		hiddenStringValue 		= " " + actualStringValue.replace(/, /g, " ").replace(/,/g, " ");
		
		while(hiddenStringValue.indexOf("  ") > -1)
			hiddenStringValue = hiddenStringValue.replace(/  /g, " ");
		
		textfieldChanged();
		
		clearInterval(keydownInterval);
		keydownInterval = setInterval(function() { keydownListener(); }, 100);
		
	}
}


DC.SearchTextfield.prototype.keydownListener = function() {
	with(this) {
		
		if(!didEdit || !focused) return;
		
		if(event != null && tagSuggestions.length > 0) {
			
			var keynum = (event.keyCode) ? event.keyCode : event.which;
			
			if(keynum == 38 || keynum == 40) {
			
				focusToEnd();
				focusToEnd();
				focusToEnd();
				
			}
				
		}

		if(field.value.length < actualStringValue.length) {
		
			// Backspace

			alertedOfMax = false;
			originalStringValue 	= field.value;
			actualStringValue 		= field.value;
			hiddenStringValue 		= " " + actualStringValue.replace(/, /g, " ").replace(/,/g, " ");
			
			while(hiddenStringValue.indexOf("  ") > -1)
				hiddenStringValue = hiddenStringValue.replace(/  /g, " ");
			
			textfieldChanged();
			
			tagCounter = hiddenStringValue.match(/ /g).length;
			if(tagCounter < maxNumOfTags) maxedTags = false;
			
		}

	}
}
*/
