var total_news = 6;
var current_id = 0;
var interval = null;
var myimages=new Array();
var images_array = new Array();
var images_list = "";


$j(document).ready(function(){																
  
  //start left side menu
  $j('#smartnews_data').find('div[id*="smartnews_"]').each(function(ind,ele){
	 _title = $j(ele).find('span[id="title"]').html();
	 _position = ind+1;
	 $j('#smartnews_menu').append('<div id="'+_position+'" class="div_small_title" onClick="javascript:load_news(this.id);">'+_title+'</div>');
 })
  //end left side menu
  
  //preload the news images
  $j('#smartnews_data').find('span[id="image"]').each(function(ind,ele){
		images_array[ind] = $j(ele).html();
   });

  preloadimages();

  //loop through the news
  loop_news();
  interval = window.setInterval("loop_news()",5000);
 });

function preloadimages()
{
	for (i=0;i<images_array.length;i++)
	{
		myimages[i]=new Image(340,240);
		myimages[i].setAttribute('border', 0);
		myimages[i].src=images_array[i];
	}
}

function load_news(id)
{
	current_id = id-1;
	clearInterval(interval);
	loop_news();
	interval = window.setInterval("loop_news()",5000);
}

function loop_news()
{
   current_id++;
   
   if(parseInt(current_id) % total_news == 0)
   {
		current_id = 1;
   }
   
   id = current_id;
   var news_div = $j("#smartnews_" + id);
   
   //Get data
   var subtitle = $j(news_div).find('span[id="subtitle"]');
   var title = $j(news_div).find('span[id="title"]');
   var image = $j(news_div).find('span[id="image"]');
   var caption = $j(news_div).find('span[id="caption"]');
   var source = $j(news_div).find('span[id="source"]');
   var description = $j(news_div).find('span[id="description"]');
   var news_link = $j(news_div).find('span[id="news_link"]');
   var video_link = $j(news_div).find('span[id="video_link"]');
   
   $j("#div_img_caption").hide();
   $j("#sn_caption").hide();
   $j("#div_smart_video").hide();
   $j("#sn_source").hide();
   $j("#sn_more_div").hide();
   $j("#sn_divider").hide();
   
   //Assign data
   $j("#sn_subtitle").html(subtitle.html());
   $j("#sn_title").html(title.html());
   //$j("#sn_image").attr('src',image.html());
   //$j("#sn_image").show();
   
   $j("#sn_image").html(myimages[id-1]);
   $j("#sn_image").show();
  
   $j("#sn_image").fadeOut(0);
   $j("#sn_image").fadeIn(500);

   //if there is a caption
   if(caption.html() != '')
   {
		$j("#sn_caption").html(caption.html());
		$j("#sn_caption").show();
		$j("#div_img_caption").show();
   }
   
   $j("#sn_source").html(source.html());
   $j("#sn_source").show();
   
   $j("#sn_description").html(description.html());
   
   //start assigning the href
   $j("#sn_title").attr('href', news_link.html());
   $j("#sn_subtitle").attr('href', news_link.html());
   $j("#sn_more").attr('href', news_link.html());
   $j("#sn_image").attr('href', news_link.html());
   
   //if there is a video link
   if(video_link.html() != '')
   {
		$j("#div_smart_video").find('a').attr('href', video_link.html());
		$j("#div_smart_video").show();
   }
   
   //remove all the selected and replace it 
   $j("div[class='div_small_title_selected']").each(function(indx,elem){
		$j(elem).removeClass('div_small_title_selected');
		$j(elem).addClass('div_small_title');
   });

   $j("#"+id).removeClass('div_small_title');
   $j("#"+id).addClass('div_small_title_selected');
   
   var top_arrow = ((id-1) * 43) + (id-1);
   $j("#div_img_caption_arrow").attr('class','div_img_caption_arrow');
   $j("#div_img_caption_arrow").css({'top': top_arrow + 'px'}); 
   
   //show more link
   $j("#sn_more_div").show();
   $j("#sn_divider").show();
   $j("#sn_loader").hide();
}

