// Copyright (C) 2010 Aaron Merriman. All rights reserved.

var label_set     = new Array(false,false,false),
    target_date   = 0,
    photo_item    = new Array(2,2,2),
    photo_count   = new Array(14,15,11),
    photo_name    = new Array('left','center','right'),
    photos_loaded = new Array(false,false,false),
    fade_flash    = new Array(false,false,false),
    flash_opacity = new Array(1,1,1),
    sponsor_list  = new Array('Break-Time,http://www.mfaoil.com/',
                              'Budweiser,http://www.budweiser.com/',
                              'AMEC,http://www.amec.org/',
                              'Farm-Bureau,http://www.mofb.org/',
                              'Midwest-Dairy,http://www.midwestdairy.com/',
                              'MoDOT,http://www.modot.mo.gov/',
                              'Missouri-Tourism,http://visitmo.com/',
                              'Missouri-Lottery,http://www.molottery.com/',
                              'Pepsi,http://www.pepsi.com/',
                              'Sedalia-Democrat,http://www.sedaliademocrat.com/',
                              'Toyota,http://www.toyota.com/',
                              'John-Deere,http://www.deere.com/'),
    sponsor_pos   = Math.floor(Math.random()*sponsor_list.length) * -69;

if (document.Images) {
 temp_image_01     = new Image();
 temp_image_01.src = "/images/bkgd_menu_pri.jpg";
 temp_image_02     = new Image();
 temp_image_02.src = "/images/bkgd_menu_sec.jpg";
 temp_image_03     = new Image();
 temp_image_03.src = "/images/photo_left_00.jpg";
 temp_image_04     = new Image();
 temp_image_04.src = "/images/photo_center_00.jpg";
 temp_image_05     = new Image();
 temp_image_05.src = "/images/photo_right_00.jpg";
}

function dissolve_flash(flash_number) {
 document.getElementById('fl_0' + (flash_number + 1)).style.opacity = flash_opacity[flash_number];
 flash_opacity[flash_number] -= .25;
 if (flash_opacity[flash_number] < 0) {
  flash_opacity[flash_number] = 1;
  clearInterval(fade_flash[flash_number]);
 }
}

function start_flash(flash_item) {
 document.getElementById('fl_0' + (flash_item + 1)).style.opacity = "1";
 fade_flash[flash_item] = setInterval('dissolve_flash(' + flash_item + ')',42);
}

function rotate_photos(photo_series) {
 var photo_number  = photo_item[photo_series],
     photo_preload = photo_number + 1;

 if (photo_number < 10) {
  photo_number = '0' + photo_number;
 }
 if (photo_preload > photo_count[photo_series]) {
  photos_loaded[photo_series] = true;
 }
 if (photo_preload < 10) {
  photo_preload = '0' + photo_preload;
 }
 start_flash(photo_series);
 document.getElementById('ph_0' + (photo_series + 1)).style.backgroundImage = "url('/images/photo_" + photo_name[photo_series] + "_" + photo_number + ".jpg')";
 photo_item[photo_series]++;
 if (photo_item[photo_series] > photo_count[photo_series]) {
  photo_item[photo_series] = 1;
 }
 if (!photos_loaded[photo_series]) {
  temp_val = new Image();
  temp_val.src = '/images/photo_' + photo_name[photo_series] + '_' + photo_preload + '.jpg';
 }
}

function start_photos() {
 for (var temp_val = 1; temp_val < 4; temp_val++) {
  document.getElementById('fl_0' + temp_val).style.opacity = "0";
  document.getElementById('ph_0' + temp_val).style.backgroundImage = "url('/images/photo_" + photo_name[temp_val - 1] + "_01.jpg')";
 }
 setInterval('rotate_photos(0)',5200);
 setTimeout("setInterval('rotate_photos(1)',5200)",700);
 setTimeout("setInterval('rotate_photos(2)',5200)",1400);
}

function random_photos() {
 for (var temp_val = 1; temp_val < 4; temp_val++) {
  var random_val = Math.floor(Math.random()*photo_count[temp_val - 1] + 1);

  if (random_val < 10) {
   random_val = '0' + random_val;
  }
  document.getElementById('fl_0' + temp_val).style.display = "none";
  document.getElementById('ph_0' + temp_val).style.backgroundImage = "url('/images/photo_" + photo_name[temp_val - 1] + "_" + random_val + ".jpg')";
 }
}

