﻿// When the Site Index page is loaded, init() check all the cookies
// If there are cookies set, this loop through all the cookies and extract the id of the divs
// that were being viewed then display them
function init() {
	//delete_all_cookies();
	var cookieString,cookieArray,cookie,i,id,element;
	cookieString = document.cookie;
	if(cookieString) {
		cookieArray = cookieString.split("; ");
		for (i = 0; i < cookieArray.length; i++) {
			cookie = cookieArray[i].split("=");
			if(cookie.length != 2) continue;
			id = cookie[0];
			element = document.getElementById(id);
			if (element) element.style.display = '';
		}
	}
}

// This function displays/hides the clicked div and set/erase the cookie indicating its status
function swap(id) {
	var element = document.getElementById(id);
	if (element.style.display == 'none') {
		element.style.display = '';
		// If there wasn't a cookie for this, set a cookie indicating this div is opening
		if(!getCookie(id)) setCookie(id,'on');
	} else {
		element.style.display = 'none';
		// Erase the opening indicating cookie (this div is closed)
		if(getCookie(id)) deleteCookie(id);
	}
}

function setCookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ) {
	var cookieString = name + "=" + escape ( value );

	if ( exp_y ) {
		var expires = new Date ( exp_y, exp_m, exp_d );
		cookieString += "; expires=" + expires.toGMTString();
	}

	if ( path )
		cookieString += "; path=" + escape ( path );

	if ( domain )
		cookieString += "; domain=" + escape ( domain );

	if ( secure )
		cookieString += "; secure";

	document.cookie = cookieString;
}

function deleteCookie ( cookieName ) {
	var cookieDate = new Date ( );
	cookieDate.setTime ( cookieDate.getTime() - 1 );
	document.cookie = cookieName += "=; expires=" + cookieDate.toGMTString();
}

function delete_all_cookies ()
{
	// Get cookie string and separate into individual cookie phrases:
	var cookie_string = "" + document . cookie;
	var cookie_array = cookie_string . split ("; ");

	// Try to delete each cookie:
	for (var i = 0; i < cookie_array . length; ++ i)
	{
		var single_cookie = cookie_array [i] . split ("=");
		if (single_cookie . length != 2)
			continue;
		var name = unescape (single_cookie [0]);
		delete_cookie (name);
	}
}

function getCookie ( cookieName ) {
	var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );

	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;
}

YAHOO.util.Event.onDOMReady(init);

