var total = {
	page: 0,
	ht: 0,
	tva: 0,
	ttc: 0
};

$(document).ready(function(){

	$("a.lightbox").fancybox(); 
	
	$('.lightbox.iframe').fancybox({
		frameWidth: 800
	});
	
	$("#commande_nb_page").keyup(function() {
		$.removeNonNumeric(this, true);
		$.recalcule();
	}); 
	$("#commande_nb_page").blur(function() {
		if ($(this).val() == '' || $(this).val() == '0') {
			$(this).val(1);
			$.recalcule();
		}
	});
	$("#commande_nb_decli").keyup(function() {
		$.removeNonNumeric(this);
		$.recalcule();
	});
	$("#commande_technologie_id").change(function() {
		$.recalcule();
	});
	$("#commande_Client_pays_id").change(function() {
		$.recalcule();
	});
	$("#commande_compatibilite").click(function() {
		$.recalcule();
	});
	$("#commande_accessibilite").click(function() {
		$.recalcule();
	});
	$("#commande_print").click(function() {
		$.recalcule();
	});
	$("#commande_mobilite").click(function() {
		$.recalcule();
	});
	
    $("#crisis_menu2 ul ul").hide();
	$("#crisis_menu2 .crisis_activated > ul").show();
	$("#crisis_menu2 .crisis_activated > a").addClass('crisis_on');
	
    $("#crisis_menu2 > ul > li > a").click(function(){
		$(this).toggleClass('crisis_on');
        $(this).next("ul").toggle();
        return false;
    });
		   
		   
	$("#switch_onglets .content_onglet").hide();
	$("#switch_onglets .content_onglet:eq(0)").show();
	
	$("#switch_onglets ul a").click(function(){
		$('"#switch_onglets ul li').removeClass('crisis_activated');
		$(this).parent().addClass('crisis_activated');
		$("#switch_onglets .content_onglet").hide();
		$(this.hash).show();
		this.blur();
		return false;
	});
  
  
	/* Navigation */
	st1 = new Object();
	$(".crisis_activated").addClass("crisis_first");
	$("#crisis_menu1 ul li a").mouseover(function() {
		if($(this).parent().parent().parent().is("div") == true) {
			$("#crisis_menu1 div > ul > li").removeClass("crisis_activated");	
			$(this).parent().addClass("crisis_activated");	
		}  
		window.clearTimeout(st1);
		menuHide();
		$(this).parent().parent().find("ul").hide();
		if($(this).next().is("ul") == true) {
			$(this).next("ul").show();
		}
	});	
	
  
});



function menuHide() {
	$("html").mouseover(function(o) {
		o = o.target;
		while(o.parentNode) {
			if(o.id == "crisis_menu1") {
				window.clearTimeout(st1);
				break;
			}
			if(o.nodeName == "HTML") {
				st1 = window.setTimeout('$("#crisis_menu1 div ul ul").hide(); $("#crisis_menu1 div > ul > li").removeClass("crisis_activated"); $(".crisis_first").addClass("crisis_activated");', 500);
				$("html").unbind();
			}
			o = o.parentNode;
		}
	});
}

jQuery.extend({
	removeNonNumeric: function(context, forceMin) {
	    var newval = $(context).val().replace(/[^\d]/g,'');
	    if (forceMin && forceMin == true && newval == '0') {
	    	newval = 1;
	    }
		$(context).val(newval);
	},
	recalcule: function() {
		var nb_pages = parseInt($('#commande_nb_page').val());
		var nb_decli = parseInt($('#commande_nb_decli').val());

		if (!nb_pages) nb_pages = 0;
		if (!nb_decli) nb_decli = 0;
		
		// calcul du tarif / page
		total.page = tarifs.page;
		if ($('#commande_technologie_id').val() != '') {
			total.page += tarifs.technologie[parseInt($('#commande_technologie_id').val())];
		}
		
		if (document.getElementById('commande_compatibilite').checked == true) {
			total.page += tarifs.compatibilite;
		}
		if (document.getElementById('commande_accessibilite').checked == true) {
			total.page += tarifs.accessibilite;
		}
		if (document.getElementById('commande_print').checked == true) {
			total.page += tarifs.print;
		}
		if (document.getElementById('commande_mobilite').checked == true) {
			total.page += tarifs.mobilite;
		}
		
		// calcul total ht
		total.ht = (total.page * nb_pages) + (total.page / 2 * nb_decli);
		
		// calcul tva 
		total.tva = CurrencyFormatted(tarifs.tva[parseInt($('#commande_Client_pays_id').val())]) ? 0.196 * total.ht : 0;
		total.tva = Math.round(total.tva * 100)/100;
		total.ttc = total.ht + total.tva;
		
		$('#recap_maitres').html(nb_pages + ' x ' + total.page + ' €');
		$('#recap_declis').html(nb_decli + ' x ' + (total.page / 2) + ' €');
		$('#total_ht').html(total.ht + ' €');
		$('#total_ttc').html(total.ttc + ' €');
		$('#total').html(total.ttc + ' €');
		$('#estimation_livraison').html(dateLivraison(nb_pages + nb_decli));
		
		if ($('#commande_Client_pays_id').val() == '1') {
			$('ul.total.fr').css({ display: 'block' });
			$('ul.total.et').css({ display: 'none' });
		} else {
			$('ul.total.fr').css({ display: 'none' });
			$('ul.total.et').css({ display: 'block' });			
		}
		
	}
});

function CurrencyFormatted(amount)
{
 var i = parseFloat(amount);
 if (isNaN(i)) { i = 0.00; }
 var minus = '';
 if (i < 0) { minus = '-'; }
 i = Math.abs(i);
 i = parseInt((i + .005) * 100);
 i = i / 100;
 s = new String(i);
 if (s.indexOf('.') < 0) { s += '.00'; }
 if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
 s = minus + s;
 return s;
}


function dateLivraison(duree)
{
  var today = new Date();
  var ctr = duree;
  while (ctr > 0)
  {
    today.setDate(today.getDate() + 1);

    var isWorkday = today.getDay() > 0 && today.getDay() < 6;
    for (var i = 0, n = vacances.length; i < n; i ++)
    {
      if (vacances[i] == (today.getMonth() + 1) + "/" + today.getDate())
      {
        isWorkday = false;
        break;
      }
    }
    if (isWorkday)
      ctr --;
  }

  var j = (parseInt(today.getDate()) < 10) ? "0" + today.getDate() : today.getDate();
  var m = (parseInt(today.getMonth()) < 9) ? "0" + (today.getMonth()+1) : (today.getMonth()+1);
  var y = today.getFullYear();
  
  // mise à jour input
  $('#commande_date_livraison').val(y + '-' + m + '-' + j);
  
  return j + '/' + m + '/' + y;
}
var vacances = new Array("5/1", "5/2", "5/3", "5/4", "7/4", "12/25");