//First and Last LI Selector
//Note: Prototype Driven
function liFirstLast() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child');
	
	firstLIs.each(function(liFirst) {
		liFirst.addClassName('first');
		});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
	
	var paragraphSiblings = $$('#contentMain #contentArticle p + p');
	paragraphSiblings.each(function(paragraphSibling){
		paragraphSibling.addClassName('sibling');
	});
	
	var paragraphSiblings = $$('#contentMain #contentArticle p + p');
	paragraphSiblings.each(function(paragraphSibling){
		paragraphSibling.addClassName('sibling');
	});
	
	var firstHeaders = $$('#contentMain #videoPlayer #flashPlayer + h2');
	firstHeaders.each(function(firstHeader){
		firstHeader.addClassName('first');
	});
}

//Video Helper
//Assists in tracking which video is currently playing,
//and queues up future videos.
//Note: Prototype Driven
function videoHelper() {
	var videos = $$('.videoList li');
	
	var currentVideo = 13;
	var nextVideo = 14;
	
	videos.each(function(video, index){
		video.title = video.down('dt').innerHTML.strip();
		video.description = video.down('dd.description').innerHTML.strip();
		video.author = video.down('dd.author').innerHTML.strip();
		video.length = video.down('dd.length').innerHTML.strip();
		
		video.observe('mouseenter', function(event){
			video.addClassName('hover');
		});
		
		video.observe('mouseleave', function(event){
			video.removeClassName('hover');
		});
		
		video.observe('click', function(event){
			video.fire('fairviewVideos:changeVideo', { indexMove: index });
		});
	});
	
	//Create and Update the Video Panel (e.g. Currently Playing)
	var videoPanel = $('videoPlayer');
	
	var playingCurrent = new Hash();
	playingCurrent.set('title', videoPanel.down('h3'));
	playingCurrent.set('author', videoPanel.down('p.meta'));
	playingCurrent.set('description', videoPanel.down('p.meta').next('p'));
	
	var playingNext = new Hash();
	playingNext.set('title', videoPanel.down('h3').next('h3'));
	playingNext.set('author', videoPanel.down('p.meta').next('p.meta'));
	
	//Observe the Document to update the various portions of the page,
	//as well as pass a function call to Flash
	document.observe('fairviewVideos:changeVideo', function(event){
		videos[currentVideo].removeClassName('current');
		
		currentVideo = event.memo.indexMove;
		videos[currentVideo].addClassName('current');
		// *******************
		//Put Flash Function Call to Actionscript here (e.g. ExternalInterface.addCallback(”JsFunctionName”, AsMethod);... I think
		//alert(currentVideo)
		getFlashMovie("flashVideo").JStoASviaExternalInterface(currentVideo);
		// *******************
		
		playingCurrent.get('title').update(videos[currentVideo].title);
		playingCurrent.get('author').update(videos[currentVideo].author);
		playingCurrent.get('description').update(videos[currentVideo].description);
		
		nextVideo = (videos.size() + ((currentVideo + 1) % videos.size())) % videos.size();

		playingNext.get('title').update(videos[nextVideo].title);
		playingNext.get('author').update(videos[nextVideo].author);
		
		//Scrolling, should the client request it, should be done via Scripty2 or Scriptaculous
		//videoPanel.scrollTo();
	});
		
	//load video player
	
	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
	params.swliveconnect = "true";
	params.allowscriptaccess = "always";
	params.allownetworking = "all";
	var attributes = {};
	attributes.id = "flashVideo";
	swfobject.embedSWF("flash/Fairview VideoPlayer.swf", "flashVideo", "640", "408", "9.0.0", false, flashvars, params, attributes);
	swfobject.addLoadEvent(swfOnloadEvent);
	//getFlashMovie("flashVideo").JStoASviaExternalInterface(currentVideo);

}

//Flash Video Portal
//Creates a portal method for the Flash to interact
//with the page
function videoPortal(videoIndex) {
	document.fire('fairviewVideos:changeVideo', { indexMove: videoIndex });
}

function swfOnloadEvent() {
	//getFlashMovie("flashVideo").JStoASviaExternalInterface(0);
}

// Provides the proper address for the movie depending on browser
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

//Input Clear
//Clears text inputs on a page on focus
//Note: Prototype driven
function inputClear() {
	var textInputs = $$('input[type="text"]');
	
	textInputs.each(function(textInput){
		textInput.initialValue = textInput.value;
		textInput.observe('focus', function(event) {
			if(textInput.value == textInput.initialValue){
				textInput.clear();
			}
		});
		textInput.observe('blur', function(event){
			if(textInput.value.blank() == true) {
				textInput.value = textInput.initialValue;
			}
		});
	});
}

// Cookie Functions
// Set the cookie 
function setCookie(name,value,days) { 
	if (days) { 
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000)); 
		var expires = ";expires="+date.toGMTString(); 
	} else { 
		expires = ""; 
	} 
	document.cookie = name+"="+value+expires+";"; 
}

// Read the cookie 
function readCookie(name) { 
	var needle = name + "="; 
	var cookieArray = document.cookie.split(';'); 
	for(var i=0;i < cookieArray.length;i++) { 
		var pair = cookieArray[i]; 
		while (pair.charAt(0)==' ') { 
			pair = pair.substring(1, pair.length); 
		} 
		if (pair.indexOf(needle) == 0) { 
			return pair.substring(needle.length, pair.length); 
		} 
	} 
	return null; 
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	Cufon.replace('h1');
	Cufon.replace('#header dl dd');
	Cufon.replace('#contentMain h2');
	Cufon.replace('#contentMain #contentRelated p strong');
	videoHelper();
});