$(function()
{
	$(document).click(function(event)
	{
		if( !$(event.target).hasClass('myselectvalue') && !$(event.target).hasClass('myselectfield') && $(event.target).parents('.myselectliste').length == 0)
		{
			hideAllSelect();
		}
	});
});


function initMySelect(s, opts, callback)
{
	var liste = $('#' + s.attr('id') + 'liste');
	
	s.html('<div class="myselectvalue"></div>');
	
	for(name in opts)
	{
		s.attr(name, opts[name]);
	}
	
	liste.find("tr").each(function()
	{
		$(this).addClass('row');
		
		if( $(this).find('a').length == 0)
		{
			$(this).mouseover(function()
			{
				$(this).css("background", "#ccc");
			});
			$(this).mouseout(function()
			{
				$(this).css("background", "white");
			});
		}
		
		if( s.attr('multi') != 'true' )
		{
			$(this).click(function()
			{
				liste.find("tr").each(function()
				{
					$(this).removeClass('selected');
				});
				
				$(this).addClass('selected');				
				
				setCurrent(liste, s);
				
				liste.hide();
				
				if( s.attr('submit') == 'true' )
				{
					$(s.attr('form')).get(0).submit();
				}
				
				if( callback != null )
				{
					callback(s);
				}
			});
		}
	});
	
	if( s.attr('multi') == 'true' )
	{
		liste.find("input").each(function()
		{
			$(this).click(function(event)
			{
				countChecked(liste, s);			
				event.stopPropagation();
			});
			
			var input = $(this);
			
			$(this).parents('tr.row').click(function(event)
			{	
				var i = input.get(0);
				
				i.checked = !i.checked;
				
				countChecked(liste, s);
				
			});
		});
		
		liste.find("a").click(function() 
		{
			var lien = $(this);
			
			liste.find("input").each(function()
			{
				$(this).get(0).checked = lien.hasClass('cocher');
			});
			
			$(this).get(0).blur();
			
			countChecked(liste, s);
			
			return false;
		});
	}
	
	
	
	if( s.attr('multi') == 'true' )
	{
		countChecked(liste, s);
	}
	else
	{
		setCurrent(liste, s);
	}
	
	var field = s.find(".myselectvalue");
	
	s.unbind().click(function() {showMySelect($(this), opts) });
	
	field.click( function() { showMySelect(s, opts); return false; } );
}

function setCurrent(liste, s)
{
	s.removeClass('selected');
	
	liste.find("tr").each(function()
	{
		if( $(this).hasClass('selected') )
		{
			$(s.attr('form')).get(0)[s.attr('field')].value = $(this).attr('ident');
			s.find(".myselectvalue").html( $(this).find('label').html());
			
			if( $(this).attr('default') != 'true' || s.attr('alwaysselected') == 'true')
			{
				s.addClass('selected');
			}
			
			if( s.attr('change') != null )
			{
				var c = eval(s.attr('change'));
				c($(this).attr('ident'));
			}
			
			return false;
		}
	});
}

function getCurrent(s)
{
	var liste = $('#' + s.attr('id') + 'liste');
	var value = null;
	
	
	liste.find("tr").each(function()
	{
		if( $(this).hasClass('selected') )
		{
			value = $(this).attr('ident');
		}
	});
	
	return value;
}

function showMySelect(s, opts)
{
	var liste = $('#' + s.attr('id') + 'liste');
	
	$('.myselectliste').each(function(){
		if( $(this).attr('id') != liste.attr('id'))
		{
			$(this).hide();
		}
	});
	
	if( liste.css("display") == 'none' )
	{
		liste.css("display", "block");
	}
	else
	{
		liste.css("display", "none");
	}
	
}

function countChecked(liste, s)
{
	var nb = 0;
	var ids = '';
	
	s.removeClass('selected');
	
	var label = null;
	
	liste.find("input").each(function()
	{
		if( $(this).get(0).checked)
		{
			if( ids.length > 0)
			{
				ids += ',';
			}
			nb++;
			
			var td = $(this).parent();
			td = td.next();
			
			label = td.html();
			
			ids += $(this).val();
		}
	});
	
	var p = (nb > 1 && s.attr('nopluriel') != 'true') ? 's' : '';
	
	$(s.attr('form')).get(0)[s.attr('field')].value = ids;
	
	var field = s.find(".myselectvalue");
	
	if( nb == 1 )
	{
		field.html(label);
		s.addClass('selected');
	}
	else if( nb > 0)
	{
		field.html(nb + " " + s.attr('libelle') + p +" selected" + s.attr('genre') + (nb > 1 ? '' : ''));
		s.addClass('selected');
	}
	else
	{
		if( s.attr('defaultTitle'))
		{
			field.html(s.attr('defaultTitle'));
		}
		else if( s.attr('noneLabel') != 'true')
		{
			field.html(s.attr('genre') == 'e' ? 'Toutes' : 'Tous');
		}
		else
		{
			field.html(s.attr('genre') == 'e' ? 'Aucune' : 'Aucun');
		}
	}
}

function hideAllSelect()
{
	$('.myselectliste').each(function(){
		$(this).hide();
	});
}
