// Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
//*************** GLOBALS VARS *****************

var helpDoc = MM.HELP_behSetNavBarImage;

var NAVBAR_ELEM_DATA = '';
var NAVBAR_ELEM_VIEW = '';

var T = ''; // TabControl object


//******************* BEHAVIOR FUNCTIONS **********************

// MM_nbGroup('init', grpName, imgName, upSrc, [imgName, downSrc]+, preload)
// MM_nbGroup('over',[imgName, overSrc, overDownSrc]+, preload)
// MM_nbGroup('out')
// MM_nbGroup('down', grpName, [imgName, downSrc]+, preload)
//
function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

MM.VERSION_MM_nbGroup = 6.0; //define latest version number for behavior inspector

//******************* API **********************


//Checks for the existence of images.
//If none exist, returns false so this Action is grayed out.

function canAcceptBehavior(behTag){
  var retVal = false;
  var selObj = dw.getBehaviorElement();
  if (!selObj && behTag)
    selObj = dw.getDocumentDOM().getSelectedNode();
  if (selObj && selObj.tagName && selObj.tagName == "IMG")
    retVal = true;
  else if (selObj && selObj.tagName && selObj.tagName == "A" &&
           selObj.getElementsByTagName("IMG").length > 0)
    retVal = true;
  else if (selObj && selObj.tagName && selObj.tagName == "AREA")
    retVal = true;
  if (retVal) retVal = "(onClick),(onMouseOver),(onMouseOut),onLoad";
  return retVal;
}


//Returns Javascript functions to be inserted in HTML head with script tags.

function behaviorFunction(){
  return "MM_preloadImages,MM_findObj,MM_nbGroup"
}



//Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>

function applyBehavior() {
  var behEvent, behObj, retVal = '';

  if (!NAVBAR_ELEM_DATA.isComplete()) {

    retVal = MSG_MissingData;

  } else {

    if (!dw.isReapplyingBehaviors()) {
      T.finish();
    }
      
    behEvent = dw.getBehaviorEvent();
    behObj = dw.getBehaviorElement();
    
    if (behObj) NAVBAR_ELEM_DATA.tagObj = behObj;
      
    NAVBAR_ELEM_DATA.updateObjectTag(behEvent);

    switch ( behEvent ){
    case "onLoad":
      retVal = NAVBAR_ELEM_DATA.getOnLoadCall();
      break;
    case "onMouseOver":
      retVal = NAVBAR_ELEM_DATA.getOnMouseOverCall();
      break;
    case "onMouseOut":
      retVal = NAVBAR_ELEM_DATA.getOnMouseOutCall();
      break;
    case "onClick":
      retVal = NAVBAR_ELEM_DATA.getOnClickCall();
      break;
    default:
    }
    updateBehaviorFns("MM_preloadImages,MM_findObj,MM_nbGroup");
    
  }
  return retVal;
}


//Returns a dummy function call to inform Dreamweaver the type of certain behavior
//call arguments. This information is used by DW to fixup behavior args when the
//document is moved or changed.
//
//It is passed an actual function call string generated by applyBehavior(), which
//may have a variable list of arguments, and this should return a matching mask.
//
//The return values are:
//  URL     : argument could be a file path, which DW will update during Save As...
//  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
//  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
//  other...: argument is ignored

function identifyBehaviorArguments(fnCallStr) {
  var argList = '';
  var args = extractArgs(fnCallStr);
  if (args[1] == 'init' || args[1] == 'down') {
    argList = "other,other";
    for (i=3; i+1 < args.length; i+=2)
      argList += ",objName,DEP";
  } else if (args[1] == 'over') {
    argList = "other";
    for (i=2; i+2 < args.length; i+=3)
      argList += ",objName,DEP,DEP";
  } else if (args[1] == 'out') {
    argList = "other";
  }
  return argList;
}


//Given the original function call, this parses out the args and updates
//the code. If there's a preload id at the end of the arglist, deletes
//the preload handler.

function deleteBehavior(behFnCallStr) {
  var args,imgList = new Array();
  //Maybe remove preload handler
  args = extractArgs(behFnCallStr);//get new list of imgObj,imgSrc pairs
  if (args[1] == 'init' && args[args.length-1] != '0') {
    for (var i=2; i+1 < args.length-1; i+=2)
      imgList.push(args[i+1]);
  } else if (args[1] == 'down' && args[args.length-1] != '0') {
    for (var i=3; i+1 < args.length-1; i+=2)
      imgList.push(args[i+1]);
  } else if (args[1] == 'over' && args[args.length-1] != '0') {
    for (var i=2; i+2 < args.length-1; i+=3) {
      imgList.push(args[i+1]);
      imgList.push(args[i+2]);
    }
  }
  if (imgList.length > 0)
    preloadUpdate("", imgList, 0);
}



