
// Intialize all the scripts needed on load
function initSite()
{
	initNewsArchiveLinks();
	initPubLinks();
	initSwapVideoLinks();
	initSwapMultiVideoLinks();
	initEmailLinks();
	initPrintLinks();
	initModalClose();
	initRemoteLinks();
	initImageLinks();
}

function getTop()
{
	var temp = window;

	while (temp != temp.parent)
	{
		temp = temp.parent
	}

	return temp;
}

function initImageLinks()
{
	$$('#the_content img').each
	(
		function(item)
		{
			var a = item.up();

			if (a.tagName.toLowerCase() == 'a')
			{
				a.addClassName('no_border');
			}
		}
	);
}

function initRemoteLinks()
{
	$$('a').each
	(
		function(item)
		{
			var url = item.href;
			var domain = document.domain;

			if (url.indexOf('http://') == 0 && url.indexOf('http://' + domain) == -1)
			{
				item.target = '_blank';
			}

			else if (url.indexOf('https://') == 0 && url.indexOf('https://' + domain) == -1)
			{
				item.target = '_blank';
			}
		}
	);

	if ($('submit_event_link') && $('guest_submit_form'))
	{
		var link = $('submit_event_link');

		Event.observe(link, 'click', function(e)
		{
			e.stop();

			$('guest_submit_form').submit();

			return false;
		});
	}
}

function initModalClose()
{
	$$('a.modal_close').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var top = getTop();

				top.Lightview.hide();

				return false;
			});
		}
	);
}

function initPrintLinks()
{
	$$('a.print_modal').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var tempField = Event.findElement(e, 'A');
				var url = window.location.href;

				url = url.split('#');
				url = url[0];

				if (url.indexOf('?') > 0)
				{
					url = url + '&print_page=1';
				}

				else
				{
					url = url + '?print_page=1';
				}


				Lightview.show
				({
					href:		url,
					rel: 		'iframe',
					title:		'Print Page',
					caption:	'Print view of the currently selected page.',
					options:
					{
						width: 765,
						height: 400,
						autosize:	false,
						topclose:	false,
						scrolling:	'auto'
					}
				});
			});
		}
	);
}

function initEmailLinks()
{
	$$('a.email_modal').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var tempField = Event.findElement(e, 'A');
				var url = window.location.href;

				url = url.split('#');
				url = url[0];

				Lightview.show
				({
					href:		'/pages/share/?share_url=' + url,
					rel: 		'iframe',
					title:		'',
					caption:	'',
					options:
					{
						width: 525,
						height: 475,
						autosize:	false,
						topclose:	false
					}
				});
			});
		}
	);
}

function initSwapMultiVideoLinks()
{
	$$('a.swap_multi_video').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var tempField = Event.findElement(e, 'A');

				updateMultiVideoPlayer(tempField.rel, 'multi_video_player_flash', 396, 223);

				return false;
			});
		}
	);

	$$('a.swap_multi_video_narrow').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var tempField = Event.findElement(e, 'A');

				updateMultiVideoPlayer(tempField.rel, 'multi_video_player_flash_narrow', 348, 196);

				return false;
			});
		}
	);

	initQTVids();
}

function updateMultiVideoPlayer(id, el, width, height)
{
	if ($(el))
	{
		$(el).update('<div id="flash_video_internal"></div>');

		// If Flash exists
		if (swfobject.hasFlashPlayerVersion('9.0.115'))
		{
			var vars =
			{
				autoplay:'false',
				config:'/videos/xml/' + id + '/0/'
			};

			var params = {scale:'noScale', salign:'lt', menu:'false', allowScriptAccess:'always'};
			var attributes = {id:'flaswf', name:'flaswf', bgcolor:'#262626', wmode:'transparent'};

			swfobject.embedSWF('/assets/swf/player.swf', 'flash_video_internal', width, height, '9.0.115', '', vars, params, attributes);
			try{swfmacmousewheel.registerObject(attributes.id);}catch(e){}
		}

		// Otherwise if QuickTime exists
		else if (_QTDetect())
		{
			if (width == 396)
			{
				JQ('#flash_video_internal').replaceWith(JQ('<div id="qt_video_full"><img src="/assets/img/videos/screenshot/' + id + '.jpg" /><a href="/assets/video/' + id + '.mov" title="Play">Play</a></div>'));
			}

			else if (width == 417)
			{
				JQ('#flash_video_internal').replaceWith(JQ('<div id="qt_video_multimedia"><img src="/assets/img/videos/screenshot/' + id + '.jpg" /><a href="/assets/video/' + id + '.mov" title="Play">Play</a></div>'));
			}

			else
			{
				JQ('#flash_video_internal').replaceWith(JQ('<div id="qt_video_small"><img src="/assets/img/videos/screenshot/' + id + '.jpg" /><a href="/assets/video/' + id + '.mov" title="Play">Play</a></div>'));
			}

			initQTVids();
		}
	}
}

function initQTVids()
{
	// Remove any preexisting click functions.
	JQ('#qt_video_full a, #qt_video_small a, #qt_video_multimedia a, #qt_video_display a').unbind('click');

	// Link to play video
	JQ('#qt_video_full a, #qt_video_small a, #qt_video_multimedia a, #qt_video_display a').click(playQTVideo);
}

