/*
	* Sugar Arrays (c) Creative Innects 2006
	* http://creativecommons.org/licenses/by-sa/2.5/
	* Author: Dustin Diaz | http://www.dustindiaz.com
	* Reference: http://www.dustindiaz.com/basement/sugar-arrays.html
*/

if(!Array.prototype.forEach) {
	Array.prototype.forEach = function (callback, thisObj) {
		var scope = thisObj || window;
		for(var i = 0, ii = this.length; i < ii; ++i) callback.call(scope, this[i], i, this);
	}
	Array.prototype.every = function (callback, thisObj) {
		var scope = thisObj || window;
		for(var i = 0, ii = this.length; i < ii; ++i) if(!callback.call(scope, this[i], i, this)) return false;
		return true;
	}
	Array.prototype.some = function (callback, thisObj) {
		var scope = thisObj || window;
		for(var i = 0, ii = this.length; i < ii; ++i) if(callback.call(scope, this[i], i, this)) return true;
		return false;
	}
	Array.prototype.map = function (callback, thisObj) {
		var scope = thisObj || window;
		var a = [];
		for(var i = 0, ii = this.length; i < ii; ++i) a.push(callback.call(scope, this[i], i, this));
		return a;
	}
	Array.prototype.filter = function (callback, thisObj) {
		var scope = thisObj || window;
		var a = [];
		for(var i = 0, ii = this.length; i < ii; ++i) {
			if(!callback.call(scope, this[i], i, this)) continue;
			a.push(this[i]);
		}
		return a;
	}
	Array.prototype.indexOf = function (el, start) {
		start = start || 0;
		for(var i = start, ii = this.length; i < ii; ++i) if(this[i] === el) return i;
		return -1;
	}
	Array.prototype.lastIndexOf = function (el, start) {
		start = start || this.length;
		if(start >= this.length) start = this.length;
		if(start < 0) start = this.length + start;
		for(var i = start; i >= 0; --i) if(this[i] === el) return i;
		return -1;
	}
	Array.forEach = function (object, callback, thisObj) {
		Array.prototype.forEach.call(object, callback, thisObj);
	}
	Array.map = function (object, callback, thisObj) {
		return Array.prototype.map.call(object, callback, thisObj);
	}
	Array.filter = function (object, callback, thisObj) {
		return Array.prototype.filter.call(object, callback, thisObj);
	}
	Array.indexOf = function (object, el, start) {
		return Array.prototype.indexOf.call(object, el, start);
	}
}

if(typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentElement) {
	var EmptyTags = {
		"IMG": true,
		"BR": true,
		"INPUT": true,
		"META": true,
		"LINK": true,
		"PARAM": true,
		"HR": true
	}

	HTMLElement.prototype.insertAdjacentElement = function (where, parsedNode) {
		switch (where) {
			case "beforeBegin": this.parentNode.insertBefore(parsedNode, this); break;
			case "afterBegin": this.insertBefore(parsedNode, this.firstChild); break;
			case "beforeEnd": this.appendChild(parsedNode); break;
			case "afterEnd":
				if(this.nextSibling) this.parentNode.insertBefore(parsedNode, this.nextSibling);
				else this.parentNode.appendChild(parsedNode);
				break;
		}
	}
	HTMLElement.prototype.insertAdjacentHTML = function (where, str) {
		var range = this.ownerDocument.createRange();
		range.setStartBefore(this);
		var parsedHTML = range.createContextualFragment(str);
		this.insertAdjacentElement(where, parsedHTML);
	}
	HTMLElement.prototype.insertAdjacentText = function (where, str) {
		var parsedText = document.addTextNode(str);
		this.insertAdjacentElement(where, parsedText);
	}
	HTMLElement.prototype.__defineSetter__("innerText", function (str) {
		this.textContent = str;
	});
	HTMLElement.prototype.__defineGetter__("innerText", function () {
		return this.textContent;
	});

	HTMLElement.prototype.__defineGetter__("outerHTML", function () {
		var tagName = this.tagName.toLowerCase();
		var str = "<" + tagName;
		Array.forEach(this.attributes, function (el) {
			str += " " + el.name + "=\"" + el.value + "\"";
		});
		if(EmptyTags[tagName]) return str + ">";
		return str + ">" + this.innerHTML + "</" + tagName + ">";
	});
	HTMLElement.prototype.__defineSetter__("outerHTML", function (str) {
		var range = this.ownerDocument.createRange();
		range.setStartBefore(this);
		var parsedHTML = range.createContextualFragment(str);
		this.parentNode.replaceChild(parsedHTML, this);
	});
}

