// $RCSfile: page.js,v $ $Revision: 1.45 $ $Date: 2009-04-07 05:50:44 $
// Copyright (c) 2008 StreetPrices.com Inc.
// All rights reserved.
var undefined;
var page;
if (!page) { page = new Object(); }


// quick and dirty text subsitution
String.prototype.substitute = function(was, becomes) {
   return this.split(was).join(becomes);
};


// Parse cgi arguments, and return the results as a javascript object.
//
// If a cgi argument is passed more than once, use the last value found;
// do not convert it to an array.
page.parseCGIArgs = function page_parseCGIArgs(w) 
{
   var args = new Object;
   var query_string = (w.location.search) ? 
                       w.location.search.replace(/\+/, '%20') : 
                       "";
   // split on & or ;
   var i=1;
   var val_start=0;
   var arg_start=1;
   for(i=1;i<query_string.length;i++) {
      if (query_string.charAt(i) == '&' || 
          query_string.charAt(i) == ';' || 
          i == query_string.length-1) 
      {
         // got an argument!  save it

         // if we're at the end of the string, special-case it
         // (i should be the index _after_ the arg/val pair ends)
         // (unless it's a trailing =, in which case we ignore it)
         if (i == query_string.length-1 && query_string.charAt(i) != '=') { 
            i++; 
         }

         if (i>arg_start) { 
            if ((val_start > arg_start))  // is there a value?
            {
               if (decodeURIComponent) {
                 args[ query_string.substring(arg_start,val_start-1)] = decodeURIComponent( query_string.substring(val_start,i) ); 
               } else {
                 args[ query_string.substring(arg_start,val_start-1)] = unescape( query_string.substring(val_start,i) ); 
               }
            }
            else if ( i > arg_start ) // is there at least an argument?
            {
               args[ query_string.substring(arg_start,i) ] = undefined;
            }
         }
         // next arg starts here
         arg_start = i+1;
      }
      else if (query_string.charAt(i) == '=')
      {
         val_start = i+1;
      }
   }
   return args;
}

// Given a list of node IDs in the correct order, find the elements
// in the DOM tree, and give them the same order.
//
// Nodes may be passed by id or as objects.
page.reorder = function page_reorder(orderedList,destNode,insertAtTop)
{
   if (!orderedList) { return false; }
   if (typeof orderedList != "object") { return false; }
   if (!orderedList.push) { return false; }

   destNode = $(destNode);
   // ok, re-order stuff!
   //
   // If no destNode was given, leave each node within its parent.
   // This lets us sort multiple lists at once.

   var i;
   for (i= (insertAtTop ? orderedList.length-1: 0); 
        (insertAtTop ? i>=0 : i<orderedList.length); 
        (insertAtTop ? i-- : i++)) {
      var node = orderedList[i];
      node = $(node);

      if (node) {
         var dest = (destNode) ? destNode : node.parentNode;
   
         if (dest) {
            if (dest.firstChild && insertAtTop)
            {
               if (dest.moveRow) { 
                 dest.moveRow(node.rowIndex,0);
               } else {
                 dest.insertBefore(node,dest.firstChild);
               }
            } else {
               if (dest.moveRow) { 
                 // IE doesn't like appendChild with tables
                 // -- it makes all the nodes disappear.  
                 // Use IE's moverow function instead.
                 dest.moveRow(node.rowIndex,dest.rows.length-1);
               } else {
                 dest.appendChild(node);
               }
            }
         }
      }
   }
   return true;
};

page.getCookie = function page_getCookie(name) 
{
   var namevaluepairs = document.cookie.split("; ");
   if (!page.cookie) { 
      page.cookie = new Object(); 
      for (var pair in namevaluepairs) {
         if (namevaluepairs[pair].split) {
            var a = namevaluepairs[pair].split("=");
            if (a[0]) {
               page.cookie[ a[0] ] = a[1];
            }
         }
      }
   }
   if (name) {
      if (page.cookie[name] == undefined)
      {
         return undefined;
      }
      if (decodeURIComponent) {
         return decodeURIComponent(page.cookie[name]);
      } else {
         return unescape(page.cookie[name]);
      }
   }
   return page.cookie;
}
page.setCookie = function page_setCookie(name,value,args) 
{
   if (name && value) {
      var expires;
      if (!args || !args['expires']) {

         // default to keep the cookie for a long, long time
         expires = 'Sat, 16 Jan 2038 00:00:01 UTC';
      }
      if (encodeURIComponent) {
         document.cookie = name + '=' + encodeURIComponent(value) + 
            '; expires=' + 
            ((args && args['expires']) ? args['expires'] : expires) +
            ((args && args['domain']) ? '; domain=' + args['domain'] : "") +
            '; path=' + ((args && args['path']) ? args['path'] : '/');
      } else {
         document.cookie = name + '=' + escape(value) + 
            '; expires=' + 
            ((args && args['expires']) ? args['expires'] : expires) +
            ((args && args['domain']) ? '; domain=' + args['domain'] : "") +
            '; path=' + ((args && args['path']) ? args['path'] : '/');
      }
      delete page.cookie;
   }
}

page.deleteCookie = function page_deleteCookie(name, args) 
{
   var value = page.getCookie(name);
   if (value) {
      document.cookie = name + '=' + 
         '; expires=Thu, 01 Jan 1977 00:00:01 UTC' +
         ((args && args['domain']) ? '; domain=' + args['domain'] : "") +
         '; path=' + ((args && args['path']) ? args['path'] : '/');
      delete page.cookie;
   }
}

page.unixTimeToString = function page_unixTimeToString(t)
{
   var d = new Date();
   d.setTime(t * 1000);
   return d.toLocaleString();

}

/* NOTE: truncateParagraphs is based on code at barelyfitz.com (MIT license)
 * Caveats:
 * - Does not handle embedded tags -- nodes must contain plaintext only.
 * - Once opened, there's no way to close the paragraph.
 */

page.truncateParagraphs = function page_truncateParagraphs(className, len)
{
return;
   var elements = document.getElementsByClassName(className);
   for (var i in elements) {
      var node = elements[i];

      var trunc = node.innerHTML;
      if (trunc && trunc.length > len) {

         /* Truncate the content of the node, then go back to the end of the
            previous word to ensure that we don't truncate in the middle of
            a word */
         trunc = trunc.substring(0, len);
         trunc = trunc.replace(/\w+$/, '');

         /* Add an ellipses to the end and make it a link that expands
            the paragraph back to its original size */
         trunc += '<a href="#" ' +
           'onclick="this.parentNode.innerHTML=' +
           'unescape(\''+escape(node.innerHTML)+'\');return false;">' +
           '...<\/a>';
         node.innerHTML = trunc;
      }
   }
}

// ------------------------------------------------------------------------
// Now do some setup work
// ------------------------------------------------------------------------

// Parse the CGI arguments right away; don't wait for the page to load.
page.args = page.parseCGIArgs(window.parent ? window.parent : window);

// BUG: doesn't handle .co.uk type domains
var tmp = document.domain.split(/\./);
page.cookieDomain = tmp[ tmp.length-2 ] + '.' + tmp[ tmp.length-1 ];
