

var d = document, dE = d.documentElement;
dE.className = 'jsPageLoad'; // Nice CSS hook to hide stuff until JS completes
var PP = new Object();
window.defaultStatus = "dotWeb.com.br - blog";

/* Onload/unload
*******************************************************************************/

PP = {
	init: function() {
		if (navigator.userAgent.indexOf('Opera') != -1) {
			// Opera note: This POS browser RANDOMLY refuses to run JavaScript
			// when the page is served as application/xhtml+xml.
		}
		if (d.getElementById) {
			extraCSS();
			PP.Widgets.StyleSwitcher.init();
			PP.Widgets.PanelToggle.init();
			PP.Widgets.HeaderToggle.init();
			dE.className = 'jsPageComplete';
			PP.Forms.CommentForm.init();
			PP.Forms.ContactForm.init();
			PP.Ajax.NewsLists.init();
			PP.Ajax.LinkLists.init();
		}
	},
	end: function() {
		PP.Widgets.StyleSwitcher.save();
		PP.Widgets.PanelToggle.save();

		// Fix some IE memory leaks. Modified version of
		// http://youngpup.net/2005/0221010713
		if (window.attachEvent) {
			var clearElementProps = [
				'data', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup',
				'ondblclick', 'onclick', 'onselectstart', 'oncontextmenu'
			];
			var el;
			for (var dL = d.all.length; dL--;) {
				el = d.all[dL];
				for (var c = clearElementProps.length; c--;) {
					el[clearElementProps[c]] = null;
				}
			}
		}
	}
};

window.onload = PP.init;
window.onunload = PP.end;

/* Cookies
*******************************************************************************/

PP.Cookies = {
	set: function(name,val,days) {
		var expires = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			expires = "; expires=" + date.toGMTString();
		}
		d.cookie = name + "=" + val + expires + "; path=/";
	},	
	get: function(name) {
		name = name + "=";
		var cs = d.cookie.split("; ");
		for (var i = 0, csL = cs.length; i < csL; i++) {
			var c = cs[i];
			if (c.indexOf(name) == 0) {
				return c.substring(name.length,c.length);
			}
		}
		return null;
	}
};

/* Widgets
*******************************************************************************/

