var slideTextcounter = 0, submenuArr = [], currentSubmenu = '', defaultSubmenu, onMain = false, onSubnav = false;

function init(page) {
	switch(page) {
		case "home__page":
			banners = new jbSlideshow();
			banners.init({
				parentdiv: 'bannerholder',
				slideRate: 3,
				beforeAnimateStart: 'animateBannerText',
				afterAnimateFinish: 'advanceHomeBannerCounter'
			});
			sponsors = new jbSlideshow();
			setTimeout ('sponsors.init({parentdiv: \'sponsorholder\',slideRate: 4})', 5000);
		break;
		case "the_festival_venues_page":
			googmap();
		break;
		case "the_festival_film_description_page":
			if ($('col1').getHeight() - 29 > $('col2').getHeight()) {
			$('col2').setStyle({
				height: ($('col1').getHeight() - 29) + 'px'
			});//-29px padding
			}
		break;
	}

	$$('.nav_main').each(function(g) {
		Event.observe(g, 'mouseover', function(g) {
			showSubnav(g);
		});
		submenuArr.push(g.id);
		if (g.hasClassName('here')) {
			defaultSubmenu = g.id;
		}
	});

	$$('.topnav_sub').each(function(g) {
		Event.observe(g, 'mouseover', function() {
			onSubnav = true;
		});
	});

	$$('.nav_main').each(function(g) {
		Event.observe(g, 'mouseout', function() {
			setTimeout(function() {
				if (!onMain) {
					restoreSubnav();
				}
			}, 100);
		});
	});

	$$('.topnav_sub').each(function(g) {
		Event.observe(g, 'mouseout', function() {
			onSubnav = false;
			setTimeout(function() {
				if (!onMain) {
					restoreSubnav();
				}
			}, 100);
		});
	});
	
	Event.observe($('topnav_main'), 'mouseover', function() {
		onMain = true;
	});

	Event.observe($('topnav_main'), 'mouseout', function() {
		onMain = false;
	});

}

function animateBannerText() {
	banners = $$('.bannernav').size();
	Effect.Fade('bannerbottomtext' + slideTextcounter, { duration: 0.7 });
	slideTextcounter++;
	if (slideTextcounter >= banners) {
		slideTextcounter = 0;
	}
	Effect.Appear('bannerbottomtext' + slideTextcounter, { duration: 0.7 });
}

function advanceHomeBannerCounter(which) {
	$$('.bannernav').each(function(e) {
		$(e).className = 'bannernav';
	});
	$('bannernav' + parseInt(which.slidecounter+1)).addClassName('here');
}

function switchThing(which, type) {
	if (which.value == '') {
		return;
	}
	var browse = which.name.substr(7);
	var key = which.value;
	if (type == 'film') {
		basepage = 'film_guide';
	} else {
		basepage = 'event_guide';
	}
	//local testing on IE PC
	if (top.location.hostname == "rigid.local") {
		URL = "http://" + top.location.hostname + "/biff/pages/" + basepage + ".php?browse=" + browse + "&bkey=" + key;
	} else {
		URL = "http://" + top.location.hostname + "/pages/" + basepage + ".php?browse=" + browse + "&bkey=" + key;
	}
	top.location.href = URL;
}

