﻿/// <reference path="jquery-1.4.2-vsdoc.js"/>

Cufon.replace('#sketchbook .verso p', { fontFamily: 'Spectre Verde BB' });
Cufon.replace('.kitchen div.login p', { fontFamily: 'Spectre Verde BB' });

// IE6 'shim' to enable ABBR element support
document.createElement("abbr");

// document ready event
$(document).ready(function ()
{
	// let CSS know javascript is present
	$('#page').removeClass('no-js').addClass('has-js');

	// dock
	$('#dock').rumadDock();

	// avatar list
	$('.avatar-list label').click(function (e)
	{
		$('#' + $(this).addClass('selected').siblings('label').removeClass('selected').attr('for')).attr('checked', 'checked').attr('checked');
	})

	// sketchbook
	if (get_cookie("sketchbookX") && get_cookie("sketchbookY"))
	{
		$('#sketchbook')
		.css({
			left: Math.min(get_cookie("sketchbookX"), $('#screen').width() - $('#sketchbook').width() / 2) + 'px',
			top: Math.min(get_cookie("sketchbookY"), $('#screen').height() - $('#sketchbook').height() / 2) + 'px'
		});
	}

	$('#sketchbook')
	.draggable({
		stop: function (e, ui)
		{
			set_cookie("sketchbookX", ui.position.left, 100);
			set_cookie("sketchbookY", ui.position.top, 100);
		},
		containment: '#screen'
	});

	// scrollpane
	$('.scrollpane').jScrollPane({ scrollbarWidth: 5, scrollbarMargin: 0, dragMinHeight: 10, dragMaxHeight: 50, reinitialiseOnImageLoad: true });

	// colorbox
	$("#sketchbook a.lightbox-videos").each(function (i)
	{
		$(this).click(function (e)
		{
			e.preventDefault();

			$.colorbox({ href: $(this).attr('href') + 'js/', slideshow: true, width: '436px', height: '302px' });
		});

		//$(this).attr('href', $(this).attr('href') + 'js/').colorbox({ slideshow: true, maxWidth: '80%', maxHeight: '80%' });
	});
	$("#sketchbook a.lightbox-pictures").colorbox({ slideshow: true, maxWidth: '80%', maxHeight: '80%' });
	$(".gallery-section a.lightbox-pictures").colorbox({ slideshow: true, maxWidth: '80%', maxHeight: '80%' });

	// biglink
	$('.file-list .biglink').ayoBigLink('a[href]:last');
	$('.video-list .biglink').ayoBigLink();

	// flash
	/*$('.video').flash(
	{
		swf: $('.video param[name=movie]').attr('value') || $('.video embed').attr('src'),
		wmode: 'opaque',
		allowFullScreen: true
	});*/

	// animations
	// sprite shed/rabbit eyes
	$('.sprite-shed-eyes, .sprite-rabbit-eyes').ayoSpriteAnimation(function (frame)
	{
		if (frame % 2)
		{
			// eyes closed length
			return 200;
		}
		else
		{
			// eyes open length
			return 4000 + (2000 - Math.random() * 4000);
		}
	});

	// sprite van eyes
	$('.sprite-van-eyes').ayoSpriteAnimation(function (frame)
	{
		if (frame % 4 == 0)
		{
			// eyes open length
			return 5000 + (1000 - Math.random() * 2000);
		}
		else if (frame % 4 == 1)
		{
			// eyes closed length
			return 100;
		}
		else if (frame % 4 == 2)
		{
			// eyes open length
			return 100;
		}
		else if (frame % 4 == 3)
		{
			// eyes closed length
			return 100;
		}
	});

	// sprite neon sign eyes
	$('.sprite-neon-sign').ayoSpriteAnimation(function (frame)
	{
		return 300;
	});

	// ie6 hover tooltips fix
	$('.home .sections a').hover(function (e) { $('span', this).show() }, function (e) { $('span', this).hide() });

	// bookcases
	$('.library .sections a').each(function (i)
	{
		$(this)
		.data('shelf', i)
		.click(function (e)
		{
			e.preventDefault();

			$(this).closest('li').addClass('current').siblings('li').removeClass('current');

			set_cookie('bookcase', $(this).data('shelf'), 100);

			var b1offset = '0px';
			var b2offset = '0px';

			switch ($(this).data('shelf'))
			{
				case 0:
					b1offset = '-126px';
					b2offset = '89px';
					break;

				case 1:
					b1offset = '-341px';
					b2offset = '89px';
					break;

				case 2:
					b1offset = '-341px';
					b2offset = '-126px';
					break;
			}

			var speed = $(this).data('instant') ? 0 : 500;

			$('.bookcase-1').stop().clearQueue().animate({
				marginLeft: b1offset
			}, speed);

			$('.bookcase-2').stop().clearQueue().animate({
				marginLeft: b2offset
			}, speed);

			$(this).data('instant', false);
		});
	})
	.eq(get_cookie('bookcase') || 0).data('instant', true).click();
});

