/************************************
 * String Pluralization
 * count.pluralize('', 'ies');
 ***********************************/
Number.prototype.pluralize = function(singular, plural) {
  var a = '', b = 's';
  if (singular && plural) { a = singular; b = plural; }
  return (this == 1) ? a : b;
};


/************************************
 * Flash Player Detection
 ***********************************/
var hasFlash = function(){

  var nRequiredVersion = 9;

  if(navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("Windows") > -1){
    document.write('<script language="VBScript"\> \non error resume next \nhasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ' + nRequiredVersion + '))) \n</script\> \n');
    /*  If executed, the VBScript above checks for Flash and sets the hasFlash variable.
      If VBScript is not supported it's value will still be undefined, so we'll run it though another test
      This will make sure even Opera identified as IE will be tested */
    if(window.hasFlash != null){
      return window.hasFlash;
    };
  };

  if(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
    var flashDescription = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description;
    return parseInt(flashDescription.substr(flashDescription.indexOf(".") - 2, 2), 10) >= nRequiredVersion;
  };

  return false;
}();


/************************************
 * Cookie Helpers
 ***********************************/
/* directly from http://www.quirksmode.org/js/cookies.html */
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);
}

  // Used to decode cookie data.
function urlDecode(encoded) {
  // Replace + with ' '
  // Replace %xx with equivalent character
  // Put [ERROR] in output if %xx is invalid.
  var HEXCHARS = "0123456789ABCDEFabcdef";
  var plaintext = "";
  var i = 0;
  while (i < encoded.length) {
    var ch = encoded.charAt(i);
    if (ch == "+") {
      plaintext += " ";
      i++;
    } else if (ch == "%") {
        if (i < (encoded.length-2)
            && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
            && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
          plaintext += unescape( encoded.substr(i,3) );
          i += 3;
        } else {
          alert( 'Bad escape combination near ...' + encoded.substr(i) );
          plaintext += "%[ERROR]";
          i++;
        }
     } else {
       plaintext += ch;
       i++;
    }
  } // while
  return plaintext;
}


/************************************
 * Clickables (big clickable targets)
 ***********************************/
var Clickables = Class.create({
  initialize: function(options) {
    selector = $$(options.selector) || $$(".clickables");

    this.clickable = options.clickable || "li";
    this.hoverclass = options.hoverclass || "over";

    selector.each(function(e) { $(e).setStyle({ cursor: 'pointer' }) });

    selector.invoke('observe', 'click', this.clicked.bind(this));
    selector.invoke('observe', 'mouseover', this.activate.bind(this));
    selector.invoke('observe', 'mouseout', this.deactivate.bind(this));
  },

  activate: function(event) {
    event.stop();
    element = Event.findElement(event, this.clickable);
    element.addClassName(this.hoverclass);
  },

  deactivate: function(event) {
    event.stop();
    element = Event.findElement(event, this.clickable);
    element.removeClassName(this.hoverclass);
  },

  clicked: function(event) {
    event.stop();
    element = Event.findElement(event, this.clickable);
    window.location = element.select("a").first().href;
  }
});
