function hover(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	current = current[0];
	
	if(what && current && current.search(/Hover$/) == -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current+'Hover");');
	} else if(!what && current && current.search(/Hover$/) != -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/Hover$/, '')+'");');
	}
}

function select(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	current = current[0];
	
	if(what && current && current.search(/^Sel/) == -1) {
		eval('who.className = who.className.replace(/'+current+'/, "Sel'+current+'");');
	} else if(!what && current && current.search(/^Sel/) != -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/^Sel/, '')+'");');
	}
}

function parseStr(str) {
	var first = str.split('&');
	var data = new Array();
	
	for(var i = 0; i < first.length; i++) {
		var last = first[i].split('=');
		
		data[last[0]] = last[1];
	}
	
	return data;
}

function fixPNG(who, scale) {
	var isIE = (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf('Opera') == -1);
	if(isIE) {
		// filter for MSIE 6.x and 5.5
		var ie6xFilter = /^.*MSIE [6]\.[0-9].*$/;
		var ie55Filter = /^.*MSIE [5].*$/;
		
		if(ie6xFilter.test(navigator.userAgent) || ie55Filter.test(navigator.userAgent)) {
			who.onload = function(){}
			who.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+who.src+'"'+(scale ? ',sizingMethod="scale"' : '')+')';
			who.src = 'images/pixel.gif';
		}
	}
}

function preload(imagePathArray) {
	var imageArray = new Array();
	for (var i = 0; i < imagePathArray.length; i++) {
		var nextImage = new Image();
		nextImage.src = imagePathArray[i];
		imageArray.push(nextImage);
	}
	return imageArray;
}

function evalScripts(data) {
	var index = data.indexOf('<script');
	var endIndex;
	
	while(index != -1) {
		index = data.indexOf('>', index) + 1;
		endIndex = data.indexOf('</script>', index);
		
		eval(data.substring(index, endIndex));
		
		index = data.indexOf('<script', endIndex);
	}
}

function processLoadPage() {
	if(document.cancelSwap) {
		document.cancelSwap();
	}
	
	var detailSlip = document.getElementById('detailSlipContainer');
	if(detailSlip && detailSlip.style.display != 'none') {
		if(detailSlip.getAttribute('keepShown') == 'yes') {
			detailSlip.removeAttribute('keepShown');
		} else {
			hideSlip('detail');
		}
	}

	var el = document.getElementById('pageLoad');
	var title = httpReq.responseText.match(/!--title:(.+)-->/);

	if(title) {
		document.title = title[1];
	}
	
	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		el.innerHTML = httpReq.responseText.replace(/\<script.+?\<\/script\>/g, '');
	}
	if(el.style.display != 'block') {
		el.style.display = 'block';
	}
	
	evalScripts(httpReq.responseText);
	
	if(document.ajaxHistory[location.hash] && document.ajaxHistory[location.hash].scrollTop) {
		window.scrollTo(0,document.ajaxHistory[location.hash].scrollTop);
	} else {
		window.scrollTo(0,0);
	}
	
	if(location.hash == '') {
		var name = location.pathname.match(/([a-z]+)\.php$/);
		
		if(name) {
			setNav(name[1], false, true);
		}
	} else {
		var currentHistory = document.ajaxHistory[location.hash];
		
		if(currentHistory) {
			setNav(currentHistory['topNav'], false, true);
		}
	}
}

