var _iTestimonialTimer = null;
var _Paused = false;

OnReady(function(){
	InitTestimonials();
});

/*	================================================	Testimonials */

function InitTestimonials(){
	// init controls
	var $cnt = $j('div.newsFeed div.right');
	var $inum =  $j('div.itemNumber', $cnt).hide();
	var $span = $j('> span', $cnt).html('<em>Loading...</em>');
	$j('a', $inum).click(ChangeFeedItem);
	// don't cache me, bro.
	$j.ajax({
		url: '/_ajax/techconf-testimonials.xml',
		cache: false,
		dataType: 'text',
		success: function(data){
			var $data = $j(data).children();
			$span.html($data);
			$j('> *', $span).hide();
			$inum.show();
			ShowTestimonial(0);
		}
	});
}

function ShowTestimonial(index){
	var $cnt = $j('div.newsFeed div.right');
	var $span = $j('> span', $cnt);
	var ni = $span.children().length;
	var $inum = $j('div.itemNumber', $cnt);
	index = index < 0 ? (ni - 1) : (index % ni);
	$j('> span', $inum).html((index + 1).toString() + ' of ' + ni);
	$span.children().hide();
	$span.children().eq(document.newsAndEventsIndex = index).fadeIn(500);

  if (!_Paused)
  {
    if (_iTestimonialTimer != null)
    {
      window.clearTimeout(_iTestimonialTimer);
      _iTestimonialTimer = null;
    }
    _iTestimonialTimer = window.setTimeout("ShowTestimonial(" + (index + 1) + ")", 5000);
  }
}

function ChangeFeedItem(e){
	var dir = $j(e.target.parentNode).hasClass('prev') ? -1 : 1;
	ShowTestimonial(document.newsAndEventsIndex + dir);
}

function PauseTestimonials(e){
  if (_iTestimonialTimer != null)
  {
    window.clearTimeout(_iTestimonialTimer);
    _iTestimonialTimer = null;
  }

  if (e.alt == "Pause")
  {
    _Paused = true;
    e.src = "/images/play.gif";
    e.alt = "Play";
  }
  else
  {
    _Paused = false;
    ShowTestimonial(document.newsAndEventsIndex);
    e.src = "/images/pause.gif";
    e.alt = "Pause";
  }
}
