/**
 * Global Javascript for page functionalities, export version.
 * This special version only supports functionality available after a
 * site has been exported, i.e. no IS/6 functionality or any URL
 * pointing back to IS/6.
 * Contains all the functionality supported by <code>util.js</code>
 * but with only dummy placeholders for the IS/6 functionality.
 * <br/>
 * This file will be part of exported sites.
 */

var utiljs_ = true;

var utiljs_add_page_dialog_url_ = "#";
var utiljs_post_page_url_ = "#";
var utiljs_delete_page_url_ = "#";
var utiljs_get_message_url_ = "#";
var utiljs_get_credentials_url_ = "#";
var utiljs_insert_comment_url_ = "#";
var utiljs_delete_comment_url_ = "#";

var utiljs_messages_ = {default_remove_page: "Are you sure you want to permanently delete the selected items?",
                        default_remove_comment: "Are you sure you want to permanently delete the comment?"};

/**
 * Assembles URL's for util.xsl templates, takes edit mode of mediator into consideration...
 */
function utiljs_getAppUrl(aParam) {
  var path = window.location.pathname.match(/([^;])*/)[0];

  // add the parameter supplied
  path += aParam ? aParam : "";

  // add editmode param if necessary
  var url = utiljs_addEditMode(path);

  return url;
}

function utiljs_addChooserMode(url)
{
  var chooser_mode;
  try {
    chooser_mode = com.hyperwave.wcm.csjs.WorkspaceMediator.static_.getInstance().isChooserMode();
  } catch(e) {
    chooser_mode = false;
  }
  if(chooser_mode){
    url += "&aMode=chooser";
  }
  return url;
}

function utiljs_isEditMode()
{
  var edit_mode;
  // try to find out which mediator is available i.e. whether in app. rendering and
  // called from page or workspace and if so query the edit mode status.
  // We rely on the fact here that noth classes (WorkspaceMediator and Mediator) are never
  // available at the same time!
  try {
    try {
      // are we on a page...?
      edit_mode = com.hyperwave.wcm.csjs.Mediator.static_.getInstance().isEditMode();
    } catch (e) {
      // or in a workspace...?
      edit_mode = com.hyperwave.wcm.csjs.WorkspaceMediator.static_.getInstance().isEditMode();
    }
  } catch(e) {
    edit_mode = false;
  }
  return edit_mode;
}
/**
 * Returns the current state of interface regarding edit mode.
 */
function utiljs_addEditMode(aParam) {
  var edit_mode = utiljs_isEditMode();
  var url = (aParam ? aParam : "") + (edit_mode ? "?edit=true" : "?edit=false");
  url = utiljs_addChooserMode(url);
  return url;
}

function utiljs_openAddPageDialog(xmlTypes)
{
  return;
}

function utiljs_publishPage(xmlType,theInputId)
{
  return;
}

function utiljs_getConfirmMessage(theResourceKey, theCallbackArguments)
{
  return;
}

function utiljs_deletePage(objectToDelete)
{
  return;
}

function utiljs_deleteComment(objectToDelete) {
  return;
}

function utiljs_deleteCB( transport, json ) {
  return;
}

function utiljs_isContentChangedAndNotSaved() {
  var changed = false;
  try {
    try {
      // are we on a page...?
      changed = com.hyperwave.wcm.csjs.Mediator.static_.getInstance().xmlWrapper_.isUndoPossible();
    } catch (e) {
      // or in a workspace...?
      changed = com.hyperwave.wcm.csjs.WorkspaceMediator.static_.getInstance().xmlWrapper_.isUndoPossible();
    }
  } catch(e) {
    changed = false;
  }
  return changed;
}
/*
 * Cient side utility functions for Comments
 */

function utiljs_insertComment(ContentElementID) {
  return;
}

function utiljs_insertCommentCB( transport, json ) {
  return;
}

function utiljs_reloadPagePreservingMode(aHash) {
  var url = utiljs_addEditMode(window.location.pathname)+"&ts="+(new Date()).getTime();
  url = aHash ? url : url + "#" + aHash;
  window.location.replace(url);
}

