$(document).ready(function() {
var rwlnk = true;
  if (bjd_locale != 'en_US') {
  	var rwlnk = false;
  }
  var gamesBase={
    games:[
      {uid:'vegastech-classic-blackjack',
      name:'Classic Blackjack',
      soft:'Vegas Tech',
      score:'9.5',
      casino:'online-vegas',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>5</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rtg-classic-blackjack',
      name:'Classic Blackjack',
      soft:'RTG',
      score:'9.7',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>2</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'topgame-classic-blackjack',
      name:'Classic Blackjack',
      soft:'Top Game',
      score:'9.3',
      casino:'rome',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'vegastech-european-blackjack',
      name:'European Blackjack',
      soft:'Vegas Tech',
      score:'9.7',
      casino:'go-casino',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>5</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rtg-european-blackjack',
      name:'European Blackjack',
      soft:'RTG',
      score:'9.7',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>2</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rtg-faceup-blackjack',
      name:'Face Up 21 Blackjack',
      soft:'RTG',
      score:'9.5',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rtg-matchplay-blackjack',
      name:'Match Play 21 Blackjack',
      soft:'RTG',
      score:'9.7',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'topgame-multihand-blackjack',
      name:'Multihand Blackjack',
      soft:'Top Game',
      score:'9.6',
      casino:'rome',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>4</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rival-multihand-blackjack',
      name:'Multihand Blackjack',
      soft:'Rival',
      score:'9.6',
      casino:'sloto-cash',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>3</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'vegastech-perfect-pairs-blackjack',
      name:'Perfect Pairs Blackjack',
      soft:'Vegas Tech',
      score:'9.8',
      casino:'online-vegas',
      us:1,
      ec:1,
      dlist:'<li>'+locale['js_ghands']+' - <strong>5</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rtg-perfect-pairs-blackjack',
      name:'Perfect Pairs Blackjack',
      soft:'RTG',
      score:'9.7',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'rtg-pontoon',
      name:'Pontoon Blackjack',
      soft:'RTG',
      score:'9.6',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'vegastech-progressive-blackjack',
      name:'Progressive Blackjack',
      soft:'Vegas Tech',
      score:'9.7',
      casino:'go-casino',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>5</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gno']+'</strong></li>'},

      {uid:'rtg-superfun-blackjack',
      name:'Super Fun 21 Blackjack',
      soft:'RTG',
      score:'9.6',
      casino:'rushmore',
      us:1,
      ec:0,
      dlist:'<li>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'},

      {uid:'vegastech-vegas-strip-blackjack',
      name:'Vegas Strip Blackjack',
      soft:'Vegas Tech',
      score:'10',
      casino:'online-vegas',
      us:1,
      ec:1,
      dlist:'<li>'+locale['js_ghands']+' - <strong>5</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gyes']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</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>'+locale['js_ghands']+' - <strong>1</strong></li><li>'+locale['js_gflash']+' - <strong>'+locale['js_gno']+'</strong></li><li>'+locale['js_gfun']+' - <strong>'+locale['js_gyes']+'</strong></li>'}
    ]
  }

  var partnersNames = {
      'sloto-cash':'Slotocash casino~160~$50 bonus',
      'rushmore':'Rushmore casino~128~$1000 bonus',
      'go-casino':'Go casino~64~n/a',
      'online-vegas':'Online Vegas casino~32~n/a',
      'rome':'Rome casino~192~25% cashback',
      'club-usa':'Club USA casino~96~$150 bonus',
      'harbour':'English Harbour casino~0~$800 bonus',
      'win-palace':'Win Palace casino~0~$5000 bonus'
  }
  var moverTemplate =	''+
  '<div class="ixp-name">$name <span>($soft)</span></div>'+
  '<div class="ixp-l">'+(rwlnk ? '<a href="$review">' : '<a href="to/picker/$casino">')+'<img src="$pic" width="161" height="120" alt="$name" />'+(rwlnk ? '</a>' :'</a>')+'</div>'+
  '<div class="ixp-r">'+
  '<ul class="ixp-desc"><li class="ixp-score">'+locale['js_gscore']+' - <strong>$score</strong></li>$dlist'+(rwlnk ? '<li class="ixp-review"><a href="$review">Read Review</a></li>' : '')+'</ul>'+
  '</div>'+
  '<div class="ixp-c">'+
  '<a target="_blank" href="to/picker/$casino" class="ixp-c-logo" style="background-position:0 -$pospx;"></a>'+
  '<div class="ixp-c-info"><span>$bonus</span><span class="ixp-c-us">'+locale['js_gwelcome']+'</span></div>'+
  '<div class="ixp-c-r"><a target="_blank" href="to/picker/$casino"">'+locale['js_gvisit']+'</a></div>'+
  '</div>';
  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
      }
    }
  }else {
    if(Math.floor(Math.random() * 101) >= 60) {
      var savedArr = "rtg-classic-blackjack";
      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;
      var soft = gamesBase[type][indexes[i]].soft.replace(' ', '').toLowerCase();
      var casino = gamesBase[type][indexes[i]].casino;
      if (casino == 'online-vegas') {
      	casino = 'go-casino';
      }
      if(typeof images[uid] == "undefined") {
        images[uid] = new Image;
        images[uid].src = "img/img-rew/games-review/161x120/" + uid.replace(soft, casino) + ".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 partner = partnersNames[currArr[counters[type]].casino].split("~");
    var logoPos = partner[1];
    var bonus = partner[2];
    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).replace(/\$bonus/g,
    bonus).replace(/\$pos/g, logoPos);
    return currCont
  }
  function addTooltip($this) {
    $this.mouseenter(function(e) {
      var this_casino = $this.attr("href").split("/");
      var tpos = e.pageX + 3;
      if(tpos + 210 >= $(window).width()) {
        tpos = $(window).width() - 210
      }
      this_casino = this_casino[2];
      $("body").append('<div class="picker-tooltip" style="left:' + tpos + "px; top:" + (e.pageY + 3) + 'px">\u201cPlay\u201d 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
      }
    }
    var soft = gamesBase[type][counters[type]].soft.replace(' ', '').toLowerCase();
    var casino = gamesBase[type][counters[type]].casino;
    if (casino == 'online-vegas') {
    	casino = 'go-casino';
    }
    images[gamesBase[type][counters[type]].uid] = new Image;
    images[gamesBase[type][counters[type]].uid].src = "img/img-rew/games-review/161x120/" + gamesBase[type][counters[type]].uid.replace(soft, casino) + ".jpg";
    var currCont = applyTemplate(type);
    $(".ixp-next").html(currCont);
    addTooltip($(".ixp-next").find(".golden-but17"))
  }
  function savePosition() {
    var str = "" + gamesBase["games"][counters["games"]].uid;
    $.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, function() {
      if(jQuery.browser.msie) {
        $(this).get(0).style.removeAttribute("filter")
      }
    });
    $moverCont.animate({left:-592}, 1E3, 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, function() {
      if(jQuery.browser.msie) {
        $(this).get(0).style.removeAttribute("filter")
      }
    });
    $moverCont.animate({left:0}, 1E3, 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()
    })
  })
});
