var btnptr = null;

var bHeight = blank._height;
var additionalVSpace = 1;
var clickedButton = 0;

var totalSteps = _root.AnswerArray[_root.AnswerArray.length - 1][0].stepnum;

_root.ShowMe._visible = false;


populateTopic();
menuInitialize();

//making the first step as default
_root.stepnum = 0;
_root.showInstruction(_root.stepnum);
_root.loadImage(_root.stepnum,null); // no previous image
clickedButton = _root.stepnum;
//eval("blank.buttonlist.step" + _root.stepnum).gotoAndStop(2);


// initialize device select buttons
//initializeDeviceButtons();
function initializeDeviceButtons()
{
    /* Don't run this function since we don't have any more devices to initialize...
    Dont't waste CPU cycles
    device0.deviceName = routerInfoArray[0].hostName;
    device0.deviceNum = 0;
    device0.gotoAndStop(2);

    for (var i=1; i<routerInfoArray.length; i++)
    {
        device0.duplicateMovieClip("device" + i, 6000 + i);
        eval("device" + i).deviceName = routerInfoArray[i].hostName;
        eval("device" + i).deviceNum = i;
        eval("device" + i)._x = device0._x + device0._width * i;
    }
    */
}



function menuInitialize()
{
    var numOfSteps = AnswerArray.length - 1;

    // if we have more than 24 buttons, we have to allow for scroll, otherwise turn it off
    if (totalSteps <= 15)
        scrollDisable();

    blank.buttonlist.attachMovie("stepblock","stepblock","8000");

    // miwang
    AnswerArray[0][0].stepnum = 0;
    var j = 0;
    blank.buttonlist.attachMovie("step","step" + j, j);
    eval("blank.buttonlist.step" + j).text = j;
    eval("blank.buttonlist.step" + j).num = 0;
    eval("blank.buttonlist.step" + j)._y = j * (eval("blank.buttonlist.step" + j)._height + additionalVSpace);
    j++;

    for (i = 1; i <= numOfSteps; i++)
    {
trace("i=" + i + ", j=" + j);
        // miwang
        // if step number is different from the previous element's, then add new button
        if (AnswerArray[i][0].stepnum != AnswerArray[i-1][0].stepnum)
        {
            blank.buttonlist.attachMovie("step","step" + j, j);
            eval("blank.buttonlist.step" + j).text = j;
            eval("blank.buttonlist.step" + j).num = i;

            eval("blank.buttonlist.step" + j)._y = j * (eval("blank.buttonlist.step" + j)._height + additionalVSpace);

            j++;
        }
    }

    _root.coverButton(0);
  /*
  additionalHSpace = 12;
  for (i = 0; i <= totalSteps; i++)
  {
    // Once we hit 24, we need to start a new column of buttons
    if (i == 24)
    {
      bHeight = 0;
    }

    if (i < 24)
    {
      blank.attachMovie("step", "step" + i, i);
      eval("blank.step" + i).text = i;

      eval("blank.step" + i)._y = bHeight;
      bHeight = blank._height + additionalVSpace;
    }
    else
    {
      blank.attachMovie("step", "step" + i, i);
      eval("blank.step" + i).text = i;

      eval("blank.step" + i)._y = bHeight;
      bHeight += eval("blank.step" +i)._height + additionalVSpace;
      eval("blank.step" + i)._x = additionalHSpace;
    }
  }
  */
}

function scrollDisable()
{
    blank.scrollUp._visible = false;
    blank.scrollDown._visible = false;
    blank.scrollbar._visible = false;
}

function stepscrollUp()
{
//trace("buttonlist _y = " +blank.buttonlist._y);
//trace("buttonlist _height = " + blank.buttonlist._height);
//trace("buttonlist _y + _height = " + (blank.buttonlist._y + blank.buttonlist._height));

//trace("blank _y = " +blank._y);
//trace("blank _height = " + blank._height);
//trace("blank _y + _height = " + (blank._y + blank._height));
      if (blank.buttonlist._y <= -12)
      {
        blank.buttonlist._y = blank.buttonlist._y + 12;
        step_setSlider(blank.buttonlist);
      }
}

function stepscrollDown()
{
//trace("buttonlist _y = " +blank.buttonlist._y);
//trace("buttonlist _height = " + blank.buttonlist._height);
//trace("buttonlist _y + _height = " + (blank.buttonlist._y + blank.buttonlist._height));

trace("blank _y = " +blank._y);
trace("blank _height = " + blank._height);
trace("blank _y + _height = " + (blank._y + blank._height));

    if (blank.buttonlist._y +blank.buttonlist._height > 259)
    {
        blank.buttonlist._y = blank.buttonlist._y - 12;
        step_setSlider(blank.buttonlist);
    }
}

