/* 
  ================================================
  Weston McNamee Link Script
  Copyright (c) 2006 Weston McNamee Development
  www.westonmcnamee.com
  Version: 2.0.0
  Dependencies: MooTools Framework
  ================================================
*/
// Javascript's Equivalent of PHP's $_GET
/*
function jsGet(type){
if(location.href.match(type)){
return location.href.split(type+'=')[1].split('&')[0];
}}
*/

var navWidget = new Class({
	options: {
		tag: 'a',
		hoverClass: 'nav_current',
		relation: 'parent'	
	},
	initialize: function(element, options){
		this.setOptions(options)
		this.element = element.toLowerCase(); // the nav container (not ul)
		this.h = document.location.href; // the url
		this.setOptions(options); // options
		this.setCurrent(); // the beef of all that is navWidget...
	},
	setCurrent: function(){
		//alert(this.options.tag + "\n" + this.options.hoverClass + "\n" + this.element);
		if(document.getElementById){
			this.ob =(this.element)?document.getElementById(this.element):document; //the navigation container
			if(this.ob){
				url_parts = this.h.split("/"); //explode the url by "/"
				page = url_parts[3]; //node directly after domain
				query_start = page.search(/\?+/); //search for a "?" which will indicate a query
				if(query_start != -1){ //if there is a query, remove everything after the "?"
					page = page.slice(0,query_start);
				}
				page = page.toLowerCase(); //this allows us to do case insensitive comparisons later
				//alert("the current page I'm on is: \"" + page + "\"");
				tA=this.ob.getElementsByTagName('A'); //get all the anchors in the nav bar
				if(page == "" || page == "topics" || page == "entries"){//we are on the home page, set tA[0] to the hoverClass
					this.changeClass(tA[0]);
				}else{
					for(i=0;i<tA.length;i++){ // loop through each A tag in the nav bar
						var aHref = tA[i].href; //get the href attr of the A tag
						//alert (aHref);
						hrefPageParts = tA[i].href.split("/"); //explode the href by "/"
						hrefPage = hrefPageParts[3]; //node directly after domain
						hrefPage = hrefPage.toLowerCase(); //this allows us to do case insensitive comparisons later
						//alert(hrefPage);
						if(page == hrefPage){ //compare the page we are on to the Href of the Anchor
							//alert("we found a match, and that is:" + "\n" + "viewing: " + page + "\n" + "href: " + hrefPage);
							this.changeClass(tA[i]);
							//alert(oTag.className);
							break;
						}
					}	
				}
			}else{
				alert("unable to find the " + this.element + " element!");
			}
		}else{
			alert("the browser does not support getElementById!");
		}
	},
	changeClass: function(theAnchor){
		tag = this.options.tag.toLowerCase(); 
		//alert("tag: " + tag);
		var oTag; //initialize the element object
		if(tag != "a"){ //if we aren't modifying the "a" element, do we search inside or outside?
			switch (this.options.relation){
				case "parent":
					oTag = theAnchor.parentNode;
					while(oTag.nodeName != tag){ //loop through the parent elements till we get to the one we want
						oTag=oTag.parentNode; 
					}								
				break;
				
				case "child":
					for(var j = 0; j < theAnchor.childNodes.length; j++) {
						if (theAnchor.childNodes[j].nodeName == tag){
							var oTag = theAnchor.childNodes[j];
							break;
						}
					}
				break;
				/*
				default:
					alert("incorrect relation specified: " + this.options.relation + "\n"
							+ "please choose either \"parent\" or \"child\".");
				break;
				*/
			}
		}else{
			oTag = theAnchor; //this is if the "A" element IS what we want to modify...
		}
		//alert(oTag.className);
		var oldClass = oTag.className; //get the current class
		if(oldClass != ""){ //if there IS a class of that element, append the hoverClass
			oTag.className = oldClass + " " + this.options.hoverClass;	
		}else{ //otherwise, element class = hoverClass
			oTag.className = this.options.hoverClass;
		}			
	}
	/*
	displayGallery: function(){
		var url_parts = this.h.split("/"); //explode the url by "/"
		var page = url_parts[3]; //node directly after domain
		var page = page.toLowerCase(); //this allows us to do case insensitive comparisons later
		if(page == ""){
				var myGallery = new gallery($('myGallery'), {
					timed: true,
					showInfopane: false,
					showArrows: false,
					showCarousel: false,
					embedLinks: true,
					delay: 2500
				});
		}else if (page == "gallery"){
			var myGallery = new gallery($('myGallery2'), {
				timed: false,
				showInfopane: false,
				showArrows: true,
				showCarousel: true,
				embedLinks: false
			});
		}
	}
	*/		
});
navWidget.implement(new Options, new Events);