// ----------------------------------------------------------------------------
//   add string helpers
// ----------------------------------------------------------------------------

Date.prototype.getFullYear =  function () { return( window.moz || window.safari ? this.getYear() + 1900 : this.getYear() ); };

String.prototype.trim =  function () { return this.replace(/^\s+/,'').replace(/\s+$/,''); };

String.prototype.endsWith = function(sEnd) {
  return (this.substr(this.length-sEnd.length)==sEnd);
};

String.prototype.startsWith = function(sStart) {
  return (this.substr(0,sStart.length)==sStart);
};

function trim(str) {
  var idx_start = 0;
  var idx_end = str.length - 1;
  while (str.charAt(idx_start) == ' ') { idx_start++; }
  while (str.charAt(idx_end) == ' ') { idx_end--; }
  return( str.substr(idx_start, (idx_end - idx_start + 1)) );
}


// ----------------------------------------------------------------------------
//   positioning functions
// ----------------------------------------------------------------------------

function getMouseRelativeX() {
  return( parseInt(window.event.clientX) );
}

function getMouseRelativeY() {
  return( parseInt(window.event.clientY) );
}

function getMouseX(e) {
  if (!e) var e = window.event;
  if (e.pageX) return e.pageX;
  return e.clientX + getViewportScrollX();
}

function getMouseY(e) {
  if (!e) var e = window.event;
  if (e.pageY) return e.pageY;
  return e.clientY + getViewportScrollY();
}

function getWidth(obj) {
  return( parseInt(obj.offsetWidth) );
}
function getHeight(obj) {
  return( parseInt(obj.offsetHeight) );
}
function getLeft(obj) {
  return( parseInt(obj.offsetLeft) );
}
function getTop(obj) {
  return( parseInt(obj.offsetTop) );
}

function setLeft(obj, distance) {
  obj.style.left = distance + "px";
}

function setTop(obj, distance) {
  obj.style.top = distance + "px";
}

function getXCoord(obj) {
  x = getLeft(obj);
  while (obj.offsetParent.tagName.toLowerCase() != "body" && obj.offsetParent.tagName.toLowerCase() != "html") {
    obj = obj.offsetParent;
    if (obj.offsetParent.tagName.toLowerCase() != "form") { x += getLeft(obj); }
  }
  return( x );
}

function getYCoord(obj) {
  y = getTop(obj);
  while (obj.offsetParent.tagName.toLowerCase() != "body" && obj.offsetParent.tagName.toLowerCase() != "html") {
    obj = obj.offsetParent;
    if (obj.offsetParent.tagName.toLowerCase() != "form") { y += getTop(obj); }
  }
  return( y );
}

function isVisible(obj) {
  return( obj.style.display != "none" );
}

function hideObject(obj) {
  if (obj) obj.style.display = "none";
}

function showObject(obj) {
  if (obj) obj.style.display = "";
}

function toggleVisibility(obj) {
  if (isVisible(obj)) {
    hideObject(obj);
  } else {
    showObject(obj);
  }
}

function getWindowHeight() {
  if (ie) {
    return( document.body.offsetHeight );
  } else {
    return( window.innerHeight );
  }
}

function getWindowWidth() {
  if (ie) {
    return( document.body.offsetWidth );
  } else {
    return( window.innerWidth );
  }
}

function expandWindowHeight(factor) {
  if (ie) {
    window.resizeBy( 0, factor );
  } else {
    window.innerHeight = window.innerHeight + factor;
  }
}

function expandWindowWidth(factor) {
  if (ie) {
    window.resizeBy( factor, 0 );
  } else {
    window.innerWidth = window.innerWidth + factor;
  }
}

function getWindowScroll(w) {
  if (!w) {
    if (document.documentElement && document.documentElement.scrollTop) { return document.documentElement.scrollTop; }
    else if (document.body.scrollTop) { return document.body.scrollTop; }
    else if (window.pageYOffset) { return window.pageYOffset; }
  } else {
    if (w.document.documentElement && w.document.documentElement.scrollTop) {  return w.document.documentElement.scrollTop; }
    else if (w.document.body.scrollTop) { return w.document.body.scrollTop; }
    else if (w.pageYOffset) { alert( w.pageYOffset ); return w.pageYOffset; }
  }
  return( 0 );
}

function getCurrentUrl() {
  if (SS_RETURN_URL) {
    return( SS_RETURN_URL );
  } else {
    return( "/" );
  }
}

function getIFrameWindow(fName) {
  if (window.safari) {
    return( window.frames[fName] );
  } else {
    return( document.getElementById(fName).contentWindow );
  }
}

function suppressEnter(e, f) {
  if (!e) { e = window.event; }
  if (e.keyCode==13) { if (f) { f(); } return false; }
  return true;
}


// ----------------------------------------------------------------------------
//   viewport functions
// ----------------------------------------------------------------------------

function getViewportHeight() {
  if (self.innerHeight) // all except Explorer
  {
    y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
    y = document.body.clientHeight;
  }
  return( y );
}

function getViewportWidth() {
  if (self.innerHeight) // all except Explorer
  {
    x = self.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    x = document.documentElement.clientWidth;
  }
  else if (document.body) // other Explorers
  {
    x = document.body.clientWidth;
  }
  return( x );
}