function toggleSideBar(show) {
	var container = document.getElementById('contentLeftContainer');
	var smallSidebarContent = document.getElementById('smallSidebarContent');
	var largeSidebarContent = document.getElementById('largeSidebarContent');	

	// stop any currently running animations
	if(container.animation) 
		container.animation.cancel();
	if(smallSidebarContent.animation) 
		smallSidebarContent.animation.cancel();
	if(largeSidebarContent.animation) 
		largeSidebarContent.animation.cancel();
	
	if(show) {
		// expand the overall container
		container.animation = new Fx.Morph(container.id, {duration: 500, transition: Fx.Transitions.Back.easeOut});
		container.animation.start({
			width: [container.offsetWidth, 600]
		});
		// and fade out the small content...
		smallSidebarContent.animation = new Fx.Morph(smallSidebarContent.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut});
		smallSidebarContent.animation.start({'opacity': [1,0]});
		// ...while fading in the large content
		largeSidebarContent.animation = new Fx.Morph(largeSidebarContent.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut});
		largeSidebarContent.animation.start({'opacity': [0,1]});

		

	} else {
		// collapse the overall container
		container.animation = new Fx.Morph(container.id, {duration: 500, transition: Fx.Transitions.Back.easeIn });
		container.animation.start({
			width: [container.offsetWidth, 227]
		});
		// and fade in the small content...
		smallSidebarContent.animation = new Fx.Morph(smallSidebarContent.id, 'opacity', {duration: 1000, transition: Fx.Transitions.Quart.easeOut});
		smallSidebarContent.animation.start({'opacity': [0,1]});
		// ...while fading out the large content
		largeSidebarContent.animation = new Fx.Morph(largeSidebarContent.id, 'opacity', {duration: 1000, transition: Fx.Transitions.Quart.easeOut});
		largeSidebarContent.animation.start({'opacity': [1,0]});

	}
}

function setNav(which, showMenu, hideClover) {
	var hashNav;

	if(location.hash == '') {
		var name = location.pathname.match(/([a-z]+)\.php$/);
		
		if(name) {
			hashNav = name[1];
		}
	} else {
		var currentHistory = document.ajaxHistory[location.hash];
	
		if(currentHistory) {
			hashNav = currentHistory['topNav'];
		}
	}
	
	var navPoints = ['programs', 'calendar', 'projects', 'resources', 'about4h', 'join4h', 'give4h'];
	
	for(var i = 0; i < navPoints.length; i++) {
		toggleNav(navPoints[i], false, hashNav);
		
		// hide if not our current one, or we need to hide it anyways
		if(which != navPoints[i] || !showMenu) {
			toggleMenu(navPoints[i], false, hashNav);
		}
	}
	
	toggleSubmenu(null, true);
	
	if(which) {
		toggleNav(which, true, hashNav, hideClover);
		if(showMenu)
			toggleMenu(which, true);
	}
}

function toggleNav(which, on, hashNav, hideClover) {
	var curClover = document.getElementById(which+'_clover');
	var curImage = document.getElementById(which+'_img');
	
	if(on) {
		if(!hideClover && curClover.style.display != 'block') {
			curClover.style.display = 'block';
		}
		if(curImage.src != 'images/buttons/'+which+'_on.png') {
			curImage.src = 'images/buttons/'+which+'_on.png';
			if(Browser.Engine.trident) {
				fixPNG(curImage);
			}
		}
	} else {
		var curMenu = document.getElementById(which+'Menu');
		// Only hide if it's showing, and if there is no menu (or the menu is finally closed)
		if(!(curMenu && curMenu.animationState) && curClover.style.display != 'none') {
			curClover.style.display = 'none';
		}
		if(which != hashNav && curImage.src != 'images/buttons/'+which+'.png') {
			curImage.src = 'images/buttons/'+which+'.png';
			if(Browser.Engine.trident) {
				fixPNG(curImage);
			}
		}
	}
}

