/* Copyright (C) 2009 Pierre Allain (contact@p3x.fr) http://expeo.net/
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2009-02-06 04:38:37 +0100  $
 * $Rev: 115 $
 *
 * Version: 1.0b
 *
 * To use the plugin : $('#MySelectID').SelectToCheckboxList({prompt:'Default Text', width:'120px', height:'100px'});
 */

(function($) {

  $.fn.SelectToCheckboxList = function(options)
	{
	
	var defaults =
	{
		prompt:           'selection',
		width:            'auto',
		height:	          'auto',
		develop:          false
	};
	// Extend our default options with those provided.
	var opts = $.extend(defaults, options);

    var $this = $(this);
    var id = $this.attr('id');
    var prefixList = 'cbl_';
    var idList = prefixList + id;
    
	var select_c = $('<div />')
		.attr('class', 'sel')
		.attr('id', 'sel_' + idList)
		.prependTo($this.parent());
	
	var container = $('<div />')
		.attr('class', 'cont')
		.attr('id', 'cont_' + idList)
		.mouseout(function()
					{
						$(this)
							.css('background', '#ffffff url(\'/hab/b-arrow.png\')')
							.css('background-repeat', 'no-repeat')
							.css('background-position', 'right');
					})
		.mouseover(function()
					{
						$(this)
							.css('background', '#ffe7e7 url(\'/hab/b-arrow2.png\')')
							.css('background-repeat', 'no-repeat')
							.css('background-position', 'right')
					})
		.click(function()
					{
						if($('#' + idList).css('display')=='none')
						{
							$('.close').hide('slow')
							$('#' + idList).show('slow')
							$('#shadow_' + idList).show('slow')
						}
						else
						{
							$('#' + idList).hide('slow')
							$('#shadow_' + idList).hide('slow')
						}
					})
		.appendTo(select_c);
	
	$('<label />')
		.text(opts.prompt)
		.appendTo(container);
	
	var s_container = $('<div />')
		.attr('class', 's_cont')
		.attr('id', 's_cont_' + idList)
		.insertAfter(select_c);
    
	var cbl = $('<div />')
					.attr('class', 'select close')
					.attr('id', idList)
					.css('height', opts.height)
					.css('width', opts.width)
					.prependTo(s_container);
	
	$('<img />')
		.attr('class', 'bt_close')
		.attr('src', '/hab/btnclose.gif')
		.click(function(){container.click();})
		.appendTo(cbl); //Bouton fermer.

	if(opts.width!='auto' && opts.height!='auto')
	{
		$('<div />')
			.attr('class', 'select_shadow close')
			.attr('id', 'shadow_' + idList)
			.height($('#' + idList).height())
			.width($('#' + idList).width())
			.prependTo(s_container);
	}
          //itère sur les optgroups 
    $('#' + id + ' optgroup')
		.each(function(index)
		{
			var groupId = id + 'group_' + index;
			//Ajoute la div, la checkbox du groupe
			var group = $('<div />')
				.attr('id', groupId)
				.attr('class', 'cbgroup')
				.appendTo(cbl);
			if(opts.develop==true)
			{
				$('<img />')
					.attr('src', '/hab/more.png')
					.attr('class', 'develop')
					.click(function(){
						if($(this).attr('src')=='/hab/more.png')
						{
							$('#'+groupId+'develop_list').show('slow');
							$(this).attr('src', '/hab/less.png');
						}
						else
						{
							$('#'+groupId+'develop_list').hide('slow');
							$(this).attr('src', '/hab/more.png');
						}
					})
					.appendTo(group);
			}
			$('<input type="checkbox" />')
				.attr('id', id + 'cbg_' + index)
				.click(function()
					{
						$('#'+ groupId + ' input')
							.attr('checked', this.checked);
					})
				.appendTo(group);
			$('<label />')
				.css('font-weight', 'bold')
				.attr('for', id + 'cbg_' + index)
				.text($(this).attr('label'))
				.appendTo(group);
			$('<br />')
				.appendTo(group);
			if(opts.develop==true)
			{
				$('<div>')
					.attr('class', 'develop_list')
					.attr('id', groupId+'develop_list')
					.hide()
					.appendTo(group);
			}
			else
			{
				$('<div>')
					.attr('class', 'develop_list')
					.attr('id', groupId+'develop_list')
					.appendTo(group);
			}
			
			// Itère sur les options de l'optgroup.
			$('#' + id + ' optgroup[label=' + $(this).attr('label') + '] option')
				.each(function(index)
				{
					//Ajoute la checkbox
					$('<label />')
						.html('-')
						.appendTo('#'+groupId+'develop_list');
					$('<input type=checkbox />')
						.attr('class', 's2cbl_cb')
						.attr('id', id + 'cb_' + groupId + '_' + index)
						.attr('name', $this.attr('name'))
						.val($(this).val())
						.attr('checked',(($(this).attr('selected') != '') || ($(this).hasClass('selected')))?true:false)
						.appendTo('#'+groupId+'develop_list');
					$('<label />')
						.attr('for', id + 'cb_' + groupId + '_' + index)
						.text($(this).text())
						.appendTo('#'+groupId+'develop_list');
					$('<br />')
						.appendTo('#'+groupId+'develop_list');
					$(this).removeAttr('selected'); //prevents posting of the select "selected" value.
					if($(this).hasClass('selected'))
					{
						$('#'+groupId+'develop_list').show();
						$('#' + groupId + ' img').attr('src', '/hab/less.png');
					}
				});
		});
		//Masque le select.
		$this.hide();
	};
})(jQuery);
