
/********************************************************************************
	
	mt_arrays.js
	prototype extensions

	adds methods:
	-	shift()
	-	unshift()
	-	push()
	-	pop()
	-	splice()
	-	roll()
	required by IE 5.0

********************************************************************************/
if(Array.prototype.push && ([0].push(true)== true))
	Array.prototype.push = null;
if(Array.prototype.splice && typeof([0].splice(0)) == "number")
	Array.prototype.splice = null;


if(!Array.prototype.shift){
	Array.prototype.shift = function(){
		var a1 = this[0];
		this.reverse();
		this.length = Math.max(this.length - 1, 0);
		this.reverse();
		return a1;
	};
};

if(!Array.prototype.unshift){
	Array.prototype.unshift = function(){
		this.reverse();
		for(var i=arguments.length-1;i>=0;i--)
			this[this.length]=arguments[i];
		this.reverse();
		return this.length;
	};
};

if(!Array.prototype.push){
	Array.prototype.push = function(){
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;;
	};
};

if(!Array.prototype.pop){
	Array.prototype.pop = function(){
	    lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
	    return lastElement;
	};
};

if(!Array.prototype.splice){
    Array.prototype.splice = function(ind,cnt){
        if(arguments.length == 0) return ind;
        if(typeof ind != "number") ind = 0;
        if(ind < 0) ind = Math.max(0,this.length + ind);
        if(ind > this.length){
            if(arguments.length > 2) ind = this.length;
            else return [];
        }
        if(arguments.length < 2) cnt = this.length-ind;
        cnt = (typeof cnt == "number") ? Math.max(0,cnt) : 0;
        removeArray = this.slice(ind,ind+cnt);
        endArray = this.slice(ind+cnt);
        this.length = ind;
        for(var i=2;i<arguments.length;i++)
            this[this.length] = arguments[i];
        for(var i=0;i<endArray.length;i++)
            this[this.length] = endArray[i];
        return removeArray;
    };
};

if(!window.Array.prototype.roll){
	window.Array.prototype.roll = function(direction){		
		direction = direction || false;
		if(!direction){
			this.splice(this.length, 1, this[0]);
			this.splice(0,1);
		}else{
			this.splice(0,0, this[this.length-1]);
			this.splice(this.length-1,1);
		};
	};
};
