
// 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;
			});
		}
	);
}

function updateMultiVideoPlayer(id, el, width, height)
{
	if ($(el))
	{
		$(el).update('<div id="flash_video_internal"></div>');
		
		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.0', '/assets/swf/expressInstall.swf', vars, params, attributes);
		try{swfmacmousewheel.registerObject(attributes.id);}catch(e){}
	}
}

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);