$.fn.rumadDock = function ()
{
	this.each(function (i)
	{
		var speed = ($.browser.msie && $.browser.version < 7) ? 0 : 200;

		/*
		// dock
		$(this)
		.find('ul')
		// show dock
		.mouseenter(
		function (e)
		{
		$(this).stop().animate({ marginTop: '-164px' }, speed, function () { if (!$(this).find('a.focus').length) { $(this).find('li:first-child a').focus() } });

		$(this).find('li a').attr('tabindex', '0');
		}
		)
		// hide dock
		.mouseleave(
		function (e)
		{
		$(this).stop().animate({ marginTop: '-50px' }, speed);

		$(this).find('li a').attr('tabindex', '-1').blur();
		}
		)
		*/

		$(this)
		.removeClass('open closed')
		.addClass(get_cookie("dockState") || 'open')
		.find('.toggle').click(function (e)
		{
			var dock = $(this).parents('#dock');

			if (dock.hasClass('open'))
			{
				dock.removeClass('open').addClass('closed');
				set_cookie('dockState', 'closed', 100);
			}
			else
			{
				dock.removeClass('closed').addClass('open');
				set_cookie('dockState', 'open', 100);
			}
		});

		// icons
		$(this)
		.find('li a')
		// take icons out of the tabindex flow
		.attr('tabindex', '-1')
		.focus(
			function (e)
			{
				$(this).stop().animate({ paddingTop: '20px', marginTop: '-20px' }, 100).addClass('focus');
			}
		)
		.blur(
			function (e)
			{
				$(this).stop().animate({ paddingTop: '0px', marginTop: '0px' }, 100).removeClass('focus');
			}
		)
		.mouseover(
			function (e)
			{
				if ($.browser.webkit || $.browser.opera)
				{
					if (!$(this).parents('ul:animated').length)
					{
						$(this).focus().parents('li').siblings('li').find('a').blur();
					}
				}
				else
				{
					$(this).focus().parents('li').siblings('li').find('a').blur();
				}
			}
		)
		.mouseout(
			function (e)
			{
				$(this).blur();
			}
		);
	});
}


/**
Global Helper function for setting cookies.
	
@param name Name of the cookie
@param value Value to store in the cookie
@param [days] Number of days to keep cookie
*/
function set_cookie(/**String*/name, /**String*/value, /**Number*/days) {
	if (days) {
		var date = new Date();

		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));

		var expires = "; expires=" + date.toGMTString();
	}
	else {
		var expires = "";
	}

	document.cookie = name + "=" + escape(value) + expires + "; path=/";
};

/**
Global Helper function for retrieving cookies.
*/
function get_cookie(/**String*/name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for (var i = 0, l = ca.length; i < l; i++) {
		var c = ca[i];

		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}

		if (c.indexOf(nameEQ) == 0) {
			return unescape(c.substring(nameEQ.length, c.length));
		}
	}

	return null;
};

/**
Global Helper function for deleting cookies.
*/
function del_cookie(/**String*/name) {
	set_cookie(name, "", -1);
};

