/**
 * SingleSelectable (v. 1.1)
 * Sept 10, 2007
 * 
 * author: Henry Ho (henryho167@gmail.com)
 * 
 * SingleSelectable is inspired by Selectable in Interface 1.2, 
 * which was broken/unusable. This class can only select one item at a time.
 * 
 *  IE6 (and 7?) Compatibility problem: 
 *  	do not use <UL> or <OL> 
 *  	since IE doesn't catch mousedown event for <UL> or <OL>
 * 
 *
 *   params
 * 
 * accept			-	string	required	the class name for items inside container
 * selectedclass	-	string	optional	when an acceptable item is selected gets this class
 * hoverclass		-	string	optional	the class applied to the hovered item
 * onselect			-	fn(id)	optional	callback that get selectId as argument
 * ondeselect		-	fn(id)	optional	callback that get deselectId as argument
 * 
 *
 */

(function($)
{
	// namespace for storing var for SingleSelectable in jQuery
	$.SingleSelectable = 
	{
		selectedId:	{},		// associative array for storing selectedId by parentId
		param:		{},		// associative array for storing params by parentId

		addHoverHandle: function(thisItem, parentId)
		{
			var param = $.SingleSelectable[parentId];
			 
			// add & remove hoverclass when hoverclass is defined  
			if (param.hoverclass)
			{
				var over = function() 
				{
					if (thisItem.attr("id") !== $.SingleSelectable.selectedId[parentId])
						thisItem.addClass(param.hoverclass);
				}
				
				var out = function() 
				{
					thisItem.removeClass(param.hoverclass);
				}

				thisItem.hover(over, out);
			}
		},
		
		addMousedownHandle: function (thisItem, parentId)
		{
			var param = $.SingleSelectable[parentId];
			
			thisItem.mousedown(function() {
				var lastSelectedId = $.SingleSelectable.selectedId[parentId];
				var thisId = thisItem.attr("id");
				
				// do something only when a new element is selected
				if (thisId !== lastSelectedId)
				{
					$.SingleSelectable.selectedId[parentId] = thisId;
					saveSelect(parentId,thisId);// renvoie id du bloc selectionn� produit_1_color_1
					
					
					if (param.hoverclass) 
						thisItem.removeClass(param.hoverclass);

					if (param.selectedclass)
					{
						$('#' + lastSelectedId).removeClass(param.selectedclass);
						thisItem.addClass(param.selectedclass);
					}
					
					// invoke ondeselect
					// setTimeout(fn(), 0) = yield to UI
					if (typeof param.ondeselect === "function")
						setTimeout(function() {
							param.ondeselect(lastSelectedId)}, 0);
					
					// invoke onselect
					// setTimeout(fn(), 0) = yield to UI
					if (typeof param.onselect === "function")
						setTimeout(function() {
							param.onselect(thisId)}, 0);
				}
			});
		},
		
		add: function (thisItem, parentId)
		{
			$.SingleSelectable.addHoverHandle(thisItem, parentId);
			$.SingleSelectable.addMousedownHandle(thisItem, parentId);
		}
	};
	
	// a function to dynamically add items that is SingleSelectable
	// must called $(...).SingleSelectable first
	$.SingleSelectableAddItem = function(thisItem, parentId)
	{
		$.SingleSelectable.add(thisItem, parentId);
	}
	
	$.fn.SingleSelectable = function(param)
	{
		if (typeof param !== "object" || typeof param.accept !== "string")
			throw "'accept' parameter is Required.";

		return $(this).each(function () 
		{
			// key to store the selected key at: $.SingleSelectable[parentId]
			var parentId = $(this).attr("id");
			
			$.SingleSelectable[parentId] = param;
			$('#' + parentId + ' .' + param.accept)
				.each(function() {
					$.SingleSelectable.add($(this), parentId);
				});
		});
	};
	
	
})(jQuery);



// function pour remplir un hidden avec la valeur sélectionnée 
function saveSelect(parentId,thisId){
	var regexp = /^produit_([0-9]+)_color_([0-9]+)$/;
	regexp.exec(thisId);
	id_produit	= RegExp.$1;
	id_color	= RegExp.$2;

	// alert(id_selected);
	// on met à jour le champ hidden avec l'id 'produit_1_color_id'
	document.getElementById(parentId+'_value').value=id_color;
	// Mise à jour des tailles en fonction de la couleur
	formProduit = document.forms['produit_'+id_produit];
	// alert('Nbr option : '+formProduit.taille.options.length+', Nbr tailles :'+tailles[id_color].length+ ', Tailles : ' + tailles[id_color][0] + ", form " + document.forms['produit_'+id_produit].name);
	valueSelected = formProduit.taille.value;
	formTaille = $(formProduit.taille);
	formTaille.empty();

	for (i = 0; i < tailles[id_produit][id_color].length; i++) {
		if (tailles[id_produit][id_color][i].optState)
		{
			o = $(document.createElement('option'));
			o.attr('value', tailles[id_produit][id_color][i].optValue);
			o.text(tailles[id_produit][id_color][i].optText);
			if (tailles[id_produit][id_color][i].optValue == valueSelected) {
				o.attr('selected', true);
			}
		}
		else
		{
			o = $(document.createElement('optgroup'));
			o.attr('label', tailles[id_produit][id_color][i].optText);
			o.addClass('option-disabled');
		}
		formTaille.append(o);
	}
}
