/**
 * jquery.studiocasey.gallery_slide.js
 *
 * Builds the slide navigation for Studio Casey 4 theme.
 * Copyright (c) 2010, David R. Casey, http://studiocasey.com All rights reserved.
 *
 * @author David R. Casey
 * @link http://www.studiocasey.com
 * @copyright (c) 2010, David R. Casey, Studio Casey. All rights reserved.
 * @version 1.01
 * @requires jQuery
 *
 */
 
(function($) { // jQuery closure

var current = 0;
$.fn.gallery_slide = function() {
	$('.slide-nav').hide();
	slide_pages = $('.slide-page').hide().css({ 'position':'absolute', 'left':0, 'top':0 });
	$(slide_pages[current]).show();
	$('#slide').append($('<a id="slide-home" class="slide-nav">&nbsp;</a><a id="slide-prev" class="slide-nav">&nbsp;</a><a id="slide-next" class="slide-nav">&nbsp;</a>'));
	
	$('#slide-home').click(function() { navigate_to( 0); });
	$('#slide-prev').click(function() { navigate(-1); });
	$('#slide-next').click(function() { navigate( 1); });
	$('.index a').each(function() { 
		$(this).removeAttr('href').css('cursor','pointer').click( function() { 
			navigate_to(parseInt($(this).attr('id').replace(/index/i,''))); 
		}); 
	});
	gallery_slide_keys();
};

// For IE?
function gallery_slide_keys() { $.fn.gallery_slide_keys(); };

/**
 * Navigates from the current object to the desired object
 * @param current The index of the current object in the collection
 * @param move The number of indices to move from the current index, positive or negative
 */
function navigate(move) {
	var size = slide_pages.length;
	var show = ((move+current)%size+size)%size;
	
	if (move > 0) { // slide left
		$(slide_pages[show]).show().css({ 'left': 960 }).animate({ 'left':0 });
		$(slide_pages[current]).animate({ 'left':-960 }, function() { $(this).hide(); });
		
	} else {        // slide right
		$(slide_pages[show]).show().css({ 'left':-960 }).animate({ 'left':0 });
		$(slide_pages[current]).animate({ 'left': 960 }, function() { $(this).hide(); });
	}
	current = show;
};
function navigate_to(index) {
	if (index == current) return;
	if (current < index) { // slide left
		$(slide_pages[index]).show().css({'left': 960 }).animate({ 'left':0 });
		$(slide_pages[current]).animate({ 'left':-960 }, function() { $(this).hide(); });
	} else {        // slide right
		$(slide_pages[index]).show().css({'left':-960 }).animate({ 'left':0 });
		$(slide_pages[current]).animate({ 'left': 960 }, function() { $(this).hide(); });
	}
	current = index;
};

$.fn.gallery_slide_keys = function() {
	document.onkeydown = function(e) { 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		switch(keycode) {
			// next item
			case 60:
			case 190: // period/greaterthan
			case 78:  // n/N
			case 39:  // right arrow
				navigate(1);
				break;
			// previous item
			case 188: // comma/lessthan
			case 80:  // p/P
			case 37:  // left arrow
				navigate(-1);
				break;
		}
	};
};


})(jQuery);