function toggleMenu(which, on, hashNav) {
	var curMenu = document.getElementById(which+'Menu');
	
	if(!curMenu)
		return;
	
	if(on) {
		// if we're closed, or in the process of closing
		if(!curMenu.animationState || curMenu.animationState > 2) {
			var curMenuMiddle = document.getElementById(which+'MenuMiddle');
			if(curMenuMiddle) {
				if(curMenu.animation) {
					curMenu.animation.cancel();
				}
				if(!curMenu.animationState) {
					curMenu.style.visibility = 'hidden';
					curMenu.style.display = 'block';
					curMenuMiddle.style.width = '1px';
					curMenuMiddle.style.height = '1px';
				}
				
				curMenu.animationState = 1;
				curMenu.animation = new Fx.Morph(curMenuMiddle.id, {duration: 300, transition: Fx.Transitions.Back.easeOut, onStart: function(){ curMenu.style.visibility='visible'; }, onComplete: function(){ curMenu.animation = null; curMenu.animationState = 2;}});
				curMenu.animation.start({
					'width': [1, curMenuMiddle.scrollWidth], 
					'height': [1, curMenuMiddle.scrollHeight]
				});
			}
		}
	} else {
		// if we're open, or the process of opening
		if(curMenu.animationState && curMenu.animationState < 3) {
			var curMenuMiddle = document.getElementById(which+'MenuMiddle');
			if(curMenuMiddle) {
				if(curMenu.animation) {
					curMenu.animation.cancel();
				}
				
				curMenu.animationState = 3;
				curMenu.animation = new Fx.Morph(curMenuMiddle.id, {duration: 300, transition: Fx.Transitions.Back.easeIn, onComplete: function(){ curMenu.style.display='none'; curMenu.style.visibility='hidden'; curMenu.animation = null; curMenu.animationState = 0; toggleNav(which, false, hashNav);}});
				curMenu.animation.start({
					'width': [curMenuMiddle.offsetWidth, 1], 
					'height': [curMenuMiddle.offsetHeight, 1]		
				});
			}
		}
	}
}

function toggleSubmenu(which, on) {	
	if(on) {
		if(document.lastSubmenu && document.lastSubmenu != which) {
			toggleSubmenu(document.lastSubmenu, false);
		}
		
		document.lastSubmenu = which;
		if(which) {
			var curMenu = document.getElementById(which+'Menu');
			if(curMenu) {
				// if we're closed
				if(!curMenu.animation) {
					var curMenuMiddle = document.getElementById(which+'MenuMiddle');
					if(curMenuMiddle) {
						curMenu.style.visibility = 'hidden';
						curMenu.style.display = 'block';
						curMenuMiddle.style.width = '1px';
						curMenuMiddle.style.height = '1px';
						
						curMenu.animation = new Fx.Morph(curMenuMiddle.id, {duration: 300, transition: Fx.Transitions.Back.easeOut, onStart: function(){ curMenu.style.visibility='visible'; }});
						curMenu.animation.start({
							'width': [1, curMenuMiddle.scrollWidth], 
							'height': [1, curMenuMiddle.scrollHeight]
						});
					}
				}
			}
		}
	} else {
		if(which) {
			var curMenu = document.getElementById(which+'Menu');
			
			if(document.lastSubmenu == which) {
				document.lastSubmenu = null;
			}
			
			curMenu.animation = null;
			curMenu.style.display='none';
			curMenu.style.visibility='hidden';
		}
	}
}

function processSlip(slip, skipShowSlip, slideTo) {
	if(!slip) {
		slip = 'detail';
	}

	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		if(slideTo && httpReq.responseText.indexOf('--skipSlide-->') == -1) {
			slideSlip(slip, slideTo, httpReq.responseText);
		} else {
			var el = document.getElementById(slip+'SlipContent');
			
			el.innerHTML = httpReq.responseText.replace(/\<script.+?\<\/script\>/g, '');
		}
	}
	
	evalScripts(httpReq.responseText);
	
	if(!skipShowSlip) {
		showSlip(slip);
	}
}

function processDiv(div) {
	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		var slideIndex = httpReq.responseText.indexOf('--slideIn:');
		if(slideIndex != -1) {
			document.slideDiv(div, slideIndex, httpReq.responseText);
		} else {
			var el = document.getElementById(div);
			if(el) {
				el.innerHTML = httpReq.responseText.replace(/\<script.+?\<\/script\>/g, '');
			}
		}
	}
	
	evalScripts(httpReq.responseText);
}

