var homeMenuArrow = null;

//load home page images
function loadHomePage() {
  cbeNewImage('car','../images/home/car.gif');
  //cbeNewImage('text','../images/home/text.gif');
  cbeNewImage('flame','../images/home/flame.gif');

  cbeNewImage('menuTitle','../images/home/menu/title.gif');

  cbeNewImage('categorySelected','../images/home/menu/category_selected.gif');
  cbeNewImage('clientSelected','../images/home/menu/client_selected.gif');
  cbeNewImage('industrySelected','../images/home/menu/industry_selected.gif');

  cbeNewImage('categoryUnselected','../images/home/menu/category_unselected.gif');
  cbeNewImage('clientUnselected','../images/home/menu/client_unselected.gif');
  cbeNewImage('industryUnselected','../images/home/menu/industry_unselected.gif');

  cbeNewImage('categoryCaption','../images/home/menu/category_caption.gif');
  cbeNewImage('clientCaption','../images/home/menu/client_caption.gif');
  cbeNewImage('industryCaption','../images/home/menu/industry_caption.gif');
}

//set the home page images.
function initHomePage() {
  currentMenuSection = 'category';
  homeMenuArrow = cbeGetElementById('home.menu.arrow');
  homeMenuArrow.cbe.resizeTo(15, 7);
  cbeSetImage('home.car.img', 'car');
  //cbeSetImage('home.text.img', 'text');
  homePageIsInit = true;
}

function setMenu() {
  cbeSetImage('home.menu.title.img', 'menuTitle');
  if(currentMenuSection == 'category'){
      homeMenuArrow.cbe.moveTo(36, 27, 300);
      cbeSetImage('home.menu.category.img', 'categorySelected');
      cbeSetImage('home.menu.client.img', 'clientUnselected');
      cbeSetImage('home.menu.industry.img', 'industryUnselected');
  } else if(currentMenuSection == 'client') {
      homeMenuArrow.cbe.moveTo(106, 27);
      cbeSetImage('home.menu.category.img', 'categoryUnselected');
      cbeSetImage('home.menu.client.img', 'clientSelected');
      cbeSetImage('home.menu.industry.img', 'industryUnselected');
  } else {
      homeMenuArrow.cbe.moveTo(176, 27);
      cbeSetImage('home.menu.category.img', 'categoryUnselected');
      cbeSetImage('home.menu.client.img', 'clientUnselected');
      cbeSetImage('home.menu.industry.img', 'industrySelected');
  }
  cbeSetImage('home.menu.caption.img', currentMenuSection+'Caption');
}

/**
 * There are two pages to the application the home page and the main page.
 * The home page is the initial page displayed to the user.
 */
function showHomePage() {
  setMenu();
  mainContainer.cbe.background("#A3A379");
  showFlame();
  showCar();
}

/**
 * Show the flame image.
 */
function showFlame() {
  var flame = cbeGetElementById('home.flame');
  with(flame.cbe) {
    zIndex(3);
    moveTo(0, 324);
    show();
  }
}

/**
 * Show the text.
 */
function showText() {
  var text = cbeGetElementById('home.text').cbe;
  text.moveTo(320, 95);
  //text.fadeIn();
  text.show();
  text.zIndex(0);
}

/**
 * Clears the page of any home images
 */
function clearHomePage() {
  cbeGetElementById('home.flame').cbe.hide();
  cbeGetElementById('home.menu').cbe.hide();
  cbeGetElementById('home.text').cbe.hide();
  driveAwayCar();
}

/**
 * Animate, "drive", the car off the page
 */
function driveAwayCar() {
  //make sure that the main page is initialized first.
  if(mainPageIsInit){
    cbeGetElementById('home.car').cbe.slideTo(containerW, 300, 800, 'cbeGetElementById("home.car").cbe.hide(); cbeGetElementById("home.car").cbe.moveTo(0, 0); showMainPage();');
  }
}

/**
 * Animate the entrance of the car.
 */
function showCar() {
  var car = cbeGetElementById('home.car');
  with(car.cbe) {
    moveTo(-containerW, 0);
    zIndex(0);
    show();
    slideTo(30, 165, 800, "showText(); showHomeMenu();");
  }
}

/**
 * Animate the entrance of the menu on the home page.
 */
function showHomeMenu() {
  setMenu();
  var menu = cbeGetElementById('home.menu');
  homeMenuArrow.cbe.show();
  with(menu.cbe){
    moveTo(320, 330);
    zIndex(2);
    show();
    //If the main page is initialized do it now while there are no animations.
    slideTo(320, 256, 500, "if(!mainPageIsInit){initMainPage();}");
  }
}

function clickMenuItem(itemNm) {
  cbeSetImage("home.menu." + currentMenuSection + '.img', currentMenuSection + 'Unselected');
  cbeSetImage("home.menu." + itemNm + '.img', itemNm + 'Selected');
  if(itemNm == 'client'){
    homeMenuArrow.cbe.slideTo(106, 27, 200, "clearHomePage()");
  } else if(itemNm == 'industry'){
    homeMenuArrow.cbe.slideTo(176, 27, 200, "clearHomePage()");
  } else if(itemNm == 'category'){
    homeMenuArrow.cbe.slideTo(36, 27, 200, "clearHomePage()");
  }
  currentMenuSection = itemNm;
}

function mouseOverMenuItem(itemNm) {
  cbeSetImage('home.menu.caption.img', itemNm + 'Caption');
  cbeSetImage('home.menu.' + currentMenuSection + '.img', currentMenuSection + 'Unselected');
  cbeSetImage('home.menu.' + itemNm + '.img', itemNm + 'Selected');
  currentMenuSection = itemNm;
}
