//global configuration
var total_news = 5;
var loop_interval = 8000;
var current_id = 0;
var interval = null;

var images_array = new Array();
var images_thumb_array = new Array();

var main_images = new Array();
var thumb_images = new Array();

$(document).ready(function(){
  //display the ajax loader
  $('#top5_loader').show();

  //preload the thumb images
  $('#slideshow_data').find('span[id="image"]').each(function(ind,ele){
		images_array[ind] = $(ele).html();
   });
  //preload the news images
  $('#slideshow_data').find('span[id="image_thumb"]').each(function(ind,ele){
		images_thumb_array[ind] = $(ele).html();
   });
   //call the preload the images function
   preloadimages();

  //start left side menu
  $('#slideshow_data').find('div[id*="slide_"]').each(function(ind,ele){
	 _title = $(ele).find('span[id="title"]').html();
	 _position = 'item_' + ind;
	 _image_thumb = $(ele).find('span[id="image_thumb"]').html();

	 $('#slideshow_menu').append('<div style="height:65px; width:204px;"><div id="'+_position+'" style="position:absolute;z-index:2;overflow:visible;cursor:pointer;width:204px">'+
	 '<table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">'+
        '<tr>'+
          '<td align="right" class="style7">'+_title+'</td>'+
          '<td width="4" align="right">&nbsp;</td>'+
          '<td id="thumb_holder_'+ind+'" height="60"></td>'+
          '<td width="4" align="right">&nbsp;</td>'+
        '</tr>'+
        '<tr>'+
        	'<td colspan="4" align="right">'+
            '<img src="/images/gfx/spacer1.jpg" width="201" height="1">'+
            '</td>'+
        '</tr>'+
      '</table>'+
      '</div>'+
      '<div class="top5_thumb_cell">'+
      '</div></div>');

	  //attache the image
	  $("#thumb_holder_"+ind).html(thumb_images[ind]);

      //attach onmouseover event-handler for the menu
	  $('#item_'+ind).hover(function(){
  		load_news(ind)
  	  }, function(){
  		loop_news_again(ind)
  	  }
	  )

  	  //attach onClick event-handler for the menu
	  $('#item_'+ind).click(function(){
  		window.location = $("#slide_" + ind).find('span[id="news_link"]').html();
  	  })
 })


  //loop through the news
  //setTimeout("foo()", 3000);
  loop_news();
  $('#top5_loader').hide();
  $('.news_ticker_container').show();
  $('#top5_holder').show();
  interval = window.setInterval("loop_news()", loop_interval);
 });

/**
 *
 * @access public
 * @return void
 **/
function foo()
{
  $('#ajax_loader').hide();
  $('#top5_holder').show();
}

function preloadimages()
{
	for (i=0;i<images_array.length;i++)
	{
		main_images[i] = new Image(440,250);
		main_images[i].setAttribute('border', 0);
		main_images[i].src=images_array[i];

		thumb_images[i] = new Image(60,34);
		thumb_images[i].setAttribute('border', 0);
		thumb_images[i].src=images_thumb_array[i];
	}
}

function load_news(id)
{
    //$("#logger").append('t');
	current_id = id;
	clearInterval(interval);
	loop_news(id);
	//interval = window.setInterval("loop_news()", loop_interval);
}

function loop_news_again(id){
	current_id = id+1;
	clearInterval(interval);
	interval = window.setInterval("loop_news()", loop_interval);
}

function loop_news()
{
   if(parseInt(current_id) % total_news == 0)
   {
		current_id = 0;
   }

   id = current_id;
   var news_div = $("#slide_" + id);

   //Get data
   var title = $(news_div).find('span[id="title"]');
   var image = $(news_div).find('span[id="image"]');
   var caption = $(news_div).find('span[id="caption"]');
   var source = $(news_div).find('span[id="source"]');
   var description = $(news_div).find('span[id="description"]');
   var news_link = $(news_div).find('span[id="news_link"]');
   var category = $(news_div).find('span[id="category"]');

   $("#sn_image").hide();
   //hide all the data
   $("#div_img_caption").hide();
   $("#sn_caption").hide();
   $("#div_smart_video").hide();
   $("#sn_source").hide();
   $("#sn_more_div").hide();
   $("#sn_divider").hide();
   $("#sn_image").hide();

   //Assign data
   $("#sn_title").html(title.html());
   $("#sn_description").html(description.html());
   $("#news_slide_category").html(category.html());
   $("#sn_image").html(main_images[id]);
   $("#sn_link").attr('href', news_link.html());

   //attach onclick event-handler for the image
   $("#news_slide_gradient").click(function(){
   window.location = news_link.html();
   });

   //Apply fadein/fadeout effect on the image
   $("#sn_image").fadeIn(500);
   //$("#sn_image").show();

   //remove all the selected and replace it
   //remove all the selected and replace it
   $("#slideshow_menu").find("div[class='top5_thumb_cell_selected']").each(function(ind,elem){
		$(elem).removeClass('top5_thumb_cell_selected');
		$(elem).addClass('top5_thumb_cell');
   });

   $("#item_"+id).next().removeClass('top5_thumb_cell');
   $("#item_"+id).next().addClass('top5_thumb_cell_selected');

   //show more link
   $("#sn_more_div").show();
   $("#sn_divider").show();
   $("#sn_loader").hide();

   current_id++;
}
