
	jQuery.extend({
	  getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
		  hash = hashes[i].split('=');
		  vars.push(hash[0]);
		  vars[hash[0]] = hash[1];
		}
		return vars;
	  },
	  getUrlVar: function(name){
		return decodeURIComponent(jQuery.getUrlVars()[name]);
	  }
	});
	
	jQuery(document).ready(function() {

		// Cycle HP
		if(jQuery('body.cms-home').length)
		{
			jQuery('div.col-main div.std')
				.before('<div id="pager" />')
				.cycle({
					pager: '#pager',
					pause: 1,
					fx: 'fade',
					timeout:  4000
				});
		}

		// Lien satisfait ou rembourse
		jQuery('div.header li.satisfait').hover(function() {
			jQuery('div.header div.satisfait').show();
		},
		function() {
			jQuery('div.header div.satisfait').hide();
		});

		jQuery('div.header div.satisfait').hover(function() {
			jQuery('div.header div.satisfait').show();
		},
		function() {
			jQuery('div.header div.satisfait').hide();
		});

		var timeoutPanier = null;
		function hidePanier()
		{
			jQuery('div.header div.cartdiv').hide()
		}

		// Lien panier
		jQuery('div.header li.panier').hover(function() {
			clearTimeout(timeoutPanier);
			jQuery('div.header div.cartdiv').show();
		},
		function() {
			timeoutPanier = setTimeout(hidePanier, 500);
		});

		jQuery('div.header div.cartdiv').hover(function() {
			clearTimeout(timeoutPanier);
			jQuery('div.header div.cartdiv').show();
		},
		function() {
			timeoutPanier = setTimeout(hidePanier, 500);
		});
		
		// Boutons OK
		jQuery('button.search-submit').hover(function() {
			jQuery(this).addClass('over');
		},
		function() {
			jQuery(this).removeClass('over');
		});
		
		jQuery('button.newsletter-submit').hover(function() {
			jQuery(this).addClass('over');
		},
		function() {
			jQuery(this).removeClass('over');
		});
		
		// Carroussel nv1
		if(jQuery('div.category-products.carroussel').length)
		{
			jQuery('div.category-products.carroussel').jcarousel();
		}
		
		if(jQuery('div.category-products.carroussel.carroussel-disable').length)
		{
			setTimeout(
				function() {
					jQuery('div.category-products.carroussel.carroussel-disable ul').css('margin', '0 auto');
					jQuery('div.category-products.carroussel.carroussel-disable div.jcarousel-prev').hide();
					jQuery('div.category-products.carroussel.carroussel-disable div.jcarousel-next').hide();
				},
				500
			);
		}
		
		// Déployer menu niveau 1
		if(jQuery('body.catalog-category-view').length)
		{
			if(!jQuery('ul.nav-left > li.active').length)
			{
				jQuery('ul.nav-left > li:first').addClass('active');
			}
		}
		
		// Carroussel over prev/next
		jQuery('div.category-products.carroussel div.jcarousel-prev').hover(function() {
			jQuery(this).addClass('over');
		},
		function() {
			jQuery(this).removeClass('over');
		});
		
		jQuery('div.category-products.carroussel div.jcarousel-next').hover(function() {
			jQuery(this).addClass('over');
		},
		function() {
			jQuery(this).removeClass('over');
		});
		
		// Colorbox fiche détail
		jQuery("a[rel='colorbox']").colorbox({opacity:0.60,  scrolling:false, title:false, scalePhotos:true, maxWidth:'700px', maxHeight:'700px',  previous:'précédent', next:'suivant', close:'Fermer', current: ''});

		// Carroussel fiche détail
		if(jQuery('#mycarousel').length)
		{
			jQuery('#mycarousel').jcarousel();
		}

		jQuery('#email').focus(function() {
			if(jQuery(this).val() == 'votre e-mail')
				jQuery(this).val('');
		})

		jQuery('#email').blur(function() {
			if(jQuery(this).val() == '')
				jQuery(this).val('votre e-mail');
		})
		
		if(jQuery('#email2').length)
		{
			var getEmail = jQuery.getUrlVar('email');
			if(getEmail && getEmail != 'undefined')
				jQuery('#email2').val(getEmail);
		}

		var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
		if(IE6) {
			IE6 = (navigator.userAgent.indexOf("MSIE 8")>=0) ? false : true;
		}
		if(IE6){
			jQuery("<div class=\"ie6-bg\">")
				.css({
					'position': 'absolute',
					'top': '0px',
					'left': '0px',
					backgroundColor: 'black',
					'opacity': '0.75',
					'width': '100%',
					'height': jQuery(window).height(),
					'cursor': 'pointer',
					zIndex: 9999
				})
				.appendTo("body")
				.click(function() {
					jQuery('div.ie6-bg').hide();
					jQuery('div.ie6-msg').hide();
				});

			var d = "<div class=\"ie6-msg\"><img src='/skin/frontend/mohair/theme/images/logo_email.jpg' alt='' style='float: left;'/><p>Attention !</p>";
			d += "<p>Pour des raisons de sécurité, votre navigateur (Internet Explorer 6) n'est pas compatible avec ce site Internet.</p>";
			d += "<p>Pour pouvoir visiter le site de la Ferme du Mohair, merci d'utiliser un navigateur récent ";
			d += "(<a href=\"http://www.microsoft.com/france/windows/internet-explorer/telecharger-ie8.aspx\">Internet Explorer 8</a>, ";
			d += "<a href=\"http://www.mozilla-europe.org/fr/firefox/\">Firefox 3</a>, ";
			d += "<a href=\"http://www.google.com/chrome\">Chrome,</a> ";
			d += "<a href=\"http://www.apple.com/fr/safari/\">Safari</a>,...)</p>";

			jQuery(d)
				.css({
					backgroundColor: 'white',
					'top': '50%',
					'left': '50%',
					marginLeft: -300,
					marginTop: -60,
					width: 620,
					padding: 10,
					height: 105,
					fontSize: '13px',
					'position': 'absolute',
					zIndex: 9999
				})
				.appendTo("body");
		}
		
		var d = new Date();
		var j = d.getDate();
		var m = d.getMonth() + 1;
		var ajd = (j < 10 ? '0' + j : j);
		ajd += (m < 10 ? '/0' + m : '/' + m);
		ajd += '/' + d.getFullYear();
		jQuery('#customdate1').val(ajd);
		
		/* Catalogue */
		/*jQuery('ul.products-grid li').hover(
			function() {
				var t = jQuery('div.more-info', this).data('timeout');
				if(typeof t != 'undefined' || t != null)
				{
					t = setTimeout(
						function($obj) {
							$obj.show();
							jQuery('div.bordure', $obj)
								.animate(
									{ width: '325px' },
									150
								);
							$obj.data('timeout', null);
						},
						400,
						jQuery('div.more-info', this)
					);
					jQuery('div.more-info', this).data('timeout', t);
				}
			},
			function() {
				var t = jQuery('div.more-info', this).data('timeout');
				if(typeof t == 'undefined' || t == null)
				{
					jQuery('div.more-info div.bordure', this)
						.animate(
							{ width: '142px' },
							10,
							function() {
								jQuery(this).parent().hide();
							}
						);
				}
				else
				{
					clearTimeout(t);
					t = null;
					jQuery('div.more-info', this).data('timeout', t);
				}
			}
		);*/
			
		jQuery('ul.products-grid li').hover(
			function() {
				jQuery(this).addClass('dessus');
				
				jQuery('div.more-info', this).delay(100).show();
				//jQuery('div.more-info', this).delay(400).fadeIn('1');
				jQuery('div.bordure', this).delay(100).animate(
					{ width: '325px' },
					100
				);
			},
			function() {
				jQuery(this).removeClass('dessus');
				
				jQuery('div.more-info', this).clearQueue();
				jQuery('div.bordure', this).clearQueue();
				
				jQuery('div.more-info div.bordure', this)
					.animate(
						{ width: '142px' },
						10,
						function() {
							jQuery(this).parent().hide();
						}
					);
			}
		);
	});

	/* Dolist begin */
	function VerifForm (f){
		var VerifMail = ControlChamp(document.getElementById((f == 'AbonnementDOLIST2' ? 'email2' : 'email')),'email');
		//var VerifDate = ControlChamp(document.getElementById('birthdate'),'date');
		var VerifDate=true;
		if ((!VerifMail)||(!VerifDate)) {
			alert ('Vous n\'avez pas rempli correctement le formulaire');
		}
		else {
			document.getElementById(f).submit();
		}
	}

	function ControlChamp(obj,typ){
		switch (typ){
			case 'email':
				var Regex = new RegExp('^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$');
				break;
			case 'date':
				var Regex = new RegExp('^(([1-9]|0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/([0-9][0-9]|[0-9][0-9][0-9][0-9]))?$');
				break;
		}
		var Result = Regex.test(obj.value);
		return Result;
	}
	/* Dolist end */


	if(typeof Product=='undefined') {
		var Product = {};
	}

	Product.ConfigPalier = Class.create();
	Product.ConfigPalier.prototype = {
		initialize: function(config, productId){
			//alert('a');
			this.config     = config;
			this.taxConfig  = this.config.taxConfig;
			this.settings   = $$('.select-palier-' + productId);
			this.state      = new Hash();

			this.settings.each(function(element){
				Event.observe(element, 'change', this.configure.bind(this))
			}.bind(this));

			// fill state
			this.settings.each(function(element){
				//var attributeId = element.id.replace(/[a-z]*/, '');
				var attributeId = element.id.substr(element.id.lastIndexOf('-') + 1);
				if(attributeId && this.config.attributes[attributeId]) {
					element.config = this.config.attributes[attributeId];
					element.attributeId = attributeId;
					this.state[attributeId] = false;
				}
			}.bind(this))

			// Init settings dropdown
			var childSettings = [];
			for(var i=this.settings.length-1;i>=0;i--){
				var prevSetting = this.settings[i-1] ? this.settings[i-1] : false;
				var nextSetting = this.settings[i+1] ? this.settings[i+1] : false;
				if(i==0){
					this.fillSelect(this.settings[i])
				}
				else {
					this.settings[i].disabled=true;
				}
				$(this.settings[i]).childSettings = childSettings.clone();
				$(this.settings[i]).prevSetting   = prevSetting;
				$(this.settings[i]).nextSetting   = nextSetting;
				childSettings.push(this.settings[i]);
			}

			// try retireve options from url
			var separatorIndex = window.location.href.indexOf('#');
			if (separatorIndex!=-1) {
				var paramsStr = window.location.href.substr(separatorIndex+1);
				this.values = paramsStr.toQueryParams();
				this.settings.each(function(element){
					var attributeId = element.attributeId;
					element.value = (typeof(this.values[attributeId]) == 'undefined')? '' : this.values[attributeId];
					this.configureElement(element);
				}.bind(this));
			}
		},

		configure: function(event){
			var element = Event.element(event);
			this.configureElement(element);
		},

		configureElement : function(element) {
			this.reloadOptionLabels(element);
			if(element.value){
				this.state[element.config.id] = element.value;
				if(element.nextSetting){
					element.nextSetting.disabled = false;
					this.fillSelect(element.nextSetting);
					this.resetChildren(element.nextSetting);
				}
			}
			else {
				this.resetChildren(element);
			}
		},

		reloadOptionLabels: function(element){
			for(var i=0;i<element.options.length;i++){
				if(element.options[i].config){
					element.options[i].text = this.getOptionLabel(element.options[i].config);
				}
			}
		},

		resetChildren : function(element){
			if(element.childSettings) {
				for(var i=0;i<element.childSettings.length;i++){
					element.childSettings[i].selectedIndex = 0;
					element.childSettings[i].disabled = true;
					if(element.config){
						this.state[element.config.id] = false;
					}
				}
			}
		},

		fillSelect: function(element){
			//var attributeId = element.id.replace(/[a-z]*/, '');
			var attributeId = element.id.substr(element.id.lastIndexOf('-') + 1);
			var options = this.getAttributeOptions(attributeId);
			this.clearSelect(element);
			element.options[0] = new Option(this.config.chooseText, '');

			var prevConfig = false;
			if(element.prevSetting){
				prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
			}

			if(options) {
				var index = 1;
				for(var i=0;i<options.length;i++){
					var allowedProducts = [];
					if(prevConfig) {
						for(var j=0;j<options[i].products.length;j++){
							if(prevConfig.config.allowedProducts
								&& prevConfig.config.allowedProducts.indexOf(options[i].products[j])>-1){
								allowedProducts.push(options[i].products[j]);
							}
						}
					} else {
						allowedProducts = options[i].products.clone();
					}

					if(allowedProducts.size()>0){
						options[i].allowedProducts = allowedProducts;
						element.options[index] = new Option(this.getOptionLabel(options[i]), options[i].id);
						element.options[index].config = options[i];
						index++;
					}
				}
			}
		},

		getOptionLabel: function(option){
			var str = option.label;
			return str;
		},

		clearSelect: function(element){
			for(var i=element.options.length-1;i>=0;i--){
				element.remove(i);
			}
		},

		getAttributeOptions: function(attributeId){
			if(this.config.attributes[attributeId]){
				return this.config.attributes[attributeId].options;
			}
		}
	}
