/**
 * 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_getHomeUrl() {
  var nav_elements = $$(".hw_navigationitem.hw_navigationdepth1.tbindex");
  if(nav_elements[0]) {
    return nav_elements[0].select("a")[0].href;
  }
  else { // just in case the homepage has no navigation item, use any item of level 1 and remove last level
  	var nav_elements = $$(".hw_navigationitem.hw_navigationdepth1");
  	if(nav_elements[0]) {
  		var href = nav_elements[0].select("a")[0].href;
  		href = href.replace(/\/index\.html$/, "");
  		href = href.replace(/\/[^\/]*$/, "");
  		href+= "/index.html";
  		return href;
  	}
  }
  return "";
}

function utiljs_highlightCurrentPageInNavigation() {
	var current_url = window.location.pathname.match(/([^;])*/)[0];
  var nav_elements = $$(".hw_navigationitem");
  for(var i=0;i<nav_elements.length;i++) {
    var href = nav_elements[i].select("a")[0].href.replace(/^(file|https?):\/\/[^\/]*/, "");
    if(nav_elements[i].readAttribute("data-objecttype") == "folder" && 
       nav_elements[i].readAttribute("data-haschildren") == "true" &&
       new RegExp("^" + RegExp.escape(href.replace(/\/[^\/]*$/, "")) ).test(current_url)) {
      nav_elements[i].addClassName("selected");
    }
    else if(current_url == href) {
	    nav_elements[i].addClassName("selected");
    }
  }
  
  // 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!).
   */
  if(document.loaded) {
    // as custom scripts are loaded dynamically (inserted in DOM @see #loadCustomScripts)
    // the registry Function may executed after the document has loaded. So the function has to
    // be executed here once.
    onloadFunction();
  }
  else {
    Event.observe(window, 'load', onloadFunction.bindAsEventListener());
  }
}

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