function toggleSearch() {
	var searchSlip = document.getElementById('footerSearchSlip');
	var searchSlipInput = document.getElementById('footerSearchInput');
	if (searchSlip && searchSlipInput) {
		
		if (searchSlip.style.display == 'block') {
			searchSlip.style.display = "none";
			// and clear out any old value
			searchSlipInput.value = "";
		}
		else {
			searchSlip.style.display = "block";
			// and set the focus on the input field
			searchSlipInput.focus();
		}
	}		
}

function processEval() {
	eval(httpReq.responseText);
}

function scrollFix(el, event) {
	if(window.event) {
		el.scrollTop -= (window.event.wheelDelta / 120 * 60);
		return false;
	} else if(this.scrollTop == 0 && event.wheelDelta > 0) {
		return false;
	} else if((el.scrollHeight - this.scrollTop) == el.offsetHeight && event.wheelDelta < 0) {
		return false;
	}
}

function submitForm(theForm, queryString, proccessFunction) {
	var data = '';
	
	if(theForm) {
		if(theForm.jt_submitted) {
			theForm.jt_submitted.value = 'isValid';
		}
		var formElements = theForm.elements;
	
		for(i = 0; i < formElements.length; i++) {
			if(formElements[i].disabled || ((formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && !formElements[i].checked))
				continue;
				
			if(i)
				data += "&";
			
			if(formElements[i].options) {
				var valueAppend = '';
			
				for(k = 0; k < formElements[i].length; k++) {
					if(formElements[i].options[k].selected) {
						if(valueAppend != "") {
							valueAppend += ",";
						}
						
						valueAppend += formElements[i].options[k].value;
					}
				}
				
				data += escape(formElements[i].name) + '=' + escape(valueAppend);
			} else {
				data += escape(formElements[i].name) + '=' + escape(formElements[i].value);
			}
		}
	}
	
	sendRequest('view_controller.php', queryString, data, (proccessFunction ? proccessFunction : 'processSlip("detail", true);'));
}

function processSearch(searchSlipId) {
	// get the terms we're searching for
	var searchSlip = document.getElementById(searchSlipId);
	// look for the input inside this slip
	var searchInput = searchSlip.getElementsByTagName('input')[0];
	if (searchInput) {
		var searchTerms = escape(searchInput.value);
		if (searchTerms != "") {
			var options = {'hash': '#search:'+searchTerms, 'query': 'searchTerms='+searchTerms, 'callback': 'processLoadPage();', 'page' : 'search.php'};
			if(setCurrentState(options)) {
				processCurrentState();
			}
		}
	}	
}

function setLoadStatus(on) {
	var el = document.getElementById('loadStatus');
	
	if(el) {
		if(on) {
			var fixedNotSupported = el.currentStyle && el.currentStyle['position'] == 'absolute';

			var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
			var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);

			var verticalPosition = fixedNotSupported ? (window.scrollY ? window.scrollY : document.documentElement.scrollTop) : 0;

			verticalPosition = (currentHeight/2)-40+verticalPosition;
			if(verticalPosition < 0) {
				verticalPosition = 0;
			}
			
			el.style.top = verticalPosition+'px';
			el.style.left = (currentWidth/2 - 41)+'px';
		
			el.style.display = 'block';
		} else {
			el.style.display = 'none';
		}
	}
}

function toggleAnnouncementIgnore(who, id) {
	var newIgnore;
	var cookies = document.cookie;
	var expireDate = new Date();
	
	expireDate.setFullYear(expireDate.getFullYear()+30);

	if(cookies.indexOf('announcementIgnore') != -1) {
		var start = cookies.indexOf('announcementIgnore') + 19;
		var end = cookies.indexOf(';', start) - 1;

		if(end < 0) {
			end = cookies.length;
		}

		newIgnore = cookies.substring(start, end).split(',');
	} else {
		newIgnore = new Array();
	}

	if(who.checked) {
		newIgnore.push(id);
	} else {
		for(var i = 0; i < newIgnore.length; i++) {
			if(newIgnore[i] == id) {
				newIgnore.splice(i, 1);
				break;
			}
		}
	}
	
	document.cookie = 'announcementIgnore='+newIgnore.join(',')+'; expires='+expireDate.toUTCString()+'; path=/';
}

