
/* REFORMAT HREF FOR INTERNAL LINKS */

// pass these query string vars through to other internal pages
qs_passthrough = ["nocss", "debug", "trackdebug", "nocache"];

function set_href(linkobj) {
  var href = linkobj.attr("href");
  
  // is link engine-internal?
  var href_internal = href.match(/(^[.]$)|(^[?])/) || linkobj.hasClass("bengine");
  
  if (href_internal) {
    var href_qs = qs_deserialize(href);
    
    // pass query string vars through
    for (var i = 0; i < qs_passthrough.length; i++) {
      if (typeof qs[qs_passthrough[i]] == "string" && qs[qs_passthrough[i]] != "") {
        href_qs[qs_passthrough[i]] = qs[qs_passthrough[i]];
      }
    }
    
    // replace the link's query string with the new one
    var new_qs = qs_serialize(href_qs);
    href = href.replace(/[?].*/, "") + (new_qs == "" ? "" : "?" + new_qs);
    if (href == "") href = ".";
    linkobj.attr("href", href);
  }
}

$(document).ready(function() {
  $("a[@href]").each(function() {
    set_href($(this));
  });
});



/* retrieve value from "prefix-value" from a class or space-delimited list of classes */

function get_value_from_class(prefix, class_list) {
  if (typeof class_list != "string") {
    class_list = $(class_list).attr("class");
  }
  class_list = " " + class_list + " ";
  prefix = prefix.replace(/(.*?)[-]?$/, " $1-");
  var idx = class_list.indexOf(prefix);
  if (idx == -1) {
    return "";
  } else {
    var val = class_list.substring(idx + prefix.length);
    val = val.substring(0, val.indexOf(" "));
    return val;
  }
}


/* give text field a default value "label" until clicked */

function set_default_text(input, default_text) {
  input.focus(function(){
    if ($(this).attr("value") == default_text) {
      $(this).attr("value", "");
      $(this).removeClass("blur");
    }
  }).blur(function(){
    if ($.trim($(this).attr("value")) == "" || $(this).attr("value") == default_text) {
      $(this).attr("value", default_text);
      $(this).addClass("blur");
    }
  }).blur();
}


/* SERIALIZE & DESERIALIZE QUERY STRING */

function qs_serialize(obj) {
  var s = [];
  for (var p in obj) {
    if (obj[p] != undefined) {
      s.push(p + "=" + obj[p]);
    }
  }
  return s.join("&");
}

function qs_deserialize(s) {
  var obj = {};
  s = (s.indexOf('?') == -1) ? s : s.substr(s.indexOf('?') + 1, s.length);
  var a = s.split('&');
  for(var i = 0; i < a.length; i++) {
    var nv = a[i].split('=');
    if (nv[0] && nv[1]) {
        obj[nv[0]] = unescape(nv[1]);
    }
  }
  return obj;
}

qs = qs_deserialize(document.location.search);



/* EASY DEBUG TRACING */

function debug() {
  // create the debug div if it doesn't exist
  if ($("#debug").length == 0) {
    debug_count = 0;
    $("<div id='debug'><b>DEBUG</b><p></p></div>").appendTo("body");
    var props = {
      position: "fixed",
      left: 0,
      top: 0,
      backgroundColor: "#fff",
      color: "#f00",
      padding: "3px"
    };
    $("#debug").css(props);
  }
  // print arguments to debug div as comma-separated list
  var txt_arr = [];
  for (var i = 0; i < arguments.length; i++) {
    txt_arr.push(String(arguments[i]));
  }
  $("#debug p").prepend(++debug_count + " | " + txt_arr.join(", ") + "<br>");
}




/* COOKIES (thx quirksmode) */

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);
}


