//  ELcookie.js
//
//  1999-03-17 pcg
//  Last modified:
//    1999-05-25 pcg replaced switch statement with if/else
//    1999-07-01 pcg added Geology
//    1999-07-02 pcg added Chemistry
//    1999-07-13 pcg added Geography
//    2000-03-27 cwd updated College URL; Social Work URL
//    2001-01-08 pcg updated Health URL
//    2002-05-22 cdr added Math URL
//    2002-09-09 cdr modified CIMC URL to use cimc.education.wisc.edu instead of cimc.soemadison.wisc.edu
//    2002-11-05 pcg changed URLs for Business, Math to vhosts
//    2003-05-20 pcg changed URL for ArCat
//    2003-06-09 pcg changed URL for Steenbock to vhost
//    2003-11-05 cwd added Music URL
//    2004-02-06 pcg added Art URL
//    2005-03-09 pcg added Law
//    2005-09-09 pcg added Astronomy
//    2006-06-22 cdr modified health/ebling url
// 		2006-08-28 sem rewrote setHTMLString() as addLocalLink() for the new website
// 		2007-09-25 cdr added slis and ccbc, and changed urls for libraries that now have domains.
// 		2008-09-10 cdr added limnology
//	  2009-08-06 rf changed CIMC to MERIT
//
//  Set and use EL cookie for localization of EL home page
//  Functions:
//
//    setHTMLString()   no longer used, see addLocalLink
// 		addLocalLink()		creates the link element and adds the appropriate text/href values
//    setCookie()       sets the cookie with the values passed to it
//    getCookie()       retrieve and parse cookie
//    deleteCookie()    what do you think?
//    fixDate()         
//    changeLocal()     
//    setLocal()        
//    useCookie()       prints the localized link
//    setOptionList()   
//
//  Note 2000-01-12: Netscape 2.0 crashes when constructing dates before 1970-01-01
//    or after 1999-12-31. Limit this script to JavaScript 1.1 browsers (currently
//    it's restricted to 1.2 and up).

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf(prefix);
	if (begin == -1)
		return null;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

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";
	}
	setOptionList();
}

function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
		date.setTime(date.getTime() - skew);
}

function changeLocal() {
	var now = new Date();
	fixDate(now);
	var nowPlusFiveYears = now.getTime() + (5 * 365 * 24 * 60 * 60 * 1000);
	
	now.setTime(nowPlusFiveYears);
	local = prompt("Please enter your library's name:", "");
	setCookie("local", local, now, "/");
}

function setLocal(theField) {

	var now = new Date();
	fixDate(now);
	var nowPlusFiveYears = now.getTime() + (5 * 365 * 24 * 60 * 60 * 1000);

	now.setTime(nowPlusFiveYears);

	setCookie("local", theField, now, "/");
}

function useCookie() {

	var now = new Date();
	fixDate(now);
	var nowPlusFiveYears = now.getTime() + (5 * 365 * 24 * 60 * 60 * 1000);
	now.setTime(nowPlusFiveYears);
	var local = getCookie("local");

	if (local) {
	
		addLocalLink(local);
		setCookie("local", local, now, "/");
	}
}

function setOptionList() {
	
	var local = getCookie("local");
	if (local) {
		for (var i = 0; i < document.forms[1].localize.length; i++) {
			if (document.forms[1].localize.options[i].value == local) {
				document.forms[1].localize.options[i].selected = true;
				break;
			}
		}
	}
	else {
		document.forms[1].localize.options[0].selected = true;
	}
}

