/**
 * @name NiceJForms
 * @description This a $ equivalent for Niceforms ( http://badboy.ro/articles/2007-01-30/niceforms/ ).  All the forms are styled with beautiful images as backgrounds and stuff. Enjoy them!
 * @param Hash hash A hash of parameters
 * @option integer selectRightSideWidth width of right side of the select
 * @option integer selectLeftSideWidth width of left side of the select 
 * @option integer selectAreaHeight
 * @option integer selectAreaOPtionsOverlap
 * @option imagesPath folder where custom form images are stored
 * @type $
 * @cat Plugins/Interface/Forms
 * @author Lucian Lature ( lucian.lature@gmail.com )
 * @credits goes to Lucian Slatineanu ( http://www.badboy.ro )
 * @version 0.1
 */

$.NiceJForms = {
	options : {
		selectRightSideWidth     : 21,
		selectLeftSideWidth      : 8,
		selectAreaHeight 	     : 21,
		selectAreaOptionsOverlap : 2,
		imagesPath               : "css/images/default/"
		// other options here
	},
	
	selectText     : 'please select',
	forms          : new Array(),
	
	//folgende Arrays wurden fuer jedes Form geschachtelt im forms abgelegt.
	preloads       : new Array(),
	inputs         : new Array(),
	labels         : new Array(),
	textareas      : new Array(),
	selects        : new Array(),
	radios         : new Array(),
	checkboxes     : new Array(),
	texts          : new Array(),
	buttons        : new Array(),
	radioLabels    : new Array(),
	checkboxLabels : new Array(),
	hasImages      : true,
	
	keyPressed : function(event)
	{
		var pressedKey = event.charCode || event.keyCode || -1;
		
		switch (pressedKey)
		{
			case 40: //down
			var fieldId = this.parentNode.parentNode.id.replace(/sarea/g, "");
			var linkNo = 0;
			for(var q = 0; q < selects[fieldId].options.length; q++) {if(selects[fieldId].options[q].selected) {linkNo = q;}}
			++linkNo;
			if(linkNo >= selects[fieldId].options.length) {linkNo = 0;}
			selectMe(selects[fieldId].id, linkNo, fieldId);
			break;
		
		case 38: //up
			var fieldId = this.parentNode.parentNode.id.replace(/sarea/g, "");
			var linkNo = 0;
			for(var q = 0; q < selects[fieldId].options.length; q++) {if(selects[fieldId].options[q].selected) {linkNo = q;}}
			--linkNo;
			if(linkNo < 0) {linkNo = selects[fieldId].options.length - 1;}
			selectMe(selects[fieldId].id, linkNo, fieldId);
			break;
		default:
			break;
		}
	},
	
	build : function(options)
	{
		if (options)
			$.extend($.NiceJForms.options, options);	
			
		/*if (window.event) {
			$('body',document).bind('keyup', $.NiceJForms.keyPressed);
		} else {
			$(document).bind('keyup', $.NiceJForms.keyPressed);
		}*/
		
		// test if images are disabled or not
		/*var testImg = document.createElement('img');
		$(testImg).attr("src", $.NiceJForms.options.imagesPath + "blank.gif").attr("id", "imagineTest");
		$('body').append(testImg);
		
		if(testImg.complete)
		{
			if(testImg.offsetWidth == '1') {$.NiceJForms.hasImages = true;}
			else {$.NiceJForms.hasImages = false;}
		}

		$(testImg).remove();
			
		if($.NiceJForms.hasImages)
		{*/
			$('form.niceform').each( function()
				{
					el = $(this);
					$.NiceJForms.forms.push(el);
					var formindex = $.NiceJForms.forms.length-1;
					$.NiceJForms.preloadImages();
					$.NiceJForms.getElements(formindex);
					$.NiceJForms.replaceRadios(formindex);
					$.NiceJForms.replaceCheckboxes(formindex);
					$.NiceJForms.replaceSelects(formindex);
					
					/*if (!$.browser.safari) {
						$.NiceJForms.replaceTexts();
						$.NiceJForms.replaceTextareas();
						$.NiceJForms.buttonHovers();
					}*/
				}
			);
		//}	
	},
	
	preloadImages: function()
	{
		$.NiceJForms.preloads = $.preloadImages($.NiceJForms.options.imagesPath + "button_left_xon.gif", $.NiceJForms.options.imagesPath + "button_right_xon.gif", 
		$.NiceJForms.options.imagesPath + "input_left_xon.gif", $.NiceJForms.options.imagesPath + "input_right_xon.gif",
		$.NiceJForms.options.imagesPath + "txtarea_bl_xon.gif", $.NiceJForms.options.imagesPath + "txtarea_br_xon.gif", 
		$.NiceJForms.options.imagesPath + "txtarea_cntr_xon.gif", $.NiceJForms.options.imagesPath + "txtarea_l_xon.gif", $.NiceJForms.options.imagesPath + "txtarea_tl_xon.gif", $.NiceJForms.options.imagesPath + "txtarea_tr_xon.gif");
	},
	
	getElements: function(formindex)
	{
		//el = elm ? $(elm) : $(this);
		
		var r = 0; var c = 0; var t = 0; var rl = 0; var cl = 0; var tl = 0; var b = 0;
		
		$.NiceJForms.forms[formindex].inputs = $('input', el);
		$.NiceJForms.forms[formindex].labels = $('label', el);
		$.NiceJForms.forms[formindex].textareas = $('textarea', el);
		$.NiceJForms.forms[formindex].selects = $('select', el);
		$.NiceJForms.forms[formindex].radios = $('input[@type=radio]', el);
		$.NiceJForms.forms[formindex].checkboxes = $('input[@type=checkbox]', el);
		$.NiceJForms.forms[formindex].texts = $('input[@type=text]', el).add($('input[@type=password]', el));		
		$.NiceJForms.forms[formindex].buttons = $('input[@type=submit]', el).add($('input[@type=button]', el));
		
		$.NiceJForms.forms[formindex].radioLabels = new Array();
		$.NiceJForms.forms[formindex].checkboxLabels = new Array();
		
		$.NiceJForms.forms[formindex].labels.each(function(i){
			labelFor = $($.NiceJForms.forms[formindex].labels[i]).attr("for");
			$.NiceJForms.forms[formindex].radios.each(function(q){
				if(labelFor == $($.NiceJForms.forms[formindex].radios[q]).attr("id"))
				{
					if($.NiceJForms.forms[formindex].radios[q].checked)
					{
						$($.NiceJForms.forms[formindex].labels[i]).removeClass().addClass("chosen");	
					}
					
					$.NiceJForms.forms[formindex].radioLabels[rl] = $.NiceJForms.forms[formindex].labels[i];
					++rl;
				}
			})
			
			$.NiceJForms.forms[formindex].checkboxes.each(function(x){
				
				if(labelFor == $(this).attr("id"))
				{
					if(this.checked)
					{
						$($.NiceJForms.forms[formindex].labels[i]).removeClass().addClass("chosen");	
					}
					$.NiceJForms.forms[formindex].checkboxLabels[cl] = $.NiceJForms.forms[formindex].labels[i];
					++cl;
				}
			})
		});
	},
	
	replaceRadios: function(formindex)
	{
		var self = this;
		
		$.NiceJForms.forms[formindex].radios.each(function(q){
		
			//alert(q);
			$(this).removeClass().addClass('outtaHere'); //.hide(); //.className = "outtaHere";
			
			
			
			var radioArea = document.createElement('div');
			//console.info($(radioArea));
			if(this.checked) {$(radioArea).removeClass().addClass("radioAreaChecked");} else {$(radioArea).removeClass().addClass("radioArea");};
			
			radioPos = $.iUtil.getPosition(this);
			
			$(radioArea)
				.attr({id: 'myRadio'+formindex+'_'+q})
				.css({left: radioPos.x + 'px', top: radioPos.y + 'px', margin : '1px'})
				.bind('click', {formindex: formindex, who: q}, function(e){self.rechangeRadios(e, formindex)})
				.insertBefore($(this));
			
			$($.NiceJForms.forms[formindex].radioLabels[q]).bind('click', {who: q}, function(e){self.rechangeRadios(e, formindex)});
			
			if (!$.browser.msie) {
				$(this).bind('focus', function(){self.focusRadios(q, formindex)}).bind('blur', function() {self.blurRadios(q, formindex)});
			}
			
			$(this).bind('click', function(e){self.radioEvent(e, formindex)});
		});
		
		return true;
	},
	
	changeRadios: function(who, formindex) {
		
		var self = this;
		
		if($.NiceJForms.forms[formindex].radios[who].checked) {
		
			$.NiceJForms.forms[formindex].radios.each(function(q){
				if($(this).attr("name") == $($.NiceJForms.forms[formindex].radios[who]).attr("name"))
				{
					this.checked = false;
					$($.NiceJForms.forms[formindex].radioLabels[q]).removeClass();	
				}
			});
			$.NiceJForms.forms[formindex].radios[who].checked = true;
			$($.NiceJForms.forms[formindex].radioLabels[who]).addClass("chosen");
			
			self.checkRadios(who, formindex);
		}
	},
	
	rechangeRadios:function(e, formindex) 
	{
		who = e.data.who;
		
		if(!$.NiceJForms.forms[formindex].radios[who].checked) {
			for(var q = 0; q < $.NiceJForms.forms[formindex].radios.length; q++) 
			{
				if($.NiceJForms.forms[formindex].radios[q].name == $.NiceJForms.forms[formindex].radios[who].name) 
				{
					$.NiceJForms.forms[formindex].radios[q].checked = false; 
					//console.info(q);
					$.NiceJForms.forms[formindex].radioLabels[q].className = "";
				}
			}
			$($.NiceJForms.forms[formindex].radios[who]).attr('checked', true); 
			$.NiceJForms.forms[formindex].radioLabels[who].className = "chosen";
			$.NiceJForms.checkRadios(who, formindex);
		}
	},
	
	checkRadios: function(who, formindex) {
		$('div').each(function(q){
			if($(this).is(".radioAreaChecked") && $(this).next().attr("name") == $($.NiceJForms.forms[formindex].radios[who]).attr("name")) {$(this).removeClass().addClass("radioArea");}
		});
		$('#myRadio' + formindex + '_' + who).toggleClass("radioAreaChecked");
	},
	
	focusRadios: function(who, formindex) {
		$('#myRadio' + formindex + '_' + who).css({border: '1px dotted #333', margin: '0'}); return false;
	},
	
	blurRadios:function(who, formindex) {
		$('#myRadio' + formindex + '_' + who).css({border: 'none', margin: '1px'}); return false;
	},
	
	radioEvent: function(e, formindex) {
		var self = this;
		if (!e) var e = window.event;
		if(e.type == "click") {for (var q = 0; q < $.NiceJForms.forms[formindex].radios.length; q++) {if(this == $.NiceJForms.forms[formindex].radios[q]) {self.changeRadios(q, formindex); break;}}}
	},
	
	replaceCheckboxes: function (formindex) 
	{
		var self = this;
		
		$.NiceJForms.forms[formindex].checkboxes.each(function(q){
			//move the checkboxes out of the way
			$($.NiceJForms.forms[formindex].checkboxes[q]).removeClass().addClass('outtaHere');
			//create div
			var checkboxArea = document.createElement('div');
			
			//console.info($(radioArea));
			if($.NiceJForms.forms[formindex].checkboxes[q].checked) {$(checkboxArea).removeClass().addClass("checkboxAreaChecked");} else {$(checkboxArea).removeClass().addClass("checkboxArea");};
			
			checkboxPos = $.iUtil.getPosition($.NiceJForms.forms[formindex].checkboxes[q]);
			
			$(checkboxArea)
				.attr({id: 'myCheckbox' + formindex + '_' + q})
				.css({
				left: checkboxPos.x + 'px', 
				top: checkboxPos.y + 'px',
				margin : '1px'
			})
			.bind('click', {who: q}, function(e){self.rechangeCheckboxes(e, formindex)})
			.insertBefore($($.NiceJForms.forms[formindex].checkboxes[q]));
			
			if(!$.browser.safari)
			{
				$($.NiceJForms.forms[formindex].checkboxLabels[q]).bind('click', {who:q}, function(e){self.changeCheckboxes(e, formindex)})
			}
			else {
				$($.NiceJForms.forms[formindex].checkboxLabels[q]).bind('click', {who:q}, function(e){self.rechangeCheckboxes(e, formindex)})
			}
			
			if(!$.browser.msie)
			{
				$($.NiceJForms.forms[formindex].checkboxes[q]).bind('focus', {who:q}, function(e){self.focusCheckboxes(e, formindex)});
				$($.NiceJForms.forms[formindex].checkboxes[q]).bind('blur', {who:q}, function(e){self.blurCheckboxes(e, formindex)});
			}	
			
			//$($.NiceJForms.forms[formindex].checkboxes[q]).keydown(checkEvent);
		});
		return true;
	},

	rechangeCheckboxes: function(e, formindex)
	{
		who = e.data.who;
		var tester = false;
		
		if($($.NiceJForms.forms[formindex].checkboxLabels[who]).is(".chosen")) {
			tester = false;
			$($.NiceJForms.forms[formindex].checkboxLabels[who]).removeClass();
		}
		else if($.NiceJForms.forms[formindex].checkboxLabels[who].className == "") {
			tester = true;
			$($.NiceJForms.forms[formindex].checkboxLabels[who]).addClass("chosen");
		}
		$.NiceJForms.forms[formindex].checkboxes[who].checked = tester;
		$.NiceJForms.checkCheckboxes(who, tester, formindex);
	},

	checkCheckboxes: function(who, action, formindex)
	{
		var what = $('#myCheckbox' + formindex + '_' + who);
		if(action == true) {$(what).removeClass().addClass("checkboxAreaChecked");}
		if(action == false) {$(what).removeClass().addClass("checkboxArea");}
	},

	focusCheckboxes: function(who, formindex) 
	{
		var what = $('#myCheckbox' + formindex + '_' + who);
		$(what).css(
					{
						border : "1px dotted #333", 
						margin : "0"
					});	
		return false;
	},

	changeCheckboxes: function(e, formindex) {
		who = e.data.who;
		
		//console.log('changeCheckboxes who is ' + who);
		if($($.NiceJForms.forms[formindex].checkboxLabels[who]).is(".chosen")) {
			$.NiceJForms.forms[formindex].checkboxes[who].checked = true;
			$($.NiceJForms.forms[formindex].checkboxLabels[who]).removeClass();
			$.NiceJForms.checkCheckboxes(who, false, formindex);
		}
		else if($.NiceJForms.forms[formindex].checkboxLabels[who].className == "") {
			$.NiceJForms.forms[formindex].checkboxes[who].checked = false;
			$($.NiceJForms.forms[formindex].checkboxLabels[who]).toggleClass("chosen");
			$.NiceJForms.checkCheckboxes(who, true, formindex);
		}
	},

	blurCheckboxes: function(who, formindex) 
	{
		var what = $('#myCheckbox' + formindex + '_' + who);
		$(what).css(
					{
						border : 'none', 
						margin : '1px'
					});	
		return false;
	},
	
	replaceSelects: function(formindex)
	{
		var self = this;
		
		$.NiceJForms.forms[formindex].selects.each(function(q){
			//create and build div structure
			var selectArea = document.createElement('div');
			var left = document.createElement('div');
			var right = document.createElement('div');
			var center = document.createElement('div');
			var button = document.createElement('a');
			var text = document.createTextNode($.NiceJForms.selectText);
			var selectWidth = parseInt(this.className.replace(/width_/g, ""));
			
			$(center)
				.attr({id:'mySelectText' + formindex + '_' + q})
				.css({width: selectWidth - 10 + 'px'});
			$(selectArea)
				.attr({id:'sarea' + formindex + '_' + q})
				.css({
					width: selectWidth + $.NiceJForms.options.selectRightSideWidth + $.NiceJForms.options.selectLeftSideWidth + 'px'
				})
				.addClass("selectArea");
			
			$(button)
				.css({
				width      : selectWidth + $.NiceJForms.options.selectRightSideWidth + $.NiceJForms.options.selectLeftSideWidth + 'px',
				marginLeft : - selectWidth - $.NiceJForms.options.selectLeftSideWidth + 'px',
				cursor: 'pointer'
				})
				.addClass("selectButton")
				.bind('click', {who:q, formindex:formindex}, function(e){self.showOptions(e)})
				.keydown($.NiceJForms.keyPressed);
			
			$(left).addClass("left");	
			$(right).addClass("right").append(button);	
			$(center).addClass("center").append(text);	
			
			$(selectArea).append(left).append(right).append(center).insertBefore(this);
			//hide the select field
			$(this).hide();
			//insert select div
			//build & place options div
			var optionsDiv = document.createElement('div');
			selectAreaPos = $.iUtil.getPosition(selectArea);
			
			var optionsDivScroll = document.createElement('div');
			$(optionsDivScroll).addClass("scroll");
			$(optionsDivScroll).css({
				width : selectWidth +1 + 'px'
			});
			$(optionsDiv).append(optionsDivScroll);
			
			$(optionsDiv)
				.attr({id:"optionsDiv" + formindex + '_' + q})
				.css({
					width : selectWidth + 18 + 'px', 
					left  : selectAreaPos.x + 'px', 
					top   : selectAreaPos.y + $.NiceJForms.options.selectAreaHeight - $.NiceJForms.options.selectAreaOptionsOverlap + 'px'
				})
				.addClass("optionsDivInvisible")
				.addClass("optionDiv_"+$.NiceJForms.forms[formindex].attr('class').split(' ').slice(1));
			
			//get select's options and add to options div
			$($.NiceJForms.forms[formindex].selects[q]).children().each(function(w){
				var optionHolder = document.createElement('p');
				var optionLink = document.createElement('a');
				var optionTxt = document.createTextNode($.NiceJForms.forms[formindex].selects[q].options[w].text);
				
				$(optionLink)
					.attr({href:'#'})
					.css({cursor:'pointer'})
					.append(optionTxt)
					.bind('click', {who: q, id:$.NiceJForms.forms[formindex].selects[q].id, option:w, select:q, formindex:formindex}, function(e){self.showOptions(e, formindex);self.selectMe($.NiceJForms.forms[formindex].selects[q].id, w, q, formindex); return false;});
				
				$(optionHolder).append(optionLink);
				$(optionsDivScroll).append(optionHolder);
				
				//check for pre-selected items
				if($.NiceJForms.forms[formindex].selects[q].options[w].selected) {self.selectMe($($.NiceJForms.forms[formindex].selects[q]).attr("id"), w, q, formindex);}
			});
			
			$('body').append(optionsDiv);
			//Erst mal eine Höhe festlegen. Diese wird in showOptions() entgültig festgelegt
			$(optionsDiv).css({
				height : 200
			});
			
			//Funktion zum Scrollen des Divs
			var scrolling =  function(evt) {
				var height = $("#optionsDiv" + evt.formindex + '_' + evt.q+" div.scroll").height();
				var top = $("#optionsDiv" + evt.formindex + '_' + evt.q+" div.scroll").css('top');
				top = parseInt(top);
				//Bei down: Nicht tiefer als die Höhe des Divs
				//Bei up: Nicht höher als 0
				if(((top*-1)< (height-203) && evt.speed<0) || ((top*-1)> 0 && evt.speed>0)) {
					$("#optionsDiv" + evt.formindex + '_' + evt.q+" div.scroll").css({
						top : top+evt.speed+'px'
					});
				}
			};
			
			var optionsDivScrollUp = document.createElement('div');
			$(optionsDivScrollUp).addClass("scroll_up");
			$(optionsDiv).append(optionsDivScrollUp);
			var optionsAScrollUp = document.createElement('a');
			$(optionsAScrollUp).text('up');
			$(optionsAScrollUp).mousehold(10, function(i) { scrolling({speed:5, formindex:formindex, q:q}); });
			$(optionsAScrollUp).bind('mouseover', function() { $(this).addClass('hover'); });
			$(optionsAScrollUp).bind('mouseout', function() { $(this).removeClass('hover'); });
			$(optionsDivScrollUp).append(optionsAScrollUp);
			
			var optionsDivScrollDown = document.createElement('div');
			$(optionsDivScrollDown).addClass("scroll_down");
			$(optionsDiv).append(optionsDivScrollDown);
			var optionsAScrollDown = document.createElement('a');
			$(optionsAScrollDown).text('down');
			$(optionsAScrollDown).mousehold(10, function(i) { scrolling({speed:-5, formindex:formindex, q:q}); });
			$(optionsAScrollDown).bind('mouseover', function() { $(this).addClass('hover'); });
			$(optionsAScrollDown).bind('mouseout', function() { $(this).removeClass('hover'); });
			$(optionsDivScrollDown).append(optionsAScrollDown);
		});
	},
	
	selectMe: function(selectFieldId, linkNo, selectNo, formindex) {
		selectField = $('#' + selectFieldId);
		sFoptions = selectField.children();
		
		selectField.children().each(function(k){
			if(k == linkNo) {sFoptions[k].selected="selected";}
			else {sFoptions[k].selected = "";}
		});
		
		textVar = $("#mySelectText" + formindex + '_' + selectNo);
		var newText = document.createTextNode($(sFoptions[linkNo]).text());
		textVar.empty().append(newText);
		
		$(document).unbind('mousedown', self._checkMouse);
	}, 

	showOptions: function(e) {
		var self = this;
		$(document).bind('mousedown', {who: e.data.who, formindex:e.data.formindex}, self._checkMouse);
		$("#optionsDiv" + e.data.formindex + '_' + e.data.who).toggleClass("optionsDivVisible").toggleClass("optionsDivInvisible").mouseout(function(e){self.hideOptions(e)});
		
		//Höhe des Divs anhand des Inhalts festlegen
		var height = $("#optionsDiv" + e.data.formindex + '_' + e.data.who+" div.scroll").height();
		if(height > 200) {
			$("#optionsDiv" + e.data.formindex + '_' + e.data.who).css({
					height : 200
			});
		} else {
			$("#optionsDiv" + e.data.formindex + '_' + e.data.who).css({
					height : height
			});
		}
	},
	
	hideOptions: function(e) {
		if (!e) var e = window.event;
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		if(((reltg.nodeName != 'A') && (reltg.nodeName != 'DIV')) || ((reltg.nodeName == 'A') && (reltg.className=="selectButton") && (reltg.nodeName != 'DIV'))) {this.className = "optionsDivInvisible";};
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	},
	
	_checkMouse: function(e) {
		var self = this;
		var el = e.target;
		var div = $("#optionsDiv" + e.data.formindex + '_' + e.data.who);
		
		while (true){
			if (el == div[0]) {
				return true;
			} else if (el == document) {
				$(document).unbind('mousedown', self._checkMouse);
				div.toggleClass("optionsDivVisible").toggleClass("optionsDivInvisible");
				
				return false;
			} else {
				el = $(el).parent()[0];
			}
		}
	}/*,
	
	
	replaceTexts: function() {
		$.NiceJForms.texts.each(function(q){
			$($.NiceJForms.texts[q]).css({width:this.size * 10 + 'px'});
			var txtLeft = new Image();
			$(txtLeft)
				.attr({src:$.NiceJForms.options.imagesPath + "input_left.gif"})
				.addClass("inputCorner");
			
			var txtRight = new Image();
			$(txtRight)
				.attr({src:$.NiceJForms.options.imagesPath + "input_right.gif"})
				.addClass("inputCorner");
			
			$($.NiceJForms.texts[q]).before(txtLeft).after(txtRight).addClass("textinput");
			
			//create hovers
			$($.NiceJForms.texts[q]).focus(function(){$(this).addClass("textinputHovered");$(this).prev().attr('src', $.NiceJForms.options.imagesPath + "input_left_xon.gif");$(this).next().attr('src', $.NiceJForms.options.imagesPath + "input_right_xon.gif");});
			
			$($.NiceJForms.texts[q]).blur(function() {$(this).removeClass().addClass("textinput");$(this).prev().attr('src', $.NiceJForms.options.imagesPath + "input_left.gif");$(this).next().attr('src', $.NiceJForms.options.imagesPath + "input_right.gif");});
		});
	},
	
	replaceTextareas: function() {
		$.NiceJForms.textareas.each(function(q){
			
			var where = $(this).parent();
			var where2 = $(this).prev();
			
			$(this).css({width: $(this).attr("cols") * 10 + 'px', height: $(this).attr("rows") * 10 + 'px'});
			//create divs
			var container = document.createElement('div');
			$(container)
				.css({width: $.NiceJForms.textareas[q].cols * 10 + 20 + 'px', height: $.NiceJForms.textareas[q].rows * 10 + 20 + 'px'})
				.addClass("txtarea");
			
			var topRight = document.createElement('div');
			$(topRight).addClass("tr");
			
			var topLeft = new Image();
			$(topLeft).attr({src: $.NiceJForms.options.imagesPath + 'txtarea_tl.gif'}).addClass("txt_corner");
			
			var centerRight = document.createElement('div');
			$(centerRight).addClass("cntr");
			var centerLeft = document.createElement('div');
			$(centerLeft).addClass("cntr_l");
			
			if(!$.browser.msie) {$(centerLeft).height($.NiceJForms.textareas[q].rows * 10 + 10 + 'px')}
			else {$(centerLeft).height($.NiceJForms.textareas[q].rows * 10 + 12 + 'px')};
			
			var bottomRight = document.createElement('div');
			$(bottomRight).addClass("br");
			var bottomLeft = new Image();
			$(bottomLeft).attr({src: $.NiceJForms.options.imagesPath + 'txtarea_bl.gif'}).addClass('txt_corner');
			
			//assemble divs
			$(topRight).append(topLeft);
			$(centerRight).append(centerLeft).append($.NiceJForms.textareas[q]);
			$(bottomRight).append(bottomLeft);
			$(container).append(topRight).append(centerRight).append(bottomRight);
			
			$(where2).before(container);
			
			//create hovers
			$($.NiceJForms.textareas[q]).focus(function(){$(this).prev().removeClass().addClass("cntr_l_xon"); $(this).parent().removeClass().addClass("cntr_xon"); $(this).parent().prev().removeClass().addClass("tr_xon"); $(this).parent().prev().children(".txt_corner").attr('src', $.NiceJForms.options.imagesPath + "txtarea_tl_xon.gif"); $(this).parent().next().removeClass().addClass("br_xon"); $(this).parent().next().children(".txt_corner").attr('src', $.NiceJForms.options.imagesPath + "txtarea_bl_xon.gif")});
			$($.NiceJForms.textareas[q]).blur(function(){$(this).prev().removeClass().addClass("cntr_l"); $(this).parent().removeClass().addClass("cntr"); $(this).parent().prev().removeClass().addClass("tr"); $(this).parent().prev().children(".txt_corner").attr('src', $.NiceJForms.options.imagesPath + "txtarea_tl.gif"); $(this).parent().next().removeClass().addClass("br"); $(this).parent().next().children(".txt_corner").attr('src', $.NiceJForms.options.imagesPath + "txtarea_bl.gif")});
		});
	},
	
	buttonHovers: function() {
		$.NiceJForms.buttons.each(function(i){
			$(this).addClass("buttonSubmit");
			var buttonLeft = document.createElement('img');
			$(buttonLeft).attr({src: $.NiceJForms.options.imagesPath + "button_left.gif"}).addClass("buttonImg");
			
			$(this).before(buttonLeft);
			
			var buttonRight = document.createElement('img');
			$(buttonRight).attr({src: $.NiceJForms.options.imagesPath + "button_right.gif"}).addClass("buttonImg");
			
			if($(this).next()) {$(this).after(buttonRight)}
			else {$(this).parent().append(buttonRight)};
			
			$(this).hover(
				function(){$(this).attr("class", $(this).attr("class") + "Hovered"); $(this).prev().attr("src", $.NiceJForms.options.imagesPath + "button_left_xon.gif"); $(this).next().attr("src", $.NiceJForms.options.imagesPath + "button_right_xon.gif")},
				function(){$(this).attr("class", $(this).attr("class").replace(/Hovered/g, "")); $(this).prev().attr("src", $.NiceJForms.options.imagesPath + "button_left.gif"); $(this).next().attr("src", $.NiceJForms.options.imagesPath + "button_right.gif")}
			);
		});
	}*/
}

