new Event.observe(window,'load',function() { initializeSubMenus(); });

function initializeSubMenus() {
	var navs = $('mainNav').select('div.subMenu');
	navs.each(function(nav,index) {
		var id = $(nav).id;
		var parent = $(nav).up('li').identify();
		new Event.observe(parent,'mouseover',function() { openSubMenu(id,parent); });
		new Event.observe(parent,'mouseout',function() { closeSubMenu(id,parent); });
	});
	var images = new Array('images/nav_selected_bg.png','images/nav_selected_left.png','images/nav_selected_right.png','images/nav_hover_left.png','images/nav_hover_right.png','images/subnav_bl.png','images/subnav_br.png','images/subnav_tr.png');
	var menuimg = new Array();
	for(var x=0;x<images.length;x++) {
		menuimg[x] = new Image();
		menuimg[x].src = images[x];
	}
}
function openSubMenu(id,parent) {
	if(Object.isElement($(id))) {
		positionSubMenu(id,parent);
		$(id).show();
		$(parent).addClassName('hover');
	}
}
function closeSubMenu(id,parent) {
	if(Object.isElement($(id))) {
		$(id).hide();
	}
	$(parent).removeClassName('hover');
}
function positionSubMenu(id,parent) {
	if(Object.isElement($(id))) {
		var p = $(parent).down().positionedOffset();
		var w = $(id).getWidth();
		$(id).setStyle({left:(p[0])+'px',top:(p[1]+23)+'px',width:w+'px'});
	}
}
function loadVimeoVideo(id,el) {
	el = $(el).identify();
	var r = '<object width="458" height="274"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1&amp;autoplay=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1&amp;autoplay=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="458" height="274"></embed></object>';
	$('player').update(r);
	var ul = $(el).up().up().up().up();
	var elements = $(ul).getElementsByClassName('selected');
	for(var i=0;i<elements.length;i++) {
		$(elements[i]).removeClassName('selected');
	}
	$(el).toggleClassName('selected');
	/*
	new Ajax.Request('/pages/vimeo/'+id,{
			method:'get',
			onSuccess:function(transport) {
				var r = transport.responseText;
				$('player').update(r);
				var ul = $(el).up().up().up().up();
				var elements = $(ul).getElementsByClassName('selected');
				for(var i=0;i<elements.length;i++) {
					$(elements[i]).removeClassName('selected');
				}
				$(el).toggleClassName('selected');
				
			},
			onFailure:function() { alert('We encountered an error while communicating with the server. Please try again.'); }
		});*/
	
	return false;
}
function toggleDetails(id) {
	new Effect.toggle(id+'Details','slide',{duration:.5});
	return false;
}
function selectSponsorshipOpportunity(id) {
	$('opportunity'+id).checked = true;
	selectFirstPriceLevel('opportunity'+id);
}
function updateSponsorshipType(el) {
	var id = $(el).identify();
	$('cmd').value = $F(el);
	elements = $$('select.cmd2');
	elements.each(function(element) {
		var tmpid = $(element).identify();
		if(tmpid != id) {
			$(element).selectedIndex = 0;
		}
	});
}
function selectFirstPriceLevel(el) {
	var found = false;
	var inputs = $(el).up().next().select('input');
	inputs.each(function(element) {
		if($(element).checked) {
			found = true;
		}
	});
	if(!found) {
		$(el).up().next().down('input').checked = true;
	}
}
function selectOtherBox(el,id) {
	$(el).previous('input').checked = true;
	var id = $(el).identify();
	elements = $(el).up().select('input');
	elements.each(function(element) {
		var tmpid = $(element).identify();
		if(tmpid != id) {
			$(element).checked = false;
		}
	});
}
function updateMeals(v1, v2) {
	var price = new Number('2.60');
	var q = $F('SponsorshipQuantity');
	var a = $F('SponsorshipAmount');
	if(v1 == 'quantity') {
		$('SponsorshipAmount').value = ((q*1)*price).toFixed(2);
		$('SponsorshipItemName').value = 'Meal Sponsorship: '+q+' Meals';
	} else {
		$('SponsorshipQuantity').value = ((a*1)/price).toFixed(2);
		$('SponsorshipItemName').value = 'Meal Sponsorship: '+q+' Meals';
	}
}
function deleteThis(element,text) {
	if(confirm('Are you sure you want to remove this '+text+'?')) {
		window.location.href = $(element).href;
	}
	return false;
}