var playing=true; // value representing whether the banner is currently cycling
var transition_speed=4500; // the number of milliseconds between transitions
var image_location="/consumerUS/img" // location where the banner images are kept (no trailing "/")
var banners=new Array("billboard_home_01_betterliving_705x270.jpg","billboard_home_02_longterm_705x270.jpg","billboard_home_03_specialist_705x270.jpg","billboard_home_04_commitment_705x270.jpg"); // array of the images in the banner
var banner_links=new Array("/content/Hearing_Aids/Hearing_Loss/Hearing_Loss_D","/content/Hearing_Aid/Hearing_Aids/Hearing_Aids_B","/content/Hearing_Aids/About_Sonus/About_Sonus","/content/Hearing_Aids/About_Sonus/What_We_Believe_In/What_We_Believe_In");
var current_image=1; // the current image in the array. (initially set to 1 because we want to start playing the second image in the array because the first image is already set in the html)
var banner_img; // the image node;

var preloaded_images=new Array();
for(var i=0;i<banners.length;i++){
  preloaded_images[i]=new Image();
  preloaded_images[i].src=image_location+"/"+banners[i];
}

function go_to_image(n){
  play_transition();
  current_image=n;
  clearInterval(banner_interval);
  next_image();
  banner_interval=setInterval("next_image()",transition_speed);
  return false;
}

function next_image(){
  if(playing==true){
    $(banner_img).fadeTo("500",.01,swap_image);
  }
}

function swap_image(){
  banner_img.src=preloaded_images[current_image].src;
  $("#banner_image_link").attr("href",banner_links[current_image]);
  $(banner_img).fadeTo("500",1);
  current_image=(current_image>=banners.length-1)?0:current_image+1;
}

function play_transition(){
  playing=true;
  return false;
}
  
function stop_transition(){
  playing=false;
  return false;
}

window.onload=function(){
  banner_img=document.getElementById("banner_billboards"); // global variable
  
  if(banner_img){
	  banner_interval=setInterval("next_image()",transition_speed);
	
	  banner_img.onmouseover=function(){
	    stop_transition();
	  };
	
	  banner_img.onmouseout=function(){
	    play_transition();
	  };
	
	  document.getElementById("banner_buttons").onmouseover=function(){
	    stop_transition();
	  };
	
	  document.getElementById("banner_buttons").onmouseout=function(){
	    play_transition();
	  };
	}	


};

