var iniText = new Array();
var nbalert = 0;
$(document).ready(function() {
	// do stuff when DOM is ready
	/*$("#newsscroll").jCarouselLite({
		hoverPause:true,
		visible: 2,
		auto:5000,
		speed:1000
	});*/
	
	if($('#ProduitListe').length>0){
	 $('body').UItoTop({ text: 'Top', min: 200, scrollSpeed: 1000, easingType: 'easeInOutExpo'});
	}
	
	initFields();
	initPanier();
	
	//$( 'textarea#texte_1, textarea#texte_2').ckeditor(function() { /* callback code */ }, { toolbar : 'MyToolbar'} );
	if(document.getElementById("texte_1")!=null){
		var oFCKeditor = new FCKeditor('texte_1');
		oFCKeditor.BasePath = "/templates/default/js/fckeditor/";
		oFCKeditor.ReplaceTextarea();
	}
	if(document.getElementById("texte_2")!=null){
		var oFCKeditor = new FCKeditor('texte_2');
		oFCKeditor.BasePath = "/templates/default/js/fckeditor/";
		oFCKeditor.ReplaceTextarea();
	}
	
	
	$('.addpan').click(function(){
		var _this = this;
		var _id = this.id;
		_id = _id.replace('a','');
		
		$.ajax({
			type: "GET",
			url: "/script/panier.ajax.php",
			data: "lang="+_langg+"&reference="+$('#reference'+_id).val()+"&prix="+$('#prix'+_id).val()+"&taille="+$('#taile'+_id+" select, #taille"+_id).val()+"&url="+$('#url'+_id).val().replace('#','_die_')+"&img="+$('#imgg'+_id).val(),
			success: function(msg){
				temp = msg.split('||');
				$('#contpanier').html(temp[1]);
				$('#lienbprod').html(temp[2]);
				
				if(temp[0]==1){
					$(_this).addClass('isbuy');
				}else{
					alert(_msg_erreur_panier);
				}
			}
		});
		
		return false;
	});
	
	
	var name = "#blpanier";
	var menuYloc = -500;
	$(window).scroll(function () { 
		offset = menuYloc+$(document).scrollTop();
		//console.log(offset+' = '+menuYloc+' + '+$(document).scrollTop());
		if(offset<0){offset=0;}
		$(name).animate({top:offset+"px"},{duration:500,queue:false});
	});
});

function popup(page) {
	window.open(page,'','width=400, height=610, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
}

function initFields(){
	$('.actinput').focus(function(){
		if(this.value==iniText[this.id]){
			this.value = '';
		}
	});

	$('.actinput').blur(function(){
		if(this.value == ''){
			this.value=iniText[this.id];
		}
	});
}

function initPanier(){
	$('#ProduitListe .addbutton').css('display','none');
	
	
	/*$('.phandle').click(function(){
		_id = this.id;
		_id = _id.replace('h','');
		
		
		$("#d"+_id).dialog({ 
			title: $('#reference'+_id).val()+' -- '+fprix($('#prix'+_id).val()),
			width:450,
			left: 0,
			draggable: true
		});
		//nbalert = ($("div[role='dialog']").length-1);
		
		$("div[aria-labelledby='ui-dialog-title-d"+_id+"']").css('left',(10+(nbalert*15))+'px'); 
		nbalert++;
		return false;
	});*/
	$('.phandle').click(function(){
		_id = this.id;
		_id = _id.replace('h','');
		ref_prod = $('#reference'+_id).val();
		/*
		//console.log(ref_prod);
		$("#d"+_id).dialog({ 
			title: $('#reference'+_id).val()+' -- '+fprix($('#prix'+_id).val()),
			width:450,
			left: 0,
			draggable: true
		});
		//nbalert = ($("div[role='dialog']").length-1);
		
		$("div[aria-labelledby='ui-dialog-title-d"+_id+"']").css('left',(10+(nbalert*15))+'px'); 
		nbalert++;
		*/
		
		//popup('/'+_langg+'/popup/?ref='+ref_prod, '','height=550, width=450, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
		popup('/index.php?lang='+_langg+'&model=popup&ref='+ref_prod);
		
		return false;
	});
}

function fprix(num){
	return number_format(num,2,',',' ')+' €';
}

function number_format(number, decimals, dec_point, thousands_sep) {

    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}


