// Copyright 2000, 2001, 2002, 2003, 2004 Macromedia, Inc. All rights reserved.

//*************** GLOBALS VARS *****************

var helpDoc = MM.HELP_behSetTextOfLayer;

//******************* BEHAVIOR FUNCTION **********************

//Passed a layer references and a string, replaces layer text.

function MM_setTextOfLayer(objName,x,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

MM.VERSION_MM_setTextOfLayer = 4.01; //define latest version number for behavior inspector

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


//Can be used with any tag and any event

function canAcceptBehavior(){
  var retVal = false;
  var nameArray = getAllObjectRefs("NS 4.0","document","LAYER");  //get layer names (includes CSS)
  if (nameArray.length > 0) retVal = "onMouseOver,(onMouseOver),onClick,(onClick)"; //default events
  return retVal;
}



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

function behaviorFunction(){
  return "MM_findObj,MM_setTextOfLayer";
}



//Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
//Calls escQuotes to find embedded quotes and precede them with \

function applyBehavior() {
  var i,index,objNS,newlinePos,msgStr="",retVal="";
  with (document.theForm) {
    index = menu.selectedIndex;
    objNS = escQuotes(document.MM_NS_REFS[index]); //get layer name from list
    msgStr = escExprStr(message.value,true);
  }
  if (objNS.indexOf(REF_UNNAMED) == 0) retVal = MSG_UnnamedLayer;
  else if (msgStr == null) retVal = MSG_BadBraces;
  else {
    objNS = getNameFromRef(objNS);
    updateBehaviorFns("MM_findObj","MM_setTextOfLayer");
    msgStr = msgStr.replace( /\r/g, '' );
    retVal = "MM_setTextOfLayer('"+objNS+"','','"+msgStr+"')";
  }
  return retVal
}



//Passed the function call above, takes prior arguments and reloads the UI.
//Removes any escape characters "\"

function inspectBehavior(fnStr){
  var argArray,found,numLayers,i;
  var objNS,objIE,startStr,endStr;

  argArray = extractExprStr(fnStr);//get new list of layer,vis pairs
  if (argArray.length == 3) { //3 args
    //set layer obj
    objNS=argArray[0];
    objIE=argArray[1];
    found = false;
    numLayers = document.MM_NS_REFS.length;
    for (i=0; i<numLayers; i++){  //check if layer is in menu
      if (objNS==document.MM_NS_REFS[i] || objNS==getNameFromRef(document.MM_NS_REFS[i])) { //if layer there
        document.theForm.menu.selectedIndex = i;
        found = true;
        break;
      }
    }
    if (!found) alert(errMsg(MSG_LayerNotFound,objNS));

    //set text, converting all string expressions to {expression} etc.
    document.theForm.message.value = unescExprStr(argArray[2],true);
  }
}



//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:
//  objName:  argument is simple object name, such as "Layer1"
//  other...: argument is ignored

function identifyBehaviorArguments(fnCallStr) {
  var argArray, retVal="", fullObjRef;;

  argArray = extractArgs(fnCallStr);
  fullObjRef = (argArray[1].indexOf(".")!=-1);
  if (argArray.length == 4) {
    retVal = (fullObjRef)?"NS4.0ref,IE4.0ref,other" : "objName,other,other";
  }
  return retVal;
}

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


//Load up the layers, set the insertion point

function initializeUI(){
  var i, niceNameSrcArray, nameArray;

	document.MM_NS_REFS = getAllObjectRefs("NS 4.0","document","LAYER"); //store parallel NS refs
	document.MM_IE_REFS = getAllObjectRefs("IE 4.0","document","LAYER"); //store parallel IE refs
	//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.
	niceNameSrcArray = document.MM_NS_REFS;
	for (i=0; i<document.MM_NS_REFS.length; i++) {
		if (document.MM_IE_REFS[i].indexOf(REF_CANNOT) == 0) {
			document.MM_IE_REFS[i] = ""; //blank it out
		}
		if (document.MM_NS_REFS[i].indexOf(REF_CANNOT) == 0) {
			document.MM_NS_REFS[i] = ""; //blank it out
			niceNameSrcArray = document.MM_IE_REFS; //use the IE list
		}
	}
	nameArray = niceNames(niceNameSrcArray,MM.TYPE_Layer);  //get layer names (includes CSS)
	with (document.theForm.menu) {
		for (i=0; i<nameArray.length; i++) options[i]=new Option(nameArray[i]); //load menu
		selectedIndex = 0;
	}
  document.theForm.message.focus(); //set focus on textbox
  document.theForm.message.select(); //set insertion point into textbox
}
