// JavaScript Document
$(document).ready(function () {	
	//SET CURRENT MENU IF SUB
	$('#nav ul.sub_menu').hover(function(){
			var theParent = $(this).parent('li');
			if(theParent.hasClass('current')) $(this).parent('li').addClass('current_parent');
			theParent.addClass('current');
		}, function(){
			if(!$(this).parent('li').hasClass('current_parent'))$(this).parent('li').removeClass('current');
		}
	);
	//SUB MENU
	$('#nav li').hover(function(){
			$('ul', this).slideDown(100);
		}, function(){
			$('ul', this).slideUp(100);			
		}
	);
	
	
	//NOS MARQUES ANIMATION
	function mycarousel_initCallback(carousel){
		// Disable autoscrolling if the user clicks the prev or next button.
		/*carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
	
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});*/
	
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};

    $('#nos_marques').css('display', 'block');
	$('#nos_marques').css('visibility', 'visible');
	$('#nos_marques').jcarousel({
        auto: 5,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
	
	//FRAIS DE PORT
	if($('input[name="frais_port"]').length > 0){
		$.Calculation.setDefaults({
			// a regular expression for detecting European-style formatted numbers
			reNumbers: /(-|-\$)?(\d+(\.\d{3})*(,\d{1,})?|,\d{1,})?/g
			// define a procedure to convert the string number into an actual usable number
			, cleanseNumber: function (v){
				// cleanse the number one more time to remove extra data (like commas and dollar signs)
				// use this for European numbers: v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".")
				return v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".");
			}
		})
		
		//toFixed is the number_format
		var checkedFraisObj = $('input[name="frais_port"]:checked');
		var frais = checkedFraisObj.addClass('subtotal');
		
		var subtotal = parseFloat($('span#sous-total>input.subtotal').val());
		var fraisdeport = parseFloat($('input[type="radio"].subtotal').val());
		var leTotal = parseFloat(subtotal+fraisdeport);
		//$('#total_price_formatted').val(leTotal.toFixed(2));
		//$('#total_price_formatted').number_format({precision: 2, decimal: ',', thousands: ' '});
		var formattedNumber = number_format(leTotal.toFixed(2), 2, ',', ' ');
		
		$('#total').html(formattedNumber+' &euro;');
		$('#total_price').val(leTotal);
		$('#frais_de_port').val(checkedFraisObj.val());
		$('#frais_port_type').val(checkedFraisObj.attr('tabindex'));
		
		$('input[name="frais_port"]').click(function(){
			$('input[name="frais_port"]').removeClass('subtotal');
			$(this).addClass('subtotal');
			//$('#total').html($('input.subtotal').sum().toFixed(2)+' &euro;');
			var subtotal = parseFloat($('span#sous-total>input.subtotal').val());
			var fraisdeport = parseFloat($('input[type="radio"].subtotal').val());
			var leTotal = parseFloat(subtotal+fraisdeport);
			//$('#total_price_formatted').val(leTotal.toFixed(2));
			//$('#total_price_formatted').number_format({precision: 2, decimal: ',', thousands: ' '});
			
			var formattedNumber = number_format(leTotal.toFixed(2), 2, ',', ' ');
			$('#total').html(formattedNumber+' &euro;');
			$('#total_price').val(leTotal);
			$('#frais_de_port').val($(this).val());
			$('#frais_port_type').val($(this).attr('tabindex'));
		});
		
	}
	
	//SEARCH REFERENCE
	$('#box_recherche form input#ref').click(function(){
		if($(this).val().length == 19 )$(this).val('');
	});
	
	
});

function applyAccordion(currentEntryId){
	if($('#accordion').length > 0){
		$('#accordion').accordion({
			header: "a.item_entry",
			activate: false
		});
		
		$('#accordion ul.niveau2 a').each(function(){
			///catalogue-pieces-auto-tourismes.php?marque=2&modele=329&motorisation=87&categ=100862
			var href = $(this).attr('href').split('&');
			var val = href[href.length-1].split('=');
			if( val[1] == currentEntryId){
				var item_entry = $(this).closest('ul.niveau2').prev('a.item_entry');
				
				//set selected entry
				//item_entry.addClass('current');
				item_entry.parent().addClass('current');
				
			}
		});
		var child_pos = $('#accordion li.current').attr('id');
		$('#accordion').activate(parseInt(child_pos));
		$('#accordion .niveau2').css('height', 'auto');
		$('#accordion>li.current ul#recent.niveau2').height('auto');
		//setTimeout(resizeCurrentUl, 2000);
	}
}

function setCount(){
	$('#res_count').html($('#total_articles').attr('title')+' produit(s)');
}

function resizeCurrentUl(){
	$('#accordion>li.current ul#recent.niveau2').height('auto');
	$('#accordion>li:first ul.niveau2').height('auto');
}

function number_format(number, decimals, dec_point, thousands_sep) {
	number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    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);
}