PP.Widgets = {

	// CSS Style Switcher
	StyleSwitcher: {
		init: function() {
			var self = this;
			var ss = d.getElementById("styleswitcher");
			var btn = ss.getElementsByTagName("a");
		
			for (var i = 0, bL = btn.length; i < bL; i++) {
				btn[i].onclick = function() {
					this.blur();
					self.setActive(this.hash.substring(1));
					return false;
				};
			}
		},
		setActive: function(title) {
			var found = 0;
			for (var i = 0, l = d.getElementsByTagName("link"), lL = l.length; i < lL; i++) {
				if (l[i].getAttribute("rel").indexOf("style") != -1 && l[i].getAttribute("title")) {
					l[i].disabled = true;
					if (l[i].getAttribute("title") == title) {
						l[i].disabled = false;
						found = 1;
					}
				}
			}
		
			if (found == 0) {
				title = this.getPreferred();
				if (title) {
					this.setActive(title);
				}
			}
		},
		getActive: function() {
			for (var i = 0, l = d.getElementsByTagName("link"), lL = l.length; i < lL; i++) {
				if (l[i].getAttribute("rel").indexOf("style") != -1
				&& l[i].getAttribute("title") && !l[i].disabled) {
					return l[i].getAttribute("title");
				}
			}
			return null;
		},		
		getPreferred: function() {
			for(var i = 0, l = d.getElementsByTagName("link"), lL = l.length; i < lL; i++) {
				if (l[i].getAttribute("rel").indexOf("style") != -1
				&& l[i].getAttribute("rel").indexOf("alt") == -1
				&& l[i].getAttribute("title")) {
					return l[i].getAttribute("title");
				}
			}
			return null;
		},
		load: function() {
			if (d.getElementById) {
				var cookie = PP.Cookies.get("ater");
				var title = cookie || this.getPreferred();
				this.setActive(title);
			}
		},
		save: function() {
			if (d.getElementById) {
				var title = this.getActive();
				PP.Cookies.set("ater", title, 365);
			}
		}
	},

	// Toggle Side Panels
	PanelToggle: {
		persistState: [
			//'sponsors',
			'styleswitcher',
			'search',
			'archives',
			'tools'
		],
		currentState: [],
		init: function() {
			var self = this;
			var sb = d.getElementById("sidebar");
			var pnl = sb.getElementsByTagName("h2");

			for (var i = 0, pL = this.persistState.length; i < pL; i++) {
				var el = d.getElementById(this.persistState[i]);
				if (el) {
					var handle = el.getElementsByTagName("h2")[0];
					el.className = "panel-visible";
					handle.title = "Tamanho do Painel";
					handle.onclick = function() {
						if (this.parentNode.className == "panel-visible") {
							this.parentNode.className = "panel-hidden";
							self.currentState[this.parentNode.id] = 0;
						} else {
							this.parentNode.className = "panel-visible";
							self.currentState[this.parentNode.id] = 1;
						}
					};

					if (typeof el.onselectstart != "undefined") {
						el.onselectstart = function() {
							return false;
						};
					}
				}
			}
			this.load();
		},
		load: function() {
			if (d.getElementById) {
				var cookie = PP.Cookies.get("aterpanels");
				if ((cookie != null) && (cookie.length > 0)) {
					var pnlArr = cookie.split(",");
					for (var i = 0, pL = this.persistState.length; i < pL; i++) {
						var el = d.getElementById(this.persistState[i]);
						if (el) {
							if (pnlArr[i] == 1) {
								el.className = "panel-visible";
								this.currentState[el.id] = 1;
							} else {
								el.className = "panel-hidden";
								this.currentState[el.id] = 0;
							}
						}
					}
				}
			}
		},
		save: function() {
			if (d.getElementById) {
				var cookie = PP.Cookies.get("aterpanels");
				if ((cookie != null) && (cookie.length > 0)) {
					var oldArr = cookie.split(",");
				}
				var pnlArr = new Array();
				var pnlCounter = 0;
	
				for (var i = 0, pL = this.persistState.length; i < pL; i++) {
					var el = d.getElementById(this.persistState[i]);
					if (this.currentState[this.persistState[i]] != undefined) {
						pnlArr[pnlCounter] = this.currentState[this.persistState[i]];
					} else {
						if (oldArr) {
							pnlArr[pnlCounter] = oldArr[pnlCounter];
						} else {
							pnlArr[pnlCounter] = 1;
						}
					}
					pnlCounter++;
				}
				PP.Cookies.set("aterpanels", pnlArr, 365);
			}
		}
	},

	// Resize Textareas with Animation
	textAreaResizer: {
		running: false,
		timer: null,
		interval: 40,
		tween: 2,
		size: 100,
		minHeight: 40,
		init: function(label, el) {
			if (d.getElementById && d.createElement) {
				var self = this;
				var label = d.getElementById(label);
				var rInc = d.createElement("a");
				var rDec = d.createElement("a");

				rInc.href = "";
				rDec.href = "";
				rInc.title = "Increase the size of the text area";
				rDec.title = "Decrease the size of the text area";	
				rInc.className = "resize-inc";
				rDec.className = "resize-dec";
				rInc.onclick = function() {
					self.resize(el,"inc");
					return false;
				};
				rDec.onclick = function() {
					self.resize(el,"dec");
					return false;
				};

				label.appendChild(d.createTextNode(" | "));
				rInc.appendChild(d.createTextNode("Increase Text Area"));
				label.appendChild(rInc);
		
				label.appendChild(d.createTextNode(" | "));
				rDec.appendChild(d.createTextNode("Decrease Text Area"));
				label.appendChild(rDec);
			}
		},
		resize: function(el, val) {
			var self = this;
			clearInterval(this.timer);
			this.running = false;
			this.timer = null;
			val = (val == "inc") ? this.size : 0-this.size;
			var el = d.getElementById(el);
			var height = el.offsetHeight;
			height = (parseInt(height,10) + parseInt(val,10));
			if (height < this.minHeight) {
				height = this.minHeight;
			}
			this.animate(el.id, height);
			this.focus(el);
		},
		animate: function(elId, newHeight) {
			var h = d.getElementById(elId);
			if (this.running) {
				var c = parseInt(h.offsetHeight,10);
				if (c < newHeight) {
					var fHeight = Math.ceil(c+((newHeight-c)/this.tween));
					if (fHeight < newHeight) {
						h.style.height = fHeight + 'px';
					} else {
						h.style.height = newHeight + 'px';
					}
				} else if (c > newHeight) {
					var fHeight = Math.floor(c-((c-newHeight)/this.tween));
					if (fHeight > newHeight) {
						h.style.height = fHeight + 'px';
					} else {
						h.style.height = newHeight + 'px';
					}
				} else {
					clearInterval(this.timer);
					this.running = false;
					this.timer = null;
					h.focus();
				}
			} else {
				this.running = true;
				this.timer = setInterval("PP.Widgets.textAreaResizer.animate('" + elId + "'," + newHeight + ")",this.interval);
			}
		},
		focus: function(el) {
			if (el.createTextRange) {
				var oRange = el.createTextRange();
				oRange.collapse(false);
				oRange.select();
			} else {
				el.focus();
			}
		}
	},

	// Header Image Toggle with Animation
	HeaderToggle: {
		header: null,
		header2: null,
		tab: null,
		running: false,
		timer: null,
		interval: 50,
		tween: 2,
		init: function() {
			var self = this;
			this.header = d.getElementById("head-banner");
			this.header2 = d.getElementById("access-helper");
			var hParent = d.getElementById("head-section");
			this.tab = d.createElement("span");
			this.tab.id = "htab";
			this.tab.title = "Tamanho do Cabecalho";
			hParent.appendChild(this.tab);
			this.tab.onclick = function() {
				if (!self.header.state) {
					self.header.state = 1;
				}
				if (self.header.state == 190) {
					self.header2.className = "";
				}
				if (self.running) {
					return false;
					self.running = false;
					self.timer = null;
					clearInterval(self.timer);
				}
				self.animate();
			}
		},
		animate: function() {
			if (this.running) {
				var c = this.header.offsetHeight;
				if (this.header.state == 1) {
					var fHeight = c/this.tween;
					if (fHeight < 1) {
						this.header.style.height = 0 + 'px';
						clearInterval(this.timer);
						this.running = false;
						this.timer = null;
						this.header.state = 190;
						this.header2.className = "up";
						this.tab.className = "up";
					} else {
						this.header.style.height = fHeight + 'px';
					}
				} else {
					var fHeight = c*this.tween;
					if (fHeight >= 190) {
						this.header.style.height = 190 + 'px';
						clearInterval(this.timer);
						this.running = false;
						this.timer = null;
						this.header.state = 1;
						this.header2.className = "";
						this.tab.className = "";
					} else {
						this.header.style.height = fHeight + 'px';
					}
				}
			} else {
				if (this.header.offsetHeight == 0) {
					this.header.style.height = 1 + 'px';
				}
				this.running = true;
				this.timer = setInterval("PP.Widgets.HeaderToggle.animate()",this.interval);
			}
		}
	}
};

