﻿
/******************************************************************************
File: array.js

This file contains methods which extends the native Array object. For example 
for adding, randomizing, etc. functionality. It also contains some cross-browser 
functionalitiy.
******************************************************************************/


/******************************************************************************/
/* Additional Array methods                                                   */
/******************************************************************************/
  Array.prototype.add              =  array_add;
  Array.prototype.clear            =  array_clear;
  Array.prototype.getItem          =  array_getItem;
  Array.prototype.getItemById      =  array_getItemById;
  Array.prototype.removeDuplicates =  array_removeDuplicates;
  Array.prototype.removeItem       =  array_removeItem;
  Array.prototype.removeItemById   =  array_removeItemById;
  Array.prototype.randomize        =  array_randomize;
  Array.prototype.lastChild        =  array_lastChild;
  
/******************************************************************************/
function array_removeDuplicates(){
  var temp=new Array();
  this.sort();
  for(i=0;i<this.length;i++) {
    if(this[i]==this[i+1]) {continue}
    temp[temp.length]=this[i];
  }
  return temp;
}

/******************************************************************************/
function array_add (_str){
  this[this.length] = _str;
  return this.lastChild();
}

/******************************************************************************/
function array_clear (){
  if (this && this.length > 0)
    this.splice(0,this.length);
}

/******************************************************************************/
function array_getItem(_str){
  for (var i=0; i<this.length; i++){
    if (this[i] == _str)
      return this[i];
  }
  // if there was not a match, return null
  return null;
}

/******************************************************************************/
function array_getItemById(_id){
  for (var i=0; i<this.length; i++){
    if (this[i].id) {
      if (this[i].id == _id)
        return this[i];
    }
  }
  // if there was not a match, return null
  return null;
}

/******************************************************************************/
function array_removeItemById(_id){
  for (var i=0; i<this.length; i++){
    if (this[i].id) {
      if (this[i].id == _id)
        this.splice(i,1);
    }
  }
}

/******************************************************************************/
function array_removeItem(_str){
  for (var i=0; i<this.length; i++){
    if (this[i] == _str)
      this.splice(i,1);
  }
}

/******************************************************************************/
function array_randomize() {
  // Er wordt gekeken of de browser safari is
  // Want deze functie werkt niet goed onder safari
  if (!browser.saf) {
    var tempArray = new Array();
    var i = this.length;
    while ( i-- ) {
      var j = Math.floor( Math.random() * ( i + 1) );
      tempArray.add(this[j]);
	    this.splice(j,1);
    } 
    for (var i = 0; i < tempArray.length; i++) {
      this.add(tempArray[i]);
    }
  }
}

/******************************************************************************/
function array_lastChild() {
  if (this.length>0)
    return this[this.length-1];
  else
    return null;
}



/******************************************************************************
Cross browser functioality for browsers not implementing the full array 
functionality of ECMA262-3. 
(by: liorean <http://liorean.web-graphics.com/>)
******************************************************************************/
if(typeof Array.prototype.copy=='undefined')
  Array.prototype.copy=function(a){
    var
      i=0,
      b=[];
    for(i;i<this.length;i++)
      b[i]=(typeof this[i].copy!='undefined')?
        this[i].copy():
        this[i];
    return b
  };

if(typeof Array.prototype.concat=='undefined')
  Array.prototype.concat=function(a){
    var
      i=0,
      b=this.copy();
    for(i;i<a.length;i++)
      b[b.length]=a[i];
    return b
  };
    
if(typeof Array.prototype.pop=='undefined')
  Array.prototype.pop=function(){
    var
      b=this[this.length-1];
    this.length--;
    return b
  };

if(typeof Array.prototype.push=='undefined')
  Array.prototype.push=function(){
    var
      i=0,
      b=this.length,
      a=arguments;
    for(i;i<a.length;i++)
      this[b+i]=a[i];
    return this.length
  };

if(typeof Array.prototype.shift=='undefined')
  Array.prototype.shift=function(){
    var
      i=0,
      b=this[0];
    for(i;i<this.length-1;i++)
      this[i]=this[i+1];
    this.length--;
    return b
  };

if(typeof Array.prototype.slice=='undefined')
  Array.prototype.slice=function(a,c){
    var
      i=0,
      b,
      d=[];
    if(!c)
      c=this.length;
    if(c<0)
      c=this.length+c;
    if(a<0)
      a=this.length-a;
    if(c<a){
      b=a;
      a=c;
      c=b
    }
    for(i;i<c-a;i++)
      d[i]=this[a+i];
    return d
  };

if(typeof Array.prototype.splice=='undefined')
  Array.prototype.splice=function(a,c){
    var
      i=0,
      e=arguments,
      d=this.copy(),
      f=a;
    if(!c)
      c=this.length-a;
    for(i;i<e.length-2;i++)
      this[a+i]=e[i+2];
    for(a;a<this.length-c;a++)
      this[a+e.length-2]=d[a-c];
    this.length-=c-e.length+2;
    return d.slice(f,f+c)
  };

if(typeof Array.prototype.unshift=='undefined')
  Array.prototype.unshift=function(a){
    var b;
    this.reverse();
    b=this.push(a);
    this.reverse();
    return b
  };