/**
 *
 *		jsColorPallet - very basic color pallet
 *		jsColorPallet is a very basic color pallet that is bound to text fields in your document.
 *		onFocus of the selected text field the selected color pallet will apear allowing the user
 *		to select a color. There are multipul color pallets the user to select colors from.
 *		
 *		Thanks to Dynarch.com for a portion of code barrowed.
 *
 *		A portion Copyright 2003-2006 (c) Dynarch.com
 *		The rest Copyright 2006 (c) Skeletal Design, Inc
 *
 *		@version	v1.0.1 2006/09/15
 *
 */


jsColorPallet = Class.create();
jsColorPallet.prototype = {
	initialize: function(title, form, fields) {
		/**
		 *	Initialization of jsColorPallet
		 *	Bind, Creates, and Draws out all the color pallets
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	title	string	optional	Title of the color pallet
		 *	@param	form	string	The id of the form that the color pallet will be bound to
		 *	@param	fields	array	array containing all the idz of the elements to bind the color pallet to show on
		 *	@date	2006/09/15
		 */
		this.workingField = "";	// The current/last field to be evaluated to display the color pallet
		this.colorPallets = new Array();	// The array containg arrays of all the color pallets and there color
		this.defineColorPallets();	// Create each color pallets array
		this.drawColorPallet(title);	// Draw all the color pallets
		
		//support: removed this. causes error in IE.  replaced it by binding only on fields passed in
//		this.bindForm(form);	// Bind the jsColorPallet to all the forms
		
		this.currentPallet = {id:0, pallet: new Array()};	// Object to manage and store each color pallet
		this.boundFields = fields;	// The array of fields to display the color pallet on
		
		//support: added this
 		for(i=0;i<this.boundFields.length;i++){
			document.getElementById(this.boundFields[i]).onfocus = function(){colorPallet.displayPallet(this);}; 
		}
		
	},
	//Displays the Pallet reletive to the Field in question
	displayPallet: function(field, pallet){
		/**
		 *	Display the color pallets
		 *	Get the current field in focus, possitions the color pallet to that field
		 *	and then displays the color pallet.
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	field	object	An object of the current working form field
		 *	@param	pallet	integer	optional	The id of the color pallet you would like to use
		 *	@date	2006/09/15
		 */
		var show = false;
		for(i=0;i<this.boundFields.length;i++){
			if(this.boundFields[i] == field.id){
				show = true;
			}
		}
		
		if(!show){
			this.togglePalletDisplay('hide');
			return;
		}
		
		//Set the Global Ref. Var to the current working field
		if(field)
			this.workingField = field;
			
		var workingFieldOffset = this.workingField.scrollWidth;
		
		if(!pallet){
			pallet = this.currentPallet.id;
		}
		else {
			this.currentPallet.id = pallet;	
		}
		
		if(!this.currentPallet.pallet[pallet])
			this.currentPallet.pallet[pallet] = $('colorPallet'+ pallet);
		
		//Get possition of the Field in question
		var textField = this.getAbsolutePos(this.workingField);
		
		this.currentPallet.pallet[pallet].style.top = (textField.y) + 'px';
		this.currentPallet.pallet[pallet].style.left = (textField.x + workingFieldOffset) + 'px';
		this.togglePalletDisplay('show');
	},
	togglePalletDisplay: function(what){
		/**
		 *	toggle the displaying of the color pallet
		 *	Check the current status of the color pallet and shows or hides it based on that
		 *	If what is past the color pallet will perform that function
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	what	string	optional	Force color pallet to be shown 'show' or hiden 'hide'
		 *	@date	2006/09/15
		 */
		if(what == 'show'){
			if(this.currentPallet.pallet[this.currentPallet.id].style.display == 'none'){
				this.currentPallet.pallet[this.currentPallet.id].style.display = 'block';
			}
		}
		else if(what == 'hide'){
			if(this.currentPallet.pallet[this.currentPallet.id].style.display == 'block'){
				this.currentPallet.pallet[this.currentPallet.id].style.display = 'none';
			}
		}
		else {
			if(this.currentPallet.pallet[this.currentPallet.id].style.display == 'none'){
				this.currentPallet.pallet[this.currentPallet.id].style.display = 'block';
			}
			else {
				this.currentPallet.pallet[this.currentPallet.id].style.display = 'none';
			}
		}
	},
	toggleColorPallet: function(pallet){
		/**
		 *	Changes color pallet
		 *	Switchs between each color pallet
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	pallet	integer	Id of the color pallet you want to show
		 *	@date	2006/09/15
		 */
		this.togglePalletDisplay();
		if(!this.currentPallet.pallet[pallet])
			this.currentPallet.pallet[pallet] = $('colorPallet'+ pallet);
			
		this.currentPallet.id = pallet;
		this.displayPallet(this.workingField);
	},
	getAbsolutePos: function(el) {
		/**
		 *	gets the position of the given element
		 *	Gets the position of the passed element. And returns that value
		 *	Function was barrowed from Dynarch.com's jsCalendar program
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	el	object	Object of the element you want to get position of
		 *	@return	r	object	returns x 'r.x', y 'r.y' of the element given
		 *	@date	2006/09/15
		 */
		  var SL = 0, ST = 0;
		  var is_div = /^div$/i.test(el.tagName);
		  if (is_div && el.scrollLeft)
			   SL = el.scrollLeft;
		  if (is_div && el.scrollTop)
			   ST = el.scrollTop;
		  var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		  if (el.offsetParent) {
			   var tmp = this.getAbsolutePos(el.offsetParent);
			   r.x += tmp.x;
			   r.y += tmp.y;
		  }
		  return r;
	 },
	 //Return the selected color to the field in question, and hide the pallet
	 returnColor: function(anchorField){
		/**
		 *	returns the picked color
		 *	Returns ths picked color back to the currently focused element
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	anchorField	string	hex color to return back to the current field
		 *	@date	2006/09/15
		 */
		if(anchorField != ''){
			this.workingField.value = anchorField;
			
			//support: added this
			this.workingField.style.background = anchorField;
		}
		//Hide pallet with cool effect.
		this.togglePalletDisplay();
	 },
	 
	 defineColorPallets: function(){
		/**
		 *	Defines the color pallets
		 *	Creates each array of colors for each color pallet
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@date	2006/09/15
		 */
		this.colorPallets[0] = new Array(
			['#000000', '#000000', '#000000', '#000000', '#003333', '#006600', '#009900', '#00CC00', '#00FF00', '#330000', '#333300', '#336600', '#339900', '#33CC00', '#33FF00', '#660000', '#663300', '#666600', '#669900', '#66CC00', '#66FF00'],
			['#000000', '#333333', '#000000', '#000033', '#003333', '#006633', '#009933', '#00CC33', '#00FF33', '#330033', '#333333', '#336633', '#339933', '#33CC33', '#33FF33', '#660033', '#663333', '#666633', '#669933', '#66CC33', '#66FF33'],
			['#000000', '#666666', '#000000', '#000066', '#003366', '#006666', '#009966', '#00CC66', '#00FF66', '#330066', '#333366', '#336666', '#339966', '#33CC66', '#33FF66', '#660066', '#663366', '#666666', '#669966', '#66CC66', '#66FF66'],
			['#000000', '#999999', '#000000', '#000099', '#003399', '#006699', '#009999', '#00CC99', '#00FF99', '#330099', '#333399', '#336699', '#339999', '#33CC99', '#33FF99', '#660099', '#663399', '#666699', '#669999', '#66CC99', '#66FF99'],
			['#000000', '#CCCCCC', '#000000', '#0000CC', '#0033CC', '#0066CC', '#0099CC', '#00CCCC', '#00FFCC', '#3300CC', '#3333CC', '#3366CC', '#3399CC', '#33CCCC', '#33FFCC', '#6600CC', '#6633CC', '#6666CC', '#6699CC', '#66CCCC', '#66FFCC'],
			['#000000', '#FFFFFF', '#000000', '#0000FF', '#0033FF', '#0066FF', '#0099FF', '#00CCFF', '#00FFFF', '#3300FF', '#3333FF', '#3366FF', '#3399FF', '#33CCFF', '#33FFFF', '#6600FF', '#6633FF', '#6666FF', '#6699FF', '#66CCFF', '#66FFFF'],
			['#000000', '#FF0000', '#000000', '#990000', '#993300', '#996600', '#999900', '#99CC00', '#99FF00', '#CC0000', '#CC3300', '#CC6600', '#CC9900', '#CCCC00', '#CCFF00', '#FF0000', '#FF3300', '#FF6600', '#FF9900', '#FFCC00', '#FFFF00'],
			['#000000', '#00FF00', '#000000', '#990033', '#993333', '#996633', '#999933', '#99CC33', '#99FF33', '#CC0033', '#CC3333', '#CC6633', '#CC9933', '#CCCC33', '#CCFF33', '#FF0033', '#FF3333', '#FF6633', '#FF9933', '#FFCC33', '#FFFF33'],
			['#000000', '#0000FF', '#000000', '#990066', '#993366', '#996666', '#999966', '#99CC66', '#99FF66', '#CC0066', '#CC3366', '#CC6666', '#CC9966', '#CCCC66', '#CCFF66', '#FF0066', '#FF3366', '#FF6666', '#FF9966', '#FFCC66', '#FFFF66'],
			['#000000', '#FFFF00', '#000000', '#990099', '#993399', '#996699', '#999999', '#99CC99', '#99FF99', '#CC0099', '#CC3399', '#CC6699', '#CC9999', '#CCCC99', '#CCFF99', '#FF0099', '#FF3399', '#FF6699', '#FF9999', '#FFCC99', '#FFFF99'],
			['#000000', '#00FFFF', '#000000', '#9900CC', '#9933CC', '#9966CC', '#9999CC', '#99CCCC', '#99FFCC', '#CC00CC', '#CC33CC', '#CC66CC', '#CC99CC', '#CCCCCC', '#CCFFCC', '#FF00CC', '#FF33CC', '#FF66CC', '#FF99CC', '#FFCCCC', '#FFFFCC'],
			['#000000', '#FF00FF', '#000000', '#9900FF', '#9933FF', '#9966FF', '#9999FF', '#99CCFF', '#99FFFF', '#CC00FF', '#CC33FF', '#CC66FF', '#CC99FF', '#CCCCFF', '#CCFFFF', '#FF00FF', '#FF33FF', '#FF66FF', '#FF99FF', '#FFCCFF', '#FFFFFF']
		);
		
		this.colorPallets[1] = new Array(
			['#000000', '#000000', '#000000', '#CCFFFF', '#CCFFCC', '#CCFF99', '#CCFF66', '#CCFF33', '#CCFF00', '#66FF00', '#66FF33', '#66FF66', '#66FF99', '#66FFCC', '#66FFFF', '#00FFFF', '#00FFCC', '#00FF99', '#00FF66', '#00FF33', '#00FF00'],
			['#000000', '#333333', '#000000', '#CCCCFF', '#CCCCCC', '#CCCC99', '#CCCC66', '#CCCC33', '#CCCC00', '#66CC00', '#66CC33', '#66CC66', '#66CC99', '#66CCCC', '#66CCFF', '#00CCFF', '#00CCCC', '#00CC99', '#00CC66', '#00CC33', '#00CC00'],
			['#000000', '#666666', '#000000', '#CC99FF', '#CC99CC', '#CC9999', '#CC9966', '#CC9933', '#CC9900', '#669900', '#669933', '#669966', '#669999', '#6699CC', '#6699FF', '#0099FF', '#0099CC', '#009999', '#009966', '#009933', '#009900'],
			['#000000', '#999999', '#000000', '#CC66FF', '#CC66CC', '#CC6699', '#CC6666', '#CC6633', '#CC6600', '#666600', '#666633', '#666666', '#666699', '#6666CC', '#6666FF', '#0066FF', '#0066CC', '#006699', '#006666', '#006633', '#006600'],
			['#000000', '#CCCCCC', '#000000', '#CC33FF', '#CC33CC', '#CC3399', '#CC3366', '#CC3333', '#CC3300', '#663300', '#663333', '#663366', '#663399', '#6633CC', '#6633FF', '#0033FF', '#0033CC', '#003399', '#003366', '#003333', '#003300'],
			['#000000', '#FFFFFF', '#000000', '#CC00FF', '#CC00CC', '#CC0099', '#CC0066', '#CC0033', '#CC0000', '#660000', '#660033', '#660066', '#660099', '#6600CC', '#6600FF', '#0000FF', '#0000CC', '#000099', '#000066', '#000033', '#000000'],
			['#000000', '#FF0000', '#000000', '#FF00FF', '#FF00CC', '#FF0099', '#FF0066', '#FF0033', '#FF0000', '#990000', '#990033', '#990066', '#990099', '#9900CC', '#9900FF', '#3300FF', '#3300CC', '#330099', '#330066', '#330033', '#330000'],
			['#000000', '#00FF00', '#000000', '#FF33FF', '#FF33CC', '#FF3399', '#FF3366', '#FF3333', '#FF3300', '#993300', '#993333', '#993366', '#993399', '#9933CC', '#9933FF', '#3333FF', '#3333CC', '#333399', '#333366', '#333333', '#333300'],
			['#000000', '#0000FF', '#000000', '#FF66FF', '#FF66CC', '#FF6699', '#FF6666', '#FF6633', '#FF6600', '#996600', '#996633', '#996666', '#996699', '#9966CC', '#9966FF', '#3366FF', '#3366CC', '#336699', '#336666', '#336633', '#336600'],
			['#000000', '#FFFF00', '#000000', '#FF99FF', '#FF99CC', '#FF9999', '#FF9966', '#FF9933', '#FF9900', '#999900', '#999933', '#999966', '#999999', '#9999CC', '#9999FF', '#3399FF', '#3399CC', '#339999', '#339966', '#339933', '#339900'],
			['#000000', '#00FFFF', '#000000', '#FFCCFF', '#FFCCCC', '#FFCC99', '#FFCC66', '#FFCC33', '#FFCC00', '#99CC00', '#99CC33', '#99CC66', '#99CC99', '#99CCCC', '#99CCFF', '#33CCFF', '#33CCCC', '#33CC99', '#33CC66', '#33CC33', '#33CC00'],
			['#000000', '#FF00FF', '#000000', '#FFFFFF', '#FFFFCC', '#FFFF99', '#FFFF66', '#FFFF33', '#FFFF00', '#99FF00', '#99FF33', '#99FF66', '#99FF99', '#99FFCC', '#99FFFF', '#33FFFF', '#33FFCC', '#33FF99', '#33FF66', '#33FF33', '#33FF00']
		);
	 },
	 
	 drawColorPallet: function(title){
		/**
		 *	Creates each color pallet
		 *	Reads throught all the defined color pallets and draws them out
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	title	string optional	Value to initial set as the title of the color pallet
		 *	@return	var	desc
		 *	@date	2006/09/15
		 */
		 if(!title)
		 	title = "jsColorPallet";
		 
		 for(i=0;i < this.colorPallets.length;i++){
			 //Draw the begining of the Color Pallet
			if(this.colorPallets.length == (i+1)){
				var next = 0;
			}
			else{
				var next = i+1;
			}
			document.write('<span id="colorPallet'+ i +'" style="display:none; position:absolute;"><table cellpadding="0" cellspacing="0" class="colorPalletTable"><tr><td style="background-color:#000000; color:#fff; text-align:left;" colspan="19"><span id="colorPalletTitle'+ i +'">' + title +'</td><td style="background-color:#000000; color:#fff;"><a href="javascript:colorPallet.returnColor(\'\');" title="Close" class="colorPallet" style="color:#FFF;">X</a></td><td style="background-color:#000000; color:#fff;"><a href="javascript:colorPallet.toggleColorPallet(\''+ (next) +'\');" title="Change Color Pallet" class="colorPallet" style="color:#fff;">&raquo;</a></td></tr>');				
			
			//Draw each row of the Color Pallet
			for(h = 0; h < this.colorPallets[i].length; h++){
					document.write('<tr>');
					for(c = 0; c < this.colorPallets[i][h].length; c++){
						document.write('<td style="background-color:' + this.colorPallets[i][h][c] + '"><a href="javascript:colorPallet.returnColor(\'' + this.colorPallets[i][h][c] + '\');" title="' + this.colorPallets[i][h][c] + '" class="colorPallet">&nbsp;</a></td>');
					}
					document.write('</tr>');
			}
			//Draw the end of the Color Pallet
			document.write('</table></span>');
		}
	},
	 
	 changeTitle: function(title){
		/**
		 *	Changes the title of all the color pallets
		 *	Goes throught and changes all the titles on all the color pallets
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	title	string	title you would like to set the color pallet to
		 *	@date	2006/09/15
		 */
		 for(i=0;i < this.colorPallets.length; i++){
		 	$('colorPalletTitle'+ i).innerHTML = title;
		 }
	 },
	 
	 bindForm: function(formBind){
		/**
		 *	Binds the color pallet to each form element
		 *	Goes throught the complete form and binds the color pallet to each form element
		 *
		 *	@author	NerdyNick	nick@skeletaldesign.com
		 *	@param	formBind	string	form id of the form to bind the color pallet to
		 *	@date	2006/09/15
		 */
		 form = $(formBind);
		 for(i=0;i<form.elements.length;i++){
			id = form.elements[i].id;
			form.elements[i].onfocus = function(){colorPallet.displayPallet(this);}; 
		}
	}
}