// Do the styleswitch ASAP.
PP.Widgets.StyleSwitcher.load();

/* Forms
*******************************************************************************/

PP.Forms = {

	// Contact Form
	ContactForm: {
		fName: "contactform",
		init: function() {
			var s = this;
			this.f = d.forms[this.fName];
			if (!this.f) { return; }
	
			this.msg = d.getElementById("sendermessage");
			this.f.onsubmit = function() {
				return s.validate();
			};
			PP.Widgets.textAreaResizer.init("resizer", "sendermessage");
		},
		validate: function() {
			if (PP.Forms.validation.empty(this.msg.value)) {
				this.msg.className = "formerror";
				alert ("Hey buddy... Wouldn't it make more sense to type in a message first?");
				this.msg.focus();
				return false;
			}
		}
	},

	// Comment Form
	CommentForm: {
		fName: "commentform",
		init: function() {
			var self = this;
			this.f = d.forms[this.fName];
			if (!this.f) { return; }
	
			this.comment = d.getElementById("comment");
			this.preview = d.getElementById("previewform");
			this.commentlist = d.getElementById("commentlist");
	
			if (this.preview) {
				this.initPrev();
			} else {
				this.f.style.display = "block";
			}
		
			PP.Widgets.textAreaResizer.init("resizer", "comment");
		
			this.f.onsubmit = function() {
				return self.validate();
			};
		},
		initPrev: function() {
			var self = this;
			this.preview.style.display = "block";
			if (this.commentlist) {
				this.commentlist.className = "mini";
			}
	
			fPreview = this.preview.getElementsByTagName("fieldset")[0];
			fEdit = this.f.getElementsByTagName("fieldset")[0];
	
			this.f.style.display = "none";
			this.togglePreview = d.getElementById("previewcomment");
			this.toggleEdit = d.getElementById("editcomment");
			this.toggleEdit.className = "off";
			this.togglePreview.onclick = function() {
				self.f.style.display = "none";
				self.preview.style.display = "block";
				self.toggleEdit.className = "off";
				this.className = "";
				this.blur();
				return false;					
			};
			this.toggleEdit.onclick = function() {
				self.preview.style.display = "none";
				self.f.style.display = "block";
				self.togglePreview.className = "off";
				this.className = "";
				this.blur();
				return false;
			};
		},
		validate: function() {
			if (PP.Forms.validation.empty(this.comment.value)) {
				this.comment.className = "formerror";
				alert ("Hey buddy... Wouldn't it make more sense to type in a comment first?");
				this.comment.focus();
				return false;
			}
		}
	},

	// Validation
	validation: {
		empty: function(v) {
			var x = /^\s*$/;
			return x.test(v);
		}
	}
};