function validateForm(which) {
	var f = document.forms[which.id];
	var str = '';
	var foc = '';
	if (which.id == 'volunteerform') {
		if (!emailCheck(f.email.value)) {
			f.email.focus();
//			return false;
		}
		mandatory = {"name": "Name",
					"address": "Address",
					"city_state": "City and State",
					"country": "Country",
					"email": "Email Address",
					"emergency_name": "Emergency Name",
					"emergency_phone": "Emergency Phone"
					};
					if (!(f.home_phone.value || f.work_phone.value || f.mobile_phone.value)) {
						str += "You must enter at least one phone number.\n";
						foc = "home_phone";
					}
	} else if (which.id == 'pressticketform') {
		mandatory = {"name": "Name",
					"publication": "Publication"
					};
	} else if (which.id == 'interviewform') {
		mandatory = {"name": "Name",
					"publication": "Publication"
					};
		for (i=1;i<=10;i++) {
			if (!(f["filmmaker_" + i].value + f["film_" + i].value + f["date_" + i].value + f["time_" + i].value)) {//nothing filled in
				continue;
			} else if (f["filmmaker_" + i].value && f["film_" + i].value && f["date_" + i].value && f["time_" + i].value) {//everything filled in
				continue;
			} else {//partially filled in
				str += "If you want to interview a filmmaker, you must fill in all the information.\n";
				foc = "filmmaker_" + i;
			}
		}
	} else if (which.id == 'pressaccredform') {
	} else if (which.id == 'mc-embedded-subscribe-form') {
		mandatory = {"EMAIL": "Email",
						"FNAME": "First Name",
						"LNAME": "Last Name"};
		if (!emailCheck(f.EMAIL.value)) {
			f.EMAIL.focus();
			return false;
		}
		if (f.FNAME.value == 'First Name') {
			alert("You must enter a first name.");
			f.FNAME.focus();
			return false;
		}
		if (f.LNAME.value == 'Last Name') {
			alert("You must enter a last name.");
			f.LNAME.focus();
			return false;
		}
	}
	for (var nicename in mandatory) {
		if (f[nicename].value == '') {
			str += "You must enter a " + mandatory[nicename] + ".\n";
			if (foc == '') {
				foc = nicename;
			}
		}
	}
	if (which.id == 'volunteerform') {
		if (!f.agree_to_terms[0].checked) {
			str += "You must agree to the terms.\n";
		}
	}
	if (str) {
		alert(str);
		if (foc != '') {
			f[foc].focus();
		}
		return false;
	}
	return true;
}

//SLIDESHOW
var isFading = false;
var isPlaying = false;
var fadeTime = 0.7;
var slidecounter = 0;
var slideRate = 3;//seconds
var slideTimer;

function slide(which) {
	if (isFading) { return; }
	isFading = true;
	nextslide = slidecounter + which;
	currentslide = slidecounter;
	if (nextslide < 0) {
		nextslide = imgCount;
	}
	if (nextslide >= imgCount) {
		nextslide = 0;
	}
	if (nextslide > slidecounter) { //foward
		$('slidediv' + nextslide).setStyle({ display: "block" });
		Effect.Fade('slidediv' + slidecounter, { duration: fadeTime,
			afterFinish: function() {
				$('slidediv' + currentslide).setStyle({ display: "none" });
				isFading = false;
			}
		});
	} else {
		Effect.Appear('slidediv' + nextslide, { duration: fadeTime,
			afterFinish: function() {
				$('slidediv' + currentslide).setStyle({ display: "none" });
				isFading = false;
			}
		});
	}
	slidecounter = nextslide;
}

function advanceSlide() {
	clearTimeout(slideTimer);
	isPlaying = true;
	slide(1);
	slideTimer = setTimeout("advanceSlide()",slideRate * 1000);
}

function googmap() {
	Event.observe(window, 'unload', function(event) {
		GUnload();
	});
}


//SUBNAVS
function showSubnav(e) {
	el = e.element();
	//if user clicks in one of the child elements, find the proper parent
	while (!el.hasClassName('nav_main')) {
		el = el.up();
	}
	submenu = 'subnav_' + el.id.substr(4);
	if (submenu == currentSubmenu) { return; }
	submenuArr.each(function(g) {
		$('sub' + g).hide();
	});
	$(submenu).show();
	currentSubmenu = submenu;
}

function restoreSubnav() {
	if (onSubnav) { return; }
	submenuArr.each(function(g) {
		$('sub' + g).hide();
	});
	if (defaultSubmenu) {
		$('sub' + defaultSubmenu).show();
		currentSubmenu = defaultSubmenu;
	} else {
		currentSubmenu = '';
	}
}
