$.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	
	
	//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]);
	}
	
	$(':text,:password, :radio, :checkbox, :button, :submit, select, textarea,:reset',self).each(function()
	{
		$(this).wrap('<div class="formelemcontainer"></div>');
	});
	

	$(self).attr('autocomplete','off').addClass('niceforms');
	
	//text and passwords
	$(':text,:password, ',self).each(function()
	{
		$(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','bottom').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');
		});	
	});
	
			//select boxes


	//checkboxes
   $(':checkbox',self).each(function()
    {
     $(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()
      {
       var TickSet = ($('#'+this.id.replace('_fake','')).attr('checked')?"":"checked");
       $('#'+this.id.replace('_fake','')).attr('checked',TickSet);
       $('#'+$(this).attr('for')).attr('checked')?$(this).removeClass('chosen'):$(this).addClass('chosen');
       if (TickSet=="")
        $('#'+this.id).addClass('checkboxArea').removeClass('checkboxAreaChecked')
       else
        $('#'+this.id).addClass('checkboxAreaChecked').removeClass('checkboxArea')
       if(this.id=="boolCopyInvoiceAddresscc_fake")CopyAddress('cc');
       if(this.id=="boolCopyInvoiceAddressinv_fake")CopyAddress('inv');
       if(this.id=="boolCopyCCAddress_fake")CopyCCAddress();
      });
     $('label[for='+this.id+']').click(function()
      {
       $('#'+$(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()
	{
		$(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()
	{
		$(this).replaceWith('<div style="width: '+($(this).width()+20)+'px; height: '+($(this).height()+20)+'px;" 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()+10)+'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');
	});
	



	$(document).mouseup(function(event) {
		var fromDropdown = $(event.target).parents(".fake_list").length;
		if (fromDropdown == 0) {	// 0 means the event is not originated from the dropdown
			$(".fake_list:visible").each(function(){$(this).toggle()});
		}
	});

return self;
}