/* Ajax-ified Parts
*******************************************************************************/

PP.Ajax = {

	LinkLists: {
		ajax: null,
		init: function() {
			if (!d.getElementById("hdlinks")) { return; }
			if (location.search) { return; }
			var self = this;
			this.ajax = XHConn();
			if (this.ajax) {
				var lists = d.getElementById("content").getElementsByTagName("ul");
				for (var i = 0, lL = lists.length; i < lL; i++) {
					lists[i].className = 'treeView';
					var alinks = lists[i].getElementsByTagName("a");
					for (var j = 0, aL = alinks.length; j < aL; j++) {
						alinks[j].onclick = function() {
							if (this.linkLoaded == true) {
								this.parentNode.className = 
									(this.parentNode.className == 'expand')
									? ''
									: 'expand';
							} else {
								self.getLinks(this);
							}
							return false;
						}
					}
				}
			}
		},
		getLinks: function(alink) {
			var self = this;
			var response = function (oXML) {
				var tempDiv = d.createElement("div");
				if (typeof DOMParser == "undefined") {
					tempDiv.innerHTML = oXML.responseText;
				} else {
					tempDiv.loadXML(oXML.responseText);
				}
				var ulParent = alink.parentNode.parentNode;
				var lis = ulParent.childNodes;
				
				for (var i = 0; i < lis.length; i++) {
					if (lis[i].tagName && lis[i].tagName.toLowerCase() == 'li') {
						lis[i].appendChild(tempDiv.getElementsByTagName("ul")[0]);
						lis[i].firstChild.linkLoaded = true;
					}
				}
				alink.parentNode.className = 'expand';
				tempDiv = null;
			};
			alink.parentNode.className = 'loading';
			this.ajax.connect(alink.search.substring(5) + '.php', "GET", alink.search.substring(1) + "&ajax=1", response);
		}
	},

	// Recent News and Quick Phunk lists on news page
	NewsLists: {
		rn: null,
		qp: null,
		currentPageNews: 1,
		currentPageQuick: 1,
		counters: null,
		init: function() {
			var self = this;
			this.rn = d.getElementById("recent-news");
			this.qp = d.getElementById("quick-phunk");
			if (this.qp && this.rn) {
				var ajax = XHConn();
				if (ajax) {
					var outCount = function (oXML) {
						eval('self.counters = ' + oXML.responseText);
						self.addPager(self.rn);
						self.addPager(self.qp);
					};
					ajax.connect("ajax-results.php", "GET", 'func=counter', outCount);
				}
			}
		},
		pageResults: function(pageNum, func) {
			var self = this;
			var outNews = function (oXML) {
				self.response(oXML, 'news');
			};
			var outQuick = function (oXML) {
				self.response(oXML, 'quick');
			};
			if (pageNum < 1) {
				pageNum = 1;
			}	
			if (func == 'news') {
				if (pageNum > this.counters['news']['count']) {
					pageNum = this.counters['news']['count'];
				}
				var ajax1 = XHConn();
				ajax1.connect("ajax-results.php", "GET", 'func='+func+'&pageNum='+pageNum, outNews);
				this.currentPageNews = pageNum;
			} else if (func == 'quick') {
				if (pageNum > this.counters['quick']['count']) {
					pageNum = this.counters['quick']['count'];
				}
				var ajax2 = XHConn();
				ajax2.connect("ajax-results.php", "GET", 'func='+func+'&pageNum='+pageNum, outQuick);
				this.currentPageQuick = pageNum;
			}
		},
		response: function(ajaxObj, parent) {
			//alert(ajaxObj.onreadystatechange);
			//alert(ajaxObj.readyState);
			//alert(ajaxObj.responseText);
			//alert(ajaxObj.responseXML);
			//alert(ajaxObj.status);
			//alert(ajaxObj.statusText);
		
			var newroot;
		
			if (parent == 'news') {
				newroot = this.rn;
				cPage = this.currentPageNews;
				lPage = this.counters['news']['count'];
			}
		
			if (parent == 'quick') {
				newroot = this.qp;
				cPage = this.currentPageQuick;
				lPage = this.counters['quick']['count'];
			}
		
			var pager = cPage + ' of ' + lPage;
		
			var ajaxStr = ajaxObj.responseText;
			var navList = newroot.getElementsByTagName("ul")[0]
			var link1 = navList.getElementsByTagName("li")[3];
			var link2 = navList.getElementsByTagName("li")[4];
			var counter = navList.getElementsByTagName("li")[2];
			var container = newroot.getElementsByTagName("dl")[0];
		
			if (cPage == lPage) {
				link1.getElementsByTagName("a")[0].clickable = false;
				link1.className = "fpPrev off";
			} else {
				link1.getElementsByTagName("a")[0].clickable = true;
				link1.className = "fpPrev";
			}
			if (cPage == 1) {
				link2.getElementsByTagName("a")[0].clickable = false;
				link2.className = "fpNext off";
			} else {
				link2.getElementsByTagName("a")[0].clickable = true;
				link2.className = "fpNext";
			}
		
			counter.className = "fpCounter";
			counter.firstChild.nodeValue = pager;
		
			if (typeof DOMParser == "undefined") {
				container.innerHTML = ajaxStr;
			} else {
				container.loadXML(ajaxStr);
			}
		},
		addPager: function(panel) {
			var self = this;
			var liCounter = d.createElement("li");
			var liNav = panel.getElementsByTagName("ul")[0];
			var liPrev = d.createElement("li");
			var liNext = d.createElement("li");
			var Prev = d.createElement("a");
			var Next = d.createElement("a");
		
			liCounter.className = "fpCounter";
			if (panel == this.rn) {
				var total = this.counters['news']['count'];
			} else {
				var total = this.counters['quick']['count'];
			}
			liCounter.appendChild(d.createTextNode('1 of '+total));
			Prev.href = "";
			Next.href = "";
			Prev.title = "Older";
			Next.title = "Newer";
			liPrev.className = "fpPrev";
			liNext.className = "fpNext off";
			Prev.clickable = true;
			Next.clickable = false;
			Prev.onclick = function() {
				this.blur();
				if (this.clickable) {
					Prev.clickable = false;
					Next.clickable = false;
					liPrev.className = "fpPrev off";
					liNext.className = "fpNext off";
					if (panel == self.rn) {
						self.pageResults(self.currentPageNews+1, 'news', self.rn);
					} else {
						self.pageResults(self.currentPageQuick+1, 'quick', self.qp);
					}
					liCounter.firstChild.nodeValue = 'Loading...';
					liCounter.className = "fpCounterLoading";
				}
				return false;
			};
			Next.onclick = function() {
				this.blur();
				if (this.clickable) {
					Prev.clickable = false;
					Next.clickable = false;
					liPrev.className = "fpPrev off";
					liNext.className = "fpNext off";
					if (panel == self.rn) {
						self.pageResults(self.currentPageNews-1, 'news', self.rn);
					} else {
						self.pageResults(self.currentPageQuick-1, 'quick', self.qp);
					}
					liCounter.firstChild.nodeValue = 'Loading...';
					liCounter.className = "fpCounterLoading";
				}
				return false;
			};
			Prev.appendChild(d.createTextNode("Older"));
			liPrev.appendChild(Prev);
			Next.appendChild(d.createTextNode("Newer"));
			liNext.appendChild(Next);
			liNav.appendChild(liCounter);
			liNav.appendChild(liPrev);
			liNav.appendChild(liNext);
		}
	}
};

