
var articleIndex;
var totalArticles;

var effectSpeed;

articleIndex = 0;
effectSpeed = 500;

function displayArticle(index)
{

  //jQuery('#whatshot-viewer-articles .whatshot-article').stop();
/*
  jQuery('#whatshot-viewer-articles .whatshot-article:visible').fadeOut(1000);
  jQuery('#whatshot-viewer-articles .whatshot-article:eq(' + index + ')').fadeIn(1000);
*/
  jQuery('#whatshot-viewer-articles .whatshot-article:visible').fadeOut(effectSpeed, function()
{
  jQuery('#whatshot-viewer-articles .whatshot-article:eq(' + index + ')').fadeIn(effectSpeed);
});
  updateNav()
}

function nextArticle()
{
  if (articleIndex >= totalArticles -1)
  {
	articleIndex = -1;
  }
  
  articleIndex += 1;
  displayArticle(articleIndex);
}

function prevArticle()
{
  if (articleIndex <= 0)
  {
  	articleIndex = totalArticles;
  }
  
  articleIndex -= 1;
  displayArticle(articleIndex);
}

function updateNav()
{
  jQuery('#whatshot-index-display').text((articleIndex + 1) + " / " + totalArticles);
}

function displayNav()
{
  jQuery('#whatshot-viewer-nav-inner').show();
}

function displayLoadIndicator()
{
  jQuery('#whatshot-viewer-loading').show();
}

function hideLoadIndicator()
{
  jQuery('#whatshot-viewer-loading').hide();
}

function displayArticles()
{
  jQuery('#whatshot-viewer-articles').show();
}


function articleImagesLoaded()
{
  hideLoadIndicator();
  updateNav();
  displayNav();
}

function setupViewer()
{

  displayLoadIndicator();
  jQuery('#whatshot-previous').click(function()
  {
    prevArticle();
  });
  jQuery('#whatshot-next').click(function()
  {
    nextArticle();
  });

  if (jQuery("#whatshot-viewer-articles img").length > 0)
  {
    jQuery("#whatshot-viewer-articles").onImagesLoad(
    { 
      selectorCallback: articleImagesLoaded 
    }); 
  }
  else
  {
      articleImagesLoaded();
  }
}

jQuery(document).ready(function()
{

  totalArticles = jQuery('#whatshot-viewer-articles .whatshot-article').length;
  setupViewer();
});

