// JavaScript Document that kickstarts the navigation
var path = null;
window.addEvent('domready', function() {
	var list = $$('#primaryNav li');
	for (x=0; x<list.length; x++) {
		if (list[x].parentNode.parentNode.nodeName=='DIV') {
			var pos = list[x].getPosition('primaryNav');
			pos = pos.x;
			pos = pos - (pos*2); //returns negative value of positive x position!
			var child_list = list[x].getElements('li');
			var last_list = list[x].getElement('li:nth-child(last)');
			if (last_list!=null) {
				last_list.className = 'last-child';
			}

			for (i=0; i<child_list.length; i++) {
				if (child_list[i]!=null) {
					child_list[i].style.backgroundPosition = pos + 'px 0px';
				}
			}
		}
	}
	
	if (Browser.Engine.trident4==true){ //if its ie6 then we need to kickstart the nav!
		for (var i=0; i<list.length; i++) {
			list[i].onmouseover=function() {
				this.className = this.className + 'hover';
			};
			list[i].onmouseout=function() {
				this.className = this.className.replace('hover','');
			};	
		}
	}
	
	if (Browser.Engine.trident==true){ //if we are using ie of any sort then the dam calendar hover wont work with :hover
		var cal = $$('div#calendar div.day');
		for (x=0; x<cal.length; x++) {
			cal[x].onmouseover=function() {
				this.className = this.className + '-hover';
				//this.style.left = 'auto';
				//alert(this.style.left);
			};
			cal[x].onmouseout=function() {
				this.className = this.className.replace('-hover','');
				this.style.left = '-10000px';
			};	
		}
	}
	
	
	path = window.location.href.replace('index.asp','').toLowerCase();
	SetupSubNav();
});

function SetupSubNav() {
	/* Following Section Handles Sub Navigation for inside pages! */
	var subnav = $$('#Column1 #Layer1 ul li'); // if we are on an inside page with a sub nav then we want to set the active tab by setting a classname on it!
	if (subnav!=null) {
		$$('#pageContentWrapper').setStyle('position','relative');
		for (x=0; x<subnav.length; x++) {
			var a = $(subnav[x].firstChild);
			if (a.nodeName=='A') {
				//alert(a.href.toLowerCase() + ' ' + path);
				if (a.href.replace('index.asp','').toLowerCase()==path) { //if url path and link href match its the active page and so put a class on the A tag!
					a.className = 'active';	
				}

				a.addEvent('click', function(event) { //provides ajax call back for the sub nav on inside pages! 			
					event.stop(); //Stops the page load when clicking on the link
					var target = this.href; // contains the url target for the ajax call
					var req = new Request({   
						url: this, 
						autoCancel: true,
						onRequest: function() {
							var pageContent = $('pageContent').getCoordinates('pageContentWrapper');
							var waitingWrapper = new Element('div', {
								'styles': {
									'height' : pageContent.height + 'px',
									'width' : pageContent.width + 'px'
								},
								'id': 'waitingWrapper'
							});
							
							var waiting = new Element('div', {
								'id':'waiting'
							});
							waiting.inject(waitingWrapper,'top');
							waitingWrapper.inject($('pageContentWrapper'), 'top');

						},   
						onSuccess: function(html) { 
							
							var match = html.match(/<style[^>]*>([\s\S]*?)<\/style>/i); 
							var styletext = (match)?match[1]:html; //returns the contents of the style tag from the document source
							
							var match = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i); //returns the contents of the body tag from the document source
							var html = (match) ? match[1] : html;
							
							html= html.replace('id="pageContent"', 'id="ajaxresult"'); //replaces pageContent id with ajaxresult so we can get the element by id later on i
							var root = new Element('div', {
								'styles': {
									'display': 'block',
									'position': 'absolute',
									'top': '-100000px',
									'left': '-100000px'
								}, //removes the div from display
								'id':'ajaxroot', //assigns it a id ref
								'html': html //inserts the innerHTML of the temp wrapper div
							}); //creates a new wrapper that houses the resultant body from the ajax call
							
							root.inject(document.body,'bottom'); //Places wrapper at the end of the body so that we can get the pagecontent div using the DOM
							var content = $$('#ajaxresult').get('html'); //returns the new page content!
							root.destroy(); //removes the ajaxroot element from the DOM
							$$('#pageContent').set('html',content); //updates page contents
							
							var style = $(document.head).getElement('style');
							if(style.styleSheet){ // if IE then we need to set the style in a stupid microsoft manner
								style.styleSheet.cssText = styletext;
							} else { // Else lets do it a nice simple way!
								style.innerHTML = styletext;
							}
							path = target.replace('index.asp','').toLowerCase(); //reassign the path variable so that the active tab is correct when SetupSubNav() is re-run!
							Slimbox.scanPage(); //setup the lightbox again if its needed! 
							$$('#waitingWrapper').destroy();
							SetupSubNav();							
						}, // ends onSuccess function
						onFailure: function(error) {
							if (error.status>=400 && error.status<=499) {
								window.location = target;
							} else if (error.status>=500 && error.status<=599) {
								alert('There was a problem loading this page, please try again later');
								$$('#waitingWrapper').destroy();
							}
						}
						
					}).send(); // ends ajax request
					
				}); // ends onclick of the links 
			}
		}
		
		if ($('parents')!=null) {
			var right = $('parents').getSize();
			$('students').setStyle('height', (right.y-22));
		}
	}
};