var PREVIEW_TIME = 5000; // 5s
var SIZE = 730;

var timer = null;
var scroll_enabled = false;
var animating = false;
var direction = true; // true = next, false = prev
var is_main = !location.href.match(/\/(goodies|portfolio)/);

$(window).load(function()
{
	$('img[src="images/spinner.gif"]').each(function(i, e)
	{
		$(e).attr('src', $(e).attr('title'));
		$(e).attr('title', '');
	});
});

$(function()
{
	// Deal with the anchor target
	var anchor = location.href.indexOf('#');
	if (anchor != -1)
	{
		var name = location.href.substring(anchor + 1);
		$('#slider .preview').each(function(i, e)
		{
			var ele = $(e);
			if (ele.hasClass(name))
			{
				$('#slider').css('marginLeft', i * -SIZE);
				if (location.href.match(/\/(goodies|portfolio)/))
				{
					$('#slider .selected').add('#thumbnails .selected').removeClass('selected');
					$($('#slider .slide')[i]).add($('#thumbnails li.thumb')[i]).addClass('selected');
				}
				else
				{
					$('#indicators .selected').removeClass('selected');
					$($('#indicators a')[i]).addClass('selected');
				}
			}
		});
	}
	
	// Keyboard scrolling
	$(document).keydown(function(e)
	{
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code != 39 && code != 37)
			return;
		
		ScrollNavigate(code == 39);
	});
	
	// Form submission
	$('#contact-form').submit(function()
	{
		// Hide any message
		$('span#contact-msg').css('opacity', 0);
		
		// Build data string
		var fields = [ 'name', 'email', 'message' ];
		var data = 'ajax=1';
		for (var i = 0; i < fields.length; i++)
			data += '&' + fields[i] + '=' + escape($('#' + fields[i]).val());
			

		// Submit
		$.ajax( { 	url : $('#contact-form').attr('action'),
					dataType: 'json',
					data: data,
					type: 'POST',
					success: function(data)
					{
						if (data.sent == true)
						{
							$('span#contact-msg').html('Thanks for the message. I will get back to you as soon as possible.');
							$('#send').css('backgroundPosition', '-207px -168px');
							$('#send').attr('disabled', 'disabled');
						}
						else
							$('span#contact-msg').html(data.error);
						
						$('span#contact-msg').animate( { opacity: 1 }, 400);
					}
				});
		
		return false;
	});
	
	if (location.href.match(/\/(goodies|portfolio)/))
	{
		// Goodies / portfolio share javascript...
		var sld = false;
		var thm = false;
		// Popup animation
		$('#slider-wrapper').mouseenter(function(e) { if (!sld) {sld=true;$('#slider').find('div.download').animate( { 'height' : 78, 'top' : 356 }, 500, 'swing'); };});
		$('#slider-wrapper').mouseleave(function(e) { if (sld) {sld=false;$('#slider').find('div.download').animate( { 'height' : 0, 'top' : 434 }, 500, 'swing'); };});
		
		// Thumb darkening effect
		$('a.thumb').mouseenter(function(e) {if (!thm) {thm=true;$(this).find('span.overlay').fadeIn(250); };});
		$('a.thumb').mouseleave(function(e) { if (thm) {thm=false;$(this).find('span.overlay').fadeOut(250); };});
		// Thumbnail navigation
		$('#thumbnails a').click(function(e)
		{
			var index = FindElementPosition($('#thumbnails a'), this);
			if (index == -1) { animating = false; return false; }
		
			// No point in animating
			$('#slider').css( { 'marginLeft' : index * -SIZE } );
			
			// Fade in the image
			$('.slide-border').add($('#slider > .slide')[index]).css('opacity', 0).animate( { opacity: 1 }, 1000);
			
			// Select the image
			$('#slider .selected').add('#thumbnails .selected').removeClass('selected');
			$($('#slider .slide')[index]).add($('#thumbnails li.thumb')[index]).addClass('selected');
			
			return false;
		});
	}
	else
	{
		// Main page javascript
		
		// Auto scrolling
		StartScrolling();
		$('#slider-wrapper').mouseleave(StartScrolling);
		$('#slider-wrapper').mouseenter(StopScrolling);
	
		// Page indicator navigation
		$('#indicators a').click(function(e)
		{
			if (animating)
				return false;
			
			animating = true;
	
			var restart_scroll = scroll_enabled;
			StopScrolling();
			scroll_enabled = restart_scroll;
		
			var index = FindElementPosition($('#indicators a'), this);
			if (index == -1) { animating = false; return false; }
		
			// No point in animating?
			if ((index * -SIZE) + 'px' == $('#slider').css('marginLeft'))
			{
				animating = false;
				return false;
			}
			
			var selected = $(this);
			$('#slider').animate( { 'marginLeft' : index * -SIZE }, 750, 'swing', function ()
			{
				$('#indicators .selected').removeClass('selected');
				selected.addClass('selected');		
				
				// Reset timeout
				if (scroll_enabled == true)
					timer = setTimeout("Scroll" + (direction ? "Next" : "Prev") + "Preview()", PREVIEW_TIME);
				
				animating = false;
			});
			return false;
		});
	}
});

