
/**
 * custom select
 */
jQuery.fn.customSelect = function(s,p) {
	
	_selectStatus = new Array();
	
	if (!p) { p = false; }
	
	var par = {
		border:			1,
		highlightClass: 'selected',
		bgPosOffset:	50,
		bgPosDefault:	3
	}
	
	// 
	
	// build up custom selects
	$(s).each(function(i,s){
		var selId = $(this).attr('id');	
		_selectStatus[selId] = false;
		var that = "select#"+selId;
		$(that).parent().append('<span id="sp_'+selId+'"></span>');
		$('body').append('<ul id="sel_'+selId+'" class="customselect"></ul>');
		posT = $(that).parent().position().top+$(that).parent().height()+par.border+'px';
		posL = $(that).parent().position().left+parseInt($(that).parent().css('marginLeft'))+'px';
		$('ul#sel_'+selId).css({'top':posT,'left':posL});
		$('ul#sel_'+selId).attr('class',$(that).parent().attr('class'));
		for(var i=0; i < $(that).find('option').length;i++ ){
			if ($(that).find('option').eq(i).attr('value') != '') {
				var sel = '';
				if($(that).find('option').eq(i).attr('selected')){
					$('span#sp_'+selId).html($(that).find('option').eq(i).html());
					var sel = (par.highlightClass) ? ' class="'+par.highlightClass+'"' : '';
				}
				$('ul#sel_'+selId).append(['<li rel="', $(that).find('option').eq(i).attr('value'), '"', sel, '>', $(that).find('option').eq(i).html(), '</li>'].join(''));
			}
		}
		var bgPos = $(that).parent().backgroundPosition().split(' ');
		$('ul#sel_'+selId).find('li').each(function(i,s){
			$(this).click(function(e){
				$(e).stop();
				$('span#sp_'+selId).html( this.innerHTML );
				$(that+' option').removeAttr('selected').end()
				$(that+' option').removeAttr('selected').end()
						.find(that+' option[value='+$(this).attr('rel')+']')
						.attr('selected','selected');
				$(this).parent().hide();	
				$(that).parent().css('backgroundPosition',bgPos[0]+' '+par.bgPosDefault+'px');
				if (par.highlightClass) {
					$(this).parent().find('li').removeClass(par.highlightClass);
					$(this).addClass(par.highlightClass);
				}
				_selectStatus[selId] = 2;
			});
			$(this).hover(function(){
				$(this).addClass('hover');
			},function(){
				$(this).removeClass('hover');
			});				
		});				
		$(that).parent().click(function(e){
			$(e).stop();
			$('ul#sel_'+selId).hide();
			$('ul#sel_'+selId).show();
			$(that).parent().css('backgroundPosition',bgPos[0]+' '+(parseInt(bgPos[1])-par.bgPosOffset)+'px');
			if (!_selectStatus[selId] || _selectStatus[selId]==2) { 
				_selectStatus[selId] = 1;
				jQuery.later(window, 100, function(){
					_selectStatus[selId] = false;
				});
			}
		});	   
		$('ul#sel_'+selId).hide();	
	});
	
	// handle clicks outside selects
	jQuery.later(window, 300, function(){
		$('html')[0].onclick = function(){
			var ar = array_keys(_selectStatus);
			for (i=0;i<ar.length;i++) {
				if ($('ul#sel_'+ar[i]).css('display')!='none' && _selectStatus[ar[i]]!=1) {
					$('ul#sel_'+ar[i]).hide();
					$('select#'+ar[i]).parent().css('backgroundPosition',$('select#'+ar[i]).parent().backgroundPosition().split(' ')[0]+' '+par.bgPosDefault+'px');
					_selectStatus[ar[i]] = false;
				}
			}
		}	
	});
}
