function searchSubmit() {
	document.getElementById("search").submit();
}

/* = EMAIL THIS PAGE
-------------------------------------------------
	grabs url and sends string to email
-------------------------------------------------*/
function emailPage() {
	var URL = location.href;
	var emailString= "mailto:?subject=I've been visiting Barter Theatre's website and thought you might be interested.&body=";
	self.location = emailString + URL;
}

/* = PREP NAV
-------------------------------------------------
	This function sets the hover events for
	nav drop downs.
	Required Nav Structure: Nexted ULs
-------------------------------------------------*/

function prepDropdowns(elementId) {
	if(!document.getElementsByTagName) return 0;
	if(!document.getElementById) return 0;
	if(!document.getElementById(elementId)) return 0;
	
	var listItem = document.getElementById(elementId).getElementsByTagName("LI");
	for(var i = 0; i < listItem.length; i++)
	{
		listItem[i].onmouseover = listItem[i].onmouseout = function() {
			
			if(this.className) {
				this.className += " hover";
			}
			else {
				this.className = "hover";
			}
			
			this.style.zIndex=99; // for ie
			
			return 0;
		};
		listItem[i].onmouseout = function() {
			
			this.className = this.className.replace(/\b ?hover ?\b/,"");
			
			return 0;
		};
	}
	return 0;
}


/* = CLEAR FIELD
-------------------------------------------------
	clears the value of any inpu field with the
	class name of clearDefault
-------------------------------------------------*/

function clickClear() {
	if(!document.getElementsByTagName) return false;
	
	var inputs = document.getElementsByTagName("INPUT");
	
	for ( var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.indexOf("clearDefault") != -1)
		{
			inputs[i].onclick = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			};
			inputs[i].onfocus = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			};
			inputs[i].onblur = function() {
				if(this.value == "")
				{
					this.value = this.defaultValue;
				}
				return 0;
			};
		}
	}
	return 0;
}


/* = MAIN NAVIGATION
---------------------------------------------------------------
	parse the url and hightlight the correct nav item
---------------------------------------------------------------*/
function prepMainNav() {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	
	/* parse the url */
	var url = new String(location);
	var urlArray = new Array();
	urlArray = url.split("/");
	var directory = urlArray[3];
	
	/* set item to search for */
	if(directory == "") {
		return false;
	}
	
	if(directory) {
		/* check  links */
		switch(directory) {
			case "plan_your_visit":
				selectItem("nav-plan");
				break;
			case "purchase_tickets":
				selectItem("nav-tickets");
				break;
			case "groups":
				selectItem("nav-groups");
				break;
			case "packages":
				selectItem("nav-packages");
				break;
			case "shows":
				selectItem("nav-shows");
				break;
			case "festival":
				selectItem("nav-festival");
				break;
			case "about":
				selectItem("nav-about");
				break;
			case "teachers_students":
				selectItem("nav-teachers");
				break;
			case "young_audiences":
				selectItem("nav-audiences");
				break;
			default:
				break;
		}
	}
	
	/* select the nav item */
	function selectItem(navItem) {
		if(document.getElementById(navItem)) {
			var item = document.getElementById(navItem);
			item.className += " selected";
		};
	}
	
	return 0;
}


/* INITIALIZE ALL FUNCTION AFTER WINDOW ONLOAD
-------------------------------------------------*/
function init() {
	if(!document.getElementById || !document.getElementsByTagName) return 0;
	// prep functions
	clickClear();
	prepMainNav();
	prepDropdowns("navigation");
	
	return 0;
}

addLoadEvent(init);


/* = ON LOAD
---------------------------------------------------------------
	add the on load events
---------------------------------------------------------------*/
function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}	else {
		window.onload = function() {
			oldOnLoad();
			func();
		};
	}
}

// ================
// = Media Player =
// ================

var mediaSettings = {
	currentTimeout: '',
	videoPauseTimeout: '',
	timeoutSpeed: 6000,
	autoChange: true
};

/**
 * Loads the video player listener
 */