function hide_menus() {
 document.getElementById('menu_01').style.display = "none";
 document.getElementById('menu_02').style.display = "none";
 document.getElementById('menu_03').style.display = "none";
 document.getElementById('nt_01').style.backgroundPosition = "left top";
 //document.getElementById('nt_02').style.backgroundPosition = "-176px top";
 //document.getElementById('nt_03').style.backgroundPosition = "-350px top";
}

function show_menu(menu_num) {
 var button_num = '';

 hide_menus();
 document.getElementById('menu_0' + menu_num).style.display = "block";
 switch (menu_num) {
  case 1:
   button_num = 'left bottom';
  break;
  case 2:
   button_num = '-176px bottom';
  break;
  case 3:
   button_num = '-350px bottom';
  break;
 }
 document.getElementById('nt_0' + menu_num).style.backgroundPosition = button_num;
}

function countdown() {
 var current_date = new Date(),
     decrement    = false,
     temp_val     = 0,
     remaining    = new Array(0,0,0,0);

 if (current_date > 1313150400000) {
  document.getElementById('countdown').style.display = "none";
  clearTimeout(decrement);
 } else if (current_date > 1282539599000) {
  if (!label_set[2]) {
   label_set[2] = true;
   document.getElementById('countdown').style.display = "block";
   document.getElementById('countdown').style.backgroundPosition = "-1000px top";
   target_date = 1313150400000;
  }
 } else if (current_date > 1281614400000) {
  if (!label_set[1]) {
   label_set[1] = true;
   document.getElementById('countdown').style.display = "block";
   document.getElementById('countdown').style.backgroundPosition = "-250px top";
   target_date = 1282539599000;
  }
 } else if (!label_set[0]) {
  label_set[0] = true;
  document.getElementById('countdown').style.display = "block";
  document.getElementById('countdown').style.backgroundPosition = "left top";
  target_date = 1281614400000;
 }
 temp_val     = target_date - current_date;
 remaining[0] = Math.floor(temp_val/(60*60*1000*24)) + '';
 remaining[1] = Math.floor((temp_val%(60*60*1000*24))/(60*60*1000)) + '';
 remaining[2] = Math.floor(((temp_val%(60*60*1000*24))%(60*60*1000))/(60*1000)) + '';
 remaining[3] = Math.floor((((temp_val%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000) + '';
 while (remaining[0].length < 3) {
  remaining[0] = '<span style="color: #ffffff;">0</span>' + remaining[0];
 }
 document.getElementById('cd_01').innerHTML = remaining[0];
 for (temp_val = 1; temp_val < 4; temp_val++) {
  if (remaining[temp_val].length < 2) {
   remaining[temp_val] = '0' + remaining[temp_val];
  }
  remaining[temp_val] = ':' + remaining[temp_val];
  document.getElementById('cd_0' + (temp_val + 1)).innerHTML = remaining[temp_val];
 }
 decrement = setTimeout('countdown()',1000);
}

function show_photo(photo_num) {
 if (photo_num < 10) {
  photo_num = '0' + photo_num;
 }
 document.getElementById('gallery_photo').innerHTML = '<img src="/images/gallery_' + photo_num + '.jpg" alt="" />';
 location.href="#content";
}

function scroll_sponsors(list_height) {
 document.getElementById('sponsor_list').style.marginTop = sponsor_pos + 'px';
 sponsor_pos = sponsor_pos - 2;
 if (sponsor_pos < list_height * -69) {
  sponsor_pos = -2;
 }
}

function start_scrolling() {
 sponsor_motion = setInterval('scroll_sponsors(' + sponsor_list.length + ')',125);
}

function rotate_sponsors() {
 var list_count = 0;

 function build_list(build_count) {
  for (temp_val = 0; temp_val < build_count; temp_val++) {
   var sponsor_val  = sponsor_list[temp_val].split(','),
       sponsor_link = new Array('','');

   if (sponsor_val[1]) {
    sponsor_link[0] = '<a href="' + sponsor_val[1] + '">';
    sponsor_link[1] = '</a>';
   }
   document.write('<div class="sponsor_image">' + sponsor_link[0] + '<img src="/images/sponsor_' + sponsor_val[0] + '.png" alt="' + sponsor_val[0] + '" />' + sponsor_link[1] + '</div>');
   list_count++;
  }
 }

 build_list(sponsor_list.length);
 build_list(2);
 start_scrolling();
}

