function createCookie(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=/";
}

function readCookie(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;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

$(document).ready(function() {
  $("h1").prepend("<span></span>");
  $("h3.callout").each(function() {
    calloutId = $(this).next("div.callout").attr("id");
    displayState = readCookie("sparticle-ltd-"+calloutId);
    if (displayState && $("#"+calloutId)) {
      $("#"+calloutId).css("display",displayState);
      if ($("#"+calloutId).css("display") == 'none') {
        $("#"+calloutId).prev("h3.callout").css("background-position","0 -33px");
      } else {
        $("#"+calloutId).prev("h3.callout").css("background-position","0 -66px");
      }
    }
    $(this).css("cursor","pointer");
  });
  $("h3.callout").click(function() {
    $(this).next("div.callout").slideToggle('fast', function() {
      if ($(this).attr("id")) {
        createCookie("sparticle-ltd-"+$(this).attr("id"),$(this).css("display"),30);
        if ($(this).css("display") == 'none') {
          $(this).prev("h3.callout").css("background-position","0 -33px");
        } else {
          $(this).prev("h3.callout").css("background-position","0 -66px");
        }
      }
    });
  });
  
  $("#moduleList th").click(function() { $(this).parent().next("tr").toggle(); });
  $("table.info tbody tr.highlight").click(function() { $(this).next("tr.hideElement").toggle(); });
  $("div.inset img.showHideButton").click(function() {$(this).prevAll("div.inset-drop-down").slideToggle("fast"); });
  $("div.inset-drop-down img.close").click(function() {
    $(this).parent().slideToggle("fast");
    $(this).parent().parent().find("img.showHideButton").attr("src","/images/more-button.gif");
  });
  $("div.inset img.showHideButton").click(function() {
    if ($(this).attr("src") == "/images/less-button.gif") {     
      $(this).attr("src","/images/more-button.gif");
    } else {
      $(this).attr("src","/images/less-button.gif");
    }
  });
});
