// ---------------------------
// INITIALIZATION INSTRUCTIONS
// ---------------------------


function init() {
	if($('email_1')) {
		$('email_1').innerHTML = '<a href="mailto:info2@' + 'dilworth.org\">info2@dil'+ 'worth.org</a>';
	}

	var url = window.location.href;
	var page_title = document.title;  
	if(category) {
		var page_category = category;
	} else {
		var page_category = '';
	}
	// addPageToViewed(url, page_title, page_category);
}

// ----------------------------
// COMMON FUNCTIONS 
// ----------------------------


function $(elemID) {
	// Refactor document.getElementById alias.
	if (document.getElementById) {
		elem = document.getElementById(elemID);
		return elem;
	} else 
		// handleError('Browser does not support W3C or IE DOM... Please use IE4+ or Netscape 6+ as your web browser');
		return false;
}

function popUp(theURL,winName,features) {
	// Launches a popup window with the url, name and size specified.

	if (features == '') 
		features = 'width=500,height=500,resizable,scrollbars';
	newWindow=window.open(theURL,winName,features);
	if (newWindow) newWindow.focus();
}

// -------------------------------------
// TEST SCRIPTS FOR OTTO RECENTLY VIEWED 
// -------------------------------------

var acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('acceptsCookies=yes') != -1) {
	acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  acceptsCookies = true;
}

function setCookie (name, value, hours, path, domain, secure) {
    if (acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
	var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} 
	
function readCookie(name) {
	if(document.cookie == '') { // there's no cookie, so go no further
    } else { // there is a cookie
   	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} 

function killCookie(name, path, domain) {
  var theValue = readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} 


function obj2Str(obj) {
// This converts an object to a string... if the value is an array... it deals with that too!
    var val, output = "";
    if (obj) {    
        output += "{";
        for (var i in obj) {
        	val = obj[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0]) {
                        output += i + ":" + arr2Str(val) + ",";
                    } else {
                        output += i + ":" + obj2Str(val) + ",";
                    }
                    break;
               case ("string"):
                   output += i + ":'" + val + "',";
                   break;
                default:
                    output += i + ":" + val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "}";
    }
    return output;
}

function arr2Str(array) {
// This converts an array to a string... if the variable is an object... it deals with that too!
    var output = "";
    if (array[0]) {
        output += "[";
        for (var i=0; i < array.length; i++) {
        	// Danny Goodman had this as a for/in loop, but that also indexed the hash table separatly
        	val = array[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0]) {
                        output += arr2Str(val) + ",";
                    } else {
                        output += obj2Str(val) + ",";
                    }
                    break;
                case ("string"):
                    output += "'" + val + "',";
                    break;
                
                default:
                    output += val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "]";
    }
    return output;
}

function str2Obj(string) {
	// Converts a well formed string into an object
    eval("var result = " + string);
    return result;
}

function str2Arr(string) {
	// Converts a well formed string into an array
    eval("var result = " + string );
    return result;
}

function page(url,title,category) {
//  Constructor function for adding a new page to the pages object;
    this.url = url;
	this.title = title;
	this.category = category;
}

function addPageToViewed(url,title,category) {

	// Get the pages array from the cookie
	var pages_string = readCookie('pages');	
	var pages = new Array();
	
	if (pages_string) {
		pages = str2Arr(pages_string);
	}
	
	// unshift the new page into the array
	var this_page = new page(url,title,category);
	
	// Check to see that this page is not already in the array.
	
	for(i in pages) {
		if((pages[i].url == this_page.url) || pages[i].title == this_page.title) {
			return false;
		}
	}
	
	// Add page to the array, and shift out the last in the stack.
	pages.push(this_page);
	if(pages.length >= 5) {
		pages.shift();
	}
		
	// Write it back to the cookie.
	setCookie('pages',arr2Str(pages),'','/');

}

function displayRecentlyViewed(target_id) {
	
	// Get the pages array from the cookie
	var pages_string = readCookie('pages');
	var pages = new Array();
	if (pages_string) {
		pages = str2Arr(pages_string);
	}

	
	if(pages.length) {
		var output = "<h3>Your Recently Viewed Pages</h3>";
		for(i in pages) {
			output += '<div class="recent_page">'
			if(pages[i].category != null)	{
				output += '<div class="category">' + pages[i].category + '</div>';	
			}
			output += '<div class="title"><a href="' + pages[i].url + '">' + pages[i].title + '</a></div>';
			output += '</div>';
		}
	$(target_id).innerHTML = output;
	}
	
	
}

window.onload=init;