
var souche = '/index.php';
if (document.location.href.indexOf('//localhost')>0)
	var souche = '/chauffage-discount/www/index.php';
if (document.location.href.indexOf('//10')>0)
	var souche = '/chauffage-discount/www/index.php';
if (document.location.href.indexOf('//ns3')>0)
	var souche = '/~chauffag/index.php';
if (document.location.href.indexOf('//87')>0)
	var souche = '/~chauffag/index.php';

(function($){
	$.fn.jbSlider = function(param){
		return this.each(function(){
			new jbSlider(this, param);
		});
	};
	var jbSlider = function(o, param){
		var obj = this;
		var obj_fond;
		var obj_fenetre;
		var config = {
			mignature: '',
			grand: '',
			photo: []
		};
		$.extend(config, param);

		$(o).click(function(ev){
			obj.init();
			ev.stopPropagation();
			return false;
		});

		this.init = function(){
			this.popup();
		};

		this.popup = function(){
			obj_fond = $('<div></div>').addClass('jbSliderBG');
			obj_fenetre = $('<div></div>').addClass('jbSlider');
			obj_fermer = $('<a></a>').attr('href','#').html('X Fermer').addClass('jbSliderFermer');
			obj_big = $('<ul></ul>');
			obj_small = $('<ul></ul>').addClass('jbsSmall');
			obj_fenetre.append($('<div></div>').addClass('jbsBig').append(obj_big)).append(obj_small).append(obj_fermer);

			$.each(config.photo, function(i, n){
				if (n != ''){
					obj_small.append(
						$('<li></li>').css('background-image', 'url('+config.mignature+n+')').addClass('o').append(
							$('<a></a>').attr('href', '#'+i).click(function(){
								obj._goto(i*600);
								return false;
							})
					));
					obj_big.append(
						$('<li></li>').append(
							$('<img>').attr('src', config.grand+n)
					));
				}
			});

			obj_fermer.click(function(){
				obj.close();
			});
			obj_fond.click(function(){
				obj.close();
			});

			$('body').append(obj_fond).append(obj_fenetre);
		};

		this._goto = function(o){
			obj_big.animate({'top': -o}, 500);
		};
		this.close = function(){
			obj_fond.hide().remove();
			obj_fenetre.hide().remove();
			return false;
		};
	};
})(jQuery);


(function($) {
	$.fn.jbcarousel = function(param){
		return this.each(function(){
			new jbcarousel(this, param);
		});
	};
	var jbcarousel = function(o, param)
	{
		var config = {
			timeout: 1,
			easing: 'swing',
			liste: false,
			largeur: 235,
			t: false
		};
		$.extend(config, param);

		$(o).width( ($('li', o).length * config.largeur) );

		$(o).mouseover(function(){
			clearTimeout(config.t);
		}).mouseout(function(){
			config.t = setTimeout(function(){ next(); }, config.timeout * 1000);
		});

		if (config.liste != false){
			$(config.liste).empty();
			for (var i = 0; i < $('li', o).length; i++){
				$(config.liste).append($('<li></li>'));
			};
			$('li:eq(0)', config.liste).addClass('activ');
			$('li', config.liste).each(function(i){
				$(this).click(function(){
					clearTimeout(config.t);
					next(i);
				});
			});
		}

		function next(pos){
			if (pos >= 0)
				var suite = -pos*config.largeur;
			else
				var suite = $(o).position().left - config.largeur;

			if ( suite <= -$(o).width() )
				suite = 0;

			$(o).animate({'left': suite});

			if (config.liste != false){
				$('li', config.liste).each(function(){
					$(this).removeClass('activ');
				});
				$('li', config.liste).eq( Math.abs(suite/config.largeur) ).addClass('activ');
			}

			config.t = setTimeout(function(){ next(); }, config.timeout * 1000);
		};

		config.t = setTimeout(function(){ next(); }, config.timeout * 1000);
	};
})(jQuery);

(function($) {
	$.fn.glisse = function(param){
		return this.each(function(){
			new glisse(this, param);
		});
	};
	var glisse = function(o, param){
		var pw = $(o).parent().width();
		var ow = $(o).innerWidth();
		$(o).animate({
			'left': 0-ow
		}, 15000, function(){
			$(this).css('left', pw).glisse();
		});
	};
})(jQuery);

