/*

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


DC.Textfield = function(id) {

	if($(id) == null) return NO;

	this.field 			= $(id);	// Establish field
	this.field.delegate 	= this;	// Establish delegate
	
	this.focused			= NO;
	this.lastText			= "";	// The placeholder text - Not used with Safari or Chrome
	this.didEdit			= NO;	// Did edit boolean
	this.fromType			= "TEXT";	// From type used for only inputs
	this.toType			= "TEXT";	// To type use for only inputs
	
	this.focusEvent		= function() {}
	this.unfocusEvent		= function() {};

	with(this) {
		
		// Add focusing events
		field.setClass("DCTextfield", YES);
		field.addEventTo("focus", 	function() { this.delegate.focus(); });
		field.addEventTo("blur", 	function() { this.delegate.blur(); });
		
		// Styling
		
		// For Safari and Chrome, makes things a whole lot easier
		if(DC.browserType == BrowserTypeSafari || DC.browserType == BrowserTypeChrome) {
			field.placeholder = field.value;	// Set placeholder
			field.style.color = "#000";		// Set color
			field.value = "";				// Reset value
		}
		else {
		
			field.style.color = "#888";	// Set field color to off gray
		
			if(field.tagName.toLowerCase() == "input")
				lastText = field.value;
			else
			if(field.tagName.toLowerCase() == "textarea")
				lastText = field.innerHTML.toString();
			
		}
	
	}
	
	DC.Textfield.allTextFields.push(this);

}
DC.Textfield.allTextFields = new Array();


DC.Textfield.prototype.value = function() {
	return this.field.value;
}


DC.Textfield.prototype.setValue = function(val) {
	this.focus();
	this.field.value = val;
	this.blur();	
}


DC.Textfield.prototype.setFromType = function(type) {
	this.fromType = type;
}


DC.Textfield.prototype.setToType = function(type) {
	this.toType = type;
}


DC.Textfield.prototype.focusToStart = function() {
	with(this) {
		
		field.selectionStart 	= 0;
		field.selectionEnd 		= 0;
		field.focus();
		field.focus();
		
	}
}


DC.Textfield.prototype.focusToEnd = function() {
	with(this) {
		
		field.selectionStart 	= field.value.length;
		field.selectionEnd 		= field.value.length;
		field.focus();
		field.focus();
			
	}
}


DC.Textfield.prototype.focus = function() {
	with(this) {
		
		focused = YES;
		
		if(!didEdit) {
		
			if(DC.browserType != BrowserTypeSafari && DC.browserType != BrowserTypeChrome) {
				
				if(field.tagName.toLowerCase() == "input")
					field.value = "";	
				else
				if(field.tagName.toLowerCase() == "textarea")
					field.innerHTML = "";
					
				field.style.color = "#000";
			
			}
			
			if(toType != fromType && field.tagName.toLowerCase() == "input") {
				
				// Accomodate Internet Explorer Swap Fields
				if(DC.browserType == BrowserTypeIE) {
	
					var newField = document.createElement("input");
	
					newField.type = toType;
	
					if(field.value)	newField.value 	= field.value;
					if(field.name)		newField.name 		= field.name;
					if(field.className)	newField.className 	= field.className;
					if(field.id)		newField.id 		= field.id;
					
					Object.conformObject(newField);
					
					new DC.Textfield(newField);
					newField.style.color = "#000";
					newField.delegate = field.delegate;
		               field.parentNode.replaceChild(newField, field);
	
					field = newField;
	
					field.focus(); field.focus();
	
				}
				// Normal isIE Type Switch
				else field.setType(toType);
			
			}
			
			didEdit = YES;
		
		}
		
		focusEvent();
		
	}
}


DC.Textfield.prototype.blur = function() {
	with(this) {
		
		focused = NO;
		
		if(
			(field.tagName.toLowerCase() == "input" && field.value == "")				||
			(field.tagName.toLowerCase() == "textarea" && field.innerHTML.toString() == "") ||
			(field.tagName.toLowerCase() == "textarea" && field.value == "") 
		) {

			didEdit = NO;
			
			if(DC.browserType != BrowserTypeSafari && DC.browserType != BrowserTypeChrome) {
	
				if(field.tagName.toLowerCase() == "input")
					field.value = lastText;	
				else
				if(field.tagName.toLowerCase() == "textarea")
					field.innerHTML = lastText;
				
				field.style.color = "#888";
	
			}
	
			if(toType != fromType && field.tagName.toLowerCase() == "input") {
			
				// Accomodate Internet Explorer Swap Fields
				if(DC.browserType == BrowserTypeIE) {
	
					var newField = document.createElement("input");
	
					newField.type = fromType;
	
					if(field.value) 	newField.value 	= field.value;
					if(field.name)		newField.name 		= field.name;
					if(field.className)	newField.className 	= field.className;
					if(field.id)		newField.id 		= field.id;
					
					Object.conformObject(newField);
					
					new DC.Textfield(newField);
					newField.delegate = field.delegate;
		               field.parentNode.replaceChild(newField, field);
	
					field = newField;
	
				}
				// Normal isIE Type Switch
				else field.setType(fromType);
			
			}
		
		}
		
		unfocusEvent();
		
	}
}

DC.Textfield.prototype.reset = function() {
	with(this){ 
	
		if(field.tagName.toLowerCase() == "input")
			field.value = "";
		else
		if(field.tagName.toLowerCase() == "textarea")
			field.innerHTML = "";
		blur();
		
	}
}


DC.Textfield.prototype.setText = function(str) {
	with(this) {
		
		field.value = str;
		didEdit = YES;
		
	}
}
