(function($){
	var slotMachine = function() {
		var credits = 0
		var totalWinVote = 0
		var vote=new Array();
			spinning = 3
			spin = [0,0,0]
			slotsTypes = {
				'cherry': [1,3,6],
				'orange': [0,0,0],
				'prune': [0,0,0],
				'bell': [0,0,0],
				'bar1': [0,0,0],
				'bar2': [0,0,0],
				'bar3': [0,0,0],
				'seven': [0,0,0],
				'anybar': [0,0,0]
			}
			
			slots = [
				['orange','bell','orange','bar2','prune','orange',
				'bar3','prune','orange','bar1','bell','cherry','orange',
				'prune','bell','bar1','cherry','seven','orange','prune',
				'orange','bell','orange'],
				['chery','prune','orange','bell','bar1','cherry','prune',
				'bar3','cherry','bell','orange','bar1','seven','cherry',
				'bar2','cherry','bell','prune','cherry','orange','cherry',
				'prune','orange'],
				['cherry','orange','bell','prune','bar2','cherry','prune',
				'orange','bar3','cherry','bell','orange','cherry','orange',
				'cherry','prune','bar1','seven','bell','cherry','cherry',
				'orange','bell']
			]
			
			startSlot = function() {
				$('a#slotTrigger').fadeIn();
				$('#slotSplash').fadeOut();
				credits = 3;
				spinning = false;
				$('#slotSplash').animate({top: -130}, 1000, 'bounceOut');
				$('#slotTrigger').removeClass('slotTriggerDisabled');
				this.blur();
				return false;
			}
	
			
			
			spin = function() {
				this.blur();
				if (spinning == false) {
					spinning = 3;
					credits --;
					$('#slotCredits').html(credits);
					spin[0] = parseInt(Math.random() * 23);
					spin[1] = parseInt(Math.random() * 23);
					spin[2] = parseInt(Math.random() * 23);
					$('#slotTrigger').addClass('slotTriggerDisabled');
					$('img.slotSpinAnimation').show();
					$('#wheel1 img:first').css('top', - (spin[0] * 32 + 16) + 'px');
					$('#wheel2 img:first').css('top', - (spin[1] * 32 + 16) + 'px');
					$('#wheel3 img:first').css('top', - (spin[2] * 32 + 16) + 'px');
					setTimeout(function(){
						stopSpin(1);
					}, 1500 + parseInt(1500 * Math.random()));
					setTimeout(function(){
						stopSpin(2);
					}, 1500 + parseInt(1500 * Math.random()));
					setTimeout(function(){
						stopSpin(3);
					}, 1500 + parseInt(1500 * Math.random()));
				}
				return false;
			},
			stopSpin = function(slot) {
				$('#wheel' + slot)
					.find('img:last')
						.hide()
						.end()
					.find('img:first')
						.animate({
							top: - spin[slot - 1] * 32
						},{
							duration: 500,
							easing: 'elasticOut',
							complete: function() {
								spinning --;
								if (spinning == 0 ) {
									endSpin();
								}
							}
						});
			},
			endSpin = function() {
				
				slotType1 = slots[0][spin[0]];
				slotType2 = slots[1][spin[1]];
				slotType3 = slots[2][spin[2]];					
					winnedCredits = 0,
					waitToSpin = 10;
								
				
				//si 1 TDI
				if ( (slotType1 == 'cherry') && (slotType2 != 'cherry') && (slotType3 != 'cherry') ) {
										
					winnedCredits = slotsTypes['cherry'][0];				
				} 
				if ( (slotType2 == 'cherry') && (slotType1 != 'cherry') && (slotType3 != 'cherry') ) {										
					winnedCredits = slotsTypes['cherry'][0];					
				} 
				if ( (slotType3 == 'cherry') && (slotType2 != 'cherry') && (slotType1 != 'cherry') ) {								
					winnedCredits = slotsTypes['cherry'][0];					
				} 
					
				
				//si 2 TDI
				if( (slotType1 == 'cherry') && (slotType2 == 'cherry') && (slotType3 != 'cherry') ) {				
					winnedCredits = slotsTypes['cherry'][1];				
				}
				if( (slotType2 == 'cherry') && (slotType3 == 'cherry') && (slotType1 != 'cherry') ) {	
					winnedCredits = slotsTypes['cherry'][1];				
				}
				if( (slotType1 == 'cherry') && (slotType3 == 'cherry') && (slotType2 != 'cherry') ) {
					winnedCredits = slotsTypes['cherry'][1];				
				}
				
				//si 3 TDI
				if( (slotType1 == 'cherry') && (slotType2 == 'cherry')&&(slotType3 == 'cherry')) {					
					winnedCredits = slotsTypes['cherry'][2];				
				} 
				
				
				
				
				if (winnedCredits > 0) {
					totalWinVote += winnedCredits		
					$("#totalVote span").html(totalWinVote)		
					waitToSpin = 200 + winnedCredits;
				}
			
				
				
				setTimeout(function() {
					if (credits == 0) {
					window.location.href="spip.php?page=EXT-slot&vote="+totalWinVote
					
					} else {
						$('#slotTrigger').removeClass('slotTriggerDisabled');
						spinning = false;
					}
				}, waitToSpin);
			};
		return {
			init: function() {
				$('#slotSplash a').bind('click', startSlot);
				$('#slotTrigger')
					.bind('mousedown', function(){
					startSlot;
						$(this).addClass('slotTriggerDown');
					})
					.bind('click', spin);
				$(document).bind('mouseup', function(){
					$('#slotTrigger').removeClass('slotTriggerDown');
				});
				$('#wheel1 img:first').css('top', - (parseInt(Math.random() * 23) * 32) + 'px');
				$('#wheel2 img:first').css('top', - (parseInt(Math.random() * 23) * 32) + 'px');
				$('#wheel3 img:first').css('top', - (parseInt(Math.random() * 23) * 32) + 'px');
			}
		};
	}();
	$.extend($.easing,{
		bounceOut: function (x, t, b, c, d) {
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		},
		easeOut:function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		},
		elasticOut: function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		}
	})
	$(document).ready(slotMachine.init);
})(jQuery);