function playQTVideo(e)
{
	e.stopPropagation();

	var vidURL = 'http://' + document.domain + JQ(this).attr('href'); // /assets/video/sample.m4v

	if (JQ(this).parents('#qt_video_full').length > 0)
	{
		var player = QT_GenerateOBJECTText_XHTML(vidURL, '396', '223', '', 'BGCOLOR', '#262626', 'CONTROLLER', 'true', 'AUTOPLAY', 'true', 'LOOP', 'false', 'ENABLEJAVASCRIPT', 'true', 'SCALE', 'ToFit', 'PLUGIN', 'quicktimeplugin', 'TYPE', 'video/quicktime', 'WMODE', 'transparent', 'QTSRC', vidURL);
		JQ('#qt_video_full').replaceWith(JQ(player));
	}

	else if (JQ(this).parents('#qt_video_multimedia').length > 0)
	{
		var player = QT_GenerateOBJECTText_XHTML(vidURL, '417', '235', '', 'BGCOLOR', '#262626', 'CONTROLLER', 'true', 'AUTOPLAY', 'true', 'LOOP', 'false', 'ENABLEJAVASCRIPT', 'true', 'SCALE', 'ToFit', 'PLUGIN', 'quicktimeplugin', 'TYPE', 'video/quicktime', 'WMODE', 'transparent', 'QTSRC', vidURL);
		JQ('#qt_video_multimedia').replaceWith(JQ(player));
	}

	else if (JQ(this).parents('#qt_video_display').length > 0)
	{
		var player = QT_GenerateOBJECTText_XHTML(vidURL, '646', '363', '', 'BGCOLOR', '#262626', 'CONTROLLER', 'true', 'AUTOPLAY', 'true', 'LOOP', 'false', 'ENABLEJAVASCRIPT', 'true', 'SCALE', 'ToFit', 'PLUGIN', 'quicktimeplugin', 'TYPE', 'video/quicktime', 'WMODE', 'transparent', 'QTSRC', vidURL);
		JQ('#qt_video_display').replaceWith(JQ(player));
	}

	else
	{
		var player = QT_GenerateOBJECTText_XHTML(vidURL, '348', '196', '', 'BGCOLOR', '#262626', 'CONTROLLER', 'true', 'AUTOPLAY', 'true', 'LOOP', 'false', 'ENABLEJAVASCRIPT', 'true', 'SCALE', 'ToFit', 'PLUGIN', 'quicktimeplugin', 'TYPE', 'video/quicktime', 'WMODE', 'transparent', 'QTSRC', vidURL);
		JQ('#qt_video_small').replaceWith(JQ(player));
	}

	return false;
}

function initSwapVideoLinks()
{
	if ($('multimedia'))
	{
		$$('a.swap_video').each
		(
			function(item)
			{
				Event.observe(item, 'click', function(e)
				{
					e.stop();

					var tempField = Event.findElement(e, 'A');

					var videoList = $$('div.multimedia_video_info');

					if (videoList.length > 0)
					{
						for (var i = 0; i < videoList.length; i++)
						{
							if (videoList[i].id == 'video_info_' + tempField.rel)
							{
								videoList[i].show();
								updateMultiVideoPlayer(tempField.rel, 'multimedia_video', 417, 235);
							}

							else
							{
								videoList[i].hide();
							}
						}
					}
				});
			}
		);
	}
}

// Add the show / hide functionality for the news arvhive links
function initNewsArchiveLinks()
{
	$$('a.archive_year').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var tempField = Event.findElement(e, 'A');
				var year = tempField.rel;

				// Hide all the month lists
				$$('ul.archive_month').each
				(
					function(item)
					{
						item.hide();
					}
				);

				// Show the month list for the selected year
				if ($('archive_months_' + year))
				{
					$('archive_months_' + year).show();
				}
			});
		}
	);
}

function initPubLinks()
{
	$$('a.tb_publish').each
	(
		function(item)
		{
			Event.observe(item, 'click', function(e)
			{
				e.stop();

				var tempField = Event.findElement(e, 'A');

				Lightview.show
				({
					href:		tempField.href,
					rel: 		'iframe',
					title:		'Page Revision Publishing',
					caption:	'',
					options:
					{
						width: 678,
						height: 353,
						autosize:	false,
						topclose:	false
					}
				});
			});
		}
	);
}

function CheckIsIE()
{
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
	{
		return true;
	}

	else
	{
		return false;
	}
}

function initWindowLoad()
{
	var maps = $$('div.google_map');

	if (maps.length > 0)
	{
		initMap();
	}

	if ($('print_container'))
	{
		// Disable all links in the print page
		$$('a').each
		(
			function(item)
			{
				Event.observe(item, 'click', function(e)
				{
					e.stop();
					return false;
				});
			}
		);

		if (CheckIsIE())
		{
			var top = getTop();

			top.document.frames['lightviewContent'].focus();
			top.document.frames['lightviewContent'].print();
		}

		else
		{
			window.print();
		}
	}
}

// Initialize all the scripts
document.observe('dom:loaded', initSite);

// Window Load Events
Event.observe(window, 'load', initWindowLoad);