//***************** LOCAL FUNCTIONS  ******************

//Load the UI with image names.

function initializeUI() {
  var behObj;

  NAVBAR_ELEM_DATA = new NavBarElem('document','group1');
  
  behObj = dw.getBehaviorElement();
  if (!behObj && dw.getBehaviorTag())
    behObj = dw.getDocumentDOM().getSelectedNode();
  if (behObj && (behObj.tagName == "IMG" || 
                 behObj.tagName == "A" || 
                 behObj.tagName == "AREA")) {
    if (behObj.tagName == "IMG" && behObj.parentNode.tagName == "A")
      behObj = behObj.parentNode;
      
    NAVBAR_ELEM_DATA.parseObjectTag(behObj);

    if (!dw.isReapplyingBehaviors()) {  // only setup UI if not re-applying

      NAVBAR_ELEM_VIEW = new NavBarElemView(document, NAVBAR_ELEM_DATA);

      var tab0 = findObject("Tab0");
      var tab1 = findObject("Tab1");
    
      //Use appropriate background & tabs for Mac OS X.
      if (dw.isOSX()) {
        findObject("tabBgWin").src = "../../Shared/MM/Images/tabBgOSX505x410.gif";
        var oldMulti = RegExp.multiline;
        RegExp.multiline = true;
        var pat1 = /tabBg\.gif/;
        tab0.innerHTML = tab0.innerHTML.replace(pat1, "tabBgOSX.gif");
	      tab1.innerHTML = tab1.innerHTML.replace(pat1, "tabBgOSX.gif");
        var pat2 = /tabBgSel\.gif/;
        tab0.innerHTML = tab0.innerHTML.replace(pat2, "tabBgSelOSX.gif");
	      tab1.innerHTML = tab1.innerHTML.replace(pat2, "tabBgSelOSX.gif");
	      RegExp.multiline = oldMulti;
    	  var bgImage = findObject("tabBgWin");
	      bgImage.height = 330;
	      window.resizeToContents();
      // Use appropriate background & tabs for WinXP with themes  
      } else if (dw.isXPThemed()) {	
        findObject("tabBgWin").src = "../../Shared/MM/Images/tabBgWinXP.gif";
        var tab0 = findObject("Tab0");
        var tab1 = findObject("Tab1");
    
        var oldMulti = RegExp.multiline;
        RegExp.multiline = true;
        var pat1 = /tabBg\.gif/;
        tab0.innerHTML = tab0.innerHTML.replace(pat1, "tabBgXP.gif");
	      tab1.innerHTML = tab1.innerHTML.replace(pat1, "tabBgXP.gif");
        var pat2 = /tabBgSel\.gif/;
        tab0.innerHTML = tab0.innerHTML.replace(pat2, "tabBgSelXP.gif");
	      tab1.innerHTML = tab1.innerHTML.replace(pat2, "tabBgSelXP.gif");
	      RegExp.multiline = oldMulti;
      // Use standard background  
      } else {	
        findObject("tabBgWin").src = "../../Shared/MM/Images/tabBgWin.gif";
      }

      // Initialize the TabControl.  (Pass in the prefix used for the tab layers)
      T = new TabControl('Tab');

      // Add tab pages.
      T.addPage('page1', new ElemControl(LABEL_Basic));
      T.addPage('page2', new AdvControl(LABEL_Advanced));

      // Initialize and display the tabs.
      T.start();

  } }
}


function browseFile(itemName, isImage) {
  var item = findObject(itemName);
  if (item != null) {
    var result = dw.browseForFileURL('select', (isImage) ? LABEL_getImage : LABEL_getURL, isImage);
    if (result) {
      item.value = result;
      T.update(itemName);
  } }
}


/* ------------------------------------------------------------------*/

function ElemControl(theTabLabel) {
  this.tabLabel = theTabLabel;
}

ElemControl.prototype.getTabLabel = ElemControl_getTabLabel;
ElemControl.prototype.load = ElemControl_load;
ElemControl.prototype.update = ElemControl_update;

function ElemControl_getTabLabel() {
  return this.tabLabel;
}

function ElemControl_load() {
  NAVBAR_ELEM_VIEW.display();
}

function ElemControl_update(itemName) {
  NAVBAR_ELEM_VIEW.update(itemName);
}


/* ------------------------------------------------------------------*/