(function($) {
	$.fn.jbonglet = function(param){
		return this.each(function(){
			new jbonglet(this, param);
		});
	};
	var jbonglet = function(o, param)
	{
		var config = {
			show: '',
			effect: '',
			listeDiv: [],
			minCarac: 0
		};
		$.extend(config, param);

		$('a', o).each(function(i){
			var name = $(this).css('outline','0').attr('href').substr(1);
			config.listeDiv[name] = $('a[name='+name+']').parent().css('display','none').get(0);

			if ($.trim($(config.listeDiv[name]).html()).length < config.minCarac)
				$(this).css('display','none');

			if (config.show == ''){
				config.show = name;
			}
			if (config.show == name){
				$('a[href=#'+config.show+']', o).addClass('activ');
				$(config.listeDiv[name]).css('display','block');
			}
		}).click(function(e){
			$('a[href=#'+config.show+']', o).removeClass('activ');
			var name = $(this).addClass('activ').attr('href').substr(1);
			$(config.listeDiv[config.show]).css('display','none');
			$(config.listeDiv[name]).css('display','block');
			config.show = name;
			e.stopPropagation();
			return false;
		});
	};
})(jQuery);

(function($){
	$.fn.jbRotator = function(param){
		return this.each(function(){
			new jbRotator(this, param);
		});
	};
	var jbRotator = function(o, paramsu){
		var params = $.extend({
			tempo: 10000
		},paramsu);

		var li = $('li', o);

		if (li.length < 2) return o;

		li.css('display', 'none');
		li.eq(0).css('display', 'block');

		setTimeout(function(){rotator(o, 0);}, params.tempo);

		function rotator(o, courent){
			var next = courent+1;
			if (li.length <= next){
				next = 0;
			}
			$('li:eq('+courent+')', o).fadeOut('slow', function(){
				$('li:eq('+next+')', o).fadeIn('slow');
			});
			//$('li:eq('+courent+')', o).css('display', 'none');
			
			setTimeout(function(){rotator(o, next);}, params.tempo);
		}

		return o;
	};
})(jQuery);

var radiateur = function(){
	var o = this;
	var prix = 0;
	var puissance = 0;
	var objTotal;
	var ztxt = $('<span></span>');

	this.init = function()
	{
		var commander = $('<a></a>').addClass('bouttong').addClass('rouge').html('Commander').click(function(){
			$('#radiateurListe td.radiateurQ').each(function(){
				var q = $(this).find('input[name^="q"]').val();
				if (q > 0){
					var inputP = $(this).find('input[name="pp"]').val();
					var inputI = $(this).find('input[name="pid"]').val();

					var confirme = false;
					panier.add(inputP, q, false, inputI);
				}
			});
			panier.msgPoursuivre();
		});
		objTotal = $('<div></div>').addClass('radiateurTotal').append(ztxt).append($('<span></span>').append(commander));

		$(window).scroll(function(){
			var parent = $('#radiateurListe');
			var parentTop = parent.append(objTotal).offset().top;
			var maxtop = parent.innerHeight()-objTotal.innerHeight()+40;
			top = $(window).height()-parentTop-objTotal.innerHeight()+$(window).scrollTop();
			if (top > maxtop) top = maxtop;
			objTotal.css('top', top);
		});

		o.actualisation();
		o.even();
	};

	this.afficher = function()
	{
		prix = Math.round(prix*100)/100;
		ztxt.html('Puissance total : '+puissance+'w - Prix total : '+prix.toString().replace('.',',')+'€ TTC');
	};

	this.actualisation = function()
	{
		prix = 0;
		puissance = 0;
		$('#radiateurListe td.radiateurQ').each(function(){
			var pr = $(this).find('input[name="pri"]').val();
			var pu = $(this).find('input[name="pui"]').val();
			var q = $(this).find('input[name^="q"]').val();

			prix = prix+(q*pr);
			puissance = puissance+(q*pu);
		});
		o.afficher();
	};

	this.even = function()
	{
		$('td.radiateurQ input[name^="q"]').keyup(function(){
			o.actualisation();
		});
	};

};

