var WOOKIEE = {
	
	fieldTypes: {
		'PRIMARY_KEY': 1,
		'TEXT': 2,
		'NUMBER': 3,
		'PARAGRAPH': 4,
		'PARENT': 5,
		'DATETIME': 6,
		'BOOLEAN': 7,
		'USER_ID': 8,
		'PASSWORD': 9,
		'DROPDOWN' : 10,
		'RELATION' : 11,
		'FILE' : 12,
		'HTML' : 13
	},
	
	userTypes: {
		'USER': 1,
		'ADMIN': 2,
		'CLIENT': 3
	},
	
	pageElementTypes: {
		'HTML': 1,
		'IMAGE': 2,
		'NEWS': 3,
		'BLOG': 4,
		'PRODUCT': 5,
		'TITLE': 6
	}
	
};

/*
** This file contains misc utilities
*/

//FDD namespace
WOOKIEE.UTIL = {
	
	//this is a helper that converts a mangled extjs form into a proper hash
	convertExtJSStringToHash: function (str){
		//extjs will return an object for the .getValues() method of the form with "+" for spaces, and "+" for pluses...
		// the string return is right, but is encoded to pass directly to a GET
		var elements = str.split('&');
		var result = {};
		for(k=0;k<elements.length;k++){
			var tmp = elements[k].split('=');
			result[tmp[0]] = tmp[1];
		}
		
		return result;
	},

	//returns true if it found the parent in the list
	findParent: function (element,parentName){
	},

	//returns a form object as a simplified hash
	getHashFromForm: function(formObj){
		var hash = { };

		for(var i = 0; i < formObj.elements.length; i++)
		{
			switch(formObj.elements[i].type)
			{
				case "text" :
				case "password" :
				case "hidden" :
				case "button" :
					hash[formObj.elements[i].name] = formObj.elements[i].value;
					break;
				case "checkbox" :
					if (formObj.elements[i].checked)
					{
						hash[formObj.elements[i].name] = "on";
					} else {
						hash[formObj.elements[i].name] = "off";
					}
					break;
				case "select-one" :
					hash[formObj.elements[i].name] = formObj.elements[i].options[formObj.elements[i].selectedIndex].value;
					break;
				default :
					//alert ("Unknown form element type in ajax.js:postForm() : " + formObj.elements[i].type + " (element name=" + formObj.elements[i].name + ") (" + i + ")");
					break;
			}
		}

		return hash;
	},

	//returns a string form object as post variables
	getPostString: function(formObj){
		var postString = "";
		for(var i = 0; i < formObj.elements.length; i++)
		{
			switch(formObj.elements[i].type)
			{
				case "text" :
				case "password" :
				case "hidden" :
					postString = postString + formObj.elements[i].name + "=" + escape(formObj.elements[i].value) + "&";
					break;
				case "checkbox" :
					if (formObj.elements[i].checked)
					{
						postString = postString + formObj.elements[i].name + "=on&";
					}
					break;
				case "select-one" :
					postString = postString + formObj.elements[i].name + "=" + escape(formObj.elements[i].options[formObj.elements[i].selectedIndex].value) + "&";
					break;
				case "button" :
					break;
				default :
					//alert ("Unknown form element type in ajax.js:postForm() : " + formObj.elements[i].type + " (element name=" + formObj.elements[i].name + ") (" + i + ")");
					break;
			}
		}
		//alert(postString);
		return postString;
	},

	//returns dimensions about the screen
	getDimensions: function(){
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			screenX = window.innerWidth;
			screenY = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			screenX = document.documentElement.clientWidth;
			screenY = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			screenX = document.body.clientWidth;
			screenY = document.body.clientHeight;
		}

		return {screenX: screenX, screenY: screenY};
	},

	getPosition: function(e){
		var left = 0;
		var top  = 0;
		var oWidth = 0;
		var oTop = 0;

		while (e.offsetParent){
			left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)):0);
			top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)):0);
			e     = e.offsetParent;
		}

		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)):0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)):0);

		return {x:left, y:top};
	},

	//from mootools, I liked the simple regex they used
	/*rgbToHex: function(array){
		var rgb = this.match(/\d{1,3}/g);
		return (rgb) ? rgb.rgbToHex(array) : false;
	},

	hexToRgb: function(array){
		var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
		return (hex) ? hex.slice(1).hexToRgb(array) : false;
	},*/

	hexToRgb: function(str){
		var hex = str.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
		if(hex){
			if(hex[1] != ''){
				hex[1] = parseInt(hex[1],16);
				hex[2] = parseInt(hex[2],16);
				hex[3] = parseInt(hex[3],16);
			}
		}

		return (hex) ? hex.slice(1) : false;
	},

	rgbToHex: function(rgb){
		//return '#'+rgb[0].toString(16)+rgb[1].toString(16)+rgb[2].toString(16);
		if (rgb.length < 3) return false;
		if (rgb[3] && (rgb[3] == 0) && !rgb) return 'transparent';
		var hex = [];
		for (var i = 0; i < 3; i++){
			var bit = (rgb[i]-0).toString(16);
			hex.push((bit.length == 1) ? '0'+bit : bit);
		}
		//return rgb ? hex : '#'+hex.join('');
		return '#'+hex.join('');
	},

	realrgbToHex: function(array){
		var rgb = array.match(/\d{1,3}/g);
		return (rgb) ? rgb : false;
	},

	//returns prefix,postfix,value eg: #ff0000 {prefix: #, value: ff0000}, abc123efg {prefix: abc, value: 123, postfix: efg}
	splitValue: function(str){
		result = {
			prefix: '',
			postfix: '',
			value: ''
		}
		if(str != ''){
			var res = str.match(/([^0-9]*)([0-9]+)([^0-9]*)/);
			//try{
			result = {
				prefix: res[1],
				postfix: res[3],
				value: res[2]
			};
			//} catch(err) {
				//console.trace();
			//}
		}
		return result;

	},

	addSelectOption: function(selectKey, option, value){
		selectObj = parent.document.getElementById(selectKey);
		var optionObj = document.createElement('option');
		optionObj.text = option;
		optionObj.value = value;
		try{
			selectObj.add(optionObj,null); //should work in most browsers that support good dom
		} catch(ex) {
			selectObj.add(optionObj); //should work in ie, if above fails
		}

	},

	purgeSelectOptions: function(selectKey){
		selectObj = parent.document.getElementById(selectKey);
		var len = selectObj.length;

		for(var i=0;i<len;i++){
			selectObj.remove(i);
		}
		selectObj.length = 0;
	},

	callFunctionWithArray: function(funcName,array){
		var str = '';

		$(array).each(function(key,element){
			switch((typeof(element)).toLowerCase()){
				case 'string':
					str = str + "'"+element+"',";
					break;
				default:
					str = str + element + ",";
					break;
			}
		});

		if(str.length > 0){
			//chop the last ',' off
			str = str.substr(0,(str.length - 1));
		}

		return eval(funcName+'('+str+');');
	}
}