function addLocalLink(cookieValue) {
	var library_url;
	var library_name;
	
	if (cookieValue == "Memorial") {
		library_url = 'http://memorial.library.wisc.edu/';
		library_name = 'Memorial Library';
	}
	else if (cookieValue == "Archives") {
		library_url = 'http://archives.library.wisc.edu/';
		library_name = 'University Archives';
	}
	else if (cookieValue == "Art") {
		library_url = 'http://art.library.wisc.edu/';
		library_name = 'Kohler Art Library';
	}
	else if (cookieValue == "Astronomy") {
		library_url = 'http://astronomy.library.wisc.edu/';
		library_name = 'Astronomy Library';
	}
	else if (cookieValue == "Business") {
		library_url = 'http://business.library.wisc.edu/';
		library_name = 'Business Library';
	}
	else if (cookieValue == "CCBC") {
		library_url = 'http://www.education.wisc.edu/ccbc/';
		library_name = 'CCBC (Cooperative Childrens Book Center)';
	}
	else if (cookieValue == "Chemistry") {
		library_url = 'http://chemistry.library.wisc.edu/';
		library_name = 'Chemistry Library';
	}
	else if ((cookieValue == "CIMC") || (cookieValue == "MERIT")) {
		library_url = 'http://merit.education.wisc.edu/';
		library_name = 'MERIT';
	}
	else if (cookieValue == "College") {
		library_url = 'http://college.library.wisc.edu/';
		library_name = 'College Library';
	}
	else if (cookieValue == "Geography") {
		library_url = 'http://geography.library.wisc.edu/';
		library_name = 'Geography Library';
	}
	else if (cookieValue == "Geology") {
		library_url = 'http://www.geology.wisc.edu/library/';
		library_name = 'Geology Library';
	}
	else if (cookieValue == "Health") {
		library_url = 'http://ebling.library.wisc.edu/';
		library_name = 'Ebling Library';
	}
	else if (cookieValue == "Historical") {
		library_url = 'http://historical.library.wisc.edu/';
		library_name = 'Wisconsin Historical Society';
	}
	else if (cookieValue == "Math") {
		library_url = 'http://math.library.wisc.edu/';
		library_name = 'Kleene Mathematics Library';
	}
	else if (cookieValue == "Law") {
		library_url = 'http://library.law.wisc.edu/';
		library_name = 'Law Library';
	}
	else if (cookieValue == "Limnology") {
		library_url = 'http://limnology.wisc.edu/Library/LimnologyLibrary/';
		library_name = 'Limnology Library';
	}
	else if (cookieValue == "Music") {
		library_url = 'http://music.library.wisc.edu/';
		library_name = 'Mills Music Library';
	}
	else if (cookieValue == "Physics") {
		library_url = 'http://physics.library.wisc.edu/';
		library_name = 'Physics Library';
	}
	else if (cookieValue == "Social") {
		library_url = 'http://socialwork.library.wisc.edu/';
		library_name = 'Social Work Library';
	}
	else if (cookieValue == "SLIS") {
		library_url = 'http://slislib.library.wisc.edu/';
		library_name = 'SLIS Laboratory Library';
	}
	else if (cookieValue == "Steenbock") {
		library_url = 'http://steenbock.library.wisc.edu/';
		library_name = 'Steenbock Library';
	}
	else if (cookieValue == "Wendt") {
		library_url = 'http://wendt.library.wisc.edu/';
		library_name = 'Wendt Library';
	}
	else if (cookieValue == "Water") {
		library_url = 'http://www.wri.wisc.edu/library/';
		library_name = 'Water Resources Library';
	}
	else if (cookieValue == "Journalism") {
		library_url = 'http://www.journalism.wisc.edu/jrr/';
		library_name = 'Journalism Reading Room';
	}
	else if (cookieValue == "Grants") {
		library_url = 'http://grants.library.wisc.edu/';
		library_name = 'Grants Information Collection';
	}

	if (library_url && library_name) {
		
		/*
		var local_link = document.createElement("a");
		local_link.setAttribute("href",library_url);
		var local_link_text = document.createTextNode(library_name);
		local_link.appendChild(local_link_text);
		local_link.style.textTransform = "none";
		local_link.style.marginRight = "2em";
		var uwlink_container = document.getElementById("uwlink");
		var uwlink = uwlink_container.getElementsByTagName("a");
		uwlink_container.insertBefore(local_link,uwlink[0]);
		*/
		var local_mobile = document.getElementById("local-mobile");
		local_mobile.setAttribute("href",library_url);
		local_mobile.innerHTML = library_name;
	}
}	
