/*
Leonardo Service Provider S.p.A.
Tutti i diritti registrati e riservati,(R)(C) 2009.
La riproduzione parziale o totale del presente software
delle pagine generate nonchè della strutturazione dei dati,
delle tabelle e di quant'altro è ad esso inerente  è illegale 
ed è punibile secondo tutte le Leggi vigenti sulla violazione del Diritto D'Autore.
L'utilizzo del presente software senza regolare licenza d'uso
è illegale e sarà perseguito secondo tutte le Leggi vigenti applicabili
*/

(function(){
	$.fn.dclicktwice = function(options){
		var defaults = {
			secs: 10,
			doit: false,
			msgs: 'Conferma la scelta entro [SEC] secondi.'
		}
		var o = $.extend(defaults, options);
		var btn = $(this); /// Conservo il pulsante di conferma
		function init(){
			var FirstButton = $('<button>');
			FirstButton.addClass('ib_scom');
			FirstButton.html(btn.html());
			FirstButton.click(function(){
				var noButton = $('<button>');
				noButton.html('No').addClass('nobtn');
				noButton.click(function(){
					clearInterval(myInterval)
					noButton.remove();
					timerDiv.remove();
					btn.html(FirstButton.html());
					btn.dclicktwice({secs:o.secs, doit:o.doit, msgs:o.msgs});
				});
				noButton.insertAfter(FirstButton);
				btn.insertAfter(FirstButton);
				btn.html('Si');
				btn.click(function(){
					clearInterval(myInterval)
					noButton.remove();
					timerDiv.remove();
					btn.html(FirstButton.html());
					btn.dclicktwice({secs:o.secs, doit:o.doit, msgs:o.msgs});
				})
					
				FirstButton.remove();
				var timerDiv = $('<div>');
				timerDiv.addClass('timerdiv').insertBefore(btn);
				var myDate = new Date();
				var stopMills = myDate.getTime()+(o.secs*1000);
				var myInterval = window.setInterval(function(){
					nowDate = new Date();
					var myMs = nowDate.getTime();
					if(myMs>=stopMills){
						clearInterval(myInterval)
						if(o.doit == true) {
							btn.click();
							noButton.click();
						} else {
							noButton.click();
						}
					}else{
						timerDiv.html(o.msgs.replace('[SEC]', Math.round((stopMills-myMs)/1000)));
					}
				}, 10);
			})
			FirstButton.insertAfter(btn);
			btn.remove();
		}
		init()
	}
})(jQuery);