/**
 * Unitstep.net global site-wide scripts
 * 
 * Copyright 2006 Peter Chng unless otherwise noted.
 * Author attribution on other code given.
 *
 * I have no problem with you using my code as long as proper attribution 
 * in the form of a link (URI or URL) is included alongside the code in use. 
 * I would also appreciate an e-mail but this is not necessary. 
 *
 * Note:
 * These scripts are dependent upon the Yahoo! User Interface Library.
 * (http://developer.yahoo.com/yui/)
 */

// Used for compatibility checks. (Credit: PPK from http://www.quirksmode.org)
var W3CDOM = (document.getElementById && document.createElement && document.getElementsByTagName);

/**
 * Taken from: http://artlung.com/blog/2006/04/21/javascript-trim-function/
 *
 * Trims trailing and leading spaces from an input string and returns the 
 * result.
 */
function trim(str)
{
  while(str.charAt(0) == " ")
    str = str.substring(1, str.length);
  while(str.charAt(str.length - 1) == " ")
    str = str.substring(0, str.length - 1);
  return str;
}

/**
 * Changes the background colour on focused form elements to improve
 * usability.
 */
function highlight(e)
{
  // Add the focus class to this element.
  this.className += this.className ? " focus" : "focus";
}
function dehighlight(e)
{
  // Remove the focus class if present.
  if (this.className) {
    var remove = this.className.match(" focus") ? " focus" : "focus";
    this.className = this.className.replace(remove, "");
  }
}

/**
 * Causes the currently focused text or text area input to be 
 * highlighted to enhance form usability.
 */
function enhanceFormUsability()
{
  inputs = document.getElementsByTagName("input");
  for (var i = 0; i < inputs.length; ++i) {
    // Add in ability to detect an "exclusion" class that would prevent these 
    // event listeners from being attached.
    if (inputs[i].type == "text") {
      YAHOO.util.Event.addListener(inputs[i], "focus", highlight);
      YAHOO.util.Event.addListener(inputs[i], "blur", dehighlight);
    }
  }
  textareas = document.getElementsByTagName("textarea");
  YAHOO.util.Event.addListener(textareas, "focus", highlight);
  YAHOO.util.Event.addListener(textareas, "blur", dehighlight);
}

/**
 * Adds default search text, and makes sure something is entered before 
 * searching.
 *
 * Check this out for how to change scope of "this": (Would allow use of object
 * members, instead of just using an Object literal)
 * http://developer.yahoo.com/yui/event/#event
 */
var searchForm = {
  // Checks if proper elements are available and assigns event handlers to them.
  init : function() {
    var searchForm = document.getElementById("searchform");
    var searchBox = document.getElementById("s");
    if (searchForm && searchBox) {
      YAHOO.util.Event.addListener("searchform", "submit", this.submit);
      YAHOO.util.Event.addListener("s", "focus", this.focus);
      // YAHOO.util.Event.addListener("s", "blur", this.blur);
      if (searchBox.value == "")
        searchBox.value = "Enter search terms";
    }
  },
  focus : function(e) {
    var searchBox = document.getElementById("s");
    if (searchBox.value == "Enter search terms")
      searchBox.value = "";
  },
  blur : function(e) {
    var searchBox = document.getElementById("s");
    if (searchBox.value == "")
      searchBox.value = "Enter search terms";
  },
  // Checks to make sure search text is not empty upon submission.
  submit : function(e){
    var isValid = true;
    var searchField = document.getElementById("s");
    if (trim(searchField.value).length < 1)
      isValid = false;
    // Stop form submission if nothing entered. 
    if (!isValid) {
      YAHOO.util.Event.stopEvent(e);
      // Change to on-screen, inline alert?
      alert("Please enter something to search for.");
    }
  }
}

/**
 * Switches the styles between a fixed and fluid width layout.
 */
function toggleStyles() {
  if (!getActiveStyleSheet())
    setActiveStyleSheet("fluid");
  else
    setActiveStyleSheet("");
}

/**
 * Creates the stylesheet switcher widget.
 * Is this the best way to do this, or would it be better to just have the 
 * widget in the document code, and have the event bound to it normally?
 */
function setupStyleSwitcher() {
  var nav = document.getElementById("nav");
  if (nav) {
    var infoMsg = "Switch between fixed and fluid width layouts";
    var p = document.createElement("p");
    var switcher = document.createElement("a");
    switcher.setAttribute("id", "switcher");
    switcher.setAttribute("title", infoMsg);
    switcher.setAttribute("href", "#");
    switcher.onclick = toggleStyles; // Change to use YUI?
    
    var switcherImage = document.createElement("img");
    switcherImage.setAttribute("src", "/images/icons/silk/layout_content.png");
    switcherImage.setAttribute("alt", infoMsg);
    
    switcher.appendChild(switcherImage);
    p.appendChild(switcher);
    nav.appendChild(p);
  }
}

// Switch to using YUI later...
window.onload = function() {
  // Compatibility check.
  if (W3CDOM) {
    initLightbox(); // Load Litebox effects. 
    enhanceFormUsability();
    searchForm.init();
    
    // Stylesheet settings - put together in a wrapper function.
    setupStyleSwitcher();
    var cookie = readCookie("unitstep.net-style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
  }
}
window.onunload = function() {
  if (W3CDOM) {
    // Save stylesheet settings - put together.
    var title = getActiveStyleSheet();
    createCookie("unitstep.net-style", title, 365);
  }
}
var cookie = readCookie("unitstep.net-style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);