function AdvControl(theTabLabel) {
  this.tabLabel = theTabLabel;
  this.imgEvent = new ListControl('imgEvent');
  this.imgEvent.setAll(MENULIST_imageEvents);
  this.downImgLabelObj = findObject('downImgLabel');
  this.downImgLabel = this.downImgLabelObj.innerHTML;
  this.downImgInputObj = findObject('downImgInput');
  this.downImgInput = this.downImgInputObj.innerHTML;
  this.downList = new ElemListView('elemList', NAVBAR_ELEM_DATA.downImages, 1);
  this.overList = new ElemListView('elemList', NAVBAR_ELEM_DATA.overImages, 2);
}

AdvControl.prototype.getTabLabel = AdvControl_getTabLabel;
AdvControl.prototype.load        = AdvControl_load;
AdvControl.prototype.update      = AdvControl_update;
AdvControl.prototype.display     = AdvControl_display;
AdvControl.prototype.lastUnload  = AdvControl_lastUnload;

function AdvControl_getTabLabel() {
  return this.tabLabel;
}

function AdvControl_load() {
  var nameSpan = findObject('nameSpan');
  if (nameSpan) {
    T.obj.visibility = 'hidden';  // work around DW3 bug.
    nameSpan.innerHTML = NAVBAR_ELEM_DATA.imgName;
    T.obj.visibility = 'visible';
  }
  this.display();
}

function AdvControl_display() {
  if (this.imgEvent.getIndex() == 0) {
    // show extra control
    T.obj.visibility = 'hidden';  // work around DW3 bug.
    this.downImgLabelObj.innerHTML = this.downImgLabel;
    this.downImgInputObj.innerHTML = this.downImgInput;
    T.obj.visibility = 'visible';
    this.overList.load();
    this.overList.display();
  } else {
    // hide extra control
    T.obj.visibility = 'hidden';  // work around DW3 bug.
    this.downImgLabelObj.innerHTML = '';
    this.downImgInputObj.innerHTML = '';
    T.obj.visibility = 'visible';
    this.downList.load();
    this.downList.display();
  }
}

function AdvControl_update(itemName) {
  if (itemName == 'imgEvent') {

    this.display();

  } else if (itemName == 'elemList' ||
             itemName == 'file0' ||
             itemName == 'file1') {
    if (this.imgEvent.getIndex() == 0){
      this.overList.update(itemName);
    }else{
      this.downList.update(itemName);
    }
  }
}

function AdvControl_lastUnload() {
  NAVBAR_ELEM_DATA.overImages = this.overList.getInfo();
  NAVBAR_ELEM_DATA.downImages = this.downList.getInfo();
}


/* ------------------------------------------------------------------*/

function ElemListView(theItemName, theInfo, imagesPerName) {
  this.elemList = new ListControl(theItemName);
  this.imagesPerName = imagesPerName;
  this.imgNiceNames = new Array();
  this.imgInfo = new Array();
  this.setElemList();
  this.setInfo(theInfo);
}

ElemListView.prototype.setInfo = ElemListView_setInfo;
ElemListView.prototype.getInfo = ElemListView_getInfo;
ElemListView.prototype.load    = ElemListView_load;
ElemListView.prototype.display = ElemListView_display;
ElemListView.prototype.update  = ElemListView_update;
ElemListView.prototype.setElemList = ElemListView_setElemList;


//Set the imgInfo array
function ElemListView_setInfo(theInfo) {
  var currInfo, currName = '';
  with (this) {
    for (var i=0; i < theInfo.length; i++) {
      if (i % (imagesPerName+1) == 0) {
        currName = theInfo[i];
        currInfo = '';
        for (var j=0; j < imgInfo.length; j++)
          if (currName == imgInfo[j].name) {
            currInfo = imgInfo[j];
            break;
          }
      } else {
        if (currInfo) {
          if (!currInfo.images) currInfo.images = new Array();
          currInfo.images.push(theInfo[i]);
  } } } }
}


//Get the array of image names and values
function ElemListView_getInfo() {
  var newName, retVal = new Array();
  with (this) {
    for (var i=0; i < imgInfo.length; i++) {
      if (imgInfo[i].images) {
        imgInfo[i].name = imgInfo[i].obj.name;
        if (!imgInfo[i].name) { // name the image
          newName = getUniqueName("Image", createObjsArray("IMG"));
          imgInfo[i].obj.name = newName;
          imgInfo[i].name = newName;
        }
        retVal.push(imgInfo[i].name);
        for (var j=0; j < imagesPerName; j++)
          retVal.push(imgInfo[i].images[j]);
  } } }
  return retVal;
}


