/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is XulApp IDE.
 *
 * The Initial Developer of the Original Code is Synesis Software (USA) Inc.
 *

 * Portions created by the Initial Developer are Copyright (C) 2001-2005
 * Synesis Software (USA) Inc. the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * 	Peter Wilson, orignal author.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the LGPL or the GPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 *
 * ***** END LICENSE BLOCK ***** */
 

const xablock = "xa-block";
const xasubblock = "xa-subblock";


const TWIPS = 12;

const collapsibleElements = ["xa-class-stag", "xa-documentation-stag", "xa-static-stag", "xa-instance-stag", "xa-function-stag", "xa-comment-stag"];
const allowsEnterKey = ["span"];

const XJS_BASE = "chrome://xulapp/content/tools/xjsEditor/";

const ViewToXjs ="xulapp-org-xjs-inverse.xsl"


// Hard wire nsIDOMKeyEvent.idl  constants for demo.
  const  VK_CANCEL         = 0x03;
  const VK_HELP           = 0x06;
  const VK_BACK_SPACE     = 0x08;
  const VK_TAB            = 0x09;
  const VK_CLEAR          = 0x0C;
  const VK_RETURN         = 0x0D;
  const VK_ENTER          = 0x0E;
  const VK_SHIFT          = 0x10;
  const VK_CONTROL        = 0x11;
  const VK_ALT            = 0x12;
  const VK_PAUSE          = 0x13;
  const VK_CAPS_LOCK      = 0x14;
  const VK_ESCAPE         = 0x1B;
  const VK_SPACE          = 0x20;
  const VK_PAGE_UP        = 0x21;
  const VK_PAGE_DOWN      = 0x22;
  const VK_END            = 0x23;
  const VK_HOME           = 0x24;
  const VK_LEFT           = 0x25;
  const VK_UP             = 0x26;
  const VK_RIGHT          = 0x27;
  const VK_DOWN           = 0x28;
  const VK_PRINTSCREEN    = 0x2C;
  const VK_INSERT         = 0x2D;
  const VK_DELETE         = 0x2E;
  
  const NODE_TYPE_ELEMENT = 1;
  const NODE_TYPE_ATTRIBUTE = 2;
  const NODE_TYPE_TEXT = 3;
  const NODE_TYPE_CDATA = 4;
  const NODE_TYPE_ENTITY_REF = 5;
  const NODE_TYPE_ENTITY = 6;
  const NODE_TYPE_PROCESSING_INSTRUCTION  = 7; 
  const NODE_TYPE_COMMENT  = 8;
  const NODE_TYPE_DOCUMENT  = 9;
  const NODE_TYPE_DOCUMENT_TYPE  = 10;
  const NODE_TYPE_DOCUMENT_FRAGMENT  = 11;
  const NODE_TYPE_NOTATION  = 12;

function OnLoad() {
	// The XSLT processor (Transfomiix) is broken: it does not output 
	// namespace declarations correctly (at all?)
	// So add one manually:
	document.documentElement.setAttribute("xmlns:xul","http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
}




function OnClick(event) {
	var elem = event.target;
	if (event.detail == 2) { // Double click.
		var parentElem = elem.parentNode;
		if (isCollapsibleElement(elem)) {
			if (parentElem.getAttribute("xa-collapsed")=="true") 
				parentElem.removeAttribute("xa-collapsed");
			else 
				parentElem.setAttribute("xa-collapsed","true");
		}
	}
}

function isCollapsibleElement(elem) {

	if (!elem)
		return false;
	for (var i=0; i<collapsibleElements.length; i++) {
		if (elem.localName == collapsibleElements[i])
			return true;
	}
	return false;
}

function OnKeyPress(event) {
	var tran;
	if (event.ctrlKey || event.altKey || event.metaKey ) {
		return;
	}	
	switch (event.keyCode) {
	case VK_RIGHT:
	case VK_LEFT:
	case VK_UP:
	case VK_DOWN:
	case VK_HOME:
	case VK_END:
	case VK_PAGE_DOWN:
	case VK_PAGE_UP:
	case VK_TAB: //TODO add other control keys which should be ignored.
		return;
	}

	var node = event.target;
	if (node.nodeType != NODE_TYPE_ELEMENT) 
		return;
	if (!isInputElement(node)) 
		return;
	var selection = window.getSelection();

	if (selection.anchorNode == selection.focusNode) {
		// Cache old values for current node.
		var elem = selection.anchorNode.parentNode;
		var textNode = node.firstChild;
		var from = selection.anchorOffset;
		var to	= selection.focusOffset;
		if (from > to) {
			from = to;
			to	= selection.anchorOffset;
		}
		//debug("From:" + from + " to:" + to);
		switch (event.keyCode) {
		case VK_BACK_SPACE:
			if (from == to) {
				from--;
				if (from < 0)
					return;
			}	
			textNode.nodeValue = textNode.nodeValue.substr(0,from)
										+ textNode.nodeValue.substr(to);
			selection.collapse(textNode, from);
			break;
		case VK_DELETE:
			if (from == to) {
				if (to > textNode.nodeValue.length)
					return;
				to++;
			}	
			textNode.nodeValue = textNode.nodeValue.substr(0,from)
										+ textNode.nodeValue.substr(to);
			selection.collapse(textNode, from);
			break;
		case VK_ENTER:
		case VK_RETURN:
			if (!elemAllowsEnterKey(event))
				break;
			textNode.nodeValue = textNode.nodeValue.substr(0,from)
										+ "\n" 
										+ textNode.nodeValue.substr(to);
			selection.collapse(textNode, from+1);											
			break;
		default:
			if (event.charCode == 0) // non-typeable character NumKey etc.
				return;
			var chars = mapEntities(event);
			textNode.nodeValue = textNode.nodeValue.substr(0,from)
										+ chars 
										+ textNode.nodeValue.substr(to);
			selection.collapse(textNode, from + chars.length);											
		}
//		tran = new ValueTransaction(textNode, prevAnchor)
//		transactionManager.doTransaction(tran);
//		OnTransaction(transactionManager);
	}
}

function elemAllowsEnterKey(event) {
	var elemName = event.target.localName;
	for (var i=0; i< allowsEnterKey.length; i++) {
		if (allowsEnterKey[i] == elemName) {
			//debug("elemAllowsEnterKey" + elemName);
			return true;
		}
	}
	return false;
}

function isInputElement(elem) {
	var enabled = document.defaultView.getComputedStyle(elem, '' ).getPropertyValue("-moz-user-input");	 
	if (enabled != "enabled")
		return false;
	var modify = document.defaultView.getComputedStyle(elem, '' ).getPropertyValue("-moz-user-modify");	
	return (modify != "read-only");
}
function mapEntities(event) {
	// Do nothing for now.	
	return String.fromCharCode(event.charCode);
}


