//var Tombolaaa = Tombolaaa || {
var Tombolaaa = {

    numbersElementes:[],
    uncalled: [],
    lastExtractedGui:null,
    soundPlayer:null,
    locale:'en',
    numberZoom:null,
    numberZoomTimeout:null,
    numberZoomFx:null,
		
		
    initialize: function(){
        var $this = this
        this.numbersElementes = $('ul.cartella li.riga > ul > li a')
        this.lastExtractedGui = $('div.lastExtracted ul')[0]        
        this.numberZoom = $('#estratto');
        this.numberZoom.css('zIndex',-1);
        /*
        this.numberZoomFx = new Fx.Tween(this.numberZoom,{
            link:'cancel',
            duration: 'long'
        })
        */
        //this.numberZoomFx = this.numberZoom.animate({opacity: '0.4'}, "slow");
        
        for(i=1;i<=90;i++)
            this.uncalled.push(i)
       	
        for(numero=1;numero<=90;numero++)
            //if (storage.get('N'+numero)=='true'){
            if (storage.get('N'+numero)){
            	//console.log(numero);
            	this.showNumber(numero,false);       
            }     	    
    },

    setLocale: function(locale){
        this.locale = locale
    },

    randomNumber: function(min,max) {
        //return Math.floor(Math.random()*(max-min+1)+min);
        return phpRandom(min,max);
    },

    extractNext: function(){
        var totalUncalled = this.uncalled.length
        
        if(totalUncalled<1) return 0
       
        
        if(totalUncalled>1)
            randomIndex = this.randomNumber(0,totalUncalled-1)
        
        var randomNumber = this.uncalled[randomIndex]
        
        this.showNumber(randomNumber,true);
        
        return randomIndex
        
    },
    
    hidBigNumber: function(){
    		//alert(this.numberZoom.html());
    	//numero=$('estratto')
			//if (numero.style.display="display"){
				this.numberZoom.stop();
				this.numberZoom.css('zIndex',-1);
				this.numberZoom.css('opacity',0);
    	//}
    },
    memExtract: function(newNumber){
			//scriviCookie("N"+newNumber,'true',600);
			storage.set('N'+newNumber,'true');
		},
		delNumber: function(oldNumber){
			this.uncalled.push(oldNumber)	
			////scriviCookie(oldNumber,'false',600)
			//cencellaCookie("N"+oldNumber);
			storage.remove('N'+oldNumber);
			
			
			var randomNumberElement = $('ul.cartella li.riga > ul > li a[name="n'+oldNumber+'"]'); 
			//[0]
			
      randomNumberElement.removeClass('extractedNumber');
      //elimina dai recenti
			
			$('#last'+oldNumber).remove();
			console.log(this.lastExtractedGui);
			
    },
		//showNumber: function(newNumber,animate=true){
		showNumber: function(newNumber,animate){
			
			
		animate = typeof animate !== 'undefined' ? animate : true
		
			//sound=createsoundbite("./audio/it/"+newNumber+".ogg","./audio/it/"+newNumber+".mp3")
        
      var randomNumberElement = $('ul.cartella li.riga > ul > li a[name="n'+newNumber+'"]');
      
      //alert(randomNumberElement.className.indexOf('extractedNumber'));
      if (randomNumberElement.hasClass('extractedNumber')){
        //cancella subito l'estratto
        //ma ci sono problemi con l'animazione e le caratteristiche che rimangono all'oggetto
	      //this.numberZoom.stop();
	      $(this).removeAttr('style');
				this.numberZoom.css('zIndex',-1);
				this.numberZoom.css('opacity',0);
	      this.delNumber(newNumber)
	      //this.hidBigNumber
	      return;
	    }
	    
			randomNumberElement.addClass('extractedNumber')
			
      this.numberZoom.html(newNumber);
			
    	//this.numberZoomFx.clearChain();
    	//this.numberZoomFx.cancel();
      
      this.memExtract(newNumber);
      
      
      
      
      //JQuery('#rumble-element').jrumble();
      //this.numberZoom.trigger('startRumble')
      //this.numberZoom.stop();
      
      console.log('Animate:' + animate);
      if(animate){
	      this.numberZoom.css('zIndex',99);
	     
	      
	      //
	      
	      //var animType=mt_rand(1,25);
	      
	      //for debug:
	      //animType = Number(this.numberZoom.html());
	      
				//controllato il tempo fino all numero:		     
	      //animType = 14;
	      
	      //this.numberZoom.html(animType);
	      
	      this.numberZoom.animate({opacity: '1'}, 2000);
	      
	      var times = storage.get('tempoNuovoNum');
	      
	      var flagAnimazioni = storage.get('flagAnimazioni');
	      var animazioniSel = storage.get('animazioniSel');
	      
	      console.log('Lista animazioni:'+ animazioniSel);
	      
	      //var animType=mt_rand(1,animazioniSel.length);
	      
	      if (flagAnimazioni!="Nessuna")
	      	var animType=mt_rand(0,27-1);
	      else{
	      	if (animazioniSel.length>0){
	      		var animType = mt_rand(0,animazioniSel.length-1);
	      		animType = Number(animazioniSel[animType]);
	      	}else animType =0;
	      }
	      console.log('Numero selezionato:' + animType);
	    	      
	      
	      if(times>0 && animType >0){
		      	
		      console.log('Numero animazione:' + animType);
		      	
		      switch (animType){
		      	case 1: //tremolio
		      		tempo = times*16;
		      		var duration = 200;
							for (var i = 0; i < tempo; i++)
					    	this.numberZoom.animate({
					      	  left: (i % 2 === 0 ? "-" : "+") + "=50"
					    			}, duration-i*(10-times/2));		
					    break;
					  case 2: //shake
					  	this.numberZoom.effect("shake",{direction :"up", distance:  20, times:times*10});
					  	break;
					  case 3: //shake
					  	this.numberZoom.effect("shake",{direction :"right", distance:  20, times:times*10});
					  	break;
					  case 4: // pulsate
					  	this.numberZoom.effect("pulsate",{times:times*10});	
					  	break;
					  case 5: // fontSize (come la 16!!)
					  	//this.numberZoom.animate({ fontSize: "90%" , duration: 1000*times, distance:2000},   2000*times);
					  	//this.numberZoom.effect("scale",{percent :350, distance:  20, times:times*10});
					  	this.numberZoom.hide({ effect: "scale", direction: "both" });
					  	break;	
					  case 6: // explode
					  	this.numberZoom.hide({ effect: "explode",pieces : 9}, 10000, times*1000);
					  	break;		
					  case 7: // fold
					  	this.numberZoom.hide({ effect: "fold", size : 200, horizFirst: true }, 0, times*1000);
					  	break;		
					  case 8: // folde
					  	this.numberZoom.hide({ effect: "fold", size : 200, horizFirst: false }, 0, times*500);
					  	break;
					  case 9: // blind
					  	this.numberZoom.hide("blind", {direction: "left"}, times*500);
					  	break;
					  case 10: // blind
					  	this.numberZoom.hide("blind", {direction: "vertical"}, times*500);
					  	break;	
					  case 11: //clip
					  	this.numberZoom.hide("clip", { direction: "vertical" }, times*500);
					  	break;
					  case 12: //clip
					  	this.numberZoom.hide("clip", { direction: "horizontal" }, times*500);
					  	break;
					  case 13: //bounce
					  	this.numberZoom.hide("bounce", { times: 5 }, times*1000);
					  	break;
					  case 14: //scale
					  	this.numberZoom.hide({ effect: "scale", direction: "left" },0, times*1000);
					  	break;
					  case 15: // rotate
					  	this.numberZoom.rotate({angle:0,animateTo:360,easing: $.easing.easeInOutExpo },10000);	
					  	break;
					  case 16: // slide	
					  	this.numberZoom.hide("slide",{direction:"left", distance:1000},times*1000);	
					  	break;
					  case 17: // slide	
					  	this.numberZoom.hide("slide",{direction:"right", distance:1000},times*1000);	
					  	break;
					  case 18: // slide	
					  	this.numberZoom.hide("slide",{direction:"up", distance:1000},times*1000);	
					  	break;
					  case 19: // slide	
					  	this.numberZoom.hide("slide",{direction: "down", distance:1000},times*1000);	
					  	break;
					  case 20: // pulsate
					  	this.numberZoom.hide({ effect: "pulsate", times: times*20 },times*1000);
					  	break;
					  case 21: // buff
					  	this.numberZoom.hide({ effect: "puff", percent : "200" },times*1000);
					  	break;
					  case 22: // opacity
					  	this.numberZoom.animate({opacity: 1/4}, times*2000);
					  	break;
					  case 23: // Tilt
						  for(i=1;i<=10;i++){
							  this.numberZoom.animate({
			        		'margin-left': '-=15px',
			        		'margin-right': '+=15px'
			    			}, 200);
			    			
				        this.numberZoom.animate({
			            'margin-left': '+=15px',
			            'margin-right': '-=15px'
			        	}, 200);
			        }
		        	this.numberZoom.hide({ effect: "puff", percent : "200" });
					  	break;	
		        	
		        case 24:
		        	x="jello";
		        	//this.numberZoom.addClass(x + ' animated');
		        	this.numberZoom.removeClass().addClass(x + ' animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
	      				$(this).removeClass();}
	      				);
	    				break;
	    				
		    		case 25: // Tilt ^|v
						  for(i=1;i<=10;i++){
							  this.numberZoom.animate({
			        		'margin-left': '-=15px',
			        		'margin-right': '+=15px'
			    			}, 200);
			    			
				        this.numberZoom.animate({
			            'margin-left': '+=15px',
			            'margin-right': '-=15px'
			        	}, 200);
			        }
		        	this.numberZoom.hide({ effect: "puff", percent : "200" });
					  	break;	
					  
					  case 26: // Tilt <->
						  for(i=1;i<=10;i++){
							  this.numberZoom.animate({
			        		'margin-top': '-=15px',
			        		'margin-bottom': '+=15px'
			    			}, 200);
			    			
				        this.numberZoom.animate({
			            'margin-top': '+=15px',
			            'margin-bottom': '-=15px'
			        	}, 200);
			        }
		        	this.numberZoom.hide({ effect: "puff", percent : "200" });
					  	break;	
					  case 27: //shake
					  	this.numberZoom.effect("shake",{direction :"left", distance:  50, times:10});	
					  	this.numberZoom.effect("shake",{direction :"up", distance:  50, times:10});
					  	break;
					  case 28: //opacity slow
					  	this.numberZoom.delay(5000).animate({opacity: '0'}, 5000);
					  }	
					
		      /*
		      
	      	*/
	      	
	      //this.numberZoom.effect("shake",{direction :"up", distance:  20, times:10});
	      //this.numberZoom.effect("shake",{direction :"right", distance:  20, times:10});
	      //this.numberZoom.effect("pulsate",{times:10});
	      //this.numberZoom.hide("slide");
	      //this.numberZoom.hide({ effect: "explode", pieces : 9 });
	      //this.numberZoom.hide({ effect: "fold", size : 200, horizFirst: true });
	      //this.numberZoom.hide({ effect: "fold", size : 200, horizFirst: false });
	      //this.numberZoom.hide("blind", {direction: "left"});
	      //this.numberZoom.hide("blind", {direction: "vertical"}); //up, down, left, right, vertical, horizontal
	      //this.numberZoom.hide("clip", { direction: "vertical" });
	      //this.numberZoom.hide("clip", { direction: "horizontal" });
	      //this.numberZoom.hide("bounce", { times: 5 }, "slow");
	      //this.numberZoom.hide({ effect: "scale", direction: "left" });
	      //this.numberZoom.effect("slide",{direction:"both"});
	      //this.numberZoom.effect("slide",{direction:"left", "right", "up", "down", distance:10});
	      
	      
	      	
	      	//this.numberZoom.hide({ effect: "pulsate", times: "10" });
	      	//this.numberZoom.hide({ effect: "puff", percent : "200" });
	      	//this.numberZoom.hide({ effect: "scale", direction: "both" });
	      	
	      	
	      	
	      	this.numberZoom.animate({opacity: '0'}, 200,function(){ showSmorfiaNumber(newNumber,true);});
		      this.numberZoom.animate({zIndex:-1},1);
		      this.numberZoom.show();
		     
	     	}
				else
				{
					this.numberZoom.delay(5000).animate({opacity: '0'}, 5000);
					//this.numberZoom.hide({ effect: "scale", direction: "left" },0, times*1000);
					
					this.numberZoom.animate({opacity: '0'}, 200);
		      this.numberZoom.animate({zIndex:-1},1);
		      this.numberZoom.show();
		      
					//this.numberZoom.show();
					//this.clearQueue();
  				//this.stop();
					//this.numberZoom.animate({opacity: '0'}, 5000);
					
					//this.numberZoom.delay(10000);
					
					//this.numberZoom.delay(10000).animate({opacity: '-1'}, 5000);
					
	      //this.numberZoom.animate({zIndex:-1},5);
	      //this.numberZoom.show();
				}  
      }
      
      
 
    

      //
      
      //clearTimeout(this.numberZoomTimeout)
      
			
			
			//sound=createsoundbite("./audio/it/"+newNumber+".ogg","./audio/it/"+newNumber+".mp3")
			
			
			//sound=playSound("./audio/it/"+newNumber+".ogg");
			
			/*
			var t = setTimeout("sound.playclip();", 500);
						
      if(this.soundPlayer && this.soundPlayer.isPlaying()) 
      	this.soundPlayer.stop();
      this.soundPlayer = Titanium.Media.createSound('app://audio/it/'+newNumber+'.ogg');
      this.soundPlayer.play();
			*/
			
      $( '<li id="last'+newNumber+'">'+newNumber+'</li>' ).prependTo( this.lastExtractedGui );
      //$( '<li>'+newNumber+'</li>' ).prependTo( this.lastExtractedGui );
      
      //Click sui numeri estratti
      //
      //solo smorfia
      //$(this.lastExtractedGui).children().first().click(function(){showSmorfiaNumber($(this).text(),true);});
      //numero + smorfia
      
      $(this.lastExtractedGui).children().first().click(function(){showNumber($(this).text(), true);});
      
      
			//$( '<li style="transition: transform 0.5s; transform: rotate(360deg);">'+newNumber+'</li>').prependTo(this.lastExtractedGui);
			//$('<li>'+newNumber+'</li>').css( { transition: "transform 0.5s", transform:  "rotate(360deg)" } );
			
			//var r = setTimeout("jQuery('#estratto').trigger('stopRumble');", 6000);
			
			var totalUncalled = this.uncalled.length
      for(i=0;i<=totalUncalled;i++){
				if (this.uncalled[i]==newNumber){
					this.uncalled.splice(i,1);
					//alert(i);
					//i=this.uncalled.lenght+1;
					i=this.uncalled.length+1;
				}
			}
			//la riga seguente fa visualizzare la smorfia all'avvio?
			//showSmorfiaNumber(newNumber);
			return newNumber
		}

};
