var _iNewsAndEventsTimer = null;
var _Paused = false;

OnReady(function(){
	InitNewsAndEventFeed();
	InitDIP();
});

/*	================================================	DIP */
function InitDIP(){
	document.dipTimer = setTimeout(NextDip, 11000);
	$j('.tabs li a').click(ShowDip);
	ShowDip({ target: $j('li.tab1 a') });
}

function ShowDip(e){
	var $a = $j(e.target);
	if(e.type){
		clearTimeout(document.dipTimer);
		document.dipTimer = setTimeout(NextDip, 10000);
	}
	if(document.currDip){
		document.currDip.$a.removeClass('current');
		document.currDip.$d.hide();
	}
	var tn = $a.closest('li').attr('class').substr(3);
	document.currDip = { '$a':$a.addClass('current'), '$d': $j('.dip'+tn).show() };	
}

function NextDip(){
	clearTimeout(document.dipTimer);
	document.dipTimer = setTimeout(NextDip, 11000);
	var tn = Number(document.currDip.$a.closest('li').attr('class').substr(3));
	var num = document.currDip.$a.closest('li').parent().children('li').length;
	tn = (tn < num ? (tn + 1) : 1);
	ShowDip({ target: $j('li.tab' + tn + ' a')});
}

/*	================================================	News & Events */

function InitNewsAndEventFeed(){
	// init controls
	var $cnt = $j('div.newsFeed div.right');
	var $inum =  $j('div.itemNumber', $cnt).hide();
  var $span = $j('> span', $cnt);
	$j('a', $inum).click(ChangeFeedItem);
  $j('> *', $span).hide();
  $inum.show();
	var $ticker = $j('div.newsFeed div.right span.item');
  $ticker.show();
  ShowNewsItem(0);
}

function ShowNewsItem(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 (_iNewsAndEventsTimer != null)
    {
      window.clearTimeout(_iNewsAndEventsTimer);
      _iNewsAndEventsTimer = null;
    }
    _iNewsAndEventsTimer = window.setTimeout("ShowNewsItem(" + (index + 1) + ")", 5000);
  }
}

function ChangeFeedItem(e){
	var dir = $j(e.target.parentNode).hasClass('prev') ? -1 : 1;
	ShowNewsItem(document.newsAndEventsIndex + dir);
}

function PauseNewsEvents(e){
  if (_iNewsAndEventsTimer != null)
  {
    window.clearTimeout(_iNewsAndEventsTimer);
    _iNewsAndEventsTimer = null;
  }

  if (e.alt == "Pause")
  {
    _Paused = true;
    e.src = "/images/play.gif";
    e.alt = "Play";
  }
  else
  {
    _Paused = false;
    ShowNewsItem(document.newsAndEventsIndex);
    e.src = "/images/pause.gif";
    e.alt = "Pause";
  }
}
