/*

jquery.nm.slidecategories.js

Custom jquery plugin for sliding categories from left to right and vice versa by Nevermore
Based on: jquery 1.4.2

*/

// Initialize all sliding categories
$.fn.initializeSlidingCategories = function(options) {
	return this.each(function() {
		$(this).click(function() {
            $('.scrollable').deactivateAllScrollingItems();
			var categoryId = $(this).attr('id').split('Bar')[0];
			var $category = $('#' + categoryId);
			var position = parseInt($category.css('left'),10);
			var width = parseInt($category.css('width'),10);
			var slideWidth = parseInt($category.css('min-width'),10);
			var slideLeftToRight = (position < width);
			if (slideLeftToRight) {
				var $next = null;
				if (categoryId == 'ctContainer') { $next = $('#vsContainer'); }
				else if (categoryId == 'vsContainer') { $next = $('#ilContainer'); }
				else if (categoryId == 'ilContainer') { $next = $('#toContainer'); }
				else if (categoryId == 'toContainer') { $next = $('#goContainer'); }
				else if (categoryId == 'goContainer') { $next = $('#homeContainer'); }
				if ($next != null) {
					slideCategories($next, slideLeftToRight, slideWidth);
				}
			}
			else {
				slideCategories($category, slideLeftToRight, slideWidth);
			}
			$category.find('.scrollable').activateScrollingItems();
		});
	});
};

// Slide all categories when $source is clicked
function slideCategories($source, slideLeftToRight, slideWidth) {
	var sourceId = $source.attr('id');
	if (slideLeftToRight) {
		var $next = null;
		if (sourceId == 'ctContainer') { $next = $('#vsContainer'); }
		else if (sourceId == 'vsContainer') { $next = $('#ilContainer'); }
		else if (sourceId == 'ilContainer') { $next = $('#toContainer'); }
		else if (sourceId == 'toContainer') { $next = $('#goContainer'); }
		else if (sourceId == 'goContainer') { $next = $('#homeContainer'); }
		if ($next != null) {
			slideCategories($next, slideLeftToRight, slideWidth);
		}
	}
	else {
		var $next = null;
		if (sourceId == 'homeContainer') { $next = $('#goContainer'); }
		else if (sourceId == 'goContainer') { $next = $('#toContainer'); }
		else if (sourceId == 'toContainer') { $next = $('#ilContainer'); }
		else if (sourceId == 'ilContainer') { $next = $('#vsContainer'); }
		if ($next != null) {
			slideCategories($next, slideLeftToRight, slideWidth);
		}
	}
	slideCategory($source, slideLeftToRight, slideWidth);
}

// Slide one specific category
function slideCategory($category, slideLeftToRight, slideWidth) {
	var position = parseInt($category.css('left'),10);
	if (slideLeftToRight && (position < slideWidth)) {
		$category.animate({ left: (position + slideWidth) });
		if ($category.attr('id') == 'homeContainer') {
			$('#homeContainerBar').css('display','block');
		}
	}
	else if (!slideLeftToRight && (position >= slideWidth)) {
		if ($category.attr('id') == 'homeContainer') {
			$('#homeContainerBar').css('display','none');
		}
		$category.animate({ left: (position - slideWidth) });
	}
}
