
//TODO load dependencies: browserdetect

//onDOMReady Event Extension
//http://clientside.cnet.com/code-snippets/event-scripting/a-dom-ready-extension-for-prototype/
Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    AC.isDomReady = true;
    if(this._readyCallbacks) this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
	if(AC.isDomReady) {
		f();
	}
    else {
		if (!this._readyCallbacks) {
			  var domReady = this._domReady.bind(this);
			  
			  if (document.addEventListener)
				document.addEventListener("DOMContentLoaded", domReady, false);
				
				/*@cc_on @*/
				/*@if (@_win32)
					document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
					document.getElementById("__ie_onload").onreadystatechange = function() {
						if (this.readyState == "complete") domReady(); 
					};
				/*@end @*/
				
				if (/WebKit/i.test(navigator.userAgent)) { 
				  this._timer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) domReady(); 
				  }, 10);
				}
				
				Event.observe(window, 'load', domReady);
				Event._readyCallbacks =  [];
			}
			Event._readyCallbacks.push(f);
	  }
  }
});


// this is called Element2 because
// adding methods to Element BLOWS UP IE7 
// for a reason I still haven't got to the bottom
// of.  It appears to be fine though, as long
// as you don't try to add additional methods
// to Element itself.
var Element2 = {};
Element2.Methods = {
	
	getInnerDimensions: function(element) {
    	
		element = $(element);
		var d = Element.getDimensions(element);
		
		var innerHeight = d.height;
		var styleOf = Element.getStyle;
		innerHeight -= styleOf(element, 'border-top-width') && styleOf(element, 'border-top-width') != 'medium' ? parseInt(styleOf(element, 'border-top-width'), 10) : 0;
		innerHeight -= styleOf(element, 'border-bottom-width') && styleOf(element, 'border-bottom-width') != 'medium' ? parseInt(styleOf(element, 'border-bottom-width'), 10) : 0;
		innerHeight -= styleOf(element, 'padding-top') ? parseInt(styleOf(element, 'padding-top'), 10) : 0;
		innerHeight -= styleOf(element, 'padding-bottom') ? parseInt(styleOf(element, 'padding-bottom'), 10) : 0;

		var innerWidth = d.width;
		innerWidth -= styleOf(element, 'border-left-width') && styleOf(element, 'border-left-width') != 'medium' ? parseInt(styleOf(element, 'border-left-width'), 10) : 0;
		innerWidth -= styleOf(element, 'border-right-width') && styleOf(element, 'border-right-width') != 'medium' ? parseInt(styleOf(element, 'border-right-width'), 10) : 0;
		innerWidth -= styleOf(element, 'padding-left') ? parseInt(styleOf(element, 'padding-left'), 10) : 0;
		innerWidth -= styleOf(element, 'padding-right') ? parseInt(styleOf(element, 'padding-right'), 10) : 0;

	    return {width: innerWidth, height: innerHeight};
	},
	
	/*
		Yes, we understand this is a hack. Safari is calculating margins for unpositioned elements
		as the total remaining viewport width
	*/
	getOuterDimensions: function(element) {
		element = $(element);
		var clone = element.cloneNode(true);
		
		document.body.appendChild(clone);
		Element.setStyle(clone, { position: "absolute", visibility: "hidden" });
		var d = Element.getDimensions(clone);
		
		var outerHeight = d.height;
		var styleOf = Element.getStyle;
		outerHeight += styleOf(clone, 'margin-top') ? parseInt(styleOf(clone, 'margin-top'), 10) : 0;
		outerHeight += styleOf(clone, 'margin-bottom') ? parseInt(styleOf(clone, 'margin-bottom'), 10) : 0;

		var outerWidth = d.width;
		outerWidth += styleOf(clone, 'margin-left') ? parseInt(styleOf(clone, 'margin-left'), 10) : 0;
		outerWidth += styleOf(clone, 'margin-right') ? parseInt(styleOf(clone, 'margin-right'), 10) : 0;

		Element.remove(clone);
		
		return {width: outerWidth, height: outerHeight};
	},
	
	removeAllChildNodes: function(element) {
		element = $(element);
		if(! element) { return; }
		
		while (element.hasChildNodes()) {
	  		element.removeChild(element.lastChild);
		}
	}
	
};

Object.extend(Element, Element2.Methods);


