(function($){

	$.fn.venteflash = function(options){

		// default configuration properties
		var defaults = {			
			tempo: 1000,
			speed: 1000,
			min: 0,
			max: 160,
			move: 16,
			startTime: 12,
			endTime: 0,
			jour: 'jour',
			heure: 'heure',
			minute: 'minute',
			seconde: 'seconde',
			pub: '.pubflash',
			type: 'normal'
		};
		
		var options = $.extend(defaults, options);
		var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);
		this.each(function() {
			var obj = $(this);
			
			if(options.startTime!=options.endTime){
				if(options.startTime>options.endTime){
					var time = options.startTime-options.endTime;
				}else{
					var time = options.endTime-options.startTime;
				}
				var timer;
				if(options.tempo){
					timer = setInterval(compt,options.tempo);
				};	
			}
			function compt(){
				if(time<0){
					if(obj.attr("class")=="pre"){
						obj.removeClass("pre");
						obj.addClass("encours");
						$(options.pub).html("<a href=\""+$(".lien", obj).html()+"\">"+$(options.pub).html()+"</a>");
						time=$(".duree", obj).html();
					}else{
						clearInterval(timer);
						obj.fadeOut("slow");
						$(options.pub).fadeOut("slow");
					}
				}
				if(time>=0){
					var s = (time % 60);
					var m = ((time-s)/60) % 60;
					var h = ((time-s-m*60)/3600) % 24;
					var j = (time-s-m*60-h*60*60)/86400;
					change(options.jour, j);
					change(options.heure, h);
					change(options.minute, m);
					change(options.seconde, s);
					time-=options.tempo/1000;
				}
			};
			function change(elt, value){
				if(value<10){
					if(value==0){
						value="00";
					}else{
						value="0"+value;
					}					
				}else{
					value=value.toString();
				}
				if(options.type == "deroulant"){
					var v = value.split("");
					for(i=0; i<2; i++){
						var topPos = parseInt(v[i])*options.move;
						$("."+elt+" .chiffre:eq("+i+")", obj).animate({bottom: (options.max-options.move-topPos) + "px"}, options.speed, "swing");
					}
				}else if(options.type == "normal"){
					$("."+elt, obj).html(value);
				}
			};
		});
		
	};
	
})(jQuery);