function utiljs_highlightCurrentPageInNavigation() {
  var current_url = window.location.pathname.match(/([^;])*/)[0];
  var name_arr = current_url.split("/");
  var name = name_arr.pop();
  var nav_element = null;

//<FIXXME author="mmair" date="Jun 11, 2008" desc="
//  Location Feedback from the client side got a lot more complicated
//  since we need to support already exported sites as well.
//  The special treatment for exported sites could be removed in case we
//  use special versions for the globals during export.
//
//  How does it work now:
//  1) Check first level navigation (IFC: element has ID "navigation")
//    a) In case something already hilighted do nothing (i.e. we are on
//       a lower level because then the item is already hilighted when it
//       reaches the client).
//    b) Try to find the item in navigation which represents current page.
//    c) As APIhwdownload adds ".htm" to the pages check again if not
//       found yet - this time without suffix.
//    d) Startpages are renamed to "index.html" as standard web servers
//       recognize files with that name as "startpage". Therefore try to
//       find links within the navigation which point to "index.htm".
//       As for subfolders the navigation directly points to the corresponding
//       startpage hilight the navigation item with the shortest href.
//  2) Check second level navigation (IFC: element has ID "subnavigation")
//     -> The same steps as for point 1)
//
// ">
  if(! $$('#navigation li.selected').length){ // 1)a)
    nav_element = $('nav_'+name); // 1)b)
    if (!nav_element){
      name = name.substring(0, name.lastIndexOf(".")); // 1)c)
      nav_element = $('nav_'+name);
    }
    if (!nav_element){ // 1)d)
      var index_link = $$('#navigation a[href*=index.html]');
      if(index_link.length){
        var href_minlength = 1000;
        var first_index;
        for(var cr=0;cr<index_link.length;++cr){
          if(index_link[cr].href.length < href_minlength){
            first_index = cr;
            href_minlength = index_link[cr].href.length;
          }
        }
        nav_element = index_link[first_index].up('li');
      }
    }
    if (nav_element)
      nav_element.addClassName("selected");
  }

  if(! $$('#subnavigation li.selected').length){ // 2)a)
    nav_element = $('nav2_'+name); // 2)b)
    if (!nav_element){
      name = name.substring(0, name.lastIndexOf(".")); // 2)c)
      nav_element = $('nav2_'+name);
    }
    if (!nav_element){ // 2)d)
      var index_link = $$('#subnavigation a[href*=index.html]');
      if(index_link.length){
        var href_minlength = 1000;
        var first_index;
        for(var cr=0;cr<index_link.length;++cr){
          if(index_link[cr].href.length < href_minlength){
            first_index = cr;
            href_minlength = index_link[cr].href.length;
          }
        }
        nav_element = index_link[first_index].up('li');
      }
    }
    if (nav_element)
      nav_element.addClassName("selected");
  }
//</FIXXME>

  // file list elements
  var nav_element = $('filelist_'+current_url);
  if (nav_element)
    nav_element.addClassName("selected");

}

function utiljs_showLoginArea() {
  return;
}

function utiljs_registerWindowOnloadFunction(onloadFunction) {
  /*
   * register navigation function in application rendering so that it is executed
   * after onload of page (and page only!). Try to find out which mediator is available,
   * order is important (derived classes!)
   */
  try {
    if (typeof com!="undefined" && typeof com.hyperwave!="undefined" && typeof com.hyperwave.wcm!="undifined") {
  	  if (com.hyperwave.wcm.csjs.WikiMediator) {
  	    com.hyperwave.wcm.csjs.WikiMediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }

  	  if (com.hyperwave.wcm.csjs.Mediator) {
  	    com.hyperwave.wcm.csjs.Mediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }

  	  if (com.hyperwave.wcm.csjs.SearchResultsMediator) {
  	    com.hyperwave.wcm.csjs.SearchResultsMediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }

  	  if (com.hyperwave.wcm.csjs.WorkspaceMediator) {
  	    com.hyperwave.wcm.csjs.WorkspaceMediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }

  	  if (com.hyperwave.wcm.csjs.MediaRepositoryMediator) {
  	    com.hyperwave.wcm.csjs.MediaRepositoryMediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }

  	  if (com.hyperwave.wcm.csjs.SnapshotMediator) {
  	    com.hyperwave.wcm.csjs.SnapshotMediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }

  	  if (com.hyperwave.wcm.csjs.MembersListingMediator) {
  	    com.hyperwave.wcm.csjs.MembersListingMediator.static_.getInstance().registerOnloadFunction(onloadFunction);
  	    return;
  	  }
    }

    // no page mediator available, must be (derived from) workspace or browser rendering mode
    Event.observe(window, 'load', onloadFunction.bindAsEventListener());

	} catch (e) {
	  // silent catch
  }
}

//--- global code ---//
utiljs_registerWindowOnloadFunction(utiljs_highlightCurrentPageInNavigation)

