//////////////////////////////////////////////////////////////
//															//
//	© 2005 - 2008 Vereyon									//
//  http://www.vereyon.nl									//
//	All rights reserved										//
//															//
//////////////////////////////////////////////////////////////

SymbolPicker.prototype.__type = "SymbolPicker";
SymbolPicker.prototype.hParentNode = null;
SymbolPicker.prototype.CallbackFunction = null;
SymbolPicker.prototype.CallbackClass = null;
SymbolPicker.prototype.bVisible = false;
SymbolPicker.prototype.bCaps = false;
SymbolPicker.prototype.arrCharacters = new Array();
SymbolPicker.prototype.arrCharactersCaps = new Array();
SymbolPicker.prototype.arrCharacterButtons = null;
SymbolPicker.prototype.hContainerDiv = null;
SymbolPicker.prototype.hCapsButton = null;
SymbolPicker.prototype.hNoCapsButton = null;

function SymbolPicker(hParentNode) {

	this.Initialize = function(hParentNode) {
	
		// Declare varaibles
		var newDiv, newHeaderDiv, newInput, newP;
		
		// Store parent node handle
		this.hParentNode = hParentNode;
		
		// Initialize character arrays
		this.arrCharacters = new Array("ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "¿", "¡", "§");
		this.arrCharactersCaps = new Array("ß", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "¿", "¡", "§");
		
		// Create container div
		this.hContainerDiv = document.createElement("DIV");
		this.hContainerDiv.className = "characterPickerContainer";
		this.hContainerDiv.style.visibility = "hidden";
		this.hContainerDiv.onclick = this.OnContainerClick;
		this.hContainerDiv.owningClass = this;
		this.hParentNode.appendChild(this.hContainerDiv);
		
		newHeaderDiv = document.createElement("DIV");
		newHeaderDiv.className = "characterPickerHeader";
		this.hContainerDiv.appendChild(newHeaderDiv);
		
		newP = document.createElement("P");
		newP.appendChild(document.createTextNode(TEXT['SymbolPicker.ToggleCaps']));
		newP.owningClass = this;
		newP.onclick = this.ToggleCaps;
		newHeaderDiv.appendChild(newP);
		
		newP = document.createElement("P");
		newP.className = "characterPickerHeaderClose";
		newP.appendChild(document.createTextNode("X"));
		newP.title = TEXT['SymbolPicker.Close'];
		newP.owningClass = this;
		newP.onclick = this.OnContainerClick;
		newHeaderDiv.appendChild(newP);
		
		this.CreateCharacterButtons();
	}
	
	this.ToggleCaps = function(e) {
	
		// Declare variables
		var srcClass, oEvent;
		
		srcClass = this.owningClass;
		
		if(window.event) {
		
			// Windows IE and DOM level 2
			// Bug: using window.event seems to slowdown event handling in IE. Perhaps IE takes a lot of time setting the window.event object.
			oEvent = window.event;
		} else {
		
			// Firefox
			oEvent = e;
		}
		
		// Stop even bubble
		try {
			oEvent.stopPropagation();
		}
		catch(ex) {
		}
		oEvent.cancelBubble = true;
	
		// Toggle caps
		this.owningClass.bCaps = !this.owningClass.bCaps;
		this.owningClass.DestroyCharacterButtons();
		this.owningClass.CreateCharacterButtons();
	}
	
	this.OnButtonMouseOver = function() {
		this.className = "characterPickerButtonHover";
	}
	
	this.OnButtonMouseOut = function() {
		this.className = "characterPickerButton";
	}
	
	this.CreateCharacterButtons = function() {
		
		// Delcare variables
		var newCharDiv, newP;
		
		// Caps or non-caps?
		if(this.bCaps) {
			
			// Iterate trough characters
			for(x = 0; x < this.arrCharactersCaps.length; x++) {
				
				// Create new character button
				newCharDiv = document.createElement("DIV");
				newCharDiv.className = "characterPickerButton";
				newCharDiv.onclick = this.OnCharacterButtonClick;
				newCharDiv.onmouseover = this.OnButtonMouseOver;
				newCharDiv.onmouseout = this.OnButtonMouseOut;
				newCharDiv.owningClass = this;
				
				newP = document.createElement("P");
				newP.appendChild(document.createTextNode(this.arrCharactersCaps[x]));
				
				newCharDiv.appendChild(newP);
				this.hContainerDiv.appendChild(newCharDiv);
			}
		} else {
			
			// Iterate trough characters
			for(x = 0; x < this.arrCharacters.length; x++) {
				
				// Create new character button
				newCharDiv = document.createElement("DIV");
				newCharDiv.className = "characterPickerButton";
				newCharDiv.onclick = this.OnCharacterButtonClick;
				newCharDiv.onmouseover = this.OnButtonMouseOver;
				newCharDiv.onmouseout = this.OnButtonMouseOut;
				newCharDiv.owningClass = this;
				
				newP = document.createElement("P");
				newP.appendChild(document.createTextNode(this.arrCharacters[x]));
				
				newCharDiv.appendChild(newP);
				this.hContainerDiv.appendChild(newCharDiv);
			}
		}
	}
	
	this.DestroyCharacterButtons = function() {
	
		// Destroy all the character buttons
		while(this.hContainerDiv.childNodes.length > 1) {
			this.hContainerDiv.removeChild(this.hContainerDiv.childNodes[this.hContainerDiv.childNodes.length - 1]);
		}
	}
	
	this.ShowDialog = function(x, y) {
	
		// Show the character picker at the given coördinates
		this.hContainerDiv.style.top = (y + "px");
		this.hContainerDiv.style.left = (x + "px");
		this.hContainerDiv.style.visibility = "visible";
		this.bVisible = true;
		
		// Wire up events
		//Sys.Document.OnClick.Suscribe(this.OnDocumentClick, this);
	}
	
	this.CloseDialog = function() {
	
		// Hide the dialog
		this.hContainerDiv.style.visibility = "hidden";
		this.bVisible = false;
		
		// Unwire events
		//Sys.Document.OnClick.Unsuscribe(this.OnDocumentClick, this);
	}
	
	this.OnContainerClick = function() {
	
		this.owningClass.CloseDialog();
	}
	
	this.OnCharacterButtonClick = function() {
		
		// Handles a click event from a character button
		if(this.owningClass.CallbackFunction)
			this.owningClass.CallbackFunction(this.firstChild.firstChild.nodeValue);
		this.owningClass.CloseDialog();
	}
	
	this.Initialize(hParentNode);
}

SymbolPicker.prototype.OnDocumentClick = function(e) {
	
	// Declare variables
	var hContainerDiv;
	var pos;
	
	hContainerDiv = $(this.hContainerDiv);
	
	// Check if the container div was clicked
	pos = hContainerDiv.getAbsolutePosition();
	//alert("ok")
	if(!(e.clientX >= pos.x && e.clientX <= pos.x + this.hContainerDiv.clientWidth && e.clientY >= pos.y && e.clientY <= pos.y + this.hContainerDiv.clientHeight)) {
		this.CloseDialog();
	}
}
