var obj_CurrentTip;

$(function () {
  $('#agencySpotlight').css('display', 'block');
  $('#rotating_content').innerfade({
    speed: 'slow',
    timeout: 4000,
    type: 'sequence',
    containerheight: '68px',
    containerwidth: '600px'
  });
  $('#spotlightDescriptions').css({ 'top': $('#spotlightLogos').offset().top });
  GroupList();
  $('#spotlightLogos img').click(ShowTip);
});

function GroupList() {
  var li_count = $('#spotlightLogos li').length;
  $('#spotlightLogos').append(
		$('#spotlightLogos li:lt(' + (5 - (li_count % 5)) + ')').clone()
	);

  while ($('ul#spotlightLogos > li[class!="li_group"]:lt(5)').length >= 1) {
    $('ul#spotlightLogos > li[class!="li_group"]:lt(5)')
		.wrapAll('<li class="li_group"><ul></ul></li>');
  }

  $('ul#spotlightLogos').cycle({
    pause: true
  });
}

function ShowTip(a) {
  a.stopImmediatePropagation();
  obj_CurrentTip = a;

  $('.description').fadeOut(500);

  var $current = $(this).parent();
  var current_id = $current.attr('id');
  $obj = $($('#description' + current_id.substr(4)));

  $('#spotlightDescriptions').animate(
		  { 'top': ($current.offset().top - ($obj.height() + 65)) }
		, 1000
		, function () { $obj.fadeIn(1000); }
	);

  $('.spotlight-close').length != 0 ? $('.spotlight-close').remove() : '';

  $('#spotlightHeader').after(
		$('<a />')
			.text('x')
			.attr({ 'class': 'spotlight-close' })
			.click(RemoveTip)
	);
}

function RemoveTip(a) {
  a.stopImmediatePropagation();

  $('.description').fadeOut(250);

  $('.spotlight-close').remove();

  $('#spotlightDescriptions').animate(
		  { 'top': $('#spotlightLogos').offset().top - 41 }
		  , 500
		  , function () { $('.description').hide(); }
	);
}

function PauseBetween(jc, state) {
  jc.pause();
  var local = setTimeout(function () { jc.play(); }, 4000);
}

