
/**
 *	Calls all initialization methods.
**/
$(document).ready(function()
{
	MediaCenter.Init();
	Gallery.Init();
	Activities.Init();
    Calend.Init();
});



/**
 *	Handles functionality for the media centre page.
**/
var MediaCenter = {
	
	Animating : false,
	Photo_Preview_URL : '/mcentr/getphotopreview.cfm?get=#get#&curNum=#curNum#&lang=#lang#',
	Animation_Speed : 400,
	Margin_Left: "104px",
	
	Init: function()
	{
		$("#photo-previews li.arrow.next a").click(MediaCenter.Handle_Next_Click);
		$("#photo-previews li.arrow.previous a").click(MediaCenter.Handle_Prev_Click);
	},
	
	Handle_Next_Click : function()
	{
		if( !MediaCenter.Animating ) {
			var Item_URL = MediaCenter.GetUrl("next");	// get the next item's ajax URL
			$.get(Item_URL, MediaCenter.Handle_Next_Callback);
		}
		return false;
	},
	
	Handle_Prev_Click : function()
	{
		if( !MediaCenter.Animating ) {
			var Item_URL = MediaCenter.GetUrl("prev");	// get the prev item's ajax URL
			$.get(Item_URL, MediaCenter.Handle_Prev_Callback);
		}
		return false;
	},
	
	Handle_Next_Callback:function(data)
	{
		// add this content to the list
		$("#photo-previews li.photo:last").after(data);
		
		// scroll the list to the left
		MediaCenter.Animating = true;
		$("#photo-previews li.photo-holder ul").animate({ marginLeft: "-=" + MediaCenter.Margin_Left }, MediaCenter.Animation_Speed, "swing", MediaCenter.Remove_First );
	},
	
	Handle_Prev_Callback: function(data)
	{
		// add this content to the list
		$("#photo-previews li.photo-holder ul").animate({ marginLeft: "-=104px"	}, 0 );
		
		$("#photo-previews li.photo:first").before(data);
		
		// scroll the list to the right
		MediaCenter.Animating = true;
		$("#photo-previews li.photo-holder ul").animate({ marginLeft: "+=104px"	}, MediaCenter.Animation_Speed, "swing", MediaCenter.Remove_Last );
	},
	
	Remove_First: function() {
		$("#photo-previews li.photo:first").remove();
		$("#photo-previews li.photo-holder ul").animate({ marginLeft: "+=104px"	}, 0 );
		MediaCenter.Animating = false;
	},
	
	Remove_Last:function() {
		$("#photo-previews li.photo:last").remove();
		MediaCenter.Animating = false;
	},
	
	GetCurrentNum : function()
	{
		return $("#photo-previews li.photo:last").attr("id").split("photo-").join('');
	},
	
	GetLang : function()
	{
		return $("meta[name$='language']").attr("content");
	},
	
	GetUrl : function(get)
	{
		var curNum = MediaCenter.GetCurrentNum();
		var lang = MediaCenter.GetLang();
		return MediaCenter.Photo_Preview_URL.split("#get#").join(get).split("#curNum#").join(curNum).split("#lang#").join(lang);
	}
	
}	//	MediaCenter
	


/**
 *	Handles functionality for the Various gallery pages.
**/
var Gallery = {
	
	Init:function()
	{
		$('#gallery-photos li a').click(Gallery.HandleThumbClick);
		$('#gallery-photos li a:first').click();
	},
	
	HandleThumbClick : function()
	{
		//	Get the values
		var title = $(this).children('img').attr('title');
		var alt = $(this).children('img').attr('alt');		
		var src = $(this).attr('href');
		var hires = $(this).next().attr('href');
		
		//	Set the values
		Gallery.GetMainImage().attr('src', src);
		Gallery.GetMainImage().attr('alt', alt);
		Gallery.GetMainImage().attr('title', alt);
		Gallery.GetMainCaption().text(title);
		
		if (hires == '' || !hires) {
			Gallery.GetHighResLink().css("display", "none");
		}
		else {
			Gallery.GetHighResLink().attr('href', hires);
			Gallery.GetHighResLink().css("display", "inline");
		}
		
		return false;
	},
	
	GetMainImageContainer : function()
	{
		return $('#gallery-photo-large');
	},
	
	GetMainImage : function()
	{
		var container = Gallery.GetMainImageContainer();
		
		//	Create the image if it doesn't exist.
		if($(container).children('img').length == 0)
		{
			$(container).prepend('<img />');
		}
		
		return container.children('img');
	},
	
	
	GetMainCaption : function()
	{
		return Gallery.GetMainImageContainer().children('p.caption');
	},
	
	GetHighResLink : function()
	{
		return Gallery.GetMainImageContainer().children('p.download_link').children('a');
	}
	
	
}	//	Gallery


