/*
 * Copyright 2005 Tridium, Inc. All Rights Reserved.
 */
 
/**
 * Portal JavaScript Support.
 * 
 * @author    Andy Frank
 * @creation  10 Apr 05
 * @version   $Revision$ $Date$
 * @since     Baja 1.0
 */

var portal = new PortalApp();
function PortalApp()
{ 
////////////////////////////////////////////////////////////////
// Attributes
////////////////////////////////////////////////////////////////
  
  var appsMenuId = "_hxPortalAppsMenuId";
  var recentOrdListCookie = "hxPortalRecentOrdList";
  var recentOrdListId = "_hxPortalRecentOrdListId";
  
  /** Index of current task */
  this.taskIndex = -1;
  
  /** Must be set for HxPortalProfile. */
  this.prefix = "";
  
  /** Elements: { name:displayName, ord:ord } */
  this.apps = new Array();

////////////////////////////////////////////////////////////////
// Init
////////////////////////////////////////////////////////////////

  this.init = function()
  {
    var task = document.getElementById("taskArrow");
    var elem = document.getElementById("navTask" + portal.taskIndex);
    if (elem != null && !hx.ie)
    {
      var x = elem.offsetLeft + ((elem.offsetWidth-11) / 2);
      var y = elem.offsetTop + elem.offsetHeight + 5;
      
      task.style.position = "absolute";
      task.style.left = x + "px";
      task.style.top  = y + "px";
      task.style.height = "7px";  
    }
  }

////////////////////////////////////////////////////////////////
// Apps
////////////////////////////////////////////////////////////////
  
  /**
   * Display the apps menu.
   */
  this.showApps = function()
  {
    var text = "";
    text += "<span style='text-align:right;'>";
    text += "<a href='javascript:portal.closeApps();'><b>Close</b></a>";
    text += "<span><br/>";    

    for (var i=0; i<portal.apps.length; i++)
    {
      var app = portal.apps[i];
      text += "<a href='" + portal.prefix + app.ord + "'>" + app.name + "</a><br/>";
    }
           
    var div = document.createElement("div");
    div.id = appsMenuId;
    div.setAttribute("class", "dashboardMenu");
    div.style.left = "2px";
    div.style.top   = "23px";    
    div.innerHTML = text;
    document.body.appendChild(div);
  }

  /**
   * Close the apps menu.
   */
  this.closeApps = function()
  {
    var elem = document.getElementById(appsMenuId);
    if (elem != null) elem.parentNode.removeChild(elem);
  } 

////////////////////////////////////////////////////////////////
// RecentOrdList
////////////////////////////////////////////////////////////////
  
  /**
   * Display the recent ord list.
   */
  this.showRecentOrdList = function()
  {
    var text = "";
    text += "<span style='text-align:right;'>";
    text += "<a href='javascript:portal.closeRecentOrdList();'><b>Close</b></a>";
    text += "<span><br/>";

    var value = portal.getCookie(recentOrdListCookie);
    if (value == null) text += "No ords.";
    else
    {
      var list = value.split("^");
      for (var i=0; i<list.length; i++)
      {
        var parts = list[i].split("~");
        text += "<a href='" + portal.prefix + parts[1] + "'>" + parts[0] + "</a><br/>";
      }
    }
        
    var div = document.createElement("div");
    div.id = recentOrdListId;
    div.setAttribute("class", "dashboardMenu");
    div.style.right = "2px";
    div.style.top   = "23px";
    //div.onmouseout = portal.closeRecentOrdList;
    div.innerHTML = text;
    document.body.appendChild(div);
  }
  
  /**
   * Close the recent ord list.
   */
  this.closeRecentOrdList = function()
  {
    var elem = document.getElementById(recentOrdListId);
    if (elem != null) elem.parentNode.removeChild(elem);
  } 
  
////////////////////////////////////////////////////////////////
// Login
////////////////////////////////////////////////////////////////

  this.login = function(redirect)
  {
	if (redirect = '/ord?portal:accounts/reset') {
		redirect = '/ord?portal:/home';
	}
    document.cookie = "niagara_uri=" +redirect + ";path=/";
    window.location="/login";
  }

////////////////////////////////////////////////////////////////
// Cookies
////////////////////////////////////////////////////////////////

  /**
   * Get the value of the named cookie - or null if the
   * cookie does not exist.
   */
  this.getCookie = function(name)
  {
    var prefix = name + "=";
    var begin = document.cookie.indexOf("; " + prefix);
    if (begin == -1)
    {
      begin = document.cookie.indexOf(prefix);
      if (begin != 0) return null;
    }
    else begin += 2;
    
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = document.cookie.length;
    
    return unescape(document.cookie.substring(begin + prefix.length, end));
  }

////////////////////////////////////////////////////////////////
// Replace
////////////////////////////////////////////////////////////////

  /**
   * Replace the content of a div in the current document.
   */
  this.replace = function(divId, arg)
  {
    var msg = new Message();    
    msg.setHeader("application/x-niagara-portal-replace-id", divId + "-" + this.getSelection(arg)); 
    msg.setHeader("Content-Type", "application/x-niagara-portal-replace");
    msg.send(window.location, "", this.doReplace);
  }
  
  /**
   * Handle replace response.
   */
  this.doReplace = function(resp)
  {
    var text = resp.responseText;
    var index = text.indexOf("@");
    var id = text.substring(0, index);
    text = text.substring(index+1);
    
    // Create a new element to hold markup
    var child = document.createElement("div");
    child.innerHTML = text;
    
    // Remove existing content
    var elem = document.getElementById(id);
    for (var i=0; i<elem.childNodes.length; i++)
      elem.removeChild(elem.firstChild);
            
    // Append new content
    elem.appendChild(child);
  }
  
  this.getSelection = function(id)
  {
    var sel = document.getElementById(id);
    return sel.value;
  }
}