var Innect = {
	$: function (id) {
		return document.getElementById(id);
	},
	$$: function (name) {
		return document.getElementsByName(name);
	},
	Class: {
		Add: function (el, className) {
			el.className += ((el.className == "")?"":" ") + className;
		},
		Exist: function (el, className) {
			return (el.className.split(" ").indexOf(className) != -1);
		},
		Get: function (el) {
			return el.className;
		},
		Remove: function (el, className) {
			el.className = el.className.split(" ").filter(function (str) {
				return (str != className);
			}).join(" ");
		},
		Set: function (el, className) {
			el.className = className;
		}
	},
	HTMLEncode: function (str) {
		return str.toString().replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
	},
	JSEncode: function (str) {
		var characters = ["Æ", "æ", "Ø", "ø", "Å", "å"];
		var unicodeEntities = ["\u00c6", "\u00e6", "\u00d8", "\u00f8", "\u00c5", "\u00e5"];
		return str.split(/\b|\B/).map(function (el) {
			var i = characters.indexOf(el);
			return (i != -1)?unicodeEntities[i]:el;
		}).join("");
	},
	QueryString: function (url) {
		var searchLocation = url.indexOf("?");
		if(searchLocation == -1) return [];
		return url.slice(searchLocation + 1).split("&").map(function (el) {
			el = el.split("=");
			return [el[0], el[1]];
		});
	},
	Style: {
		Get: function (el, cssAttribute) {
			if(el.currentStyle) {
				if(cssAttribute == "opacity") {
					var filter = el.currentStyle.filter;
					return Number(String(filter).slice(filter.indexOf("=") + 1, filter.length - 1)) / 100;
				}
				else return el.currentStyle[cssAttribute];
			}
			else return document.defaultView.getComputedStyle(el, null)[cssAttribute];
		},
		Set: function (el, cssAttribute, cssValue) {
			if(cssAttribute == "opacity" && String(el.style.opacity) == "undefined") el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + cssValue * 100 + ")";
			else el.style[cssAttribute] = cssValue;
		}
	}
}

String.prototype.convertLineBreaks = new Function("return this.replace(/\\n|\\r\\n/g, '<br>');");

String.prototype.setLeadingZeros = function (len) {
	len = len - this.length;
	var zeros = "";
	var i = 0;
	while(i < len) {
		zeros += "0";
		i++;
	}
	return (zeros += this);
}

String.prototype.toBoolean = function () {
	return String(this).match(/true|True|1|-1/);
}

String.prototype.toBooleanSymbol = function () {
	return (this.toBoolean())?"x":"";
}

/*
	* Title Caps
	* Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008
	* Original by John Gruber - http://daringfireball.net/ - 10 May 2008
	* License: http://www.opensource.org/licenses/mit-license.php
*/

String.prototype.toTitleCase = function () {
	var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
	var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
	var title = this;
	var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;	
	while(true) {
		var m = split.exec(title);
		parts.push( title.substring(index, m ? m.index : title.length)
			.replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all) {
				return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
			})
			.replace(RegExp("\\b" + small + "\\b", "ig"), lower)
			.replace(RegExp("^" + punct + small + "\\b", "ig"), function (all, punct, word) {
				return punct + upper(word);
			})
			.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));		
		index = split.lastIndex;		
		if(m) parts.push(m[0]);
		else break;
	}
	return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
		.replace(/(['Õ])S\b/ig, "$1s")
		.replace(/\b(AT&T|Q&A)\b/ig, function(all) {
			return all.toUpperCase();
		});
    
	function lower(word) {
		return word.toLowerCase();
	}
    
	function upper(word) {
		return word.substr(0,1).toUpperCase() + word.substr(1);
	}
}

String.prototype.trim = new Function("return this.replace(/\\s+$|^\\s*/gi,'');");

String.prototype.truncate = function (len) {
	return (this.length > len)?this.substr(0, len) + "...":this;
}