// array.js
/* 
ARRAY EXTENSIONS
version 0.1
by Caio Chassot (http://v2studio.com/k/code/)
*/




if (!Array.prototype.push) Array.prototype.push = function() { 
	for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
	return this.length;
}

Array.prototype.find = function(value, start) {
	start = start || 0;
	for (var i=start; i<this.length; i++) 
		if (this[i]==value)
			return i;
	return false; 
}

Array.prototype.has = function(value) {
	return this.find(value)!==false;
}

Array.prototype.count = function(value) {
	var pos, start = 0, count = 0;
	while ((pos = this.find(value, start))!==false) {
		start = pos + 1;
		count++;
	}
	return count;
}

Array.prototype.remove = function(value,all) {
	while (this.has(value)) {
		this.splice(this.find(value),1);
		if (!all) break
	}
	return this;
}

Array.prototype.last = function() {
	return this[this.length-1];
}

Array.prototype.sjoin = function() { return this.join(' ') }
Array.prototype.njoin = function() { return this.join('\n') }
Array.prototype.cjoin = function() { return this.join(', ') }


// functional.js
/*
FUNCTIONAL
version 0.1
by Caio Chassot (http://v2studio.com/k/code/)

uses: string.js

string.js is necessary for __strfn. if you pass only real functions (no strings)
to map, filter and reduce, string.js is not needed.
*/



function __strfn(args, fn) { 
	function quote(s) { return '"' + s.replace(/"/g,'\\"') + '"' }
	if (!/\breturn\b/.test(fn)) {
		fn = fn.replace(/;\s+$/, '');
		fn = fn.insert(fn.lastIndexOf(';')+1, ' return ');
	}
	return eval('new Function({0},{1})'.subArgs(
		map(args.split(/\s*,\s*/), quote).join(),
		quote(fn)));
}

function map(list, fn) {
	if (typeof(fn)=='string') return map(list, __strfn('item,idx,list', fn));

	var result = [];
	fn = fn || function(v) {return v};
	for (var i=0; i < list.length; i++) result.push(fn(list[i], i, list)); 
	return result;
}

function filter(list, fn) { 
	if (typeof(fn)=='string') return filter(list, __strfn('item,idx,list', fn));

	var result = [];
	fn = fn || function(v) {return v};
	map(list, function(item,idx,list) { if (fn(item,idx,list)) result.push(item) } );
	return result;
}

function reduce(list, fn, initial) { 
	if (typeof(fn)=='string') return reduce(list, __strfn('a,b', fn), initial);
	if (isdef(initial)) list.splice(0,0,initial);
	if (list.length===0) return false;
	if (list.length===1) return list[0];
	var result = list[0];
	var i = 1;
	while(i<list.length) result = fn(result,list[i++]);
	return result;
}








// utils.js
/*
MISC UTILITIES
version 0.1
by Caio Chassot (http://v2studio.com/k/code/)

uses: functional.js (for maprange)
*/



// MISC CLEANING-AFTER-MICROSOFT STUFF

function isUndefined(v) { 
	var undef;
	return v===undef;
}


// LAZINESS

function undef(v) {  return  isUndefined(v) }
function isdef(v) {  return !isUndefined(v) }



// dom.js
/* 
DOM AND DOM EVENTS UTILITIES
version 0.1
by Caio Chassot (http://v2studio.com/k/code/)

uses: utils.js, array.js, functional.js
*/



// BASIC DOM

function getElem(elem) { 
	if (document.getElementById) {
		if (typeof elem == "string") {
			elem = document.getElementById(elem);
		}
	} 
	return elem;
}

function getElementsByClass(className, tagName, parentNode) { 
	parentNode = isdef(parentNode)? getElem(parentNode) : document;
	if (undef(tagName)) tagName = '*';
	return filter(parentNode.getElementsByTagName(tagName), 
		function(elem) { return hasClass(elem, className) });
}


// CLASS MANIPULATION

function hasClass(elem, className) { 
	return getElem(elem).className.split(' ').count(className);
}

function remClass(elem, className, all) { 
	elem = getElem(elem);
	elem.className = elem.className.split(' ').remove(className,all).join(' ');
}

function addClass(elem, className, allowDuplicates) { 
	elem = getElem(elem);
	if (!allowDuplicates && elem.className.split(' ').has(className)) return;
	elem.className += ' ' + className;
}


// SHORTHANDS

function getHtml() { 
	return document.getElementsByTagName('html')[0];
}

function getHead() { 
	return document.getElementsByTagName('head')[0];
}

function getBody() { 
	return document.getElementsByTagName('body')[0];
}

function getAll(tagName, parent) { 
	return (!isUndefined(parent)? getElem(parent) : document).
		getElementsByTagName(!isUndefined(tagName)? tagName : '*');
}


// DOM EVENTS

function listen(event, elem, func) { 
	elem = getElem(elem);
	if (elem.addEventListener)  // W3C DOM 
		elem.addEventListener(event,func,false);  
	else if (elem.attachEvent)  // IE DOM
		elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
}

function mlisten(event, elem_list, func) { 
	map(elem_list, function(elem) { listen(event, elem, func) } );
}

function W3CDOM_Event(currentTarget) { 
	this.currentTarget   = currentTarget;
	this.preventDefault  = function() { window.event.returnValue  = false }
	this.stopPropagation = function() { window.event.cancelBubble = true }
	this.target  = window.event.srcElement;
	this.clientX = event.clientX;
	this.clientY = event.clientY;
	return this;
}



AddDropDivs = function() {
	if (document.getElementById) {
		thenodelist=getElementsByClass("dropshadow");
		for (i=0; i<thenodelist.length; i++) {
			node = thenodelist[i]; 
			newdiv1 = document.createElement("div");
			newdiv1.className = "wrap1";
			newdiv2 = document.createElement("div");
			newdiv2.className = "wrap2";
			newdiv3 = document.createElement("div");
			newdiv3.className = "wrap3";
			node.parentNode.replaceChild(newdiv1, node);
			newdiv1.appendChild(newdiv2);
			newdiv2.appendChild(newdiv3);
			newdiv3.appendChild (node);
		}
	}
}


window.onload=AddDropDivs;
