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

	CLASS_ATTRIBUTES: 'product_option',

	initialize: function(){

    },
	
	/**
	 * Affiche une boîte de dialogue de confirmation si l'utilisateur ferme la fenêtre de son navigateur
	 * @param {String} msg
	 */
	confirmOnCloseDocument: function(msg) {
		window.addEvent('domready', function() {
			window.onbeforeunload = function(e) {return msg;};
			$$('a').each(function(el) {
				el.addEvent('click', function(e) {
					window.onbeforeunload = null;
				});
			})
			$$('form').each(function(el) {
				el.addEvent('submit', function(e) {
					window.onbeforeunload = null;
				});
			});
		});
	},
	
	/**
	 * Affiche une boîte de dialogue de confirmation si l'utilisateur ferme la fenêtre de son navigateur
	 * OU quitte la procédure de commande
	 * @param {String} msg
	 */
	confirmOnExitCheckout: function(msg) {
		window.addEvent('domready', function() {
			window.onbeforeunload = function(e) {return msg;};
			$$('#pageContent a').each(function(el) {
				el.addEvent('click', function(e) {
					window.onbeforeunload = null;
				});
			})
			$$('#pageContent form').each(function(el) {
				el.addEvent('submit', function(e) {
					window.onbeforeunload = null;
				});
			});
		});
	},
	
	/**
	 * 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('tag') == 'select' ? attributesElt[i].get('value') : this.getRadioValue(attributesElt[i]);
			attributes.push(attribute);
		}
		return attributes;
	},
	
	/**
	 * Renvoie la valeur d'un radio button ou -1 si aucun bouton n'est coché
	 * @param {Object} firstRadio
	 * @return {String} value
	 */
	getRadioValue: function(firstRadio) {
		var radios = firstRadio.form.getElements("input[name='"+firstRadio.getProperty('name')+"']");
		for(var i = 0; i < radios.length; i++) {
			if(radios[i].checked) {
				return radios[i].value;
			}
		}
		return -1;
	},
	
	/**
	 * 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(/{|}/g,'-').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);
			});
		});
	}
	
});