/*
 * jQuery bolhas  plug-in 1.5.7
 *
 * http://sim.assec.pt
 *
 * Copyright (c) 2009 Pedro Duarte, Assec SIM
 *
 *
 */
 
	var AsBolinhas = new Array(); 	
    
	function bolinhasGo(){
		for (i=0; i < AsBolinhas.length ; i++)  AsBolinhas[i].vai();
		}

    $.bolaSabao = function(obj,img,w,h){
		this.objNome= obj;
		this.imgsrc = img;
		this.bolinhaW = w;
		this.bolinhaH = h;
		
		if ( w && h)
			this.racio = Math.floor( h/w);
		}; 
	
	$.bolaSabao.prototype = { 
		racio : 0, 
		deltaH: 0,
		passos:17,
		passo:1,
		_iniX : 0, 
		_iniY : 0, 
		_fimY:-44,
		delay: 10000,
		_revolucaoX: 0, 
		_revolucaoY:10, 
		idB: 0,
		bolita: null,
		objT: null, 
		cacheIni:0,
		cacheVelocidade:0,
		cachePreCGeraX: null,
		cachePreVelocidade: null,
		cachePrePosX:null,
		cachePrePosY:null,
		revolucao: function(v){ // devia ser dinamica com  equação de amortecimento
			switch(v){
				case 1: this._revolucaoX = 0;	break;
				case 2: this._revolucaoX = -40;	break;
				case 3: this._revolucaoX = 40;	break;
				case 4: this._revolucaoX = -20;	break;
				case 5: this._revolucaoX = -84;	break;
				case 6: this._revolucaoX = 84;	break;
				case 7: this._revolucaoX = -84;	break;
				case 8: this._revolucaoX = 24;	break;
				case 9: this._revolucaoX = -140;	break;
				case 10: this._revolucaoX = -110;	break;
				case 11: this._revolucaoX = -70;	break;
				case 12: this._revolucaoX = 50;	break;
				case 13: this._revolucaoX = 80;	break;
				case 14: this._revolucaoX = 8;	break;
				case 15: this._revolucaoX = 88;	break;
				case 16: this._revolucaoX = 78;	break;
				case 17: this._revolucaoX = 55;	break;
				 
				  
				} 
			return this._revolucaoX;		
			},
		  
		inicia:function(){ 
			
			this.idB = Math.floor(Math.random()*30061); 
			this.deltaH = Math.floor(this._iniY/this.passos);
			
			// Pre-caching randoms for more speed
			if (this.cacheIni==0 ) {
				this.cachePreCGeraX = $.makeArray()	;
				this.cachePreVelocidade = $.makeArray()	;
				this.cachePredeltaH = $.makeArray()	;
				this.cachePrePosX = $.makeArray()	;
				this.cachePrePosY = $.makeArray()	;
				 
				for(var i=1; i < this.passos+1 ; i++) this.cachePreCGeraX[i] =( Math.cos(Math.random()*360)/this.passos);    
				for(var i=1; i < this.passos+1 ; i++) this.cachePreVelocidade[i] =  ( (1500*this.passos)+this.randInt(8761))/this.passos ;   
				for(var i=1; i < this.passos+1 ; i++) this.cachePredeltaH[i] =  (this.deltaH - this.randInt(this.deltaH))/2 ;   	
				for(var i=1; i < this.passos+1 ; i++) this.cachePrePosX[i] = this.geraXc(i);
				for(var i=1; i < this.passos+1 ; i++) this.cachePrePosY[i] = this.geraYc(i);
				//
				this._revolucaoY =  (this._iniY - this._fimY)/ this.passos;
				
				this.Blargura= this.bolinhaW - Math.floor(Math.random()*this.bolinhaW +2)+10;
				this.Baltura=  Math.floor(this.Blargura*this.racio);
				this.cacheIni =1; 
				}
			
			$('#'+this.objNome).append('<img id="bolita'+this.idB+'" class="bolinha" src="'+this.imgsrc+'" />');
			 
			this.bolita = $('#bolita'+this.idB);
			 
			this.run(); 
			},

		run: function () {
		    var objT = this; 
			this.passo=1;
			this.bolita.css({'width': this.Blargura +'px','height':this.Baltura+'px',
				// 	 'left':objT._iniX+'px','top':(objT._iniY+this.deltaH)+'px',opacity: '0.4'}) ; 
				 	 'left':objT._iniX+'px','top':(objT._iniY+this.deltaH)+'px',opacity: '1.0'}) ; 
				 
			}, 
			
		vai: function () {
			// arranca 
			var objT = this; 
			setTimeout( function (){ objT.runFilaQ();}, this.randInt(this.delay));
			},	
			
		runFilaQ: function(){ 
			 var objT2 = this; 
			this.bolita.animate({ 
				'top': this.cachePrePosY[this.passo]+'px', 
				'left': this.cachePrePosX[this.passo]+'px'},
				this.cachePreVelocidade[this.passo],  'linear', function(){
					objT2.passo++;
					
					if ( objT2.passo < objT2.passos+1 ) objT2.runFilaQ();
						else { 
							objT2.run();
							objT2.runFilaQ();
							}
					});
			 
			},
		posX:  function(val){ this._iniX = val;},	
		posY:  function(val){ this._iniY = val;},	
		espera:  function(val){ this.delay = val +10000;},	
		
			
		geraYc: function(v){	
			if (v<2) var dt= this._iniY;
				else var dt= this.cachePrePosY[v-1]; 	 
			var dh =  (dt - this._revolucaoY - this.cachePredeltaH[v]*(v+1)); 
			  
			return 	Math.floor(dh) ;
			},
		    
		geraXc: function(v){ 
			return Math.floor( this._iniX + this._iniX*this.cachePreCGeraX[v] + this.revolucao(v) ) ;
			},
		 
		randInt: function(v){
			return Math.floor(Math.random()*v);
			}
			
		}