/**
 *	Activities page
**/
var Activities = 
{
	Init : function()
	{
		Activities.GetList().addClass("activities-js");
		Activities.HideDetails();
		Activities.ActivateLinks();
		
		Activities.GetList().find('li>img').each(function(i){
			$(this).attr('rel','dialog-' + i);
			$(this).next('div.details').attr('id','dialog-' + i);
		});
		Activities.GetList().find('li div.details').each(function() {
			var Dialog_Title = $(this).find('h4').text();
			$(this).dialog({ autoOpen: false, width: 400, title: Dialog_Title });
		});
	},
	
	GetList : function()
	{
		return $('ol#activities');
	},
	
	HideDetails : function()
	{
		$('div.details').dialog('close');
	},
	
	ActivateLinks : function()
	{
		Activities.GetList().find('li>img').click(function(){
			Activities.HideDetails();
			var Target_ID = $(this).attr('rel');
			
			
			// figure out the left and right position of the body area
			var Body_Object = $('div.colLayout>div.center');
			Body_Left = Body_Object.offset().left;
			Body_Right = Body_Left + Body_Object.width();
			Body_Middle = ((Body_Right - Body_Left) / 2) + Body_Left;
			
			// get the current clicked image's position
			var Location = $(this).offset();
			var Width = $(this).width();
			var Height = $(this).height();
			
			// dialog width
			var Dialog_Width = $('#' + Target_ID).dialog('option', 'width');
			
			if (Location.left > Body_Middle) {
				Location.left = (Location.left + Width) - Dialog_Width;
			}
			
			/*alert('Location top: ' + Location.top);
			alert('Scroll top: ' + $('html').scrollTop() );*/
							
			$('#' + Target_ID).dialog('option', 'position', [Location.left, (Location.top + Height + 5 - $('html').scrollTop())]);
			
			
			$('#' + Target_ID).dialog('open');
		});
	}
	
}	//	Activities

/**
 *	Calendar page
**/
var Calend = 
{
	Init : function()
	{
		Calend.GetList().addClass("calend-js");
		Calend.HideDetails();
		Calend.ActivateLinks();
		
		Calend.GetList().find('li>img').each(function(i){
			$(this).attr('rel','dialog-' + i);
			$(this).next('div.details').attr('id','dialog-' + i);
		});
		Calend.GetList().find('li div.details').each(function() {
			var Dialog_Title = $(this).find('h4').text();
			$(this).dialog({ autoOpen: false, width: 400, title: Dialog_Title });
		});
	},
	
	GetList : function()
	{
		return $('ol#calend');
	},
	
	HideDetails : function()
	{
		$('div.details').dialog('close');
	},
	
	ActivateLinks : function()
	{
		Calend.GetList().find('li>img').click(function(){
			Calend.HideDetails();
			var Target_ID = $(this).attr('rel');
			
			
			// figure out the left and right position of the body area
			var Body_Object = $('div.colLayout>div.center');
			Body_Left = Body_Object.offset().left;
			Body_Right = Body_Left + Body_Object.width();
			Body_Middle = ((Body_Right - Body_Left) / 2) + Body_Left;
			
			// get the current clicked image's position
			var Location = $(this).offset();
			var Width = $(this).width();
			var Height = $(this).height();
			
			// dialog width
			var Dialog_Width = $('#' + Target_ID).dialog('option', 'width');
			
			if (Location.left > Body_Middle) {
				Location.left = (Location.left + Width) - Dialog_Width;
			}
			
			/*alert('Location top: ' + Location.top);
			alert('Scroll top: ' + $('html').scrollTop() );*/
							
			$('#' + Target_ID).dialog('option', 'position', [Location.left, (Location.top + Height + 5 - $('html').scrollTop())]);
			
			
			$('#' + Target_ID).dialog('open');
		});
	}
	
}	//	Calendar





