/* Quicklinks Script for Virginia Tech  - as used by answers.vt.edu*/

/* ----- GLOBAL VARIABLES ------ */

// MAX_QL_ITEMS: maximum number of items that can be stored in quicklinks
//   Note: the maximum size a cookie is 4KB 
var MAX_QL_ITEMS = '30';

// COOKIE_TIME: Time in days the cookie should last (5 years)
var COOKIE_TIME = '1825';

// COOKIE_DOMAIN: Host/ Domain for which the cookie applies
var COOKIE_DOMAIN = "answers.vt.edu";

// QL_ID: ID of the list (UL) that will contain the quicklinks
var QL_ID = "kb_ql_list";

// QL_COOKIE_NAME: Name of the cookie that will be stored
var QL_COOKIE_NAME = "kb_quicklinks";

// names of Default and all items (used as a selector to determine
//    what array to use in function createQL() )
var QL_DEFAULT_NAME = "default_ql";
var QL_ALL_NAME = "all_ql";

// name of the Customize Quicklinks label
var CUSTOMIZE_NAME = "";
// URL to the Quicklinks customize page
// Set to '' if you don't want to automatically add it to QL_ID
var CUSTOMIZE_URL = "";

// default_ql and az_index are predefined 2d arrays with a list of urls and titles
// this checks to see if it exists, else sets the global var to be blank if it doesn't
// az_index is defined in quicklinks_customize.js
if (typeof(qldefaults)!="undefined"){
  var QL_DEFAULT = qldefaults;
}else{
  var QL_DEFAULT = '';
}

// ----- END GLOBAL VARIABLE DEFINITIONS -----
addLoadEvent(checkQLCookie);

// will check to see if a quicklink cookie exists, if not will load defaults
function checkQLCookie(){
  var quicklinks=readCookie(QL_COOKIE_NAME);
  if (quicklinks!=null && quicklinks!="")
    {loadQLCookie();}
  else 
    {createQL();}
}

function createQL(){
  qlItemArray = QL_DEFAULT;
  if (qlItemArray.length > (MAX_QL_ITEMS)){
    alert("You are only allowed to have " + MAX_QL_ITEMS + " in your quicklinks");
  }else{
    // Don't set a cookie unless they specifically save it, hence next line is commented out
    // setQLCookie(qlItemArray);
    if(document.getElementById(QL_ID)){createQLList(qlItemArray, QL_ID);}
  }
}

// Given a string and preset format (currently only cookie)
//   will break that string appropriately and return an array
function createItemArrayFromStr(docStr, format){
  if (format == "cookie"){
    // unescape cookie before splitting it
    var splitStr = (unescape(docStr)).split("<i>");
    var numItems = (splitStr.length-1)/2;
    var qlItems = new Array(numItems);
    for (i = 0; i<numItems; i++){
      qlItems[i] = new Array(2);
      // Need offset by one b/c first element in split is empty
      qlItems[i][0]=splitStr[(i*2)+1];
      qlItems[i][1]=splitStr[(i*2)+2];
    }
    return qlItems;
  }else{
    alert("unrecognized format");
  }
}
function loadQLCookie(){
  var qlCookie = readCookie(QL_COOKIE_NAME);
  var qlItems = createItemArrayFromStr(qlCookie, "cookie");
  createQLList(qlItems, QL_ID);
}

function setQLCookie(qlItemArray)
{
  var cookieStr = '';
  for (i = 0; i <qlItemArray.length; i++)
  {
    //escape before placing it into the cookie (addresses safari error)
    cookieStr += '<i>'
        + escape(qlItemArray[i][0])
        + '<i>'
        + escape(qlItemArray[i][1]);
  }
  createCookie(QL_COOKIE_NAME, cookieStr, COOKIE_TIME);
}

// Given an array of items and a ul id, will add lis of the item to the ul 
// qlItemArray is a 2d array of a title and URL
function createQLList(qlItemArray, elementId){
// element must exist before we do anything to it
  if (document.getElementById(elementId)){
    var qlList = document.getElementById(elementId);
    // clear out anything that may already be there.
    qlList.innerHTML = '';
    var newNode, newItem;
    for (i = 0; i < qlItemArray.length; i++){
          newNode = document.createElement("li"); 
          newItem = '<a href="'
          + qlItemArray[i][1]
          + '">'
          + qlItemArray[i][0]
          + '</a>';
          newNode.innerHTML = newItem;
          qlList.appendChild(newNode);
    }
    // append the customize name & link to the end of QL_ID
    if (elementId == QL_ID && CUSTOMIZE_URL != '' && CUSTOMIZE_NAME != ''){
          newNode = document.createElement("li"); 
          newItem = '<a href="'
          + CUSTOMIZE_URL
          + '">'
          + CUSTOMIZE_NAME
          + '</a>';
          newNode.innerHTML = newItem;
          qlList.appendChild(newNode);
    }
  }
}

/* 
-------------------------
Generic Cookie Functions 
-------------------------
*/
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=/; domain="+COOKIE_DOMAIN;
}

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));
}
// ----- END Generic Cookie Functions -----


function addLoadEvent(func) {
  var oldOnLoad = window.onload
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldOnLoad();
      func();
    }
  }
}
// ------ END DomReady Code -----
