// JavaScript Document

var pH = {

	addListeners: function () {
		if (!document.getElementsByTagName || !document.getElementById)
			return;		

		pH.moveContent();

		// get the panel divs
		pH.objPANEL_Projects = document.getElementById('rwProjects');
		pH.objPANEL_Marathon = document.getElementById('rwMarathon');
		pH.objPANEL_AboutUs = document.getElementById('rwAboutUs');
		pH.objPANEL_Contact = document.getElementById('rwContact');
		
		// set listeners for panel depth
		rwScript.addEvent(pH.objPANEL_Projects, 'click', pH.getClickForDepth(pH.objPANEL_Projects), false);
		rwScript.addEvent(pH.objPANEL_Marathon, 'click', pH.getClickForDepth(pH.objPANEL_Marathon), false);
		rwScript.addEvent(pH.objPANEL_AboutUs, 'click', pH.getClickForDepth(pH.objPANEL_AboutUs), false);
		rwScript.addEvent(pH.objPANEL_Contact, 'click', pH.getClickForDepth(pH.objPANEL_Contact), false);
		
		// move panels to starting positions
		pH.objPANEL_Projects.style.left = pH.panelPosition[pH.intPANEL_Projects] + 'px';		
		pH.objPANEL_Marathon.style.top = pH.panelPosition[pH.intPANEL_Marathon] + 'px';
		pH.objPANEL_AboutUs.style.right = pH.panelPosition[pH.intPANEL_AboutUs] + 'px';
		pH.objPANEL_Contact.style.bottom = pH.panelPosition[pH.intPANEL_Contact] + 'px';

		// set animation states
		pH.objPANEL_Projects.animState = 'none';
		pH.objPANEL_Marathon.animState = 'none';
		pH.objPANEL_AboutUs.animState = 'none';
		pH.objPANEL_Contact.animState = 'none';

		// set interval
		setInterval(pH.movePanels, 10);

		//set listeners for panel slide
		var btnProjects = document.getElementById('btnProjects');
		var btnMarathon = document.getElementById('btnMarathon');
		var btnAboutUs = document.getElementById('btnAboutUs');
		var btnContact = document.getElementById('btnContact');
		rwScript.addEvent(btnProjects, 'click', pH.getClickForSlide(btnProjects), false);
		rwScript.addEvent(btnMarathon, 'click', pH.getClickForSlide(btnMarathon), false);
		rwScript.addEvent(btnAboutUs, 'click', pH.getClickForSlide(btnAboutUs), false);
		rwScript.addEvent(btnContact, 'click', pH.getClickForSlide(btnContact), false);
		
		// set listener for window resize
		rwScript.addEvent(window, 'resize', pH.moveContent, false);
		
		// set listener for home rollover
		var objHome = document.getElementById('imgLogo');
		rwScript.addEvent(objHome, 'mouseover', pH.getHomeMOver(objHome), false);
		rwScript.addEvent(objHome, 'mouseout', pH.getHomeMOut(objHome), false);
		objHome.style.right = '25px';		
		
		// fix up display problems in IE
		var isIE = !window.opera && navigator.userAgent.indexOf('MSIE') != -1;
		if (isIE) {
			var objContactMap = document.getElementById('contactMap');
			objContactMap.style.margin = '0px 0px 10px 5px';
			btnAboutUs.style.height = '220px';
		}
		
	},

// Home Link Event Functions
	homeMOver: function (e, targetElement) {
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;

		el.src = 'images/logoRunWeb.gif';
	},
	getHomeMOver: function (node) { return function(e) { pH.homeMOver(e, node); }; },

	homeMOut: function (e, targetElement) {
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;

		el.src = 'images/logoRunWeb_grey.gif';
	},
	getHomeMOut: function (node) { return function(e) { pH.homeMOut(e, node); }; },

// Page Positioning Functions
	moveContent: function () {
		var objContent = document.getElementById('rwContent');
		
		if (document.getElementById('scrContainer')) {
			var objScrollContainer = document.getElementById('scrContainer');
			var objScrollPane = document.getElementById('scrPane');
			var objScrollBar = document.getElementById('scrBar');
			var objScrollBtnUp = document.getElementById('scrButtonUp');
			var objScrollBtnDown = document.getElementById('scrButtonDown');
		}

		if (window.innerWidth) {
			var intWinHeight = window.innerHeight;
			var intWinWidth = window.innerWidth;
		} else {
			var intWinHeight = document.documentElement.clientHeight;
			var intWinWidth = document.body.clientWidth;
		}
		
		// Adjust height of scroll Pane
		if (objScrollContainer) {
			objScrollContainer.style.height = (intWinHeight - 190) + 'px';
			if (parseInt(objScrollContainer.offsetHeight) > parseInt(objScrollPane.offsetHeight)) {
				if (parseInt(objScrollContainer.offsetHeight) > parseInt(objScrollPane.offsetHeight) + 10) {
					objScrollContainer.style.height = (parseInt(objScrollPane.offsetHeight) + 10) + 'px';
				}
				objScrollBar.style.visibility = 'hidden';
				objScrollBtnUp.style.visibility = 'hidden';
				objScrollBtnDown.style.visibility = 'hidden';
				objScrollPane.style.top = '0px';
				objScrollPane.style.left = '0px';
				objScrollContainer.style.clip = 'rect(0px ' + parseInt(objScrollContainer.offsetWidth) + 'px ' + parseInt(objScrollContainer.offsetHeight) + 'px 0px)';
			} else {
				objScrollBar.style.visibility = 'visible';
				objScrollBtnUp.style.visibility = 'visible';
				objScrollBtnDown.style.visibility = 'visible';
				objScrollBar.style.height = (intWinHeight - 212) + 'px';
			}
		}
		
		var intObjHeight = objContent.offsetHeight;
		var intObjWidth = objContent.offsetWidth;
		
		var intNewYPos = ((intWinHeight - intObjHeight) / 2);
		var intNewXPos = ((intWinWidth - intObjWidth) / 2);
		
		if (intNewYPos <= 40) {
			intNewYPos = 40;
		}
		
		objContent.style.left = intNewXPos + 'px';
		objContent.style.top = intNewYPos + 'px';
	},

// Functions for Panel Depth Change
	getClickForDepth: function (node) {
		return function(e) { pH.changeDepth(e, node); };
	},

	changeDepth: function (e, targetElement) {
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;
		
		var intCurrentPosition = 0;
		var strID = el.id;

		// determine target panel's current depth
		for (i = 0; i < pH.panelArrangement.length; i++) {
			if (pH.panelArrangement[i] == strID) {
				if (i == 0) {
					return;
				} else {
					intCurrentPosition = i;
				}
			}
		}
		
		// re-order depth list
		for (i = intCurrentPosition; i >= 1; i--) {
			pH.panelArrangement[i] = pH.panelArrangement[i - 1];
		}
		pH.panelArrangement[0] = strID;

		// set new depths based on depth list
		var objTop = document.getElementById(pH.panelArrangement[0]);
		var objOne = document.getElementById(pH.panelArrangement[1]);
		var objTwo = document.getElementById(pH.panelArrangement[2]);
		var objThree = document.getElementById(pH.panelArrangement[3]);

		objTop.style.zIndex = 10;
		objOne.style.zIndex = 7;
		objTwo.style.zIndex = 6;
		objThree.style.zIndex = 5;
	},

// Panel Slide functions
	panelSlide: function (e, targetElement) {
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;
		
		var strID = el.id;
				
		switch (strID) {
		case 'btnProjects':
			if (pH.objPANEL_Projects.animState == 'full') {
				pH.objPANEL_Projects.animState = 'goingIn';
			} else if (pH.objPANEL_Projects.animState == 'none') {
				pH.objPANEL_Projects.animState = 'goingOut';
			}
			break;
		case 'btnMarathon':
			if (pH.objPANEL_Marathon.animState == 'full') {
				pH.objPANEL_Marathon.animState = 'goingIn';
			} else if (pH.objPANEL_Marathon.animState == 'none') {
				pH.objPANEL_Marathon.animState = 'goingOut';
			}
			break;
		case 'btnAboutUs':
			if (pH.objPANEL_AboutUs.animState == 'full') {
				pH.objPANEL_AboutUs.animState = 'goingIn';
			} else if (pH.objPANEL_AboutUs.animState == 'none') {
				pH.objPANEL_AboutUs.animState = 'goingOut';
			}
			break;
		case 'btnContact':
			if (pH.objPANEL_Contact.animState == 'full') {
				pH.objPANEL_Contact.animState = 'goingIn';
			} else if (pH.objPANEL_Contact.animState == 'none') {
				pH.objPANEL_Contact.animState = 'goingOut';
			}
			break;
		} 
	},

	getClickForSlide: function (node) {
		return function(e) { pH.panelSlide(e, node); };
	},

	movePanels: function() {
	// Projects Panel
		if (pH.objPANEL_Projects.animState == 'full' || pH.objPANEL_Projects.animState == 'none') {
			// do nothing
		} else {
			var intPosition = parseInt(pH.objPANEL_Projects.style.left);
			if (pH.objPANEL_Projects.animState == 'goingOut') {
				intPosition += (pH.intANIM_Speed * 3);
				if (intPosition >= 0) {
					pH.objPANEL_Projects.animState = 'full';
					intPosition = 0;
				}
			} else if (pH.objPANEL_Projects.animState == 'goingIn') {
				intPosition -= (pH.intANIM_Speed * 3);
				if (intPosition <= pH.panelPosition[pH.intPANEL_Projects]) {
					pH.objPANEL_Projects.animState = 'none';
					intPosition = pH.panelPosition[pH.intPANEL_Projects];
				}
			}
			pH.objPANEL_Projects.style.left = intPosition + 'px';
		}

	// Marathon Panel
		if (pH.objPANEL_Marathon.animState == 'full' || pH.objPANEL_Marathon.animState == 'none') {
			//do nothing
		} else {
			var intPosition = parseInt(pH.objPANEL_Marathon.style.top);
			if (pH.objPANEL_Marathon.animState == 'goingOut') {
				intPosition += pH.intANIM_Speed;
				if (intPosition >= 0) {
					pH.objPANEL_Marathon.animState = 'full';
					intPosition = 0;
				}
			} else if (pH.objPANEL_Marathon.animState == 'goingIn') {
				intPosition -= pH.intANIM_Speed;
				if (intPosition <= pH.panelPosition[pH.intPANEL_Marathon]) {
					pH.objPANEL_Marathon.animState = 'none';
					intPosition = pH.panelPosition[pH.intPANEL_Marathon];
				}
			}
			pH.objPANEL_Marathon.style.top = intPosition + 'px';
		}
		
	// About Us Panel
		if (pH.objPANEL_AboutUs.animState == 'full' || pH.objPANEL_AboutUs.animState == 'none') {
			//do nothing
		} else {
			var intPosition = parseInt(pH.objPANEL_AboutUs.style.right);
			if (pH.objPANEL_AboutUs.animState == 'goingOut') {
				intPosition += (pH.intANIM_Speed * 2.2);
				if (intPosition >= 0) {
					pH.objPANEL_AboutUs.animState = 'full';
					intPosition = 0;
				}
			} else if (pH.objPANEL_AboutUs.animState == 'goingIn') {
				intPosition -= (pH.intANIM_Speed * 2.2);
				if (intPosition <= pH.panelPosition[pH.intPANEL_AboutUs]) {
					pH.objPANEL_AboutUs.animState = 'none';
					intPosition = pH.panelPosition[pH.intPANEL_AboutUs];
				}
			}
			pH.objPANEL_AboutUs.style.right = intPosition + 'px';
		}
		
	// Contact Panel
		if (pH.objPANEL_Contact.animState == 'full' || pH.objPANEL_Contact.animState == 'none') {
			//do nothing
		} else {
			var intPosition = parseInt(pH.objPANEL_Contact.style.bottom);
			if (pH.objPANEL_Contact.animState == 'goingOut') {
				intPosition += (pH.intANIM_Speed * 1.7);
				if (intPosition >= 0) {
					pH.objPANEL_Contact.animState = 'full';
					intPosition = 0;
				}
			} else if (pH.objPANEL_Contact.animState == 'goingIn') {
				intPosition -= (pH.intANIM_Speed * 1.7);
				if (intPosition <= pH.panelPosition[pH.intPANEL_Contact]) {
					pH.objPANEL_Contact.animState = 'none';
					intPosition = pH.panelPosition[pH.intPANEL_Contact];
				}
			}
			pH.objPANEL_Contact.style.bottom = intPosition + 'px';
		}

	},

	intANIM_Speed: 10,
	intPANEL_Projects: 0,
	intPANEL_Marathon: 1,
	intPANEL_AboutUs: 2,
	intPANEL_Contact: 3,
	panelArrangement: ['rwProjects','rwMarathon','rwAboutUs','rwContact'],
	panelPosition: [-530,-140,-310,-240],
	objPANEL_Projects: {} ,
	objPANEL_Marathon: {},
	objPANEL_AboutUs: {},
	objPANEL_Contact: {}
};

rwScript.addEvent(window, 'load', pH.addListeners, false);
rwScript.addEvent(window, 'unload', EventCache.flush, false);
