
		$(document).ready(function(){
			
			var gamesBase={
				games:[
					{uid:'vegastech-classic-blackjack', 
					name:'Classic Blackjack', 
					soft:'Vegas Tech', 
					score:'9.5', 
					casino:'go-casino',
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>5</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'topgame-classic-blackjack', 
					name:'Classic Blackjack', 
					soft:'Top Game', 
					score:'9.3', 
					casino:'rome', 
					us:1,
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rtg-classic-blackjack', 
					name:'Classic Blackjack', 
					soft:'RTG', 
					score:'9.7', 
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>2</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'vegastech-european-blackjack', 
					name:'European Blackjack', 
					soft:'Vegas Tech', 
					score:'9.7', 
					casino:'online-vegas', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>5</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rtg-european-blackjack', 
					name:'European Blackjack', 
					soft:'RTG', 
					score:'9.7',
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>2</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rtg-faceup-blackjack', 
					name:'Face Up 21 Blackjack', 
					soft:'RTG', 
					score:'9.5', 
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rtg-matchplay-blackjack', 
					name:'Match Play 21 Blackjack', 
					soft:'RTG', 
					score:'9.7', 
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'topgame-multihand-blackjack', 
					name:'Multihand Blackjack', 
					soft:'Top Game', 
					score:'9.6', 
					casino:'rome', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>4</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rival-multihand-blackjack', 
					name:'Multihand Blackjack', 
					soft:'Rival', 
					score:'9.6', 
					casino:'sloto-cash', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>3</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'vegastech-perfect-pairs-blackjack', 
					name:'Perfect Pairs Blackjack', 
					soft:'Vegas Tech', 
					score:'9.8', 
					casino:'go-casino', 
					us:1, 
					ec:1, 
					dlist:'<li>Hands - <strong>5</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rtg-perfect-pairs-blackjack', 
					name:'Perfect Pairs Blackjack', 
					soft:'RTG', 
					score:'9.7', 
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rtg-pontoon', 
					name:'Pontoon Blackjack', 
					soft:'RTG', 
					score:'9.6', 
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'vegastech-progressive-blackjack', 
					name:'Progressive Blackjack', 
					soft:'Vegas Tech', 
					score:'9.7', 
					casino:'online-vegas', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>5</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>no</strong></li>'},
					
					{uid:'rtg-superfun-blackjack', 
					name:'Super Fun 21 Blackjack', 
					soft:'RTG', 
					score:'9.6', 
					casino:'rushmore', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'vegastech-vegas-strip-blackjack', 
					name:'Vegas Strip Blackjack', 
					soft:'Vegas Tech', 
					score:'10', 
					casino:'go-casino', 
					us:1, 
					ec:1, 
					dlist:'<li>Hands - <strong>5</strong></li><li>Flash version - <strong>yes</strong></li><li>Fun version - <strong>yes</strong></li>'},
					
					{uid:'rival-vegas-strip-blackjack', 
					name:'Vegas Strip Blackjack', 
					soft:'Rival', 
					score:'9.3', 
					casino:'sloto-cash', 
					us:1, 
					ec:0, 
					dlist:'<li>Hands - <strong>1</strong></li><li>Flash version - <strong>no</strong></li><li>Fun version - <strong>yes</strong></li>'}
				]
			}
			
			var partnersNames = {
					
					'sloto-cash':'Slotocash casino',
					'rushmore':'Rushmore casino',
					'go-casino':'Go casino',
					'online-vegas':'Online Vegas casino',
					'rome':'Rome casino',
					'club-usa':'Club USA casino',
					'harbour':'English Harbour casino'
					
				
			}
			
			var moverTemplate =	''+
				'<div class="ixp-l"> <a href="$review"><img src="$pic" width="166" height="124" alt="$name" /></a><strong>$name</strong> ($soft)</div>'+
				'<div class="ixp-r"><div class="ixp-score$ec">$score</div>'+
                '$us'+
                '<ul class="ixp-desc">$dlist</ul>'+
				'</div>'+
              	'<a href="$review" class="silver-but17">Review</a> <a target="_blank" href="to/game-$uid/$casino" class="golden-but17">Play</a>';
				
			var currType;
			
			var counters = {
				games:0
			}
			
			var images = {};
			
			if($.cookie('GamesPickerPosition')){
				var savedArr = $.cookie('GamesPickerPosition');
					for(var i = 0, l = gamesBase.games.length; i<l; i++){
						if(gamesBase.games[i].uid == savedArr){
							counters.games = i;
							break;
						}
					}
			
			}
			
			cacheImg('games');
			
			function cacheImg(type){
				var indexes = [
					counters[type],
					((counters[type] + 1) > gamesBase[type].length-1)?0:counters[type] + 1,
					((counters[type] - 1) < 0)?gamesBase[type].length-1:counters[type] - 1
				]
				
				for (var i = 0; i < 3; i++){
					var uid = gamesBase[type][indexes[i]].uid
					if(typeof images[uid] == 'undefined'){
						images[uid] = new Image();
						images[uid].src = 'img/img-rew/games-review/166x124/'+uid+'.jpg';
					}
				}
				
			}
			
			initMover('games');

			function applyTemplate(type){
				var currArr = gamesBase[type];
				var currUs = (currArr[counters[type]].us)?'<span class="ixp-us">us welcome</span>':'';
				var currEc = (currArr[counters[type]].ec)?' ixp-ec':'';
				var currCont = moverTemplate
					.replace(/\$uid/g, currArr[counters[type]].uid)
					.replace(/\$name/g, currArr[counters[type]].name)
					.replace(/\$casino/g, currArr[counters[type]].casino)
					.replace(/\$score/g, currArr[counters[type]].score)
					.replace(/\$soft/g, currArr[counters[type]].soft)
					.replace(/\$ec/g, currEc)
					.replace(/\$review/g, currArr[counters[type]].uid+'.htm')
					.replace(/\$pic/g, images[currArr[counters[type]].uid].src)
					.replace(/\$dlist/g, currArr[counters[type]].dlist)
					.replace(/\$us/g, currUs)
				return currCont;
			}
			
			function addTooltip($this){
				$this.mouseenter(function(e){
					var this_casino = $this.attr('href').split('/');
					this_casino = this_casino[2];
					$('body').append('<div class="picker-tooltip" style="left:'+(e.pageX+3)+'px; top:'+(e.pageY+3)+'px">“Play” forwards you to '+partnersNames[this_casino]+' where you can <span>play this game for fun or real money</span>.</div>');
					
					
				});
				$this.mouseleave(function(){
					$('.picker-tooltip').remove();
				})
			};
			
			function initMover(type){
				currType = type;
				var currCont = applyTemplate(type);
				$('.ixp-curr').html(currCont);
				
				addTooltip($('.ixp-curr').find('.golden-but17'));
			}
			function updateMover(type,increment){
				
				if((counters[type] + increment) < 0){
					counters[type] = gamesBase[type].length-1;
				} else if((counters[type] + increment) > gamesBase[type].length-1)
				{
					counters[type] = 0;
				} else {
					counters[type] += increment;
				}
				
				images[gamesBase[type][counters[type]].uid] = new Image();
				images[gamesBase[type][counters[type]].uid].src = 'img/img-rew/games-review/166x124/'+gamesBase[type][counters[type]].uid+'.jpg';
				var currCont = applyTemplate(type);
				$('.ixp-next').html(currCont);
				addTooltip($('.ixp-next').find('.golden-but17'));
			}
			
			function savePosition(){
				var str = '' + gamesBase['games'][counters['games']].uid;
				//console.log(str)
				$.cookie('GamesPickerPosition', str, { expires: 30 })
			}
			
			var $arrLeft = $('.ixp-larr');
			var $arrRight = $('.ixp-rarr');
			var $moverCont = $('.ixp-mover-cont');
			
			$arrRight.click(function(){
				var $currFrame = $('.ixp-curr');
				var $nextFrame = $('.ixp-next');
				
				$arrLeft.hide();
				$arrRight.hide();
				
				updateMover(currType,1);
				$nextFrame.css({'left':592, opacity:0.2});
				
				$currFrame.animate({opacity:0.2},500)
				$nextFrame.animate({opacity:1},500)
				
				$moverCont.animate(
					{left:-592},
					1000,
					function(){
						$moverCont.css('left',-296);
						$nextFrame.css('left',296);
						$currFrame.css('left',0);
						$arrLeft.show();
						$arrRight.show();
						$currFrame.removeClass('ixp-curr').addClass('ixp-next');
						$nextFrame.removeClass('ixp-next').addClass('ixp-curr');
						savePosition();
					}
				);
				
			})
			$arrLeft.click(function(){
				var $currFrame = $('.ixp-curr');
				var $nextFrame = $('.ixp-next');
				
				$arrLeft.hide();
				$arrRight.hide();

				
				updateMover(currType,-1);
				$nextFrame.css({'left':0, opacity:0.2});
				
				$currFrame.animate({opacity:0.2},500)
				$nextFrame.animate({opacity:1},500)
				
				$moverCont.animate(
					{left:0},
					1000,
					function(){
						$moverCont.css('left',-296);
						$nextFrame.css('left',296);
						$currFrame.css('left',0);
						$arrLeft.show();
						$arrRight.show();
						$currFrame.removeClass('ixp-curr').addClass('ixp-next');
						$nextFrame.removeClass('ixp-next').addClass('ixp-curr');
						savePosition();
					}
				);
			})
			
		
		})

	