var aMin = new Array();
var aMax = new Array();


$(document).ready(function()
{
    initCat();
});

function initCat()
{
    processClassiqueProducts();
    processPackageProducts();

    processTooltips();
    
    //masquage du calendrier selon les params de la categorie
	if($('.categorie li.active').hasClass('hideDate')){
	    $('form.mainParams span.paramDate').hide();
	    $('form.mainParams h3').hide();
	}
}


function processClassiqueProducts()
{
    $('.classique form').each(function ()
    {
        //$(this).find('p.oneField select[name^=quantity]').each(function()
        $(this).find('p.oneField select').each(function()
        {
            shortid = $(this).attr('id').substring(9);
            aMin[shortid] = parseInt($(this).children(':first').val());
            aMax[shortid] = parseInt($(this).children(':last').val());
            newContent = '<span class="numeric-stepper"><input name="'+$(this).attr('name')+'" id="'+$(this).attr('id')+'" value="' + $(this).val() + '" size="2" />';
            newContent+= '<button type="button" id="'+$(this).attr('id')+'_plus" value="1" class="plus"><span>+</span></button><button type="button" id="'+$(this).attr('id')+'_minus" value="-1" class="minus"><span>-</span></button></span>';             
            $(this).replaceWith(newContent);
            
            //gestion des stepper :
            $('#'+$(this).attr('id')+'_plus').bind('click',function(){
                idInput = $(this).attr('id').substring(0,$(this).attr('id').length-5);
                changeClassiquePrice(idInput,1);
				$(this).blur();
            });
            $('#'+$(this).attr('id')+'_minus').bind('click',function(){
                idInput = $(this).attr('id').substring(0,$(this).attr('id').length-6);
                changeClassiquePrice(idInput,-1);
				$(this).blur();
            });
            //changement manuel :
            $('#'+$(this).attr('id')).bind('change',function(){
               changeClassiquePrice($(this).attr('id'),0);
            });
            
                
        });
        
    });
}

function changeClassiquePrice(id,step){
    shortid = id.substring(9);
    oldQty = parseInt($('#'+id).val());
    qty = (isNaN(oldQty)|| (oldQty + step) < aMin[shortid])?(aMin[shortid]):(oldQty+step);
    if(parseInt(qty)> aMax[shortid]){
        qty = aMax[shortid];
    }
    $('#'+id).val(qty);
    
    prx = parseFloat($('#price_'+shortid).val().replace(',','.')) * parseInt($('#'+id).val());
    prx = (parseFloat(prx)> 0)?(Math.round(prx*100)/100 +' &euro;'):('');
    
    tmpOld = $('#oldprice_'+shortid).val();
    oldPrx = (tmpOld !='' && !isNaN(tmpOld))?(parseFloat(tmpOld.replace(',','.')) * parseInt($('#'+id).val())):(0);
    oldPrx = (parseFloat(oldPrx)> 0)?(Math.round(oldPrx*100)/100 +' &euro;'):('');
    
    $('#total_'+shortid).html(prx);
    $('#oldtotal_'+shortid).html(oldPrx);
}
function processPackageProducts()
{
    $('.package form').each(function ()
    {
        var idProd = $(this).attr('id').substring(8);
        $(this).find('p.oneField select').each(function()
        {
            shortid = $(this).attr('id').substring(9);
            aMin[shortid] = parseInt($(this).children(':first').val());
            aMax[shortid] = parseInt($(this).children(':last').val());
            newContent = '<span class="numeric-stepper"><input name="'+$(this).attr('name')+'" id="'+$(this).attr('id')+'" value="' + $(this).val() + '" size="2" />';
            newContent+= '<button type="button" id="'+$(this).attr('id')+'_plus" value="1" class="plus"><span>+</span></button><button type="button" id="'+$(this).attr('id')+'_minus" value="-1" class="minus"><span>-</span></button></span>';             
            $(this).replaceWith(newContent);
            
            //gestion des stepper :
            $('#'+$(this).attr('id')+'_plus').bind('click',function(){
                idInput = $(this).attr('id').substring(0,$(this).attr('id').length-5);
                idClicked = idInput.substring(9);
                oldQty = parseInt($('#'+idInput).val());
                qty = (isNaN(oldQty)|| (oldQty + 1) < aMin[idClicked])?(aMin[idClicked]):(oldQty+1);
                if(parseInt(qty)> aMax[idClicked]){
                    qty = aMax[idClicked];
                }
                $('#'+idInput).val(qty);
                changePackagePrice(idProd);
            });
            $('#'+$(this).attr('id')+'_minus').bind('click',function(){
                idInput = $(this).attr('id').substring(0,$(this).attr('id').length-6);
                idClicked = idInput.substring(9);
                oldQty = parseInt($('#'+idInput).val());
                qty = (isNaN(oldQty)|| (oldQty - 1) < aMin[idClicked])?(aMin[idClicked]):(oldQty-1);
                if(parseInt(qty)> aMax[idClicked]){
                    qty = aMax[idClicked];
                }
                $('#'+idInput).val(qty);
                changePackagePrice(idProd);
				$(this).blur();
            });
            //changement manuel :
			changePackagePrice(idProd);
            $('#'+$(this).attr('id')).bind('change',function(){
            	idClicked = $(this).attr('id').substring(9);
                oldQty = parseInt($('#'+$(this).attr('id')).val());
                qty = (isNaN(oldQty)|| (oldQty - 1) < aMin[idClicked])?(aMin[idClicked]):(oldQty-1);
                if(parseInt(qty)> aMax[idClicked]){
                    qty = aMax[idClicked];
                }
                $('#'+$(this).attr('id')).val(qty);
                changePackagePrice(idProd);
				$(this).blur();
            });
            
                
        });
        
    });
}

function changePackagePrice(idProd){  
    tot = 0;
    $('.package p.prod'+idProd+' input[id^="quantity"]').each(function(){
        tot += parseFloat($('#price_'+$(this).attr('id').substring(9)).val().replace(',','.')) * parseInt($(this).val());
    });
    prx =( parseFloat(tot)> 0)?(Math.round(tot*100)/100 +' &euro;'):('');
    $('#totalPack'+idProd).html(prx);
}


function processTooltips()
{
    $('.product form').each(function ()
    {
        $(this).find('span.info').each(function()
        {
            $(this).tooltip({ 
                bodyHandler: function() { 
                    return $('#moreDescProd'+$(this).attr("id").substring(8)).html(); 
                },
                track: true, 
                delay: 0, 
                showURL: false, 
                showBody: " - ", 
                extraClass: "pretty", 
                opacity: 0, 
                left: -120 
            });
            
                
        });
        
    });
}           

