var p = {
	
	util : {

		configEvents : function() {
		     if (document.addEventListener) {
		          this.addEvent = function(el, type, func, capture) {
		            el.addEventListener(type, func, capture);  
		          };
		          this.stopBubble = function(evt) { evt.stopPropagation(); };
		          this.stopDefault = function(evt) { evt.preventDefault(); };
		          this.findTarget = function(evt, targetNode, container) {
		            var currentNode = evt.target;
		            while (currentNode && currentNode !== container) {
		              if (currentNode.nodeName.toLowerCase() === targetNode) {
		                  return currentNode; break;
		              }
		              else { currentNode = currentNode.parentNode; }
		            };
		            return false;
		          };
		      }
		      else if (document.attachEvent) {
		          this.addEvent = function(el, type, func) {
		            el["e" + type + func] = func;
		            el[type + func] = function() { el["e" + type + func] (window.event); };
		            el.attachEvent("on" + type, el[type + func]);
		          };
		          this.stopBubble = function(evt) { evt.cancelBubble = true; };
		          this.stopDefault = function(evt) { evt.returnValue = false; };
		          this.findTarget = function(evt, targetNode, container) {
		            var currentNode = evt.srcElement;
		            while (currentNode && currentNode !== container) {
		              if (currentNode.nodeName.toLowerCase() === targetNode) {
		                  return currentNode; break;
		              }
		              else { currentNode = currentNode.parentNode; }
		            };
		            return false;
		          };
			}
			if (document.addEventListener) {
				this.removeEvent = function(el, type, func, capture) {
					el.removeEventListener(type, func, capture);
				};
			}
			else if (document.attachEvent) {
				this.removeEvent = function(el, type, func) {
					el["e" + type + func] = null;
					el[type + func] = null;
					el.detachEvent("on" + type, el[type + func]);
				};
			}
		},
    
		createCookie : function(name,value,expiration,path,domain,secure) {
		      var data = name + "=" + escape(value);
		      if (expiration) { 
		          var expiresAt = new Date();
		          expiresAt.setTime(expiration);
		          data += "; expires=" + expiresAt.toGMTString();
		      }
		      if (path) { data += "; path=" + path; }
		      if (domain) { data += "; domain=" + domain; }
		      if (secure) { data += "; secure"; }
		      document.cookie = data;
		},    

		findCookie : function(name) {  
		      var query = name + "=";
		      var queryLength = query.length;
		      var cookieLength = document.cookie.length;
		      var i=0;
		      while (i<cookieLength) {
		        var position = i + queryLength;
		        if (document.cookie.substring(i,position) === query) {
		           return this.findCookieValue(position);
		        }
		        i = document.cookie.indexOf(" ", i) + 1;
		        if (i == 0) { break; }  
		      }
		      return null;  
		},

		findCookieValue : function(position) {
		      var endsAt = document.cookie.indexOf(";", position);
		      if (endsAt == -1) { endsAt = document.cookie.length; }
		      return unescape(document.cookie.substring(position,endsAt));
		},
 
		eraseCookie : function(name) {
			if(this.findCookie(name)) {
				var data = name + "=";
				data += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
				document.cookie = data;
			}
		},
		
		sendRequest : function(url, func, postData) {
			var xhr = this.createXHR();
			if (!xhr) { return; }
			var method = (postData) ? "POST" : "GET";
			xhr.open(method, url, true);
			xhr.setRequestHeader('User-Agent','XHR');
			if (postData) {
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			}
			xhr.onreadystatechange = function() {
				if (xhr.readyState !== 4) { return; }
				if (xhr.status !== 200 && xhr.status !== 304) {
					alert('HTTP error ' + xhr.status);
					return;
				}
			func(xhr);
			};
			if (xhr.readyState === 4) { return; }
				xhr.send(postData);
		},

		XHRoptions : [
			function () {return new XMLHttpRequest()},
			function () {return new ActiveXObject("Msxml2.XMLHTTP")},
			function () {return new ActiveXObject("Msxml3.XMLHTTP")},
			function () {return new ActiveXObject("Microsoft.XMLHTTP")}
		],
		XHRmethod : null,

		configXHR : function() {
			var xmlhttp = false;
			for (var i=0, allOptions=this.XHRoptions.length; i<allOptions; i++) {
			try { xmlhttp = this.XHRoptions[i](); }
				catch (e) { continue; }
				break; // stop loop once we have a successful method
			}
			this.XHRmethod = i;
			this.createXHR = function() {
			    var xmlhttp = this.XHRoptions[this.XHRmethod]();
			    return xmlhttp;
			}
		}

		
	},//end of util
		    
	init : function() {
		
		//object detection
		if(!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.createTextNode) {return;}
		this.util.configEvents();  
		
		//this.create_back_to_top();
		
		//p.controlNavs();
		
		//open external links in a new tab 
		this.theBody = document.getElementsByTagName('body')[0];
		this.util.addEvent(this.theBody,'click',this.openNewTab,false);
		this.globalNav = document.getElementById('globalNav');
		
		//show or hide local nav when mousing on or off
		/*this.util.addEvent(this.globalNav,'mouseover',this.showLocalNav,false);
		this.util.addEvent(this.globalNav,'mouseout',this.hideLocalNav,false);*/
		
		//add bullets on resume page
		/* this.resumeContent = document.getElementById('resumeContent');
		if(this.resumeContent) {
			this.workExp = document.getElementById('workExp');
			this.jobs = this.workExp.getElementsByTagName('div');
			
			this.addColExpBullets();
			this.util.addEvent(this.resumeContent,'click',this.expColDetails,false);
		}*/
	},
	
	/* showLocalNav : function(evt) {
		var theLink = p.util.findTarget(evt,'li',this);
		if(!theLink) {return;}
		
		var sub_nav = theLink.getElementsByTagName('div')[0];
		if(sub_nav) {			
			sub_nav.className = "showing";
			//keep globalNav link styled for hovering
		}
	},
	
	hideLocalNav : function(evt) {
		var theLink = p.util.findTarget(evt,'li',this);
		if(!theLink) {return;}
		
		var sub_nav = theLink.getElementsByTagName('div')[0];
		if(sub_nav) {			
			sub_nav.className = "";
		}
		
		
		//alert('localOff');
	
	},*/

	/* controlNavs : function() {
	
		//determine current page via path name
		var currentPath = location.pathname;			
		var startName = currentPath.lastIndexOf("/") + 1;
		var thePage = currentPath.substring(startName);
		var pageStart = thePage.substring(0,7);
		
		//pinpoint the globalNav ul 
		var globalNav = document.getElementById('globalNav');
		
		//create arrays for nav links and labels
		navLabels = ['Home','Portfolio','Design Process','About Me'];
		navLinks = ['index','portfolio','process','about'];
		
		//create top nav		
		for(var n=0, allLinks=navLinks.length; n<allLinks; n++) {
			var gNavLi = document.createElement('li');
			//if the corresponding link does not match the current page create a link
			if(navLinks[n] + '.html' !== thePage) {
				var gNavA = document.createElement('a');
				gNavA.href = navLinks[n] + '.html';
				gNavA.appendChild(document.createTextNode(navLabels[n]));
				gNavLi.appendChild(gNavA);
			}
			//if the label does match create the current page label
			else {
				var gNavSpan = document.createElement('span');
				gNavSpan.appendChild(document.createTextNode(navLabels[n]));
				gNavLi.appendChild(gNavSpan);
				gNavLi.className = 'current';
			}	
			globalNav.appendChild(gNavLi);
		}
		
		//if there is no thePage var, then this is the home page
		if(!thePage) {
			var homeLink = globalNav.getElementsByTagName('li')[0];
			var theA = homeLink.getElementsByTagName('a')[0];
			homeLink.removeChild(theA);
			var newSpan = document.createElement('span');
			newSpan.appendChild(document.createTextNode('Home'));
			homeLink.appendChild(newSpan);
		}
		
		
		
		//unlink current page on bottom nav
		var theFooter = document.getElementById('footer');
		var fNav = theFooter.getElementsByTagName('ul')[0];
		var fNavLis = fNav.getElementsByTagName('li');
		//identify the link that bottom link that matches the current page
		for(var l=0,allFLis=fNavLis.length; l<allFLis; l++) {
			var fA = fNavLis[l].getElementsByTagName('a')[0];
			if(fA) {
				var fHref = fA.href;
				var position = fHref.lastIndexOf("/") + 1;
				var fPage = fHref.substring(position);
				if(fPage === thePage) {
					var theLabel = fA.firstChild.nodeValue;
					//var itsLi = fNavLis[l].parentNode;
					fNavLis[l].removeChild(fA);
					var newSpan = document.createElement('span');
					newSpan.appendChild(document.createTextNode(theLabel));
					fNavLis[l].appendChild(newSpan);
				}
			}
		}
		
	}, */
		
	openNewTab : function(evt) {
		var theLink = p.util.findTarget(evt,'a',this);
		if(!theLink) {return;}
		
		var itsHref = theLink.href;
					
		if(theLink.className === 'exLink') {
			p.util.stopDefault(evt);
			window.open(theLink,'_blank');
		}
	},
	
	addColExpBullets : function() {
		for(var j=0, allJobs=p.jobs.length; j<allJobs; j++) {
			//add collapsed bullet to each job
			var theP = p.jobs[j].getElementsByTagName('p')[0];
			if(theP.className === 'date') {
				var bullet = document.createElement('img');
				bullet.src = 'images/collapsedBullet.gif';
				bullet.title = 'Click to view details about this position.';
				bullet.className = 'collapsed';
				p.jobs[j].insertBefore(bullet, theP);
			}
			//hide details about each job
			var theDetails = p.jobs[j].getElementsByTagName('ul')[0];
			theDetails.className = 'hidden';
		}
	
	},
	
	create_back_to_top : function() {
		var the_divs = document.getElementsByTagName('div');
		var all_divs = the_divs.length;
		for(var d=0; d<all_divs; d++) {
			if(the_divs[d].className === 'port_cat') {
				var back_p = document.createElement('p');
				var back_link = document.createElement('a');
				back_link.href = '#header';
				back_link.appendChild(document.createTextNode('Back to Top'));
				back_p.appendChild(back_link);
				back_p.className = "back_top";
				the_divs[d].appendChild(back_p);
			}
		}
	},
	
	
	expColDetails : function(evt) {
		var theBullet = p.util.findTarget(evt,'img',this);
		if(!theBullet) {return;}
		
		var theJob = theBullet.parentNode;
		var theDetails = theJob.getElementsByTagName('ul')[0];
		
		if(theBullet.className === 'collapsed') {
			theBullet.className = 'expanded';
			theBullet.src = 'images/expandedBullet.gif';
			theBullet.title = 'Click to hide details about this position.';
			theDetails.className = '';
		}
		else {
			theBullet.className = 'collapsed';
			theBullet.src = 'images/collapsedBullet.gif';
			theBullet.title = 'Click to view details about this position.';
			theDetails.className = 'hidden';
		}
	}
	
	
		
	
}; //end of 

p.init();