Element.Methods.childNodeWithNodeTypeAtIndex = function(element, nodeType,index) {
	var node = element.firstChild;
    if (!node) return null;
	var i=0;
    while (node) {
		if(node.nodeType === nodeType) {
			if(index === i) {
				return node;
			}
			i++;
		}
		node = node.nextSibling;
	}
    return null;
};



/* String Extensions Begin */
String.prototype.lastPathComponent = function() {
	var index = this.lastIndexOf("/");
	if(index != -1) {
		return this.substring(index+1,this.length-1);
	}
	else return null;
}

String.prototype.stringByDeletingLastPathComponent = function() {
	var index = this.lastIndexOf("/");
	if(index != -1) {
		return this.slice(0,index);
	}
	else return null;
}

String.prototype.stringByAppendingPathComponent = function(value) {
 	return (this.lastIndexOf("/") !== (this.length-1)) ? (this+"/"+value) : (this+value);
}

String.prototype.stringByRemovingPrefix = function(value) {
	var index = this.indexOf(value);
	if(index > -1) {
		var result = this.substring(index+value.length,this.length);
		return result;
	}
	else {
		return this;
	}
}
String.prototype.pathExtension = function() {
	var lastPathComponent = this.lastPathComponent();
	var index = lastPathComponent.lastIndexOf(".");
	if(index != -1) {
		return lastPathComponent.slice(index,lastPathComponent.length);
	}
	else return "";
}


/* String Extensions End */

/* Array Extensions Begin */

Array.prototype.addObjectsFromArray = function(array) {
	if(array.constructor === Array) {
		this.push.apply(this,array);
	}
	else {
		for(var i=0,item;(item = array[i]);i++) {
			this[this.length] = item;
		}
	}
}

//To let an array behave like a node list
Array.prototype.item = function(index) {
	return this[index];
}
 
/* Array Extensions End */


document._importNode = function(node, allChildren) {
	/* find the node type to import */
	if (node.nodeType === Node.ELEMENT_NODE) {
			/* create a new element */
			var newNode = document.createElement(node.nodeName);
			var i, il;
			/* does the node have any attributes to add? */
			if (node.attributes && node.attributes.length > 0)
				/* add all of the attributes */
				var nodeAttributes = node.attributes;
				var iNodeName, iNodeValue;
				for (i = 0, il = node.attributes.length; i < il;) {
					iNodeName = nodeAttributes[i].nodeName;
					iNodeValue = node.getAttribute(nodeAttributes[i++].nodeName);
					if(iNodeName === "class") {
						//iNodeName = "className";
						newNode.setAttribute("className", iNodeValue);
					}
					newNode.setAttribute(iNodeName, iNodeValue);
				}
			/* are we going after children too, and does the node have any? */
			if (allChildren && node.childNodes && node.childNodes.length > 0){
				/* recursively get all of the child nodes */
				for (i = 0, il = node.childNodes.length; i < il;)
					newNode.appendChild(document._importNode(node.childNodes[i++], allChildren));
			}
			return newNode;
	}
		else if (node.nodeType === Node.TEXT_NODE) {
			return document.createTextNode(node.nodeValue);
		}
		else if(node.nodeType === Node.COMMENT_NODE) {
			return document.createComment(node.nodeValue);
		}
		else if(node.nodeType === Node.CDATA_SECTION_NODE) {
			return document.createCDATASection(node.nodeValue);
		}
		else return null;
};
if(!document.importNode) {
	document.importNode = document._importNode;
}

if(AC.Detector.isIEStrict()) {

	Element.Methods.hasAttribute = function(element, attributeName ) {
		if ( attributeName == "class") attributeName = "className";
		else if(attributeName == "for") attributeName = "htmlFor";
		var result = element.getAttribute(attributeName);
		return ((result != null) && (result !== ""));
		
	};

	document._getElementsByName = document.getElementsByName;
	document._HTMLElementsWithName = ["a","apple","button","form","frame","iframe","img","input","object","map","meta","param","textarea","select"];

	document.getElementsByName = function(name) {
		var _HTMLElementsWithName = this._HTMLElementsWithName;
		var result = [], ieResult, i, iNode;
		for(var e=0, element;(element = _HTMLElementsWithName[e]);e++) {
			ieResult = document.getElementsByTagName(element);
			for(i = 0;(iNode=ieResult[i]); i++) {
				if(iNode.name === name) {
					 result[result.length] = iNode;
				}
			}
		}

		return result;
	}
}