document.slideDiv = function(div, slideIndex, data) {
	var content = document.getElementById(div);
	
	if(content) {
		var scroller = content.parentNode;
		var container = scroller.parentNode;
		var newContent = document.createElement('div');
		
		if(scroller && container && newContent && !scroller.animation) {
			var direction = data.charAt(slideIndex+10);
			
			content.style.width = content.offsetWidth+'px';
			content.style.height = content.offsetHeight+'px';
			
			container.style.width = container.offsetWidth+'px';
			container.style.height = container.offsetHeight+'px';
			container.style.overflow = 'hidden';
			
			content.style.top = '0px';
			content.style.left = '0px';
			content.style.position = 'absolute';
			
			scroller.style.top = '0px';
			scroller.style.left = '0px';
			scroller.style.position = 'absolute';
			
			newContent.style.position = 'absolute';
			newContent.style.left = content.offsetWidth+'px';
			
			newContent.innerHTML = data.replace(/\<script.+?\<\/script\>/g, '');
			
			scroller.appendChild(newContent);
			
			if(direction == 'l') {
				newContent.style.left = (-newContent.offsetWidth)+'px';
			} else if(direction == 't') {
				newContent.style.left = (-(content.offsetWidth - newContent.offsetWidth) / 2)+'px';
				newContent.style.top = newContent.offsetHeight+'px';
			} else if(direction == 'b') {
				newContent.style.left = (-(content.offsetWidth - newContent.offsetWidth) / 2)+'px';
				newContent.style.top = content.offsetHeight+'px';
			}

			setSlipScrollers(container, 'hidden');

			if(container.offsetHeight != newContent.offsetHeight || container.offsetWidth != newContent.offsetWidth) {
				if(container.animation) {
					container.animation.cancel();
				}
				
				container.animation = new Fx.Morph(container.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
						if(!scroller.animation) {
							container.style.overflow = 'visible';
							container.style.width = 'auto';
							container.style.height = 'auto';
							
							if(Browser.Engine.trident) {
								setTimeout('document.getElementById("'+scroller.id+'").style.display = "none"; document.getElementById("'+scroller.id+'").style.display = "block";', 1);
							}
						}
				
						container.animation = null;
					}
				});
				container.animation.start({
					'height': [container.offsetHeight, newContent.offsetHeight],
					'width': [container.offsetWidth, newContent.offsetWidth]
				});
			}

			scroller.animation = new Fx.Morph(scroller.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
					scroller.removeChild(content);
					newContent.id = div;
					setSlipScrollers(newContent, 'auto');
					if(data.indexOf('--evalScriptsOnSlide-->') != -1) {
						evalScripts(data);
					}
					
					scroller.style.position = 'static';
					newContent.style.position = 'static';
					
					if(!container.animation) {
						container.style.overflow = 'visible';
						container.style.width = 'auto';
						container.style.height = 'auto';
						
						if(Browser.Engine.trident) {
							setTimeout('document.getElementById("'+scroller.id+'").style.display = "none"; document.getElementById("'+scroller.id+'").style.display = "block";', 1);
						}
					}
					
					scroller.animation = null;
				}
			});
			
			scroller.animation.start({
				'left': [scroller.offsetLeft, -newContent.offsetLeft],
				'top': [scroller.offsetTop, -newContent.offsetTop]
			});
		}
	}
}

function toggleJTechComments() {
	var jtechComments = document.getElementById('jtechCommentsContainer');
	
	if(jtechComments) {	
		jtechComments.style.display = jtechComments.style.display == 'block' ? 'none' : 'block';
	}
}
