$(document).ready(function()
{
	var width = 130, // width of artist thumbnail
		gutter = 9, // space between artist thumbnails
		$mover = $('#artists-mover'),
		$left = $('#artists-nav-left'),
		$right = $('#artists-nav-right');
	
	// Move slider to show a specific artist first
	var first = $.cookie('slider-position');
	if (!first)
	{
		first = $mover.data('first').substr(7);
	}
	if (first)
	{
		while ($mover.children().first().attr('id') != first)
		{
			$mover.children().first().appendTo($mover);
		}
	}
	
	$right.click(function(event) {
		event.preventDefault();
		if (!$right.hasClass('temporarily-disabled'))
		{
			$right.addClass('temporarily-disabled');
			$mover.animate({'margin-left': -1*(width+gutter)}, 300, 'swing', 
				function()
				{
					$mover.css('margin-left', -1*gutter).children().first().appendTo($mover);
					$right.removeClass('temporarily-disabled');
				});
		}
	});
	
	$left.click(function(event) {
		event.preventDefault();
		if (!$left.hasClass('temporarily-disabled'))
		{
			$left.addClass('temporarily-disabled');
			$mover.css('margin-left', -1*(width+gutter)).children().last().prependTo($mover);
			$mover.animate({'margin-left': -1*gutter}, 300, 'swing', 
				function()
				{
					$left.removeClass('temporarily-disabled');
				});
		}
	});
	
	// When user clicks to view an artist, set cookie to store slider position
	$mover.find('a').click(function()
	{
		$.cookie('slider-position', $mover.children().first().attr('id'), { expires: 1, path: '/' });
	});

});

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
