var UTIL = UTIL ? UTIL : new Object();

UTIL.indexOf = function indexOf(a, obj)
{

    for(var i=0; i< a.length; i++){
    	
        if(a[i]==obj){
            return i;
        }
    }
    return -1;
}

UTIL.removeItem = function(originalArray, itemToRemove) {	
		var j = 0;	while (j < originalArray.length) 
		{	
			if (originalArray[j] == itemToRemove) 
			{			
				originalArray.splice(j, 1);	} 
				else { j++; }
			}
	return originalArray;
}

UTIL.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

UTIL.readCookie = function(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;
}

UTIL.eraseCookie = function(name) {
	createCookie(name,"",-1);
}

/*
$(function(){
	
	//hide all nested lists
	$('.openClose ul').hide(); 
	
	//show child of li to open on default
	$('.defaultOpen > ul').show();
	//show parents of li to open on default
	$('.defaultOpen').parents('ul').show(); 
	$('.defaultOpen > a').addClass('showMinus');
	
	//add a toggle button to li which contain a nested list
	$('.openClose ul').parent('li').prepend('<a class="toggleButton">&nbsp;</a>');
	//remove the background from these (the arrow)
	$('.openClose ul').parent('li').css('background-image', 'none');
	


	$('.moreLess ul').hide(); 	
	$('.moreLess ul').parent('li').append('<a class="toggleButton"></a><span>Show more</span>');
	$('.moreLess ul').parent('li').addClass('removeBullet');


	$('.defaultOpen').find('.toggleButton:first').addClass('showMinus');
	$('.defaultOpen').parents('li').find('.toggleButton:first').addClass('showMinus');
	
	// click link to change plus to minus and show/hide nested list
	$('.toggleButton').click(function(){
	
		$(this).toggleClass("showMinus");	
		$(this).siblings('ul').slideToggle();
		
		// change the text in the moreLess list on click
		if ( $(this).next('span').text() == 'Show more') {
			$(this).next('span').text("Show less");
		} else {
			$(this).next('span').text("Show more");		
		}
	});	
});	*/

$(function(){
	
	//hide all nested lists
	$('.openClose ul').hide(); 
	
	//show child of li to open on default
	$('.defaultOpen > ul').show();
	//show parents of li to open on default
	$('.defaultOpen').parents('ul').show(); 
	$('.defaultOpen > a.toggleButton').addClass('showMinus');
	
	//convert lis which contain a nested list into toggleButton links
	$('.openClose ul').parent('li').each(function()
		{
			if (this.firstChild.nodeType == 3)
			{
				$(this.firstChild).replaceWith('<a class="toggleButton" href="#"><span>' + this.firstChild.nodeValue + '</span></a>');
			}
			else if (this.firstChild.nodeName.toLowerCase() == "a")
			{
				$(this.firstChild)
					.addClass("toggleButton")
					.wrapInner('<span></span>')
			}
			else
			{
				$(this.firstChild).wrap('<a class="toggleButton" href="#"><span></span></a>')
			}
		}
	)
	//remove the background from these (the arrow)
	$('.openClose ul').parent('li').css('background-image', 'none');
	


	$('.moreLess ul').hide(); 	
	$('.moreLess ul').parent('li').append('<a class="toggleButton" href="#">Show more</a>');
	$('.moreLess ul').parent('li').addClass('removeBullet');


	$('.defaultOpen').find('.toggleButton:first').addClass('showMinus');
	$('.defaultOpen').parents('li').find('.toggleButton:first').addClass('showMinus');

	
	// click link to change plus to minus and show/hide nested list
	$('.toggleButton').click(function(event){
		
		if ($(this).parents("#sections").length > 0 && $(event.target).is("span") && this.getAttribute("href") != "#")
		{
			return true;
		}
		
		$(this).toggleClass("showMinus");	
		$(this).siblings('ul').toggle();
		
		// change the text in the moreLess list on click
		if ( $(this).text() == 'Show more') {
			$(this).text("Show less");
		} else if ($(this).text() == 'Show less') {
			$(this).text("Show more");		
		}
		// get id of parent (li)
		
		var ssid = $(this).parent('li')[0].id;

		var cookiePath = location.pathname.replace("/","")
		var ssidList = UTIL.readCookie(cookiePath);
		var ssidArr = new Array();
		
		if (ssidList != null)
		{
			ssidArr = ssidList.split(",");
		}
		
		if ( ! $(this).siblings('ul').is(':hidden') ) 
		{ 
			// add id to list
			ssidArr[ssidArr.length] = ssid;
			ssidArr = ssidArr.unique();
		}
		else
		{
			UTIL.removeItem(ssidArr,ssid);
		}
		UTIL.createCookie(cookiePath, ssidArr.join(","),1);
		return false;
	});	

	// fire click events on the items which are stored as 'open' in the cookie
	var ssidList = UTIL.readCookie(location.pathname.replace("/",""));
	
	if (ssidList !=null)
	{
			var ssidArr = ssidList.split(",");
			for (var i=0; i < ssidArr.length; i++)
			{
				var ssid = ssidArr[i];
				$('#' + ssid+ ' a').click();
			}
	}

});	


Array.prototype.unique = function () {

	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

