/*------------------------------------------------------------------------------
Filename:       jsUtilities Library
Author:         Aaron Gustafson (aaron at easy-designs dot net)
                unless otherwise noted
Creation Date:  4 June 2005
Version:        2.1
Homepage:       http://www.easy-designs.net/code/jsUtilities/
License:        Creative Commons Attribution-ShareAlike 2.0 License
                http://creativecommons.org/licenses/by-sa/2.0/
Note:           If you change or improve on this script, please let us know by
                emailing the author (above) with a link to your demo page.

Added by Lars Gunther:
+ createElement (simon.incutio.com)
+ getWeekNr (PPK)
------------------------------------------------------------------------------*/
// ---------------------------------------------------------------------
//                      array.push (if unsupported)
// ---------------------------------------------------------------------
if(Array.prototype.push == null) {
  Array.prototype.push = function(item) {
    this[this.length] = item;
    return this.length;
  };
};
// ---------------------------------------------------------------------
//                      array.shift (if unsupported)
// ---------------------------------------------------------------------
if (Array.prototype.shift == null) {
  Array.prototype.shift = function() {
    var response = this[0];
    for (var i=0; i < this.length-1; i++) {
      this[i] = this[i + 1];
    };
    this.length--;
    return response;
  };
};
// ---------------------------------------------------------------------
//                  function.apply (if unsupported)
//           Courtesy of Aaron Boodman - http://youngpup.net
// ---------------------------------------------------------------------
if (!Function.prototype.apply) {
  Function.prototype.apply = function(oScope, args) {
    var sarg = [];
    var rtrn, call;
    if (!oScope) oScope = window;
    if (!args) args = [];
    for (var i = 0; i < args.length; i++) {
      sarg[i] = "args["+i+"]";
    };
    call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
    oScope.__applyTemp__ = this;
    rtrn = eval(call);
    oScope.__applyTemp__ = null;
	return rtrn;
  };
};
// ---------------------------------------------------------------------
//                               inArray()
//                           [Port from PHP]
//               Hunts for a value in the specified array
// ---------------------------------------------------------------------
Array.prototype.inArray = function (needle) {
  for (var i=0; i < this.length; i++) {
    if (this[i] === needle) {
      return i;
    }
  }
  return false;
};
// ---------------------------------------------------------------------
//                               isArray()
//                           [Port from PHP]
//                  verifies if something is an array
// ---------------------------------------------------------------------
Array.prototype.isArray = function() {
  return (typeof(this.length)=="undefined") ? false : true;
};
// ---------------------------------------------------------------------
//                               ksort()
//                           [Port from PHP]
//                     sorts an array by key names
// ---------------------------------------------------------------------
Array.prototype.ksort = function() {
  var sArr = [];
  var tArr = [];
  var n = 0;
  for (i in this)
    tArr[n++] = i+"|"+this[i];
  tArr = tArr.sort();
  for (var i=0; i<tArr.length; i++) {
    var x = tArr[i].split("|");
    sArr[x[0]] = x[1];
  }
  return sArr;
};

// ---------------------------------------------------------------------
//                             addClass()
//                 appends the specified class to the object
// ---------------------------------------------------------------------
var addClass = function(theClass) {
  if (this.className != '') {
    this.className += ' ' + theClass;
  } else {
    this.className = theClass;
  }
};
// Funktionen måste finnas i global namespace för MSIE
Object.prototype.addClass = addClass;
// ---------------------------------------------------------------------
//                           removeClass()
//                 removes the specified class to the object
// ---------------------------------------------------------------------
var removeClass = function(theClass) {
  var oldClass = this.className;
  var regExp = new RegExp('\\s?'+theClass+'\\b');
  if (oldClass.indexOf(theClass) != -1) {
    this.className = oldClass.replace(regExp,'');
  }
};
Object.prototype.removeClass = removeClass;
// ---------------------------------------------------------------------
//                      lastChildContainingText()
//  finds the last block-level text-containing element within an object
// ---------------------------------------------------------------------
var lastChildContainingText = function() {
  var testChild = this.lastChild;
  var contentCntnr = ['p','li','dd'];
  while (testChild.nodeType != 1) {
    testChild = testChild.previousSibling;
  }
  var tag = testChild.tagName.toLowerCase();
  var tagInArr = inArray.apply(contentCntnr, [tag]);
  if (!tagInArr && tagInArr!==0) {
    testChild = lastChildContainingText.apply(testChild);
  }
  return testChild;
};
Object.prototype.lastChildContainingText = lastChildContainingText;

// ---------------------------------------------------------------------
//              HTML and XHTML secure way of adding DOM-nodes
// @see http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
// This one goes to the global namespace !
// ---------------------------------------------------------------------
function createElement(element) {
  if (typeof document.createElementNS != 'undefined') {
    return document.createElementNS('http://www.w3.org/1999/xhtml', element);
  }
  if (typeof document.createElement != 'undefined') {
    return document.createElement(element);
  }
  return false;
}
// ----------------------------------------------------------------
//          getWeekNr returns weeknumber from a date
// @see http://www.quirksmode.org/js/week.html, edited
// ----------------------------------------------------------------
Date.prototype.getWeekNr = function()
{
    // When in a month that starts on a sunday this function would originally  return a number too high -> See correction
    var Year = this.getFullYear();
    var Month = this.getMonth();
    var Day = this.getDate();
    var now = Date.UTC(Year,Month,Day+1,0,0,0);
    var Firstday = new Date();
    Firstday.setYear(Year);
    Firstday.setMonth(0);
    Firstday.setDate(1);
    var then = Date.UTC(Year,0,1,0,0,0);
    var Compensation = Firstday.getDay();
    if (Compensation > 3) Compensation -= 4;
    else Compensation += 3;
    var NumberOfWeek =  Math.round((((now-then)/86400000)+Compensation)/7);
    // correction
    if ( this.getDay() == 0 && this.getDate() < 8 ) NumberOfWeek -= 1;

    return NumberOfWeek;
};


