// Set global variables

	// get the basic page name
	var page = window.location.href;
	page = page.match(/[-_\w]+[.][\w]+$/i)[0];
	page = page.substring(page, page.lastIndexOf('.'));

	// cookie duration: one year (almost)
	var duration = 365;

// Various toolkit functions

// Modified cookie functions
// Original by Scott Andrew, found at http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days,path) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var thePath = '; path=/';
		if (path) {
			thePath = '; path=' + path;
		}
	document.cookie = name+"="+value+expires+thePath;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// returnObjById: Cross-browser compatibility function
// Found at http://www.netlobo.com/javascript_get_element_id.html
function returnObjById( id )
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

// removeVal: Remove a specified value from an array
// Script author unknown, found via Google
function removeVal(arr, valToRemove){
  // Normalize to a string like !val!!val!!val!
  var s = '!' + arr.join('!!') + '!';
  // Remove targeted values with delimiters
  s = s.replace(new RegExp('!' + valToRemove + '!', 'g'), '');
  // Remove delimiter added to end in step 1
  s = s.replace(/^!/, '');
  // Remove delimiter added to start in step 1
  s = s.replace(/!$/, '');
  // Convert to array
  return s.split('!!');
}

// preset: On load, hides content boxes as appropriate
// Reads a pipe-separated string (converted array) in a cookie, for IDs of boxes to hide
function preset(){
	// if cookie's set, hide all boxes in the list
	if(readCookie(page)){
		var boxstring = readCookie(page);
		var boxarray = boxstring.split('|');
		for(var i in boxarray){
			var abox = returnObjById(boxarray[i]);
	  	abox.style.display = 'none';
		}
	}
}

// roller: onClick, toggles content boxes as hidden/visible; modifies cookie appropriately
// If visible, hides and adds to cookie; if invisible, unhides and removes from cookie
function roller(parentid){
	var getthis = parentid + 'box';
	var box = returnObjById(getthis);
	// varbox is a child div of var, containing feeds etc.
	if(box.style.display != 'none'){
	  box.style.display = 'none';
	  // if cookie, appends value if not already present; otherwise creates cookie with value
	  if(readCookie(page)){
	    var cvalue = readCookie(page);
   	  var cfinal = cvalue + "|" + getthis;
			createCookie(page, cfinal, duration);
		} else if(!(readCookie(page))){
		  createCookie(page, getthis, duration);
		}
	} else if(box.style.display == 'none'){
	  box.style.display = 'block';
	  var cvalue = readCookie(page);
	  var carray = cvalue.split('|');
	  carray = removeVal(carray, getthis);
	  var cfinal = carray.join('|');
	  createCookie(page, cfinal, duration);
	}
}

// onload...
//    if page subcookie is set:
// 			get value of subcookie for this $page
// 			foreach listed, set display:none
//    else:
//      nothing, can't set empty subcookie

// onclick...
// if display:block:
//		display:none
//    if page subcookie is set:
//      add to subcookie
//    else:
//      create subcookie
//      add to subcookie
// else if display:none:
//    display:block
//    if page subcookie is set:
//    	remove from subcookie
//    else:
//      do nothing, it's not there anyway