function loadVideos () {		
	var playerVariables = {
		key: '@8d76cd70043b04fcaaa',
		plugins: {
			controls: {
				url: '/js/flowplayer.controls-3.1.3.swf',
			
				backgroundColor: 'transparent',
				backgroundGradient: 'none',
				buttonColor: '#114B67',
				buttonOverColor: '#7E361E',
			
				autoHide: 'always',
				stop: false,
				fullscreen: true,
				time: false
			}
		}
	};
	
	flowplayer("player", {src: "/js/flowplayer.commercial-3.1.3.swf", wmode: 'transparent'}, playerVariables);
}

/**
 * Scrolls the playlist
 */
function scrollPlaylist () {
	jQuery('#playlist-container').scrollable({
		items: '#playlist',
		next: '#next',
		prev: '#previous'
	});
}

/**
 * Starts the playlist by changing content to the first item
 */
function startPlaylist () {
	changeContent(jQuery('#playlist li:first a'));
}

/**
 * Changes the slideshow to the next available slide.
 */
var changeSlide = function() {
	if (mediaSettings.autoChange == true){
		clearTimeout(mediaSettings.currentTimeout);
		
		var $nextLink;
		if (jQuery('#playlist li.active').next().size() > 0) {
			$nextLink = jQuery('#playlist li.active').next().find('a');
		} else {
			$nextLink = jQuery('#playlist li:first a');
		}
		
		changeContent($nextLink);
	}
};

/**
 * Loads the content of the clicked playlist item
 */
function changeContent ($clickedItem) {
	// Add active to the list item and remove it from it's siblings
	$clickedItem.parent().addClass('active').siblings().removeClass('active');
	
	var $playlistLink = $clickedItem;
	var clickedHref = $clickedItem.attr('href');
	var splitHref = clickedHref.split('.');
		
	if (splitHref[splitHref.length - 1] === "flv") { // Then its a video
		// Hide the image and show the player
		jQuery('#media-item').children('img').hide();
		jQuery('#media-item').children('a').show().attr('href', clickedHref);
		
		// Change the thumbnail
		var thumbnailSource = $clickedItem.parent().find('span.thumbnail img').attr('src');
		jQuery('#media-item #player img').attr('src', thumbnailSource);
	} else { // Otherwise its an image
		// Hide the player and show the image
		jQuery('#media-item').children('a').hide();
		jQuery('#media-item').children('img').show().attr('src', clickedHref);
	};
	
	// Fire off timeout
	mediaSettings.currentTimeout = setTimeout("changeSlide();", mediaSettings.timeoutSpeed);
	
	// Change the text
	jQuery('#media #caption').fadeOut('normal', function() {
		jQuery('#media #caption').text($playlistLink.siblings('p').text()).fadeIn('normal');
	});
	
	// Fire Google Analytics track
	pageTracker._trackPageview(clickedHref);
}

/**
 * Listens for clicks on the thumbnails, and the next and previous buttons
 */
function controlListener () {
	jQuery('#playlist li a').click(function(event) {
		event.preventDefault();
		changeContent(jQuery(this));
	});
	
	jQuery('#playlist li a, #media #player, #media #next, #media #previous').click(function(event) {
		event.preventDefault();
		mediaSettings.autoChange = false;
		clearTimeout(mediaSettings.currentTimeout);
	});
}

/**
 * Used to load a specific image when passed to the URL
 */
function loadFromUrl (mediaID) {
	var $playlistLink = jQuery('#playlist li a:eq(' + mediaID + ')');
	changeContent($playlistLink);
}


// ================
// = jQuery Ready =
// ================
if (typeof jQuery != 'undefined'){
	jQuery(document).ready(function() {
		jQuery.noConflict();
	
		// Media Player
		loadVideos();
		controlListener();
		scrollPlaylist();
		startPlaylist();
	
		// Lightbox
		jQuery(".lbOn").fancybox({'frameWidth': 640,'frameHeight': 480,'overlayOpacity': 0.7}); 
	});
}