//Display the list of images
function ElemListView_load() {
  with (this) {
    file0 = findObject('file0');
    file1 = findObject('file1');
    elemList.setAll(imgNiceNames);
  }
}


//Update the image list display
function ElemListView_display(index) {
  if (this.imgInfo.length > 0) with (this) {
    index = (index != null) ? index : elemList.getIndex();

    // mark the set images
    for (var i=0; i < imgInfo.length; i++) {
      if (imgInfo[i].images)
        elemList.set(imgNiceNames[i] + " *", i);
      else
        elemList.set(imgNiceNames[i], i);
    }
    elemList.setIndex(index);

    // display the text fields
    if (imgInfo[index].images) {
      file0.value = imgInfo[index].images[0];
      if (imagesPerName > 1){
        file1.value = imgInfo[index].images[1];
      }
    } else {
      file0.value = '';
      if (imagesPerName > 1) file1.value = '';
  } }
}


//Update the values in the imgInfo array
function ElemListView_update(itemName) {
  var index = this.elemList.getIndex();
  if (this.imgInfo.length > 0) with (this) {
    if (itemName == 'elemList') {
      display(index);
    } else if (itemName == 'file0') {
      if (!imgInfo[index].images) imgInfo[index].images = new Array();
      imgInfo[index].images[0] = stripSpaces(file0.value);
      if (imgInfo[index].images[1] == null)
        imgInfo[index].images[1] = '';
      display(index);
    } else if (itemName == 'file1') {
      if (!imgInfo[index].images) imgInfo[index].images = new Array();
      imgInfo[index].images[1] = stripSpaces(file1.value);
      display(index);
    }
    if (imgInfo[index].images &&
        !imgInfo[index].images[0] && !imgInfo[index].images[1])
      imgInfo[index].images = ['',''];
  }
}


//Set the imgNiceNames and imgNames arrays
function ElemListView_setElemList() {
  var name, refArray, objArray, node, niceNameSrcArray = new Array();
  var nsRefs = getAllObjectRefs("NS 4.0","IMG");
  var ieRefs = getAllObjectRefs("IE 4.0","IMG");

  //Search for unreferenceable objects.
  // <DIV id="foo"> is IE only, <LAYER> is NS only.
  //if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
  refArray = nsRefs;
  for (var i=0; i < nsRefs.length; i++) {
    if (ieRefs[i].indexOf(REF_CANNOT) == 0)
      ieRefs[i] = ""; //blank it out
    if (nsRefs[i].indexOf(REF_CANNOT) == 0) {
      nsRefs[i] = ""; //blank it out
      refArray = ieRefs; //use the IE list
    }
  }
  objArray = createObjsArray("IMG");
  for (var i=0; i < refArray.length; i++) {
    if (refArray[i]) {
      name = getNameFromRef(refArray[i]);
      if (name != NAVBAR_ELEM_DATA.imgName && objArray[i] != NAVBAR_ELEM_DATA.tagObj) {
        node = new Object();
        node.name = name;
        node.images = '';
        node.obj = objArray[i];
        this.imgInfo.push(node);
        niceNameSrcArray.push(refArray[i]);
  } } }
  this.imgNiceNames = niceNames(niceNameSrcArray, MM.TYPE_Image);
}

//Return an array of objects with the given tag name in this document and
// its enclosing frames.
function createObjsArray(tagName) {
  var frameListLen,objsArray=new Array(),thisFrame;
  if (dw.getDocumentDOM('parent')){//if frames
    frameListLen = dw.getDocumentDOM('parent').getElementsByTagName('frame').length;
    for (i=0;i<frameListLen;i++){
      thisFrame = 'parent.frames[' + i + ']';
      objsArray = objsArray.concat(dw.getDocumentDOM(thisFrame).getElementsByTagName(tagName));
    }
  } else //if no frames
    objsArray = dw.getDocumentDOM("document").getElementsByTagName(tagName);
  return objsArray;
}

//Return a unique name based on the tagString
function getUniqueName(tagString,tagNameObjsArray) {
  var frameListSize,objName,dupe=true,counter=1;
  while (dupe==true){ //check new name against name of all other tagName objs
    dupe=false;
    objName = tagString + counter++;
    //iterates through possible names: tagName1, then tagName2, etc.
    for (var i=0; dupe == false && i < tagNameObjsArray.length; i++){
      //if another object of this type has the same name
      if (tagNameObjsArray[i].getAttribute("name") == objName)
        dupe=true; //then repeat the loop, trying a new name
  } }
  return objName; //return new name
}
