/* Copyright (c) 2008 Extentech Inc. All Rights Reserved

##### Sheetster&trade; Web Application #####

This file is a part of the Sheetster&trade; Web Application

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

If you would like to redistribute this software in a closed-source application,
dual-licensed commercial versions are available from Extentech.

For a fully supported and redistributable commercial license, 
please visit www.extentech.com or contact us at:

 sales@extentech.com
 Extentech Inc.
 1032 Irving Street #910
 San Francisco, CA 94122
 415-759-5292
 
*/
/**
	Edit cursor is a javascript class that takes an element as a constructor and
	floats a textarea on top of the element, cloning its size, font, color, etc.
	
	The constructor for the edit cursor takes the following fields:
	targetElement:  a div on which the edit cursor will be floated
	onSumbitFunction:  a function to be called when the cursor is submitted
	defaultValue: a value to place in the cursor.  This is optional, and the cursor can try to get its own value from the field
	vertical:	true if it's a vertical text field, optional
**/

editCursor = Class.create();
editCursor.prototype = {
	
	initialize: function(targetElement, onSubmitFunc, defaultValue, vertical, setlocation){
		var sloc = true;
		if(setlocation != undefined)
			sloc = setlocation;
		this.target = targetElement;
		this.onSubmit = onSubmitFunc;
		this.cursor = null;
		this.initializeCursor();
		this.defaultVal= null;
		if (defaultValue) {
			this.defaultVal = defaultValue;
		}
		if (vertical) this.vertical= true;
		this.setCursorContents();
		if(sloc){
			this.setCursorLocation();
		}else{ // in-frame cursor should show at top of window
			this.cursor.style.top = 0;
		}
		//this.cursor.observe('blur', this.submitChangeListener); // needs to happen after activate so onblur event not kicked off
	},
	
	keyCheck: function(e){
		if (e==null)e=event;
		var KeyID = (window.event) ? event.keyCode : e.keyCode;
		switch(KeyID){
			case 13: // enter key		
				this.submitChange();
				break;
			case 27: //escape key
				this.cancelChange();
			  	  break;
			default: 
				return true
		}
		return false;
	},
	
	setDefaultVal: function(defaultValue){
		this.defaultVal = defaultValue;
		this.cursor.value = this.defaultVal;	
	},
	
	/**
		Submit the new contents of the text box to the function passed
		into the cursor.
	**/
	submitChange: function(){		
		this.onSubmit(this.cursor.value);
		this.reset();
	},
	
	/**
	clear and hide the cursor for next usage.
	**/
	cancelChange: function(){
		this.reset();
	},
	/*
		Insert the cursor object into the dom and apply needed actions to it
	**/
	initializeCursor: function(){
		if (this.cursor == null){
			this.cursor = $('editCursor');
			if (!this.cursor){
				this.cursor = Element.extend(document.createElement('textarea'));	 		
				this.cursor.id = 'editCursor';
				document.body.appendChild(this.cursor);
				this.cursor.appendChild(document.createTextNode('new name'));
				this.cursor.enable();
				this.cursor.hide();
				// Position.absolutize(this.cursor);
				this.cursor.addClassName('editCursor');
				
				// copy target size and location
				this.cursor.style.height = this.target.style.height;
				this.cursor.style.width = this.target.style.width; 
				this.cursor.style.left = this.target.style.left;
				this.cursor.style.top = this.target.style.top;
				
				this.cursor.style.overflow = "hidden";
				// this.cursor.style.wordWrap="break-word";
				// event handling for lose focus
				this.cursor.observe('keydown', this.keyCheck.bindAsEventListener(this));
				// this.submitChangeListener = this.submitChange.bindAsEventListener(this);
			}
		}
	},
	
	/**
	setCursorLocation moves the location of the cursor to where the selection'
	is occurring.  
	
	Both the edit cursor and the multi-select can be updated.
	**/
	 setCursorLocation: function() {
		var theCell = this.target;
		var offsets = theCell.viewportOffset();
		this.cursor.style.position = 'fixed';
		if (is.ie){ // ie box model is wrong, so offsets slightly different
			this.cursor.style.left = (offsets[0] -1)  + 'px';
			this.cursor.style.top = (offsets[1] -1) + 'px';
			if (!this.vertical) {	// 20080602 KSC: if vertical, switch w/h for edit cursor
				this.cursor.style.width = (theCell.offsetWidth -3) + 'px';			
				this.cursor.style.height = (theCell.offsetHeight -3)+ 'px';
			} else {	// it's vertical
				this.cursor.style.height= (theCell.offsetWidth) + 'px';			
				this.cursor.style.width = (theCell.offsetHeight)+ 'px';
				this.target.hide();	
			}
		}else{
			this.cursor.style.left = (offsets[0] -1)  + 'px';
			this.cursor.style.top = (offsets[1] -1) + 'px';
			if (!this.vertical) {	// if vertical, switch w/h for edit cursor
				this.cursor.style.width = (theCell.offsetWidth -1) + 'px';
				this.cursor.style.height = (theCell.offsetHeight -1)+ 'px';
			} else {	// it's vertical
				this.cursor.style.height= (theCell.offsetWidth +10) + 'px';
				this.cursor.style.width = (theCell.offsetHeight+10)+ 'px';	
				this.target.hide();	
			}
		}
		this.cursor.style.zIndex = 150;
	},
	
	/**
		sets the cursor contents to the contents
		of the activeCell, and the style to that of the 
		underlying cell
	**/
	setCursorContents: function(){
		//this.cursor.value = this.target.getVal();					
		if(this.defaultVal){
			this.cursor.value = this.defaultVal;	
		}else{
			
		}
		var classArr = $w(this.target.className);
		for (var i=0;i<classArr.length;i++){
			this.cursor.addClassName(classArr[i]);	  
		}
		this.cursor.show();
		this.cursor.activate(); // timing issue with blur below..
	},
	
	/**
		reset the cursor, remove class names, hide the element, etc
	**/
	reset: function(){
		var classArr = $w(this.target.className);
		for (var i=0;i<classArr.length;i++){
			this.cursor.removeClassName(classArr[i]);	  
		}
		this.cursor.hide();
		if (this.vertical) this.target.show();
		this.cursor.blur();
	},
	
	/**
		Is the edit in place tool currently active?
	**/
	isActive: function(){
		if(this.cursor.visible())return true;
		return false;
	}
	
	
}
