
var M2 = (function(){
	return {
		getYear: function()
		{
			var theDate = new Date();
			var theYear = theDate.getYear();
			if (theYear < 1900) {
				theYear += 1900;
			}
			document.write(theYear);
		},
		
		assignEventHandlers: function()
		{
			// Menu
			$('#menu a').each(function() {
				var img		= $(this).find('img');
				var offSrc	= img.attr('src');
				var onSrc	= img.attr('srcover');

				$(this).hover(
					function() {
						img.attr('src', onSrc);
					},
					function() {
						img.attr('src', offSrc);
					}
				);
			});

			// Gallery
			if ($('.gallery-link').size()) {
				$('#gallery-images a').fancybox({
					'titlePosition':	 'over',
					'height': 			 '75%',
					'width': 			 '75%',
					'overlayOpacity':	 0.8,
					'overlayColor':		 '#000',
					'showThumbnails':	 true,
					'thumbnailMoreText': 'VIEW MORE',
					'thumbnailLessText': 'VIEW PREVIOUS',
					'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
						return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
					}
				});

				$('.gallery-link').click(function() {
					$('#gallery-images a[rel="workGallery' + $(this).attr('rel') +  '"]').eq(0).click();
				});
			}
			
			// Video links
			if ($('.video-link').size()) {
				$('.video-link').click(function() {
					$.fancybox({
						'type': 			'iframe',
						'href': 			this.href.replace(new RegExp('watch\\?v=', 'i'), 'embed/') + '?rel=0&autoplay=1',
						'overlayShow': 		true,
						'centerOnScroll': 	true,
						'speedIn': 			100,
						'speedOut': 		50,
						'width': 			640,
						'height': 			480
					});
					
					return false;
				});
			}
		},
		
		defaultInputText: function()
		{
			$('.defaultText')
				.live('focus', function() {
					$(this).removeClass("idleField").addClass("focusField");

					if ($(this).val() == $(this).data('tooltip')) {
						$(this).val("");
					}

					if ($(this).val() != $(this).data('tooltip')) {
						$(this).addClass("focusField").select();
					}
				})

				.live('blur', function() {
					if ($(this).val() == "") {
						$(this).removeClass("focusField").addClass("idleField").val($(this).data('tooltip'));
					}
				})

				.focus().blur();
		},
	
		compactTables: function()
		{
			if ($('.tableSection').size()) {
				$('.tableSection').addClass('closed');

				$('.tableSection h3 a').click(function() {
					$(this).closest('.tableSection').toggleClass('closed');
				}).show();
				
				// Check for a direct link. If we have one, open the folder
				if (window.location.hash) {
					$(window.location.hash).find('h3 a').click();
				}
			}
		},
	
		archiveTables: function()
		{
			if ($('.tableSection .archive-trigger').size()) {
				$('.archive-trigger a').click(function() {
					var t = $(this);
					var p = $(this).closest('tr');

					if (p.hasClass('inactive')) {
						p.closest('table').find('tr.archive').removeClass('closed');
						t.html('Hide archived documents');
						p.removeClass('inactive');
					} else {
						p.closest('table').find('tr.archive').addClass('closed');
						t.html('Show archived documents');
						p.addClass('inactive');
					}
				});
			}
		}
	};
}());

$(document).ready(function() {
	M2.assignEventHandlers();
	M2.compactTables();
	M2.archiveTables();
	M2.defaultInputText();
});
