﻿
window.addEvent('domready', function() {
	var UIEl = new UIElements();
});

var UIElements = new Class({
	initialize: function() {
		// Listes de choix
		var dropDownLists = $$('.UI_DropDownList');
		var dropDownListsTogglers = $$('.UI_DropDownListToggler');
		dropDownLists.each(function(item, index) {
			var UIddl = new UIElements.DropDownList(dropDownListsTogglers[index], item);
		});
	}
});

UIElements.DropDownList = new Class({
	initialize: function(toggler, element) {
		this.toggler = toggler;
		this.element = element;
		this.setMinWidth();
		this.hide();
		var thisDropDownList = this;
		// Evenements
		this.toggler.addEvent('click', function(e) {
			e.stop();
			thisDropDownList.toggle();
		});
		/*this.element.addEvent('click', function(e) { 
		e.stop();
		});*/
		var closer = this.element.getElement('.UI_DropDownListCloser');
		if (closer) {
			closer.addEvent('click', function() {
				thisDropDownList.hide();
			});
		}
	},
	setMinWidth: function() {
		var minWidth = this.toggler.getStyle('width').toInt() + this.toggler.getStyle('padding-left').toInt() + this.toggler.getStyle('padding-right').toInt();
		var elWidth = this.element.getStyle('width').toInt() + this.element.getStyle('padding-left').toInt() + this.element.getStyle('padding-right').toInt();

		if (elWidth < minWidth) {
			this.element.setStyle('width', minWidth);
		}
	},
	toggle: function() {
		if (this.element.getStyle('display') == 'none')
			this.show();
		else
			this.hide();
	},
	show: function() {
		/*var thisDropDownList = this;
		$(document).addEvent('click', function(e) { 
		thisDropDownList.hide();
		});*/
		this.element.setStyle('display', 'block');
	},
	hide: function() {
		this.element.setStyle('display', 'none');
	}
});


function changePicture(thumb, normal, iPictureZoom)
{
	if($chk(thumb)) {
		
		var regThumb = new RegExp("/Thumb/", "g");
		var regNormal = new RegExp("/Normal/", "g");

		var link = $(iPictureZoom);
		
		var srcThumb =  thumb.src;
		var srcNormal = $(normal).src;
		
		if ($chk(srcNormal) && $chk(srcThumb) && $chk(link)) {
			$(normal).src = srcThumb.replace(regThumb, "/Normal/");
			$(iPictureZoom).src = srcThumb.replace(regThumb, "/Real/");
		}
	}
}

function displayCategory() {
	var catChild = $$('.categorySecond');
	
	if(catChild.getStyle('display') == 'none')
	    catChild.setStyle('display', 'block');
    else
	    catChild.setStyle('display', 'none');
}

function displayCategoryParent() {
	var catRoot = $$('.categoryFirst');
	if (catRoot.getStyle('display') == 'none')
		catRoot.setStyle('display', 'block');
	else
	    catRoot.setStyle('display', 'none');
}


function SetUniqueRadioButton(nameregex, current) {
	re = new RegExp(nameregex);
	for (i = 0; i < document.forms[0].elements.length; i++) {
		elm = document.forms[0].elements[i]
		if (elm.type == 'radio') {
			if (re.test(elm.name)) {
				elm.checked = false;
			}
		}
	}
	current.checked = true;
}
