// Modified from Bill Dortch's Cookie Functions (hidaho.com)
// (found in JavaScript Bible)
function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + encodeURIComponent(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

// from Bill Dortch's Cookie Functions (hidaho.com)
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";
  }
}

function popUp(url, width, height){
	options = "status=no,scrollbars=no,";
	var leftPos = (screen.availWidth - width) / 2;
	var topPos = (screen.availHeight - height) / 2;
	options += 'width=' + ((parseInt(width)) + 20) + ',height=' + ((parseInt(height)) + 25) + ',left=' + leftPos + ',top=' + topPos;
	window.open(url, '', options);
}

function backClose(){
	h = window.history;
	if ( h.length > 1 ) { // if there is a history
    	h.back();     // equivalent to clicking back button
	} else window.close();
}

function showHideLink(id, showText, hideText){
	text = document.getElementById("link_box_d" + id);
	link = document.getElementById("link_box_l" + id);

	if(text.style.display != "block"){
		text.style.display = "block";
		link.firstChild.nodeValue = hideText;
	} else {
		text.style.display = "none";
		link.firstChild.nodeValue = showText;
	}
}

g_onload = null;

function addLoadEvent(func) {
   var oldonload = g_onload;
   if (typeof oldonload != 'function') {
      g_onload = func;
   }
   else {
      g_onload = function() {
     	 if (oldonload) {
        	oldonload();
      	 }
     	 func();
    	}
	}
	window.onload = g_onload;
}

function onLoad(){
	if(g_onload){
		g_onload();
	}
}

// javascript letters
$(function(){
	var menu = $(".js-letters");
	var otherLabel = "...";
	var otherClass = "other";

	if(menu.length == 1){
		var list = $("<ul></ul>");

		// letter = char or "-" for "other letters"
		var selectLetter = function(letter){
			menu.find("> LI").hide();
			menu.find("> LI."+letter).show();
			list.find("A").removeClass("selected");
			list.find("A."+letter).addClass("selected");
		};

		var charsAssociative = [];
		var chars = [];
		var links = [];
		var other = false;

		var items = menu.find("> LI > SPAN");
		items.each(function(){
			var ch = $(this).text().charAt(0).toUpperCase();
			if(!/[A-Z]/.test(ch)) other = true;
			else charsAssociative[ch] = true;
		});
		for(var ch in charsAssociative) chars[chars.length] = ch;
		chars.sort();
		if(other) chars[chars.length] = otherLabel;

		menu.before(list);
		for(var x = 0; x < chars.length; x++) list.append($("<a class='"+chars[x]+"' href='#'>"+chars[x]+"</a>").click(function(){
			// onclick function
			var letter = $(this).text();

			if(letter != otherLabel) selectLetter(letter);
			else selectLetter(otherClass);
		}));
		list.wrap('<div class="navigation-letters"></div>').find("a").wrap("<li></li>");

		firstChar = chars[0];
		selectedChar = null;

		menu.find("> LI").each(function(){
			var ch = $(this).find("> SPAN").text().charAt(0).toUpperCase();
			if(!/[A-Z]/.test(ch)) ch = otherClass;

			if(selectedChar == null && $(this).find(".selected").length > 0) selectedChar = ch;
			$(this).addClass(ch);
		});

		selectLetter(selectedChar ? selectedChar : firstChar);
	}
});