// Store the mousemove event data
$().mousemove(function(e) { window.mouse = e; }); 

// Util function to check if the mouse cursor is over the bounding box of a given element
function isOverElement(element)
{
	e = window.mouse;
	var top = element.offset().top;
	var left = element.offset().left;
	var bottom = top + element.outerHeight();
	var right = left + element.outerWidth();
	return (e.pageX >= left && e.pageX <= right && e.pageY >= top && e.pageY <= bottom)
}



/* Add as many images down the side as possible */
function sideImages()
{
	// 	<a href="img/photo.jpg" style="background-image: url('img/thumb.jpg');" rel="lightbox-gallery"></a>
	$('#gallery_wrapper').css('height', ($('#content_wrapper').outerHeight()) + 'px');
	
	// Stores the element that raised the overlay
	var clicked = null;
	
	// If the overlay is active and the mouse is no longer over the
	// element that raised the overlay, close the overlay
	function closeOverlay(e)
	{
		if (clicked && ( ! isOverElement(clicked) || isOverElement($('#menu')) || isOverElement($('#footer'))) )
		{
			// Pretend to click the close button
			$('#lbCloseLink').click();
			clicked = null;
			return true;
		}
		return false;	
	}
	
	// Catch any cases where the mouse moves off the overlay without causing it to close
	function overlayTimeout()
	{
		if ( ! closeOverlay(window._e))
		{
			setTimeout(overlayTimeout, 1000);
		}
	}
	
	// Opens the overlay my pretending to click the SlimBox element
	function openOverlay(element)
	{
		if (clicked == null || clicked.offset().top != element.offset().top)
		{
			clicked = element;
			element.click();
			setTimeout(overlayTimeout, 1000);
		}
	}
		
	// Check to see if the mouse has moved off the triggering element
	$('#lbImage').mousemove(function(e) { closeOverlay(e); });
	
	for (var i=1; i<=20; i++)
	{
		var img = $('<a href="img/gallery/full/' + i + '.jpg" style="background-image: url(\'img/gallery/thumb/' + i + '.jpg\');"></a>');
		
		img.slimbox({'imageFadeDuration': 200, 'resizeDuration': 200});
		
		// Trigger the overlay on moving the mouse over the element
		img.mousemove(function() { openOverlay($(this)); });
		// Close the overlay if the mouse leaves the element
		img.mouseleave(function(e) { closeOverlay(e); });
		
		$('#gallery').append(img);
	}
	
	//$('#menu').append('<div id="gallery_scroll"><li class="gallery_scroll_text">Gallery</li><li class="gallery_scroll" id="gallery_scroll_up">Up</li><li class="gallery_scroll_text">/</li><li class="gallery_scroll" id="gallery_scroll_down">Dn</li></div>');
	
	var scrollSpeed = 500; // px/sec
	// Hook up the scroll up/down buttons
	$('#fade_bottom').mouseenter(function() {
		var top = -64 - ($('#gallery').children().length*164) + $('#gallery_wrapper').innerHeight();
		var cur = parseInt($('#gallery').css('marginTop'));
		if (top != cur)
		{
			$('#fade_bottom').addClass('active');
			var duration = (cur - top) * 1000 / scrollSpeed;
			$('#gallery').stop().animate({'marginTop': top + 'px'}, duration, '', function() { $('#fade_bottom').removeClass('active') });
		}
	});
	$('#fade_top').mouseenter(function() {
		var cur = parseInt($('#gallery').css('marginTop'));
		var duration = (-64 - cur) * 1000 / scrollSpeed;
		if (cur != 0)
		{
			$('#fade_top').addClass('active');
			$('#gallery').stop().animate({'marginTop': 0}, duration, '', function() { $('#fade_top').removeClass('active') });
		}
	});

	// Kill the scrolling
	$('#fade_top, #fade_bottom').mouseleave(function() {
		$('#fade_top').removeClass('active');
		$('#fade_bottom').removeClass('active');
		$('#gallery').stop();
	});
	
	// Scroll to the bottom initially
	var top = -64 - ($('#gallery').children().length*164) + $('#gallery_wrapper').innerHeight();
	$('#gallery').css({'marginTop': top + 'px'});
	$('#gallery').animate({'marginTop': 0}, 1000);
}

/* Add the 'first' class to the first child in the content block */
/* and the 'last' class to the last child in the content block */
function contentFirstLast()
{	
	$('#content div > :first-child').addClass('first');
	$('#content div > :last-child').addClass('last');
}

/* Add the deprecated target attribute to external links */
function externalLinks()
{
	$('a[class="external"]').attr('target', '_blank').attr('title', 'Click to open external link in new window');
}

/* Add input type to all form elements */
function inputTypes()
{
	$('input').each(function() {
		$(this).addClass('input_' + $(this).attr('type'));
	});
}


/* Show/hide the collapsible block */
function toggleCollapsible(object)
{
	object.slideToggle('slow');
}

/* Add the show more botton to collapsible blocks */
function initCollapsible()
{
	$('div.collapsible').each(function () {
		var panel = $(this);
		panel.wrap('<div></div>');
		var lessLink = $('<a href="#">Show less...</a>').wrap('<p></p>');
		var moreLink = $('<a href="#">Show more...</a>').wrap('<p></p>');
		lessLink.click(function () {
			moreLink.click();
			return false;
		});
		moreLink.toggle(
			function () {
				toggleCollapsible(panel);
				moreLink.text('Show less...');
				return false;
			},
			function ()
			{		
				toggleCollapsible(panel);
				moreLink.text('Show more...');
				return false;
			}
		);
		panel.append(lessLink.parent());
		panel.before(moreLink.parent());
		panel.hide();
	});
	
//	$('a.more').click(function () {
//		toggleCourseDetails($(this).attr('rel'));
//		return false;
//	});
//	$('a.more').each(function () {
//		$('#' + $(this).attr('rel')).hide();
//	});
}

function fixLIHover()
{
	$('#menu li').each(function() {
		$(this).mouseenter(function() { $(this).addClass('hover'); });
		$(this).mouseleave(function() { $(this).removeClass('hover'); });
	});
}



$(function() {



	externalLinks();
	contentFirstLast();
	inputTypes();
	
	initCollapsible();
	fixLIHover();
	
	if ( ! ADMIN)
	{
		// Wait until all images have loaded
		$(window).load(function() {	sideImages(); });
	}
	
});


