(function($) {
	// Makenice 0.1 beta by Exact

	$.fn.makenice = function() {
		this.each(function(a,b){
			if(!$(this).attr('id')){ return; }
			var tmp1 = $(this)[0].offsetWidth;
			var tmp2 = $(this).css('margin-top');
			var tmp3 = $(this).css('margin-right');
			var tmp4 = $(this).css('margin-bottom');
			var tmp5 = $(this).css('margin-left');

			if(this.tagName == 'SELECT'){
				buildNiceselect($(this));
				$(this).css({
					position: 'absolute', left: '-1000px', top: '-1000px'
				}).bind("change click keydown keyup focus", function(e){
					if( !$('#niceselect-'+$(this).attr('id')+'-drop')[0] ){ buildNiceselectDrop(this); }
					$('#niceselect-'+$(this).attr('id')+'-drop LI.active').removeClass('active');
					$($('#niceselect-'+$(this).attr('id')+'-drop LI')[$(this).attr('selectedIndex')]).addClass('active');
					$('#niceselect-'+$(this).attr('id')+' SPAN').html($($('OPTION', this)[$(this).attr('selectedIndex')]).text());
				}).bind("focus", function(e){
					$('#niceselect-'+$(this).attr('id')).addClass('niceselect-focus');
				}).bind("blur", function(e){
					$('#niceselect-'+$(this).attr('id')).removeClass('niceselect-focus');
				}).bind('keydown', function(e){
					if (e.which == 13){
						closeNiceselect({data:{opener: $('#niceselect-'+$(this).attr('id'))}});
					} else if (e.which == 9){
						closeNiceselect({data:{opener: $('#niceselect-'+$(this).attr('id'))}});
					}
				});
				$($('#niceselect-'+$(this).attr('id'))[0]).css({
					width: tmp1 + 'px',
					marginTop: tmp2,
					marginRight: tmp3,
					marginBottom: tmp4,
					marginLeft: tmp5
				});
			} else if (this.tagName == 'INPUT'){
				$(this).wrap(
					'<div class="niceinput" id="niceinput-'+$(this).attr('id')+'"><div></div></div>'
				).focus(function(){
					$('#niceinput-'+$(this).attr('id')).addClass('niceinput-focus');
				}).blur(function(){
					$('#niceinput-'+$(this).attr('id')).removeClass('niceinput-focus');
				}).css({
					width: tmp1 - ($('#niceinput-'+$(this).attr('id'))[0].offsetWidth - $(this)[0].offsetWidth) + 'px'
				});
				$($('#niceinput-'+$(this).attr('id'))[0]).css({
					width: tmp1 + 'px',
					marginTop: tmp2,
					marginRight: tmp3,
					marginBottom: tmp4,
					marginLeft: tmp5
				});
			}
		});
		$('DIV.niceselect').click(function(){
			toggleNiceselect({data:{opener: this}});
			$($('#'+$(this).attr('id').split('niceselect-')[1])).focus();
			return false;
		});
	};
	function buildNiceselect(r){
		$(r).after('<div class="niceselect" id="niceselect-'+$(r).attr('id')+'"><div><span>'+$($('OPTION', r)[$(r).attr('selectedIndex')]).text()+'</span></div></div>');
		$('#niceselect-'+$(r).attr('id')).css({ width: $(r)[0].offsetWidth + 'px' });
	}
	function buildNiceselectDrop(r){
		$('BODY').append('<div class="niceselect-drop" id="niceselect-'+$(r).attr('id')+'-drop"><ul></ul></div>');
		$('OPTION',r).each(function(c,d){
			$('#niceselect-'+$(r).attr('id')+'-drop UL').append('<li><a '+ ($(r).attr('id')=='select01'?'onclick="redirect(\''+c+'\');"':'') +' href="#" class="' + $(r).attr('id') + '" name="' + c + '">' + $(d).text() + '</a></li>');
		});
		$('#niceselect-'+$(r).attr('id')+'-drop').css({ width: $(r)[0].offsetWidth + 'px' });
		$('DIV.niceselect-drop A').click(function(e){
			$($('#' + $(this).attr('class') + ' OPTION')[$(this).attr('name')]).attr({selected: 'selected'});
			$('#' + $(this).attr('class')).focus();
			return false;
		});
	}
	function toggleNiceselect(opener){
		opener = opener.data.opener;
		if(!$(opener).hasClass('niceselect-open')){
			if( !$('#'+$(opener).attr('id')+'-drop')[0] ){
				buildNiceselectDrop($('#'+$(opener).attr('id').split('niceselect-')[1]));
			}
			$(opener).addClass('niceselect-open').bind('mouseup',returnFalse);
			$('#' + $(opener).attr('id') + '-drop').show().css({
				left: $(opener).offset().left + 'px',
				top: $(opener).offset().top + $(opener)[0].offsetHeight + 'px'
			});
			$(document).bind('mouseup', {opener: opener}, toggleNiceselect);
		} else {
			$(opener).removeClass('niceselect-open').unbind('mouseup', returnFalse);
			$(document).unbind('mouseup', toggleNiceselect);
			$('#' + $(opener).attr('id') + '-drop').hide();
		}
	}
	function openNiceselect(opener){
		opener = opener.data.opener;
		if( !$('#'+$(opener).attr('id')+'-drop')[0] ){
			buildNiceselectDrop($('#'+$(opener).attr('id').split('niceselect-')[1]));
		}
		$(opener).addClass('niceselect-open');
		$('#' + $(opener).attr('id') + '-drop').show().css({
			left: $(opener).offset().left + 'px',
			top: $(opener).offset().top + $(opener)[0].offsetHeight + 'px'
		});
	}
	function closeNiceselect(opener){
		opener = opener.data.opener;
		$(opener).removeClass('niceselect-open');
		$('#' + $(opener).attr('id') + '-drop').hide();
		$(document).unbind('mouseup', toggleNiceselect);
	}
	function returnFalse(){	return false; }
})(jQuery);