function step_setScroll(slider)
{
  //trace("Y = " +slider._y);


  _root.blank.buttonlist._y = (1-(slider._y/450) * (_root.blank.buttonlist._height-271.4));
  trace("buttonlist Y = " + _root.blank.buttonlist._y);
}

function step_setSlider(buttonlist)
{
  //trace("Y = " +slider._y);
  //trace("buttonlist Y = " + _root.menu.buttonlist._y);

  _root.blank.scrollbar.slider._y = (425 * ((Math.abs(_root.blank.buttonlist._y))/(_root.blank.buttonlist._height-271.4)));
  trace("buttonlist._y = " + _root.blank.buttonlist._y);
  trace("buttonlist._height = " + _root.blank.buttonlist._height);
  trace("scrollbar slider._y = " + _root.blank.scrollbar.slider._y);
  trace("scrollbar._height = " + _root.blank.scrollbar._height);
}

function stepClick(text)
{
    // reset the matrix check array
    matrix = null;
    var prevStep = _root.stepnum; // THIS is important for remembering the previous step.

    //check to see if he has clicked on the same step
    i = Number(text);
    if (_root.stepnum == i)
    {
        matrix = null;
        _root.showInstruction(_root.stepnum);
        return;
    }
    clickedButton = i;
    _root.stepnum = i;

    //begin loading the images is done here
    _root.loadImage(_root.stepnum, prevStep);
    //end loading the images is done here

    //check to see if the answer button is clicked if so then display the answer
    if (showAns == true)
    {
        //if it step zero then reset everything
        if (i > 0)
        {
            showAnswer(i);
        }
        else
        {
            _root.showInstruction(_root.stepnum);
            answerInitialize(false);
            _root.exitRouter();
            _root.menu.DoneButton.gotoAndPlay("Show");
            showAns = false;
            _root.ShowMe._visible = false;

            // miwang
            _root.coverButton(0);
        }
    }
    else
    {
        _root.goToNextStep(_root.stepnum, prevStep);

        //add for step 0
        _root.showInstruction(_root.stepnum);
        if (_root.stepnum == 0)
        {
            _root.exitRouter();
        }
        else
        {
            var rptr = eval("_root.r" + _root.VISIBLE_ROUTER);
            rptr.lineIndexCounter = 0;

            _root.commandline_setMode(_root.stepModeArray[_root.stepnum], _root.active_router);
            output_write("\n");
            _root.commandline_changeProcess("commandline_processCommandLine");
            _root.commandline_commandLine();
        }
    }
}

function coverButton(step)
{
    blank.buttonlist.stepblock._visible = true;
    blank.buttonlist.stepblock._x = eval("blank.buttonlist.step" + step)._x;
    blank.buttonlist.stepblock._y = eval("blank.buttonlist.step" + step)._y;
    blank.buttonlist.stepblock.text = step;
}

function stepRollOver()
{
/*
  x = _xmouse;
  y = _ymouse;

  for (i = 0; i <= totalSteps; i++)
  {
    found = eval("blank.buttonlist.step" + i).hitTest (x, y, false);
    if (found)
    {
      eval("blank.buttonlist.step" + i).gotoAndStop(2);
    }
  }
  */
}




function stepRollOut()
{
/*
  x = _xmouse;
  y = _ymouse;

  for (i = 0; i <= totalSteps; i++)
  {
    found = eval("blank.buttonlist.step" + i).hitTest (x, y, false);
    if (!found)
    {
      if (i != clickedButton)
        eval("blank.buttonlist.step" + i).gotoAndStop(1);
    }
  }
  */
}


function exitRouter()
{
    // normal exit
    var r = eval("_root.r" + _root.active_router);
    output_write("\n");
    r.userHistory = new Array();
    r.configHistory = new Array();
    _root.commandline_changeProcess("router_startup_processStartUp");
    eval("_root.r" + _root.active_router + ".router_startup") = false; // forcing to be restarted
    router_startup_checkStartUp();
    _root.disableAllSteps()
}


function disableAllButtons()
{
/*
  for (i = 1; i <= totalSteps; i++)
  {
      eval("blank.buttonlist.step" + i)._visible = false;
  } */
  blank.buttonlist.stepblock._visible = false;
}


function populateTopic()
{
    _root.menu.topicCovered = _root.chapterCovered ;
    if (_root.wantShowTopology == false)
        _root.menu.TopologyButton._visible = false;

}