function StartScrolling()
{
	if ($('#slider > .slide').size() > 1 && scroll_enabled == false)
	{
		scroll_enabled = true;
		timer = setTimeout("Scroll" + (direction ? "Next" : "Prev") + "Preview()", PREVIEW_TIME);
	}
}

function StopScrolling()
{
	if (timer != null)
		clearTimeout(timer);
	scroll_enabled = false;
}

function ScrollNextPreview()
{
	animating = true;
	
	var animate_to = '-='+SIZE+'px';
	
	// Are we at the end?
	if ($('#slider').css('marginLeft') == (($('#slider > .slide').size() - 1) * -SIZE) + 'px')
		animate_to = '0px';
		
	$('#slider').animate( { 'marginLeft' : animate_to }, 750, 'swing', function ()
	{
		// Index value...
		var links = $(is_main ? '#indicators a' : '#thumbnails li.thumb');
		var index = FindElementPosition(links, $('#' + (is_main ? 'indicators' : 'thumbnails') + ' .selected')[0]);
		if (index == -1) { animating = false; return; }
		
		// Remove the selection now
		$('#' + (is_main ? 'indicators' : 'thumbnails') + ' .selected').removeClass('selected');
		$('#slider .selected').removeClass('selected');
		
		// Add it to the next one now
		if (index + 1 < $('#slider > .slide').size())
		{
			if (!is_main)
				$($('#slider .slide')[index + 1]).addClass('selected');
			$(links[index + 1]).addClass('selected');
		}
		else
		{
			if (!is_main)
				$($('#slider .slide')[0]).addClass('selected');
			$(links[0]).addClass('selected');
		}
		
		// Reset timeout
		if (scroll_enabled == true)
			timer = setTimeout("ScrollNextPreview()", PREVIEW_TIME);
		
		animating = false;
	});
}

function ScrollPrevPreview()
{
	animating = true;
	
	var animate_to = '+='+SIZE+'px';
	
	// At the start?
	if ($('#slider').css('marginLeft') == '0px')
		animate_to = (($('#slider > .slide').size() - 1) * -SIZE) + 'px';
	
	$('#slider').animate( { 'marginLeft' : animate_to }, 750, 'swing', function ()
	{
		// Index value...
		var links = $(is_main ? '#indicators a' : '#thumbnails li.thumb');
		var index = FindElementPosition(links, $('#' + (is_main ? 'indicators' : 'thumbnails') + ' .selected')[0]);
		if (index == -1) { animating = false; return; }
		
		// Remove the selection now
		$('#' + (is_main ? 'indicators' : 'thumbnails') + ' .selected').removeClass('selected');
		$('#slider .selected').removeClass('selected');
		
		// Add it to the next one now
		if (index - 1 < 0)
		{
			if (!is_main)
				$($('#slider .slide')[$('#slider > .slide').size() - 1]).addClass('selected');
			$(links[$('#slider > .slide').size() - 1]).addClass('selected');
		}
		else
		{
			if (!is_main)
				$($('#slider .slide')[index-1]).addClass('selected');
			$(links[index - 1]).addClass('selected');
		}
		
		// Reset timeout
		if (scroll_enabled == true)
			timer = setTimeout("ScrollPrevPreview()", PREVIEW_TIME);

		animating = false;
	});
}

function ScrollNavigate(next)
{
	if (animating)
		return;
		
	direction = next;

	var restart_scroll = scroll_enabled;
	StopScrolling();
	scroll_enabled = restart_scroll;
	
	if (next)
		ScrollNextPreview();
	else
		ScrollPrevPreview();
}

function FindElementPosition(items, element)
{
	var _i = -1;
	$(items).each(function(i, e)
	{
		if (e == element)
			_i = i;
	});
	
	return _i;
}
