// Main Image
var current_image = 'am';
var fading = new Array();
fading['am'] = false;
fading['manufacturing'] = false;
fading['military'] = false;
fading['transit'] = false;
fading['agriculture'] = false;
fading['events'] = false;
fading['process_and_energy'] = false;

var icon_state= new Array();
icon_state['am'] = false;
icon_state['manufacturing'] = false;
icon_state['military'] = false;
icon_state['transit'] = false;
icon_state['agriculture'] = false;
icon_state['events'] = false;
icon_state['process_and_energy'] = false;

var slideshow_timer = null;

// Rewrite the images to use the javascript-enabled image maps.
function rewrite_maps()
{
  $(image_am).useMap = '#homemap_am_js';
  $(image_manufacturing).useMap = '#homemap_manufacturing_js';
  $(image_agriculture).useMap = '#homemap_agriculture_js';
}

// We have these functions so at least the status bar doesn't look too bad.
function ShowAstonMartin()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('am', false);
}

function ShowManufacturing()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('manufacturing', false);
}

function ShowMilitary()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('military', false);
}

function ShowTransit()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('transit', false);
}

function ShowAgriculture()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('agriculture', false);
}

function ShowEvents()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('events', false);
}

function ShowProcessAndEnergy()
{
  if (slideshow_timer != null) clearTimeout(slideshow_timer);
  select_image('process_and_energy', false);
}


function select_image(image, do_expand_icon)
{
  if (current_image != image && !fading[image])
  {
    fading[image] = true;
    var new_image_tag = 'image_' + image;
    var current_image_tag = 'image_' + current_image;
    $(current_image_tag).className = 'shown_image banner';
    $(new_image_tag).className = 'appearing_image banner';
    $(current_image_tag).fade({duration: 1.0, afterFinish: function() { after_fade(image, current_image_tag, new_image_tag); } });
    var old_image = current_image;
    current_image = image;
    contract_icon(old_image);
    if (do_expand_icon)
    {
      expand_icon(current_image);
    }
  }
}

function after_fade(image, image_to_hide, image_to_show)
{
  $(image_to_show).className = 'shown_image banner';
  $(image_to_hide).className = 'hidden_image banner';
  $(image_to_hide).style.display = 'block';
  fading[image] = false;
}

function expand_icon(icon)
{
  var icon_id = 'icon_' + icon;
  //$(icon_id).style.height = '45px';
  new Effect.Morph(icon_id, { 'style': 'height: 45px', 'duration': 0.2 });
}

function contract_icon(icon)
{
  if (icon == current_image) return;
  var icon_id = 'icon_' + icon;
//  $(icon_id).morph('height: 35px');
  new Effect.Morph(icon_id, { 'style': 'height: 30px', 'duration': 0.2 });
}

function slideshow()
{
  slideshow_timer = setTimeout(slide1, 10000);
}

function slide1() { select_image('manufacturing',      true); slideshow_timer = setTimeout(slide2, 10000); }
function slide2() { select_image('military',           true); slideshow_timer = setTimeout(slide3, 10000); }
function slide3() { select_image('transit',            true); slideshow_timer = setTimeout(slide4, 10000); }
function slide4() { select_image('agriculture',        true); slideshow_timer = setTimeout(slide5, 10000); }
function slide5() { select_image('events',             true); slideshow_timer = setTimeout(slide6, 10000); }
function slide6() { select_image('process_and_energy', true); slideshow_timer = setTimeout(slide7, 10000); }
function slide7() { select_image('am', true); }

// News Ticker

var current_article = 0;
var total_articles = 0;
var first_run = true;
var timer = null;

function news_ticker(article_count)
{
  total_articles = article_count;

  change_article();
}

function change_article()
{
  if (first_run)
  {
    first_run = false;
  }
  else
  {
    var current_index = current_article;
    ++current_article;
    if (current_article >= total_articles) current_article = 0;

    var current_id = 'article_link_' + current_index;
    var next_id = 'article_link_' + current_article;
    transition_article(current_id, next_id);
  }
  timer = setTimeout(change_article, 4000);
}

function transition_article(old_id, new_id)
{
  $(old_id).className = '';
  $(old_id).fade({duration: 0.3 });
  $(new_id).appear({duration: 0.4});
}

function stop_timer()
{
  clearTimeout(timer);
}

function start_timer()
{
  timer = setTimeout(change_article, 2000);
}