function getViewportScrollX() {
  if (self.pageYOffset) // all except Explorer
  {
    x = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
  {
    x = document.documentElement.scrollLeft;
  }
  else if (document.body) // all other Explorers
  {
    x = document.body.scrollLeft;
  }
  return( x );
}

function getViewportScrollY() {
  if (self.pageYOffset) // all except Explorer
  {
    y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
  {
    y = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
    y = document.body.scrollTop;
  }
  return( y );
}

function toggleContentElement(srcCheckbox, tgtElement) {
  if ( srcCheckbox.checked ) {
    showObject(tgtElement);
  } else {
    hideObject(tgtElement);
  }
}

function toggleContentElementByValue(val, tgtElement) {
  if ( val ) {
    showObject(tgtElement);
  } else {
    hideObject(tgtElement);
  }
}

// ----------------------------------------------------------------------------
//   cookie functions
// ----------------------------------------------------------------------------

function setCookie(sName, sValue, expires, path, domain) {
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + (expires == null ? new Date("January 1, 2023").toGMTString() : expires) + "; path=" + ((path == null)  ? "/" : path) + ((domain == null)  ? "" : "; domain=" + domain);
}

function delCookie(sName, path, domain) {
  document.cookie = sName + "=" + escape(getCookie(sName)) + ";expires=" + new Date("December 31, 1975").toGMTString() + "; path=" + ((path == null) ? "/" : path) + ((domain == null)  ? "" : "; domain=" + domain);
}

function getCookie(sCookie) {
  var aCookie = document.cookie.split(";");
  for (var i = 0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sCookie == trim(unescape(aCrumb[0]))) {
      return( unescape(aCrumb[1]) );
    }
  }
  return( null );
}

function getCookieDomain(fullhost) {
  var hs = fullhost.split(".");
  if (hs.length - 2 < 0) {
    return( "" );
  } else {
    return( "." + hs[hs.length - 2] + "." + hs[hs.length - 1] );
  }
}


// ----------------------------------------------------------------------------
//   event management
// ----------------------------------------------------------------------------

/*
AddEvent Manager (c) 2005 Angus Turnbull http://www.twinhelix.com
Free usage permitted as long as this credit notice remains intact.
*/

if (typeof aeOL == 'undefined')
{
 var aeOL = [];
 var addEvent = function(o, n, f, l)
 {
  var d = 'addEventListener', h = 'on' + n, t, a;
  if (o[d] && !l) return o[d](n, f, false);
  if (!o.aE) { o.aE = aeOL.length || 1; aeOL[o.aE] = { o:o } }
  t = aeOL[o.aE][n] || (aeOL[o.aE][n] = []);
  for (var i = 0; i < t.length; i++)
   for (var j = 0; j < t[i].length; j++)
    if (t[i][j] == f) return;
  if (o[h] && o[h]._ae)
  {
   a = t[t.length - 1];
   a[a.length] = f;
  }
  else
  {
   t[t.length] = o[h] ? [o[h], f] : [f];
   o[h] = new Function('e', 'var r = true, i = 0, o = aeOL[' + o.aE + '].o,' +
    'a = aeOL[' + o.aE + ']["' + n + '"][' + (t.length - 1) + '];' +
    'for (; i < a.length; i++) { ' +
   'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
    '} return r');
   o[h]._ae = 1;
  }
 };

 var removeEvent = function(o, n, f, l)
 {
  var d = 'removeEventListener', t, a, i, j, s;
  if (o[d] && !l) return o[d](n, f, false);
  if (!o.aE || !aeOL[o.aE]) return;
  t = aeOL[o.aE][n];
  i = t.length;
  while (i--)
  {
   a = t[i];
   j = a.length;
   s = 0;
   while (j--)
   {
    if (a[j] == f) s = 1;
    if (s) a[j] = a[j + 1];
   }
   if (s) { a.length--; break }
  }
 };

}



var SS_FOLDER_STATES = new Array();


function toggleFolder( id, makeVisible ) {

  // perform visibility toggle

  var icon = document.getElementById( "folderToggleIcon" + id );
  var content = document.getElementById( "folderContent" + id );
  var btn = document.getElementById( "folderControl" + id );

  if (!content || !btn) {
    SS_FOLDER_STATES[id] = false;
    saveStates();
    return;
  }

  // see if we're forcing visibility

  if (makeVisible && isVisible(content)) { return; }

  // perform toggle

  toggleVisibility( content );

  if (!icon) {

    if (!isVisible(content)) {
      SS_FOLDER_STATES[id] = false;
      btn.className = "folder-link-expand";
    } else {
      SS_FOLDER_STATES[id] = true;
      btn.className = "folder-link-contract";
    }

  } else {

    if (!isVisible(content)) {
      SS_FOLDER_STATES[id] = false;
      icon.src = icon.src.replace("contract", "expand");
    } else {
      SS_FOLDER_STATES[id] = true;
      icon.src = icon.src.replace("expand", "contract");
    }

  }

  // serialize folder state

  saveStates();

}

function saveStates() {

  var folderStateSerialized = "";

  for( s in SS_FOLDER_STATES ) {
    if (SS_FOLDER_STATES[s] == true) {
      folderStateSerialized += ",";
      folderStateSerialized += s
    }
  }

  setCookie( "SS_FOLDER_STATES", folderStateSerialized.substr(1), null, '/' );

}

function restoreFolderStates() {

  var states = getCookie("SS_FOLDER_STATES");
  if (states) {
    states = states.split(",");
    for( fid in states ) {
      toggleFolder(states[fid], true);
      SS_FOLDER_STATES[states[fid]] = true;
    }
  }

}

addEvent(window, "load", restoreFolderStates);