nForms=true;
$.fn.jForms = 
function(options) {
	var lastKeypress = 0;
    var keyBuffer = '';
    var self = this;
    var defaults = {
		imagePath : '/niceforms/default/',
		listSize:20
	}
	//if($.browser.safari){return false;}
	var opts = $.extend(defaults, options);
	var imagePath = opts.imagePath;
	
	//alert(imagePath);
	
	//preload images
	var images = [imagePath + "button_left_xon.gif", imagePath + "button_right_xon.gif",
				imagePath + "input_left_xon.gif", imagePath + "input_right_xon.gif",
				imagePath + "txtarea_bl_xon.gif", imagePath + "txtarea_br_xon.gif",
				imagePath + "txtarea_cntr_xon.gif", imagePath + "txtarea_l_xon.gif",
				imagePath + "txtarea_tl_xon.gif", imagePath + "txtarea_tr_xon.gif"];
	var imgs = new Array();
	for(var i = 0; i<images.length; i++){
		imgs[i] = $("<img>").attr("src", images[i]);
	}
	
	$(self).attr('autocomplete','off').addClass('niceforms');
	
	
	
	//text and passwords
	$(':text,:password',self).each(function(){
		if (!$(this).hasClass("noJForm")) {
			if (!$(this).hasClass("error")) {
				$(this).addClass('textinput').before('<img src = "' + imagePath + 'input_left.gif" class="inputCorner" />').after('<img src="' + imagePath + 'input_right.gif" class="inputCorner" />').focus(function(){
					$(this).css('background-position', '0 -23px').prev().attr('src', imagePath + 'input_left_xon.gif');
					$(this).next().attr('src', imagePath + 'input_right_xon.gif');
					
				}).blur(function(){
					$(this).css('background-position', 'top').prev().attr('src', imagePath + 'input_left.gif');
					$(this).next().attr('src', imagePath + 'input_right.gif');
				});
			}
		}
	});
	
	//input file // claudio for triboo
	$(':file',self).each(function(){
		if (!$(this).hasClass("noJForm")) {
			$(this).addClass('file').after('<br /><br />');
			$(this).change(function(){
				$('#' + $(this).attr('name') + '_fakeFile').val($(this).val());
			});
			$(this).before('<div class="parentNiceForms"> <div class="fakefile">' +
					'<img src = "' + imagePath + 'input_left.gif" class="inputCorner" />' +
					'<input id="' + $(this).attr('name') + '_fakeFile" class="textinput" type="text" value="" maxlength="100" size="20" readonly="readonly"/>' +
					'<img src = "' + imagePath + 'input_right.gif" class="inputCorner" />' +
					'<img src="' + imagePath + 'button_select.gif"/>'+
				'</div></div>');
		}
	});
	
	
	
	//select boxes
	$('select',self).each(function () {
		if (!$(this).hasClass("noJForm")) {
			$('body').append('<ul id = "' + this.id + '_fake_list" class="fake_list" ></ul>');
			$(this).children().each(function(){
				$('#' + $(this).parent().attr('id') + '_fake_list').append('<li>' + $(this).text() + '</li>');
			});
			$('#' + this.id + '_fake_list').css('height', ($('#' + this.id + '_fake_list li:first').height() + 4) * ($('#' + this.id + '_fake_list li').size() < opts.listSize ? $('#' + this.id + '_fake_list li').size() : opts.listSize)).css('width', $(this).width() + 18).toggle();
			$('#' + this.id + '_fake_list > li').hover(function(){
				$(this).addClass('selected')
			}, function(){
				$(this).removeClass('selected');
			}).each(function(i){
				$(this).click(function(){
					var id = $(this).parent().attr('id');
					$('#' + id).toggle();
					$('#' + id.replace('_list', '')).attr('value', $(this).text());
					$('#' + id.replace('_fake_list', '')).attr('selectedIndex', i).change();
					$('#' + id + ' >  li').removeClass('selected');
					$(this).addClass('selected');
				});
			});
			
			$(this).before('<div class="selectArea" style="width:' + ($(this).width() + 43) + 'px;float:left;"><div class="left"></div><div class="right"><a class="selectButton" href="javascript:;" onclick="$(\'#' + this.id + '_fake\').focus().click()"></a></div><input type = "text" class="center" style="width:' + ($(this).width()) + 'px;cursor:default" id="' + this.id + '_fake"  value="' + this[this.selectedIndex].text + '"  /></div>').css('position', 'absolute').css('left', '-6000px');
			
			$('#' + this.id + '_fake').click(function(){
				$('#' + this.id + '_list').focus();
				$('#' + this.id + '_list').css('left', $(this).offset().left - 8);
				
				if (($('#' + this.id + '_list').height() + $(this).offset().top - document.documentElement.scrollTop) > $(window).height()) {
					$('#' + this.id + '_list').css('top', $(this).offset().top - $('#' + this.id + '_list').height() - 4);
				} else {
					$('#' + this.id + '_list').css('top', $(this).offset().top + $(this).height() + 4);
				}
				$('#' + this.id + '_list > li').removeClass('selected');
				$('#' + this.id + '_list > li').get($('#' + this.id.replace('_fake', '')).attr('selectedIndex')).className = 'selected';
				
				
				$('.fake_list:visible:not(#' + this.id + '_list)').toggle();
				$list = $('#' + this.id + '_list');
				$list.toggle();
				var offSet = (($('.selected', $list).length > 0 ? $('.selected', $list).offset().top : 0) - $list.offset().top);
				$list.animate({
					scrollTop: offSet
				});
			}).css('font-size', $(this).css('font-size')).css('padding', '2px 0px 2px 2px').keyup(function(e){
				var pressedKey = e.charCode || e.keyCode || -1;
				var $dd = $('#' + this.id.replace('_fake', ''));
				$('#' + this.id + '_list > li').removeClass('selected');
				switch (pressedKey) {
					//down
					case 40:
						var curr = ($dd.attr('selectedIndex') + 1 >= $('option', $dd).length ? 0 : $dd.attr('selectedIndex') + 1);
						break;
					case 38:
						var curr = ($dd.attr('selectedIndex') - 1 < 0 ? $('option', $dd).length - 1 : $dd.attr('selectedIndex') - 1);
						break;
					case 13:
						$('#' + this.id + '_list').toggle();
						return false;
						break;
					default:
						var t = new Date();
						if (t.getTime() - lastKeypress > 1000) {
							lastKeypress = t.getTime();
							keyBuffer = '';
						}
						keyBuffer += String.fromCharCode(pressedKey).toLowerCase();
						curr = -1;
						$('#' + this.id + '_list > li').each(function(i){
							if ($(this).text().toLowerCase().indexOf(keyBuffer) == 0 && curr == -1) {
								curr = i;
								return;
							}
						});
						break;
				}
				if (curr == -1) {
					curr = 0;
				}
				$($('#' + this.id + '_list > li').get(curr)).addClass('selected').focus();
				$list = $('#' + this.id + '_list');
				var offSet = (($('.selected', $list).length > 0 ? $('.selected', $list).offset().top : 0) - $list.offset().top);
				$list.attr('scrollTop', offSet);
				$dd.attr('selectedIndex', curr).change();
				$(this).attr('value', $($('#' + this.id + '_list > li').get(curr)).text());
				return false;
			
			}).focus(function(){
				$(document).keypress(function(e){
					var pressedKey = e.charCode || e.keyCode || -1;
					if (pressedKey == 13) {
						return false;
					}
				});
			}).blur(function(){
				$(document).unbind('keypress').unbind('click');
			});
		
		}	// resticcion noJForm
	
	});
	
	
	
	//checkboxes 
	//TODO arreglar para que funcionen los checkboxes.
	/*
	$(':checkbox',self).each(function(){
		if (!$(this).hasClass("noJForm")) {
			$(this).before('<div style="margin: 1px;" id="' + this.id + '_fake"></div>');
			$(this).addClass('outtaHere');
			this.checked ? $('label[for=' + this.id + ']').addClass('chosen') : '';
			$('#' + this.id + '_fake').addClass(this.checked ? 'checkboxAreaChecked' : 'checkboxArea').click(function(){
				$('label[for=' + this.id.replace('_fake', '') + ']').click();
				$('#' + this.id.replace('_fake', '')).attr('checked') ? $('#' + this.id.replace('_fake', '')).attr('checked', '') : $('#' + this.id.replace('_fake', '')).attr('checked', 'checked');
			});
			$('label[for=' + this.id + ']').click(function(){
				//these are backwards on purpose -click functions are called before the checkbox is selected
				$('#' + $(this).attr('for')).attr('checked') ? $(this).removeClass('chosen') : $(this).addClass('chosen');
				$('#' + $(this).attr('for') + '_fake').addClass($('#' + $(this).attr('for')).attr('checked') ? 'checkboxArea' : 'checkboxAreaChecked').removeClass($('#' + $(this).attr('for')).attr('checked') ? 'checkboxAreaChecked' : 'checkboxArea');
			});
		}
	});*/
	
	
	
	//radios
	$(':radio',self).each(function(){
		if (!$(this).hasClass("noJForm")) {
			$(this).after('<div style="margin: 1px;" id="' + this.id + '_fake"></div>').addClass('outtaHere');
			this.checked ? $('label[for=' + this.id + ']').addClass('chosen') : '';
			$('#' + this.id + '_fake').addClass(this.checked ? 'radioAreaChecked' : 'radioArea').click(function(){
				$(':radio[name=' + $('#' + this.id.replace('_fake', '')).attr('name') + ']').each(function(){
					this.checked = false;
				});
				$('#' + this.id.replace('_fake', '')).attr('checked', 'checked');
				$('label[for=' + this.id.replace('_fake', '') + ']').click();
			});
			$('label[for=' + this.id + ']').click(function(){
				$(':radio[name=' + $('#' + $(this).attr('for')).attr('name') + ']').each(function(){
					$('label[for=' + this.id + ']').removeClass('chosen');
					$('#' + this.id + '_fake').addClass('radioArea').removeClass('radioAreaChecked');
					$('label[for=' + this.id.replace('_fake', '') + ']').removeClass('chosen');
				});
				$(this).addClass('chosen');
				$('#' + $(this).attr('for') + '_fake').addClass('radioAreaChecked');
			})
		}
	});
	
	
	
	//text areas
	$('textarea',self).each(function(){
			if (!$(this).hasClass("noJForm")) {
				//alert($(this).width() +" ...." + $(this).height());
				$(this).replaceWith('<div style="width: ' + ($(this).width() + 10) + 'px; height: ' + ($(this).height() + 10) + 'px;float:left;" class="txtarea" id = "' + this.id + '_fake"><div class="tr"><img src="'+imagePath+'txtarea_tl.gif" class="txt_corner"></div><div class="cntr"><div style="height: ' + ($(this).height() + 0) + 'px;" class="cntr_l"></div></div><div class="br"><img src="' + imagePath + 'txtarea_bl.gif" class="txt_corner"></div></div>');
				$('#' + this.id + '_fake .cntr').append($(this));
			}
		}).focus(function()	{
			$('#'+this.id+'_fake .tr').removeClass('tr').addClass('tr_xon');
			$('#'+this.id+'_fake .br').removeClass('br').addClass('br_xon');
			$('#'+this.id+'_fake .cntr').removeClass('cntr').addClass('cntr_xon');
			$('#'+this.id+'_fake .cntr_l').removeClass('cntr_').addClass('cntr_l_xon');
			$('#'+this.id+'_fake img:first').attr('src',imagePath+'txtarea_tl_xon.gif');
			$('#'+this.id+'_fake img:last').attr('src',imagePath+'txtarea_bl_xon.gif');
		}).blur(function(){
			$('#'+this.id+'_fake .tr_xon').addClass('tr').removeClass('tr_xon');
			$('#'+this.id+'_fake .br_xon').addClass('br').removeClass('br_xon');
			$('#'+this.id+'_fake .cntr_xon').addClass('cntr').removeClass('cntr_xon');
			$('#'+this.id+'_fake .cntr_l_xon').addClass('cntr_').removeClass('cntr_l_xon');
			$('#'+this.id+'_fake img:first').attr('src',imagePath+'txtarea_tl.gif');
			$('#'+this.id+'_fake img:last').attr('src',imagePath+'txtarea_bl.gif');
		});
	
	
	
	//button y submit
	$(':button,:submit',self).each(function(){
		if (!$(this).hasClass("noJForm")) {
			$(this).before('<img class="buttonImg" src="' + imagePath + 'button_left.gif">').after('<img class="buttonImg" src="' + imagePath + 'button_right.gif">').addClass('buttonSubmit').hover(function(){
				$(this).prev().attr('src', imagePath + 'button_left_xon.gif');
				$(this).next().attr('src', imagePath + 'button_right_xon.gif');
				$(this).addClass('buttonSubmitHovered').removeClass('buttonSubmit');
			}, function(){
				$(this).prev().attr('src', imagePath + 'button_left.gif');
				$(this).next().attr('src', imagePath + 'button_right.gif');
				$(this).removeClass('buttonSubmitHovered').addClass('buttonSubmit');
			});
		}
	});
	
	
	return self;
}
