function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    // discuss at: http://phpjs.org/functions/number_format
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +     input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +     improved by: davook
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Jay Klehr
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *     example 10: number_format('1.20', 2);
    // *     returns 10: '1.20'
    // *     example 11: number_format('1.20', 4);
    // *     returns 11: '1.2000'
    // *     example 12: number_format('1.2000', 3);
    // *     returns 12: '1.200'
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

/**
 * Met a jour les données taille dans le selecteur fix
 * @param t object contenant les stocks et leurs tailles
 * @return
 */
function updateTailles(t)
{

		$('#tfixwrap').empty()
		for(key in t){
			var stock = t[key];
			$('#tfixwrap').append('<div class="tailleSelectorOffFix" id="tsf_' + stock.sid + '">' + stock.label + '</div>')
			
			$('#htailleselectFix').val('');
			taille_selected_fix = false;
			$('#selectedtailleFix').html('');
		}
	
}

$(document).ready(function() {
	$('#textpanel').tabs();
	$('.sizeselector').click(function(){
		taille_selected = true;
		$('#tailleAlert').hide();
	});

	$('#lastlink').click(function(){
		$('#assoctab').hide();
		$('#lasttab').show();
	});
	$('#crosslink').click(function(){
		$('#assoctab').show();
		$('#lasttab').hide();
	});

	$('.restorelinks').click(function(){
		$('#assoctab').show();
		$('#lasttab').show();
	});


	var pid;
	$('.minithumb a').bind('mouseover', function(i){
		pid = this.id.split('_').pop();
		var curImg = jpics[pid];
		var newmidsrc = curImg.med;
		var newbigsrc = curImg.full;

		scale = curImg.scale;
		medholdersize = curImg.medholdersize;
		$('#thetarget').attr('src', newmidsrc);
		$('#zoom').attr('src',newbigsrc);
	});

	var tpos = $('#mainimage').position();

	//$('#tpos').html(tpos.left + "," + tpos.top);
	var bpos = {
		top: parseInt(tpos.top) + parseInt(medholdersize),
		left:parseInt(tpos.left) +parseInt(medholdersize)
	};

	var tx = Math.floor(300/scale);
	var tx2= Math.floor(tx/2);
	var tileH, tileW, tileLeft, tileTop;

	$('#detailBox').mousemove(function(pos){
		if ((tpos.left < pos.pageX) && (pos.pageX < bpos.left) && (tpos.top < pos.pageY) && ( pos.pageY < bpos.top )){

			tileH = tx;
			tileW = tx;
			tileTop = pos.pageY - tx2 - tpos.top + $('#mainimage').scrollTop();
			tileLeft= pos.pageX - tx2 - tpos.left;
			$('#tile').css({
				height: tileH,
				width: tileW,
				top: tileTop,
				left: tileLeft,
				display: ''
				});

			$('#zoomer').scrollLeft( scale * (pos.pageX-tx2-tpos.left));
			$('#zoomer').scrollTop (scale * ((pos.pageY+$('#mainimage').scrollLeft()) - tx2 - tpos.top) ) ;


		} else {
			$('#tile').css("display", 'none');
			$('#infos').css('visibility', 'visible');
			$('#zoomer').css('display', 'none');
			return;
		}
	});

	$('#thetarget').hover(
		function(e){
			//$('#tile').css('display', '');
			$('#zoomer').css('display', '');
			$('#infos').css('visibility', 'hidden');
		}, function(e){
			//
		});
	
	
	/** Gestion des switch fixation**/
	$('#rFix').click(function(){
		var source = children_datas[right];
		var target = children_datas[main];
		var tmpright = right;
		var tmpmain  = main;

		$('#mMarque').html(source.marque);
		$('#mDesign').html(source.design);
		$('#mPrix').html(source.prix + ' &euro;');
		$('#mainFixImg').attr('src', source.image);
		
		$('#topMarque').html(source.marque);
		$('#topDesign').html(source.design);
		
		$('#rMarque').html(target.marque);
		$('#rDesign').html(target.design);
		$('#rPrix').html(target.prix + ' &euro;');
		$('#rcImg').attr('src', target.image);
		
		$('#fixchildid').val(source.id);
		
		var packPrice = parseFloat(source.fprixpack) + parseFloat(parentPackPrice);
		var packCatPrice = parseFloat(source.fprix) + parseFloat(parentPrice);
		
		$('#packPrice').html(number_format(packPrice, 2, ',', ''));
		$('#packCatPrice').html(number_format(packCatPrice, 2, ',', ''));
		
		//On switch les avis et tech
		$('#detailhafix').html(source.tech);
		$('#avishafix').html(source.avis);
		
		updateTailles(source.tailles);
		
		right=tmpmain;
		main=tmpright;
		
	});

	$('#lFix').click(function(){
		var source = children_datas[left];
		var target = children_datas[main];
		
		///console.debug(source);
		///console.debug(target);
		
		var tmpleft = left;
		var tmpmain  = main;

		$('#mMarque').html(source.marque);
		$('#mDesign').html(source.design);
		$('#mPrix').html(source.prix + ' &euro;');
		$('#mainFixImg').attr('src', source.image);
		
		$('#topMarque').html(source.marque);
		$('#topDesign').html(source.design);
		
		$('#lMarque').html(target.marque);
		$('#lDesign').html(target.design);
		$('#lPrix').html(target.prix + ' &euro;');
		$('#lcImg').attr('src', target.image);
		
		$('#fixchildid').val(source.id);
		
		//console.debug('source fprix: %s', source.fprix);
		//console.debug('source fprix pack: %s', source.fprixpack);
		//console.debug('parentPackPrice: %s', parentPackPrice);
		//console.debug('parentPrice: %s', parentPrice);
		
		var packPrice = parseFloat(source.fprixpack) + parseFloat(parentPackPrice);
		var packCatPrice = parseFloat(source.fprix) + parseFloat(parentPrice);
		
		//console.debug(packPrice);
		//console.debug(packCatPrice);
		
		$('#packPrice').html(number_format(packPrice, 2, ',', ''));
		$('#packCatPrice').html(number_format(packCatPrice, 2, ',', ''));
		
		$('#detailhafix').html(source.tech);
		$('#avishafix').html(source.avis);
		
		left=tmpmain;
		main=tmpleft;
		
		updateTailles(source.tailles);
	});
	
	
/** Ajout au panier: Alerte*/

$('#addbt').click(function(){
	if (!taille_selected_board || !taille_selected_fix){
		return false;
	}
});

$('#addbt').hover(function(){
	if (!taille_selected_board || !taille_selected_fix){
		var pos = $(this).position();
		$('#avertissement').css({
			top: pos.top -25,
			left: pos.left - 163,
			display: ''
		})
	}
},function(){
	$('#avertissement').hide();
});


//Gestion de l'ajout au comparateur
$('#addcompare').click(function(){

    $.ajax({
        'url': '/catalogue/addtocompare',
        'data': 'prd='+prdid + '&cid=' + cid,
		'success' : function(ret){
			if (ret == 1){
				Growl.show('Article ajout&eacute; au comparateur.');
			}
		}
    });
	
	
    
});

//Remplacement des selects taille par un seul hidden et les div affichÈs
var addHiddenSizeBoard = false;
var addHiddenSizeFix = false;

$('.tailleradioBoard').each(function(){
	var chelements = $(this).children();
	imp = chelements[0];
	lbl = chelements[1];
	$(this).parent().append('<div class="tailleSelectorOffBoard" id="tsb_' + imp.value + '">' + $(lbl).html() + '</div>');
	$(this).remove();
	addHiddenSizeBoard = true;
});

$('.tailleradioFix').each(function(){
	var chelements = $(this).children();
	imp = chelements[0];
	lbl = chelements[1];
	$(this).parent().append('<div class="tailleSelectorOffFix" id="tsf_' + imp.value + '">' + $(lbl).html() + '</div>');
	$(this).remove();
	addHiddenSizeFix = true;
});


if(addHiddenSizeBoard){
	
	$('#taillesBoard').append('<input type="hidden" id="htailleselectBoard" name="tailleboard" value="" />');
	
	$('.tailleSelectorOffBoard').live('click',function(){
		if ($(this).hasClass('tailleSelectorOnBoard')){
			$(this).addClass('tailleSelectorOffBoard').removeClass('tailleSelectorOnBoard');
			$('#htailleselectBoard').val('');
			taille_selected_board = false;
			$('#selectedtailleBoard').html('');
		} else {
			$(this).addClass('tailleSelectorOnBoard').removeClass('tailleSelectorOffBoard');
			$(this).siblings().removeClass('tailleSelectorOnBoard').addClass('tailleSelectorOffBoard');
			var stockid = this.id.split('_').pop();
			$('#htailleselectBoard').val(stockid);
			taille_selected_board = true;
			$('#selectedtailleBoard').html($(this).html());
		}

	});
	}


if(addHiddenSizeFix || fakeFixSize){
	
	$('#taillesFix').append('<input type="hidden" id="htailleselectFix" name="taillefix" value="" />');
	
	$('.tailleSelectorOffFix').live('click',function(){
		if ($(this).hasClass('tailleSelectorOnFix')){
			$(this).addClass('tailleSelectorOffFix').removeClass('tailleSelectorOnFix');
			$('#htailleselectFix').val('');
			taille_selected_fix = false;
			$('#selectedtailleFix').html('');
		} else {
		
			$(this).addClass('tailleSelectorOnFix').removeClass('tailleSelectorOffFix');
			$(this).siblings().removeClass('tailleSelectorOnFix').addClass('tailleSelectorOffFix');
			var stockid = this.id.split('_').pop();
			$('#htailleselectFix').val(stockid);
			taille_selected_fix = true;
			$('#selectedtailleFix').html($(this).html());
		}

	});
	
	
	$('.tailleSelectorOnFix').live('click',function(){
		if ($(this).hasClass('tailleSelectorOnFix')){
			$(this).addClass('tailleSelectorOffFix').removeClass('tailleSelectorOnFix');
			$('#htailleselectFix').val('');
			taille_selected_fix = false;
			$('#selectedtailleFix').html('');
		} else {
			$(this).addClass('tailleSelectorOnFix').removeClass('tailleSelectorOffFix');
			$(this).siblings().removeClass('tailleSelectorOnFix').addClass('tailleSelectorOffFix');
			var stockid = this.id.split('_').pop();
			$('#htailleselectFix').val(stockid);
			taille_selected_fix = true;
			$('#selectedtailleFix').html($(this).html());
		}

	});
	}

});
