/* My Portfolio Scripts */


/*
The Personalization Tabs: Personalizer */
function personalizer(target) {
	typeof target == "object" ? this.element = target : this.element = document.getElementById(target);
	if (!this.element) return false;
	this.ul = this.element.getElementsByTagName("ul")[0];
	this.tabs = this.ul.getElementsByTagName("li");
	this.tabContent = this.getTabContent();
	this.bind();
}

personalizer.prototype.getTabContent = function() {
	tabContent= new Array();
	this.divs = this.element.getElementsByTagName("div");
	for(var i = 0; i < this.divs.length; i++) {
		if (/thetab/i.test(this.divs[i].className)) {
			tabContent.push(this.divs[i]);
		}
	}
	return tabContent;
}

personalizer.prototype.bind = function() {
	var o = this;
	for(var i = 0; i < this.tabs.length; i++) {
		this.tabs[i].onclick = function() {
			if (this.className != 'on') {
				o.show(this); return false;
				var a = this.getElementsByTagName("a")[0];
				if (a) a.onclick = function() {
					return false;
				}
			}
			else {
				return false;
			}
		}
	}
}

personalizer.prototype.show = function(caller) {
	for(var i = 0; i < this.tabs.length; i++) {
		var tab = this.tabs[i];
		if (tab == caller) {
			this.hide();
			tab.className = "on"
			this.tabContent[i].className = 'thetab tabopen';
			
			/* turn off click event for an open tab */
			var a = tab.getElementsByTagName("a")[0];
			if (a) a.onclick = function() {
				return false;
			}
			
			//document.getElementById("opentab").value = i+1;
			createCookie('opentab', i+1);
		}
	}
}

personalizer.prototype.hide = function() {
	for(var i = 0; i < this.tabs.length; i++) {
		this.tabs[i].className = "off";
		this.tabContent[i].className = 'thetab tabclose';
	}
}



/*
The My Portfolio Sections */
function sections(target) {
	typeof target == "object" ? this.element = target : this.element = document.getElementById(target);
	if(!this.element) return false;
	
	this.ul = this.element.getElementsByTagName("ul")[0];
	this.sections = this.getSections();
	this.sectionContent = this.getSectionContent();
	this.bind();
}

sections.prototype.getSections = function() {
	sections = [];
	var i = (this.ul && this.ul.childNodes) ? this.ul.childNodes.length : 0;
	while (i--) {
		var node = this.ul.childNodes[i];
		if( node.nodeType == 1 && node.nodeName.toLowerCase() == "li") {
			sections.push(this.ul.childNodes[i]);
		}
	}
	return sections.reverse();
}

sections.prototype.getSectionContent = function () {
	sectionContent = new Array();
	this.divs = this.element.getElementsByTagName("div");
	for(var i=0; i<this.divs.length; i++) {
		if(/body/i.test(this.divs[i].className)) {
			sectionContent.push(this.divs[i]);
		}
	}
	return sectionContent;
}

sections.prototype.bind = function() {
	var o = this;

	for(var i=0; i<sections.length;i++) {
		var sLnks = sections[i].getElementsByTagName("a");
		var sectId = sections[i].id;
		
		for(var j=0;j<sLnks.length;j++) {
			var sLnk = sLnks[j];
			
			sLnk.onclick = function() {
				var lnkParent = this.parentNode.parentNode;
				if(this.className == "group") {
				
					if(lnkParent.className == "open") {
						o.hide(lnkParent);
					}
					else {
						o.show(lnkParent);
					}
					
					return false;
				}
			}
		}
	}
}


sections.prototype.show = function(caller) {
	for(var i=0; i<sections.length; i++) {
		var section = sections[i];
		if(section == caller) {
			this.hide();
			section.className = "open";
			
			var sId = section.id;
			//document.getElementById("opensection").value = sId;
			createCookie('opensection', sId);
		}
	}
}

sections.prototype.hide = function() {
	for(var i=0; i<this.sections.length; i++) {
		this.sections[i].className="closed";
		
		//document.getElementById("opensection").value = "none";
		createCookie('opensection', '');
	}
}


function hoverButtons() {
	if(!document.getElementById("mync")) return;
	btns = document.getElementById("mync").getElementsByTagName("button");
	for(var i=0;i<btns.length;i++) {
		btns[i].onmouseover = function() {
			this.className = "hover";
		}
		btns[i].onmouseout = function() {
			this.className = "";
		}
	}
}


function initPersonalize() {
	new personalizer("personalized");
	new sections("myp");
	hoverButtons();
}

YAHOO.util.Event.addListener(window, "load", initPersonalize);