$(function() {
  $('.tpg_tab img[data-ct]:not(.selected)')
    .mbTooltip({
      opacity : .90, //opacity
      wait:500, //before show
      anchor:"parent", //"parent"
      cssClass:"default", // default = default
      timePerWord:70, //time to show in milliseconds per word
      hasArrow:false,
      color:"white",
      imgPath:"images/",
      shadowColor:"black",
      fade:700
    })
    //.live('mouseover', function() {
    //  var idx = $(this).attr('data-idx');
    //  $('.tpg_tab_on[data-idx=' + idx + ']').show();
    //})
    //.live('mouseout', function() {
    //  var idx = $(this).attr('data-idx');
    //  $('.tpg_tab_on[data-idx=' + idx + ']').hide();
    //})
    .live('click', function(e) {
      // update rotator position and pause there
      rotr.currPos = parseInt($(this).attr('data-idx')) - 1;
      rotr.setTabInterval();
      rotr.moveTo();
      $('#tpg_rot_tabs').stopTime('nextTab');
    });
    
  var aRot = sRot;
  var rotr = {
    currPos: 0,
    availPos: aRot.length,
    maxPos: 9,
    tabFade: 'slow',
    interval: '4s',
    init: function() {
      rotr.setTabInterval();
      rotr.moveTo();
      $('#learnmore')
        .bind('mouseenter', function(e) { 
          $(this).attr('src', WSX_BUTTONS + 'learnmore.png');
          window.status = WS_ROOT + aRot[rotr.currPos - 1]['link'];
        })
        .bind('mouseleave', function(e) { 
          $(this).attr('src', WSX_BUTTONS + 'learnmore_off.png'); 
          window.status = '';
        })
        .click(function(e) { window.location = aRot[rotr.currPos - 1]['link']; });
      //$('#tpg_rot_content')
      $('#tpg_rotator')
        .bind('mouseenter', function(e) { $('#tpg_rot_tabs').stopTime('nextTab'); })
        .bind('mouseleave', function(e) { rotr.setTabInterval(); });
    },
    setTabInterval: function() {
      $('#tpg_rot_tabs')
        .stopTime('nextTab')
        .everyTime(rotr.interval, 'nextTab', function() { rotr.moveTo(); });
      //rotr.moveTo();
    },
    moveTo: function(iPos) {
      iPos = (typeof iPos == 'undefined' ? rotr.currPos + 1 : iPos);
      // iPos = (iPos > rotr.maxPos ? 1 : iPos);
      $('.tpg_tab img.selected').removeClass('selected').fadeOut(rotr.tabFade);
      if (iPos > rotr.availPos) {
        iPos = rotr.maxPos + 1;
        $('#tpg_tab_selectr').animate({left: 11 + (iPos - 1) * 51}, 'fast').queue('fx', function() { $(this).css({left: -51}).dequeue(); }).animate({left: 11}, 'fast');
        iPos = 1;
      } else {
        $('#tpg_tab_selectr').animate({left: 11 + (iPos - 1) * 51}, 'fast');
      }
      $('#tpg_rot_content').hide();

      if ( aRot[iPos - 1]['link_type'] == 'opx' ) {
        $('#tpg_rot_content #tpg_ad_content').html('<' + "iframe id='a58b2e3f' name='a58b2e3f' src='http://www8.tucows.com/delivery/afr.php?n=a58b2e3f&amp;zoneid=" + aRot[iPos - 1]['adzone'] + "&amp;cb=" + Math.floor(Math.random()*11000000) + " framespacing='0' frameborder='no' scrolling='no' width='423' height='170'><" + "a href='http://www8.tucows.com/delivery/ck.php?n=aca1bca0&amp;cb=" + Math.floor(Math.random()*11000000) + "' target='_blank'><" + "img src='http://www8.tucows.com/delivery/avw.php?zoneid=" + aRot[iPos - 1]['adzone'] + "&amp;cb=" + Math.floor(Math.random()*11000000) + "&amp;n=aca1bca0' border='0' alt='' /></a></" + "iframe><" + "script type='text/javascript' src='http://www8.tucows.com/delivery/ag.php'><" + "/script>");
        $('#tpg_rot_content #tpg_sub_content').hide();
        $('#tpg_rot_content #tpg_ad_content').show();
      }
      else {
        $('#tpg_rot_content #tpg_sub_content').show();
        $('#tpg_rot_content #tpg_ad_content').hide();

        $('#tpg_rot_content #tpgr_title').html('<a href="' + aRot[iPos - 1]['link'] + '">' + aRot[iPos - 1]['title'] + '</a>');
        $('#tpg_rot_content #tpgr_desc').html(aRot[iPos - 1]['desc']);
        if (aRot[iPos - 1]['img_link'] > '') {
          $('#tpg_rot_content #tpgr_img').html('<img src="' + aRot[iPos - 1]['img_link'] + '" />');
        } else {
          $('#tpg_rot_content #tpgr_img').html('');
        }
      }
      $('#tpg_rot_content').fadeIn();
      rotr.currPos = iPos;
      $('.tpg_tab img[data-idx=' + iPos + '].tpg_tab_on').fadeIn(rotr.tabFade).addClass('selected');
      // console.log('currPos: ' + rotr.currPos);
    },
  }
  rotr.init();
});

