if(!cs2) {
     var cs2 = jQuery.noConflict();
}

cs2.fn.jForms = 
function(options)
{
	var lastKeypress = 0;
    var keyBuffer = '';
    var self = this;
    var defaults = {
		listSize:18
	}	
	var opts = cs2.extend(defaults, options);
	cs2(self).attr('autocomplete','off').addClass('niceforms');
	
			//select boxes
cs2('select',self).each(function ()
{
	if(cs2('#'+this.id+'_fake_list').length)
		cs2('#'+this.id+'_fake_list').remove();
		
	cs2('body').append('<ul id = "'+this.id+'_fake_list" class="fake_list"></ul>');
	cs2(this).children().each(function()
	{
		cs2('#'+cs2(this).parent().attr('id')+'_fake_list').append('<li>'+cs2(this).text()+'</li>');
	});
	cs2('#'+this.id+'_fake_list').css('height',(cs2('#'+this.id+'_fake_list li:last').height()+4)*(cs2('#'+this.id+'_fake_list li').size() < opts.listSize ? cs2('#'+this.id+'_fake_list li').size(): opts.listSize )).css('width',cs2(this).width()+18).toggle();

	cs2('#'+this.id+'_fake_list > li').hover(function(){cs2(this).addClass('selected')},function(){cs2(this).removeClass('selected');}).each(function(i)
	{
		cs2(this).click(function(){
		var id = cs2(this).parent().attr('id');
		cs2('#'+id).toggle();
	    cs2('#'+id.replace('_list','')).attr('value',cs2(this).text());
		cs2('#'+id.replace('_fake_list','')).attr('selectedIndex',i).change();
		cs2('#'+id+' >  li').removeClass('selected');
		cs2(this).addClass('selected');	
	});
	});


cs2(this).before('<div class="selectArea" style="width:'+(cs2(this).width()+22)+'px"><div class="left"></div><div class="right"><a class="selectButton" href="javascript:;" onclick="cs2(\'#'+this.id+'_fake\').focus().click()"></a></div><input type = "text" class="center" style="width:'+(cs2(this).width()-4)+'px;cursor:default" id="'+this.id+'_fake"  value="'+this[this.selectedIndex].text+'"  /></div>').css('position','absolute').css('left','-6000px');

	cs2('#'+this.id+'_fake').click(function(){
		cs2('#'+this.id+'_list').focus();
		cs2('#'+this.id+'_list').css('left',cs2(this).offset().left);
		
		if((cs2('#'+this.id+'_list').height()+cs2(this).offset().top -document.documentElement.scrollTop) > cs2(window).height())
		{
			cs2('#'+this.id+'_list').css('top',cs2(this).offset().top-cs2('#'+this.id+'_list').height()-4);
		}
		else
		{
			cs2('#'+this.id+'_list').css('top',cs2(this).offset().top+cs2(this).height()+4);
		}

		cs2('#'+this.id+'_list > li').removeClass('selected');
		cs2('#'+this.id+'_list > li').get(cs2('#'+this.id.replace('_fake','')).attr('selectedIndex')).className='selected';
		cs2('.fake_list:visible:not(#'+this.id+'_list)').toggle();
		cs2list = cs2('#'+this.id+'_list');	
		cs2list.toggle();		
		var offSet = ((cs2('.selected', cs2list).length>0? cs2('.selected', cs2list).offset().top:0)- cs2list.offset().top);
		cs2list.animate({scrollTop: offSet});
		}).keyup(function(e)
		{
			var pressedKey = e.charCode || e.keyCode || -1;
			var cs2dd = cs2('#'+this.id.replace('_fake',''));
			cs2('#'+this.id+'_list > li').removeClass('selected');
			switch(pressedKey)
			{
				//down
				case 40:						
					var curr = (cs2dd.attr('selectedIndex')+1>=cs2('option',cs2dd).length?0:cs2dd.attr('selectedIndex')+1);
				break;
				case 38:						
					var curr = (cs2dd.attr('selectedIndex')-1<0?cs2('option',cs2dd).length-1:cs2dd.attr('selectedIndex')-1);						
				break;
				case 13:
					cs2('#'+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;
					cs2('#'+this.id+'_list > li').each(function(i)
					{
						if(cs2(this).text().toLowerCase().indexOf(keyBuffer)==0&&curr==-1)
						{
							curr = i;
							return;
						}
					});
					break;
			}
			if(curr==-1)
			{
				curr=0;
			}
			cs2(cs2('#'+this.id+'_list > li').get(curr)).addClass('selected').focus();
			cs2list = cs2('#'+this.id+'_list');
			var offSet = ((cs2('.selected', cs2list).length>0? cs2('.selected', cs2list).offset().top:0)- cs2list.offset().top);
			cs2list.attr('scrollTop',offSet);
			cs2dd.attr('selectedIndex',curr).change();
			cs2(this).attr('value', cs2(cs2('#'+this.id+'_list > li').get(curr)).text());
			return false;
						
		}).focus(function()
		{
			cs2(document).keypress(function(e)
			{
				var pressedKey = e.charCode || e.keyCode || -1;
				if(pressedKey==13)
				{
					return false;
				}
			});
		}).blur(function()
		{
			cs2(document).unbind('keypress').unbind('click');
		});

});

	

	return self;
}
