/**
* MSC : Multiple Select Checkboxes
* You can use it freely as long as this header remains
* Author : Yann Lohier / zzzombie.com
* rev. : 0.1
**/

MultipleSelectCheck_Controller = {
	current : null,
	_s_openLock :false,
	
	init	: function()
	{
		Event.observe(document, 'mousedown', function() {
			if (MultipleSelectCheck_Controller.current != null && !MultipleSelectCheck_Controller._s_openLock)
			{
				window.setTimeout("MultipleSelectCheck_Controller.documentClick()", 100);
			}
			MultipleSelectCheck_Controller._s_openLock = false;
		});
	},
	
	documentClick : function()
	{
		if (this.current != null)
		{
			this.current.documentClick();
		}
	},
	
	openStart : function()
	{
		this._s_openLock = true;

		if (this.current != null)
		{
			this.current.close();
		}
	},

	globalSwitch : function(objMSC)
	{
		objMSC = eval(objMSC);
		
		var state = $(objMSC.htmlMSCId + '_globalSwitch').checked;
		objMSC.globalSwitchChecked = state;
		
		var optionsCollection = $(objMSC.htmlMSCId + '_optionsContainer').getElementsByTagName('input');

		for (i=0; i<optionsCollection.length; i++)
		{
			currentInput	= optionsCollection.item(i);
			if (currentInput.getAttribute('value') )
			{
				currentInput.checked = state;
			}
		}
		objMSC.updateMSCinput();
	}
}

	
	
