// JavaScript Document for Tabbed LPs
// created: September 4, 2008 by G Givan
// modified: November 17, 2008

function toggleLayer( whichTabClicked ){	
	$("#"+whichTabClicked).toggleClass("hidden");
	$("#"+whichTabClicked+"_table").toggleClass("hidden");
}


function ClassManipulation(a,o,c1,c2)
/*
a    defines the action you want the function to perform.
o    the object in question.
c1   the name of the first class
c2   the name of the second class 
*/
{
alert('in ClassManipulation function');
switch (a){
	case 'swap':
		o.className=!ClassManipulation('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
		break;
	case 'add':
		if(!ClassManipulation('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
	case 'remove':
		var rep=o.className.match(' '+c1)?' '+c1:c1;
		o.className=o.className.replace(rep,'');
		break;
    case 'check':
		alert(' inside the check case ');
		return new RegExp('\\b'+c1+'\\b').test(o.className)
		break;
  }
}

/************************************************* COOKIE LOGIC ************************************************************/ 
// retrieve an expiration date in currect format and pass three integer parameters for the number of days, hours, and minutes
function getExpDate(days, hours, minutes) {
    var expDate = new Date( );
    if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
        expDate.setDate(expDate.getDate( ) + parseInt(days));
        expDate.setHours(expDate.getHours( ) + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
        return expDate.toGMTString( );
    }
}
   
// utility function called by getCookie( )
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
   
// retrieve cookie by name
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return "";
// end of getCookie
}
   
// store cookie value with optional details as needed. this is just for readablility
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}
   
// remove the cookie by setting ancient expiration date. again for readability
function deleteCookie(name,path,domain) {
    if (getCookie(name)) document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; 
}

function SetTabCookie(whichTabClicked){ setCookie('previous_tab', whichTabClicked, getExpDate(7, 7, 7));}

function WriteTabCookie(){
	// cookies enabled?
	if (navigator.cookieEnabled) {
		if(previous_tab){ //if it exists, restore the last viewed tab
			toggleLayer(previous_tab);
		}
		else{
			SetTabCookie(tab_names[0]);

		}
	}
	// need to check on the passed in tab request from the URL query string.
	CheckQueryString();
// end of WriteTabCookie	
}

function CheckQueryString(){
	var qs = new querystring(window.location.search);
	
	if(qs.hasKey("toggleLayer")){
		toggleLayer(qs.get("toggleLayer"));
	}
// end of CheckQueryString
}


//var previous_tab = getCookie('previous_tab');
//window.onload = WriteTabCookie;
/************************************************* END of COOKIE LOGIC ****************************************************************/ 



/************************************************* START of QUERY LOGIC ****************************************************************
* querystring library 
* Create a new instance of querystring
* @classDescription        Defines a querystring
* @return {coordinate}     Returns a new querystring.
* @type {Object}
* @constructor             optional argument
*/

function querystring()
{
   this._obj = new Object();
   if(arguments.length == 1)
   {
      var str = arguments[0].replace(/\+/g, " ");
      if(str.indexOf("?") == 0)
      {
         str = str.substring(1,str.length);
      }
      var arr = str.split("&");
      for(var i = 0; i < arr.length; i++)
      {
         var p = arr[i].split("=");
         if(p.length == 2)
         {
            this._obj[unescape(p[0])] = unescape(p[1]);
         }
      }
   }

   /* Adds an entry with the specified name and value.
      @return {void}
   */
   this.add = function(pKey,pValue) {
     if(pKey != null && pValue != null)
     {
        this._obj[pKey] = pValue;
     }
   }

   /* Removes all entries
      @return {void}
   */ 
   this.clear = function() {
      this._obj = new Object();
   }
   
   /* Executes a function while iterating through the entries 
      @return {void}
   */ 
   this.enumerate = function(f) {
      for(k in this._obj)
      {
         f(k,this._obj[k]);
      }
   }

   /* Gets the value at the specified name
      @return {string}
   */ 
   this.get = function(pKey) {
      return this._obj[pKey];
   }

   /* Gets the number of key/value pairs
      @return {int}
   */  
   this.getCount = function() {
      return this.getKeys().length;
   }
   
   /* Gets all keys
      @return {string[]}
   */ 
   this.getKeys = function() {
      var arr = new Array();
      var f = function() {
         arr.push(arguments[0]);
      }
      this.enumerate(f);
      return arr;
   }

   /* Gets all values
      @return {string[]}
   */  
   this.getValues = function() {
      var arr = new Array();
      var f = function() {
         arr.push(arguments[1]);
      }
      this.enumerate(f);
      return arr;
   }
   
   /* Returns a value indicating whether or not the key exists.
      @return {bool}
   */  
   this.hasKey = function(pKey) {
      return this._obj[pKey] != null;
   }
   
   /* Returns a value indicating whether or not keys exist.
      @return {bool}
   */ 
   this.hasKeys = function() {
       return this.getKeys().length > 0;
   }
   
   /* Removes an entry
      @return {void}
   */  
   this.remove = function(pKey){
      this._obj[pKey] = null;
   }
   
   /* Returns a string representaton of the querystring, a series of key/value pairs separated by an ampersand (&).
      @return {string}
   */  
   this.toString = function() {
      if(this.getCount() == 0)
      {
         return "";
      }
      var str = "?";
      var fnc = function() {
         str += (escape(arguments[0]) 
                   + "=" 
                   + escape(arguments[1]) 
                   + "&");
      }
      this.enumerate(fnc);
      str = str.replace(/" "/g,"+")
      str = str.substring(0,str.length - 1);
      return str;
   }
}

/************************************************* END of QUERY LOGIC ****************************************************************/
