// Copyright 2002, 2003 Macromedia, Inc. All rights reserved.

//---------------   GLOBAL VARIABLES   ---------------

var helpDoc = MM.HELP_pageKeywordsAndDescription;

var KEYWORD_TAG;
var DESCRIPTION_TAG;

//---------------     API FUNCTIONS    ---------------

//Only run if there's an open document with a DOM
function canAcceptCommand() {
    
    if (dw.isPreviewMode())
        return false;
        
var docExists = false;
  
  if (dw.getDocumentDOM() != null)
  {
	if ( dw.getFocus() != 'browser' && dw.getDocumentDOM().getParseMode() == 'html' && !(CCWorkspaceManager.getManager(dw.getDocumentDOM()).isBlogSite()))
	 docExists = true;
  }

  return docExists;
}

//Return list of buttons to draw
function commandButtons(){
 // return new Array(MM.BTN_OK,"okClicked()", MM.BTN_Cancel, "cancelClicked()", MM.BTN_Help, "displayHelp()");
	return new Array( "PutButtonsOnBottom",
						 "OkButton", MM.BTN_OK,     "okClicked()", 
                         "CancelButton", MM.BTN_Cancel,  "cancelClicked()",
                         "PutButtonOnLeft", MM.BTN_Help,    "displayHelp()");

}

//---------------     LOCAL FUNCTIONS    ---------------

function okClicked()
{
  applyChanges();
  window.close();
}

function cancelClicked()
{
  window.close();
}


function initializeUI()
{
  //initialize metatag pointers
  KEYWORD_TAG = findMetaTag("keywords");
  DESCRIPTION_TAG = findMetaTag("description");

  if (KEYWORD_TAG)
  { //if existing keyword meta tag found, load it's content into this UI
    document.theForm.keywords.value = unencodeQuotes(KEYWORD_TAG.getAttribute("content"));
  }

  if (DESCRIPTION_TAG)
  { //if existing description meta tag found, load it's content into this UI
    document.theForm.description.value = unencodeQuotes(DESCRIPTION_TAG.getAttribute("content"));
  }

}

function applyChanges()
{
  var insertString = "";  //the string of new tags to insert, if any

  //get keywords from the UI
  var keywords = document.theForm.keywords.value;
  if (KEYWORD_TAG)
  { //if existing meta tag, rewrite it's content attribute
    KEYWORD_TAG.setAttribute("content", keywords);
  }
  else if (keywords)
  { //if no keyword meta tag exists, generate a string for the new tag
    insertString += '<meta name="keywords" content="' + encodeQuotes(keywords) + '">';
  }

  //get description from the UI
  var description = document.theForm.description.value;
  if (DESCRIPTION_TAG)
  { //if existing meta tag, rewrite it's content attribute
    DESCRIPTION_TAG.setAttribute("content", description);
  }
  else if (description)
  { //if no description meta tag exists, append a string for the new tag
    insertString += '<meta name="description" content="' + encodeQuotes(description) + '">';
  }

  //if new tag(s) to be inserted, insert them. DW knows that meta tags belong in the HEAD tag
  if (insertString)
  {
    var theDom = dw.getDocumentDOM();

    //special case for wierd bug 77420. For template instances, basic insertHTML() fails.
    //For those cases, just append the new meta tags to the title tag.
    if (theDom.documentElement.outerHTML.indexOf('BeginEditable name="doctitle"') > 0 //if any type of template instance
        || theDom.documentElement.outerHTML.indexOf('BeginEditable "doctitle"') > 0)
    { //force the tags after the TITLE tag.
      var titleTags = theDom.getElementsByTagName("TITLE");
      if (titleTags && titleTags.length>0)
      {
        titleTags[0].outerHTML += insertString;  //add string to title tag
      }
    }
    else
    { //normal case: insertHTML and let DW figure out where to put it 
      // #78528 - but don't replace current selection (added 0 second argument) 
      theDom.insertHTML(insertString,0);
    }
  }
}


//Given a meta tag name (such as "keyword") returns a pointer to the
//first tag with that name, or null if none found.
function findMetaTag(tagName)
{
  var tagPointer = null;  //return value for function
  var theDom = dw.getDocumentDOM();
  var metaTagName;

  if (tagName)  //if tagName passed in
  {
    tagName = tagName.toLowerCase();  //force lowercase

    //find any existing meta tags
    var allMetaTags = theDom.getElementsByTagName("META");
    if (allMetaTags && allMetaTags.length > 0) //if meta tags exist
    {
      for (i=0; i<allMetaTags.length && !tagPointer; i++)  //scan meta tags
      {
        metaTagName = allMetaTags[i].getAttribute("name");
        if (metaTagName)
        {
          metaTagName = metaTagName.toLowerCase(); //force lowercase
          if (metaTagName == tagName) //if meta tag found, point to it (only 1st one)
          {
            tagPointer = allMetaTags[i];
          }
        }
      }
    }
  }
  return tagPointer;
}


function encodeQuotes(theStr)
{
  return theStr.replace(/"/g,"&quot;");
}


function unencodeQuotes(theStr)
{
  return theStr.replace(/\&quot;/g,'"');
}