/* Utils
*******************************************************************************/

// Load some extra CSS for gecko

function extraCSS() {
	if (d.getElementById && d.createAttribute) {
		var eHead = d.getElementsByTagName("head")[0];
		var eLink = d.createElement("link");

		eLink.type = "text/css";
		eLink.media = "screen, projection";
		eLink.href = "/c/adv-moz.css";
		eLink.rel = "stylesheet";
		eHead.appendChild(eLink);
	}
}

/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/

function XHConn() {
	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { xmlhttp = false; }}}
	if (!xmlhttp) return null;
	this.connect = function(sURL, sMethod, sVars, fnDone) {
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();

		try {
			if (sMethod == "GET") {
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			} else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState == 4 && !bComplete) {
					bComplete = true;
					fnDone(xmlhttp);
				}
			};
			xmlhttp.send(sVars);
		} catch(z) {
			return false;
		}
		return true;
	};
	return this;
}

// modified version of http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
// needed a way to do innerHTML with gecko application/xhtml+xml

if (window.DOMParser) {
	Object.prototype.loadXML = function (s) {
		s = '<div xmlns="http://www.w3.org/1999/xhtml">' + s + '</div>';
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		while (this.hasChildNodes()) this.removeChild(this.lastChild);
		for (var i = 0, dl = doc2.documentElement.childNodes.length; i < dl; i++) {
			this.appendChild(document.importNode(doc2.documentElement.childNodes[i], true));
		}
	};
}