/* This file defines the Pop-Up library
**
** The goal of this library is ease of use and speed
*/


//POP namespace
WOOKIEE.POP = {

	//show the popup
	show: function (divId,anchorId,offsetX,offsetY,orientation,relative,insideRelativeObject){
		// insideRelativeObject should be set to true if your popup div is located inside an element marked as "position: relative;", false otherwise
		//orientation default = TOPLEFT
		//relative default = TOPLEFT

		//this will prevent windows popping past the right/bottom of the screen
		allowPopupPastScreen = false;

		if(arguments.length > 6){
			//they overrode it
			allowPopupPastScreen = arguments[6];
		}

		//div.cloneNode(true)
		if(typeof(divId) == 'string'){
			div = document.getElementById(divId);
		} else {
			div = $(divId).get(0);
		}
		if(typeof(anchorId) == 'string'){
			anchor = document.getElementById(anchorId);
		} else {
			anchor = $(anchorId).get(0);
		}

		//if the element or anchor is bad, return false
		if(!div || !anchor)
			return false;

		//popup the div attached to the anchor via the orientation and offset

		//if we need to figure out the offsets of the element, then we need to first display it... sucks but oh well
		// this will cause the element to "flash" at it's innitial position
		// to prevent this flashing let's set the element behind our regular display
		var oldZIndex = div.style.zIndex;
		div.style.display  = 'block';
		div.style.zIndex = '-20'; //hide the div behind our main display
		WOOKIEE.POP.anchor(div,anchor,offsetX,offsetY,orientation,relative,allowPopupPastScreen,insideRelativeObject);
		div.style.display  = 'block';
		div.style.zIndex = oldZIndex; //set whatever the zindex was back to it
	},

	//hide the popup
	hide: function (divId){
		//div = document.getElementById(divId);
		//div.style.display  = 'none';
		if(typeof(divId) == 'string'){
			div = document.getElementById(divId);
		} else {
			div = $(divId);
		}
		$(div).hide();
	},

	//moves a div to the anchor'd div
	anchor: function (div,anchor,offsetX,offsetY,orientation,relative,allowPopupPastScreen,insideRelativeObject){
		if(insideRelativeObject){
			var linkPos = WOOKIEE.POP.findRelativePos(anchor);
			var divPos = WOOKIEE.POP.findRelativePos(div);
		} else {
			var linkPos = WOOKIEE.POP.findAbsolutePos(anchor);
			var divPos = WOOKIEE.POP.findAbsolutePos(div);
		}
		
		//we need to make the position absoute in order for it to move to the coord
		div.style.position = 'absolute';

		var addX = 0;
		var addY = 0;
		
		switch(orientation){
			case "TOPRIGHT":
				addX = linkPos.width;
				break;
			case "BOTTOMLEFT":
				addY = linkPos.height;
				break;
			case "BOTTOMRIGHT":
				addX = linkPos.width;
				addY = linkPos.height;
				break;
			case "LEFT":
				addY = (linkPos.height / 2);
				break;
			case "RIGHT":
				addX = linkPos.width;
				addY = (linkPos.height / 2);
				break;
			case "BOTTOM":
				addX = (linkPos.width / 2);
				addY = linkPos.height;
				break;
			case "TOP":
				addX = (linkPos.width / 2);
				break;
			default: //TOPLEFT
				break;
		}

		/*
		** TODO look at the TOP,LEFT,BOTTOM,RIGHT and compare the orientation with the relative
		**  determine if an adjustment should be made
		*/
		switch(relative){
			case "BOTTOMLEFT":
				//move the element UP as tall as the element is
				addY -= divPos.height;
				break;
			case "BOTTOMRIGHT":
				addY -= divPos.height;
				addX -= divPos.width;
				break;
			case "TOPRIGHT":
				addX -= divPos.width;
				break;
			case "LEFT":
				addY += (divPos.height / 2);
				break;
			case "RIGHT":
				addY += (divPos.height / 2);
				addX -= divPos.width;
				break;
			case "TOP":
				addX -= (divPos.width / 2);
				break;
			case "BOTTOM":
				addX -= (divPos.width / 2);
				addY -= divPos.height;
				break;
			default: //TOPLEFT
				break;
		}

		//now that we have all the offsets, move the element
		if(allowPopupPastScreen){
			div.style.top = linkPos.y + offsetY + addY + "px";
			div.style.left = linkPos.x + offsetX + addX + "px";
		} else {
			var dim = WOOKIEE.UTIL.getDimensions();


			//see if the computed value is "off the screen"
			var computedX = linkPos.x + offsetX + addX;
			var computedY = linkPos.y + offsetY + addY;

			//alert(dim.screenX + " : " + computedX + " : " + div.offsetWidth + " : " + (computedX + div.offsetWidth) );

			if((computedX + div.offsetWidth) > dim.screenX){
				//it's overlapping, so nudge it back to the left + 20 pixels (for padding)
				computedX = computedX - (((computedX + div.offsetWidth) - dim.screenX) + 20);
			}
			
			div.style.top = computedY + "px";
			div.style.left = computedX + "px";
		}
	},

	//returns the absolute coords based on where it is on the page (works w/ scrolled content)
	findAbsolutePos: function(el){
		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 };
		r.width = el.offsetWidth;
		r.height = el.offsetHeight;

		if (el.offsetParent) {
			var tmp = this.findAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	},

	//this is just like the findAbsolutePos, but stops if a parent's css is "position:relative;"
	findRelativePos: function(el){
		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 };
		r.width = el.offsetWidth;
		r.height = el.offsetHeight;

		if (el.offsetParent) {
			if($(el.offsetParent).css('position') != 'relative'){
				//stop if we have a relative parent
				var tmp = this.findRelativePos(el.offsetParent);
				r.x += tmp.x;
				r.y += tmp.y;
			} else {
				r.x += 0;
				r.y += 0;
			}
		}
		return r;
	}

}