function MultipleSelectCheckbox(objName)
{
	this.objName			= objName;
	this.htmlSelectName		= null;
	this.htmlSelectId		= null;
	
	this.htmlMSCName		= null;
	this.htmlMSCId			= null;

	this.MSCoptionsKeys		=  Array();
	this.MSCoptions			=  Array();

	this.globalSwitch		= null;
	this.globalSwitchValue	= null;
	this.URLSeparator		= ',';
	this.visualSeparator	= ',';

	this._s_opened			= false;
	this._s_clicked			= false;



	this.updateMSCinput		= function()
	{
		tmp = Array();
		tmp2 = Array();

		var optionsCollection = $(this.htmlMSCId + '_optionsContainer').getElementsByTagName('input');

		var allChecked = true;
		var noneChecked = true;

		for (i=0; i<optionsCollection.length; i++)
		{
			currentInput	= optionsCollection.item(i);
			if (currentInput.checked)
			{
				noneChecked = false;
				if (currentInput.getAttribute('value') )
				{
					tmpValue = currentInput.getAttribute('value');
					tmp.push( tmpValue );
					if (tmpValue != this.globalSwitchValue) tmp2.push( this.MSCoptions[tmpValue] );
				}
			}
			else
			{
				if (currentInput.getAttribute('value') && currentInput.getAttribute('value') != this.globalSwitchValue) allChecked = false;
			}
		}
		
		this.globalSwitchChecked = allChecked;
		$(this.htmlMSCId + '_globalSwitch').checked = this.globalSwitchChecked;
		
		
		$(this.htmlMSCId).value	= tmp.join(this.URLSeparator);

		if (this.globalSwitch != null && this.globalSwitchChecked)
		{
			tmpHTML = this.globalSwitch;
		}
		else
		{
			tmpHTML = tmp2.join(this.visualSeparator);
		}
		 
		
		$(this.htmlMSCId + '_selectedView').innerHTML = tmpHTML; 
	}



	this.addGlobalSwitch = function(label, value, checked)
	{
		this.globalSwitch = label;
		this.globalSwitchValue = value;
		this.globalSwitchChecked = checked;
	}



	this.create = function (htmlSelectName, htmlSelectId, htmlMSCName, htmlMSCId)
	{
		this.htmlSelectName		= htmlSelectName;
		this.htmlSelectId		= htmlSelectId;		
		this.htmlMSCName		= htmlMSCName;
		this.htmlMSCId			= htmlMSCId;
	
		var html = '';
		var htmlOptions = '';
		
		var width = $(htmlSelectId).getWidth() + 15;
		var optionsCollection = $(htmlSelectId).getElementsByTagName('option');

		var ie6_hover = document.all ? 'onmouseover="Element.addClassName(this,\'hover\');" onmouseout="Element.removeClassName(this,\'hover\')" ' : '';

		for (i=0; i<optionsCollection.length; i++)
		{
			currentOption	= optionsCollection.item(i);
			currentValue	= currentOption.getAttribute('value');
			currentChecked	= currentOption.getAttribute('checked') != null;
			currentId		= htmlMSCId + '_' + i;

			this.MSCoptionsKeys[currentValue] = currentValue;
			this.MSCoptions[currentValue] = currentOption.firstChild.data;

			htmlOptions +=	'<div class="MCP_option" ' + ie6_hover + '>'
							+ '<input type="checkbox" id="' + currentId + '" value="' + currentValue + '" '
									+  (currentChecked ? ' checked="checked" ' : '')
							+ ' />'
							+ '<label style="width:' + (width - 20) + 'px" '
									+ 'onclick="$(\'' + currentId + '\').checked =!$(\'' + currentId + '\').checked; '
									+ this.objName + '.updateMSCinput();">' + currentOption.firstChild.data + '</label>'
							+ '<div style="clear:both;"></div>'
							+ '</div>';
		}

		if (this.globalSwitch !== null)
		{
			var tmpOnclick = 'MultipleSelectCheck_Controller.globalSwitch(\'' + this.objName + '\');';
			htmlOptions =	'<div class="MCP_option global" ' + ie6_hover + '>'
							+ '<input type="checkbox" id="' + htmlMSCId + '_globalSwitch"'
									+ ' onclick="' + tmpOnclick + '" '
									+  (this.globalSwitchChecked ? ' checked="checked" ' : '')
									+  (this.globalSwitchValue ? ' value="' + this.globalSwitchValue + '" ' : '')
							+ ' />'
							+ '<label style="width:' + (width - 20) + 'px" '
								+ 'onclick="$(\'' + htmlMSCId + '_globalSwitch\').checked = !$(\'' + htmlMSCId + '_globalSwitch\').checked;' + tmpOnclick + '">' + this.globalSwitch + '</label>'
							+ '<div style="clear:both;"></div>'
							+ '</div>'
						+ htmlOptions;
		}


		html += '<div class="MCP_control" style="width:' + width +  'px" id="' + htmlMSCId + '_select"  onmousedown="' + this.objName + '.toggle()">'
					+ '<div class="toggle"></div>'
					+ '<div style="padding:1px 3px;"><label>Tous les Flux</label></div>'
				+ '<div style="clear:both"></div>'
				+ '</div>';
		
		html += '<div class="MCP_optionsContainer" id="' + htmlMSCId + '_optionsContainer" style="width:' + (width + 2) + 'px" onmousedown="' + this.objName + '._s_clicked=true;">';
		html += htmlOptions;
		html += '</div>';

		html += '<div style="clear:both"></div>';
		html += '<div class="MCP_selectedView" id="' + htmlMSCId + '_selectedView"></div>';		
		html += '<input type="hidden" id="' + htmlMSCId + '" name="' + htmlMSCName + '" value="" />';
		
		
		Insertion.After( $(htmlSelectId), html);
		Element.remove( $(htmlSelectId) );

		// si globalSwitch à la création, on coche tout
		if (this.globalSwitchChecked)
		{
			MultipleSelectCheck_Controller.globalSwitch(this.objName);
		}

		this.updateMSCinput();
	}



	this.toggle = function()
	{
		if (this._s_opened) this.close();
		else this.open();
	}



	this.open = function()
	{
		MultipleSelectCheck_Controller.openStart();
		$(this.htmlMSCId + '_optionsContainer').style.display = 'block';
		MultipleSelectCheck_Controller.current = this;
		this._s_opened = true;
	}



	this.close = function()
	{
		$(this.htmlMSCId + '_optionsContainer').style.display = 'none';
		MultipleSelectCheck_Controller.current = null;
		this._s_opened = false;
		this.updateMSCinput();
	}



	this.documentClick = function()
	{
		if (!this._s_clicked)
		{
			this.close();
		}
		this._s_clicked = false;
	}

}


Event.observe(window, 'load', function() {
	MultipleSelectCheck_Controller.init();
});