$.preloadImages = function()
{
	var imgs = new Array();
	for(var i = 0; i<arguments.length; i++)
	{
		imgs[i] = $("<img>").attr("src", arguments[i]);
	}
	
	return imgs;
}

$.iUtil = {
	getPosition : function(e)
	{
		var x = 0;
		var y = 0;
		var restoreStyle = false;
		var es = e.style;
		if ($(e).css('display') == 'none') {
			oldVisibility = es.visibility;
			oldPosition = es.position;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
			restoreStyle = true;
		}
		var el = e;
		while (el){
			x += el.offsetLeft + (el.currentStyle && !$.browser.opera ?parseInt(el.currentStyle.borderLeftWidth)||0:0);
			y += el.offsetTop + (el.currentStyle && !$.browser.opera ?parseInt(el.currentStyle.borderTopWidth)||0:0);
			el = el.offsetParent;
		}
		el = e;
		while (el && el.tagName  && el.tagName.toLowerCase() != 'body')
		{
			x -= el.scrollLeft||0;
			y -= el.scrollTop||0;
			el = el.parentNode;
		}
		if (restoreStyle) {
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {x:x, y:y};
	},
	getPositionLite : function(el)
	{
		var x = 0, y = 0;
		while(el) {
			x += el.offsetLeft || 0;
			y += el.offsetTop || 0;
			el = el.offsetParent;
		}
		return {x:x, y:y};
	}
};



/**
 * $ mousehold plugin - fires an event while the mouse is clicked down.
 * Additionally, the function, when executed, is passed a single
 * argument representing the count of times the event has been fired during
 * this session of the mouse hold.
 *
 * @author Remy Sharp (leftlogic.com)
 * @date 2006-12-15
 * @example $("img").mousehold(200, function(i){  })
 * @desc Repeats firing the passed function while the mouse is clicked down
 *
 * @name mousehold
 * @type $
 * @param Number timeout The frequency to repeat the event in milliseconds
 * @param Function fn A function to execute
 * @cat Plugin
 */

$.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 100;
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			$(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			})

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			$(this).mouseout(clearMousehold);
			$(this).mouseup(clearMousehold);
		})
	}
}