/**
 * => Mootools 1.2
 * 	=> Core
 * @classDescription
 * Classe Helper générale
 * @author M@nu/Baphira
 */
var Helper = new Class({

	CLASS_ATTRIBUTES: 'product_option',

	initialize: function(){

    },
	
	/**
	 * Redirige vers le lien (s'il existe) de l'élément <a>.
	 * Si le lien commence par "javascript:", le script est exécuté.
	 * @param {Element} el l'élément <a>
	 */
	performHrefLink: function(el) {
		if (el.get('tag') != 'a') {
			throw "the tag of element must be <a>";
		} else {
			var href = el.getProperty('href');
			if ($chk(href)) {
				if (href.test('javascript:')) {
					eval(href.split('javascript:')[1]);
				}
				else {
					document.location.href = href;
				}
			}
		}
	},
	
	submitSelectOnChange: function(id) {
		window.addEvent('domready', function(){
			$(id).addEvent('change', function() {
				if(this.get('value').length > 0) {
					this.form.submit();
				}
			});
		});
	},
	
	/**
	 * Récupère l'objet "liste des attributs"
	 * @param {Element} form
	 * @return {Object} attributes
	 * 	[{
	 * 		idAttr: '1', idAttrValue: '11'
	 * 	}, {
	 * 		idAttr: '2', idAttrValue: '22'
	 * 	}]
	 */
	getAttributes: function(form) {
		var attributes = new Array();
		var attributesElt = form.getElements('.'+this.CLASS_ATTRIBUTES);
		//@see AttributesChecker
		//@see AvailabilityChecker
		for(var i = 0; i < attributesElt.length; i++) {
			var attribute = new Object();
			attribute.idAttr = attributesElt[i].id.split(this.CLASS_ATTRIBUTES)[1];
			attribute.idAttrValue = attributesElt[i].get('value');
			attributes.push(attribute);
		}
		return attributes;
	},
	
	/**
	 * Récupère les attributs en fonction de l'id produit (ex 8{4}50{6})
	 * @param {String} id
	 * @return {Object} attributes
	 * 	[{
	 * 		idAttr: '1', idAttrValue: '11'
	 * 	}, {
	 * 		idAttr: '2', idAttrValue: '22'
	 * 	}]
	 */
	getAttributesById: function(id) {
		var attributes = new Array();
		var numbers = id.replace('{','-').replace('}','-').split('-');
		for(var i = 1; i < numbers.length; i+=2) {
			var attribute = new Object();
			attribute.idAttr = numbers[i];
			attribute.idAttrValue = numbers[i+1];
			attributes.push(attribute);
		}
		return attributes;
	},
	
	/**
	 * Assigne une cible à un évenement
	 * @param {Event} event
	 * @param {Element} el
	 * @see getTarget()
	 */
	setTarget: function(event, el){
		event.realTarget = el;
	},
	
	/**
	 * Récupère la cible de l'évenement
	 * @param {Event} event
	 * @see setTarget()
	 * @return {Object} target
	 */
	getTarget: function(event){
		var target = event.target || event.srcElement;
		return $chk(event.realTarget)? event.realTarget : target;
	},
	
	/**
	 * Précharge des images
	 * @param {String[]} src
	 */
	preloadImage: function(srcs) {
		window.addEvent('domready', function(){
			srcs.each(function(src) {
				new Element('img', {'src':src, 'style':'display:none'}).inject(document.body);
			});
		});
	}
	
});