function jbconfirm(userParam){
	var obj = this;
	var obj_fenetre;
	var obj_fond;
	var obj_oui;
	var obj_non;

	var param = {
		msg: 'pas de message',
		oui: 'oui',
		non: 'non',
		ouiCallback: function(){},
		nonCallback: function(){}
	};
	$.extend(param, userParam);

	function init(){
		obj_fond = $('<div></div>').attr('id','popupBG');
		obj_fenetre = $('<div></div>').addClass('popup');
		obj_oui = $('<a></a>').addClass('boutton').attr('href','#');
		obj_non = $('<a></a>').addClass('boutton').attr('href','#');

		obj_oui.html(param.oui);
		obj_non.html(param.non);
		obj_fenetre.append($('<p></p>').html(param.msg));
		obj_fenetre.append($('<p></p>').addClass('c').append(obj_oui).append(obj_non));

		obj_fenetre.css('top', ($(window).height()/2-100)+'px');

		$('body').append(obj_fond);
		$('body').append(obj_fenetre);

		obj_oui.click(function(){
			obj.fermer(true);
			param.ouiCallback();
			return false;
		});
		obj_non.click(function(){
			obj.fermer(false);
			param.nonCallback();
			return false;
		});
		// Fermer quand clique sur le background.
		obj_fond.click(function(){
			obj.fermer(false);
			return false;
		});

		obj_fond.show();
		obj_fenetre.show();
		/*
		obj_fond.fadeIn(200);//.show();
		obj_fenetre.fadeIn(200);
		*/
	}

	this.fermer = function(reponce){
		obj_fond.hide().remove();
		obj_fenetre.hide().remove();
		return reponce;
	};

	init();
};

var AjaxPanier = function(){
	/**
	 * Ajout un produit au panier.
	 */
	this.add = function(idProduit, q, confirme, idEnfant)
	{
		var o = this;
		var numRef = 0;
		if (confirme == null)
			confirme = true;

		var des = souche+'/PanierAjax/add/?id_produit='+idProduit+'&q='+q;
		if (idEnfant)
			des = des+'&id_enfant='+idEnfant;

		$.ajaxSetup({
			async: false
		});

		$.getJSON(des, function(data){
			numRef = data.numRef;
			updatePanier(numRef);
			showAlready(idProduit);

			if (confirme){
				o.msgPoursuivre();
			}

			//pageTracker._trackEvent('Panier','AjoutPanier', data.nomproduit, '');
		});
	};

	this.msgPoursuivre = function()
	{
		jbconfirm({
			msg: 'Votre produit est bien enregistré dans votre panier voulez-vous poursuivre votre commande ?',
			oui: 'Voir mon panier',
			non: 'Poursuivre mes achats',
			ouiCallback: function(){
				document.location.href = souche+'/panier/';
			}
		});
	};

	/**
	 * Supprime un produit au panier.
	 */
	this.remove = function(idProduit, subRef)
	{
		var numRef = 0;
		var result = false;

		$.ajaxSetup({
			async: false
		});

		$.getJSON(souche+'/PanierAjax/remove/?id_produit='+idProduit+'&subref='+subRef, function(data){
			numRef = data.numRef;
			if (numRef < 1)
				window.location.reload();

			updatePanier(data.numRef);
			result = true;
			if (data.port == 0)
				$('.port').html('Gratuit');
			else
				$('.port').html(numberFormat(data.port));
			$('.total').html(numberFormat(data.total));
			//showAlready(idProduit);
		});

		if (numRef < 1)
			window.location.reload();

		return result;
	};

	/**
	 * Recuperation du panier.
	 */
	this.getPanier = function()
	{
		$.getJSON(souche+'/PanierAjax/getpanier/', function(data){
			updatePanier(data.numRef);
			if (data.numRef > 0){
				$.each(data.panier, function(i, n){
					showAlready(i);
				});
			}
		});
	};

	/**
	 * Change la quantite d'un produit dans le panier.
	 */
	this.updateQuantity = function (idProduit, q, subRef)
	{
		var reponse = '';
		$.ajaxSetup({async: false});
		$.getJSON(souche+'/PanierAjax/quantity/', {id_produit: idProduit, qt: q, subref: subRef}, function(data){
			data.prix = numberFormat(data.prix);
			data.port = numberFormat(data.port);
			data.total = numberFormat(data.total);
			reponse = data;
		});

		return reponse;
	};

	/**
	 * Mise a jour de l'affichage du panier sur le site.
	 */
	function updatePanier(numRef)
	{
		// Get panier.
		$('span.numArticles').html(numRef);
	}

	/**
	 * Masque le bouton ajouter au panier et affiche le lien ver le panier
	 */
	function showAlready(ref)
	{
		$('#deja'+ref+' a:eq(0)').css('display','none');
		$('#deja'+ref+' span.dejaCommande').css('display','block');
	}

	/**
	 * Format le prix en francais
	 */
	function numberFormat(num)
	{
		r = num.toString().replace('.',',');
		if (num > 100){
			var pos = r.indexOf(',');
			if (pos == -1){
				pos = r.length;
				r = r+',00';
			}
			r = r.substr(0, pos-3)+' '+r.substr(pos-3);
		}
		return r;
	}

};



//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};
jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};
jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};
jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null;};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x);},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t);};
	z.timer=setInterval(function(){z.step();},13);
};
