
/******************************
  IPS Plugin
	by chris.franson@gmail.com
*/
(function($) {

	var fullHash,
		hash,
		$mainButtonsList,
		$categoryLinks,
		$categories,
		selectedCategoryId = '',
		$selectedCategory,
		$tabs,
		$selectedCategoryTabs,
		$selectedCategoryTabLinks,
		selectedTabId = '',
		$selectedTab,
		ignoreHashChange = false,
		msie = $.browser.msie;

	var settings = {
		fadeSpeed: 'slow',
		sliderSpeed: 6
	};

	var methods = {
		
		init: function(options) {

			return this.each(function() {
				
				var $this = $(this);

				if (options) $.extend(settings, options);
				$this.data('settings', settings);
				
				$ipsContainer = $this;
				
				var $catButtons = $('ul#ips-main-buttons');
				if ($catButtons.length == 0 || $catButtons.find('li').length == 0 || $catButtons.hasClass('buttons-0')) {
					$catButtons.hide();
					$ipsContainer.addClass('no-cat-buttons');
					
					// $('#ips-category-bkg').css('top', '105px');
				}
				
				$mainButtonsList = $('#ips-main-buttons');
				$categoryLinks = $mainButtonsList.find('a');
				$categories = $('.category-content');
				$tabs = $('.ips-main-content');

				// Get the selected category from the URL
				urlSelect();

				$(window).bind('hashchange', function() {
					if (ignoreHashChange) {
						ignoreHashChange = false;
						return;
					}
					urlSelect();
				});
			});
			
		}
		
	};

	/* Private Methods */

	function urlSelect() {

		fullHash = $.trim(window.location.hash.replace('#!/', '').replace('#', '')).split('/');
		hash = '';

		var scrolled = false;

		$.each(fullHash, function(i, v) {
			if (v.match(/^category[0-9]+?/)) hash = v;
			else if (!scrolled && $('#'+v).length > 0) {
				window.scrollTo(0, $('#'+v).offset().top);
				scrolled = true;
			}
		});

		if (hash != '') {
			hash = hash.replace('/', '-');
			var hashArray = hash.split('-');
			selectedCategoryId = hashArray[0];
			selectedTabId = (hashArray[1] && $.trim(hashArray[1]) != '') ? hash : '';
		}
		selectCat(selectedCategoryId);
	}

	function selectCat(catId) {
		
		if (!catId) catId = $('#ips-main-buttons li').first().find('a').attr('href');
		if (catId.indexOf("#") != -1) catId = catId.split('#')[1];
		
		if ($('#'+catId).length < 1) {
			
			var cid = catId.replace('category', '');
			
			$.get('/_ssi/category.php?category=' + cid, function(data) {
				var $newCat = $(data);
				$newCat.hide();
				if ($newCat.hasClass('category-content')) {
					var $tabs = $newCat.find('.ips-tabs');
					$tabs.addClass('tabs-' + $tabs.children('li').length);
					$('#ips').append($newCat);
					$categories = $('#ips').find('.category-content');
					$tabs = $('.ips-main-content');
					setCat(catId);
				}
			}, 'html');
			
		}
		else setCat(catId);
			
	}
	
	function setCat(catId) {

		if (catId && $('#'+catId).length > 0) $selectedCategory = $('#'+catId);
		else $selectedCategory = $categories.first();
		
		// Select the actual category
		if (!$selectedCategory.hasClass('selected')) {
			$categories.removeClass('selected').hide();
			// $selectedCategory.find('.ips-main-content:not(".selected")').hide();
			$('.ips-main-content').hide();
			$selectedCategory.addClass('selected').show();
		}

		// Select the category link
		selectedCategoryId = $selectedCategory.attr('id');
		$categoryLinks.removeClass('selected');
		$mainButtonsList.find('a[href="#'+selectedCategoryId+'"]').addClass('selected');
		
		selectTab(selectedTabId);
	}

	function selectTab(tabId) {
		
		// if (!$selectedCategory || $selectedCategory.length == 0) selectCat();

		$selectedCategoryTabs = $selectedCategory.find('.ips-main-content');

		if (tabId && $('#'+tabId).length > 0) $selectedTab = $('#'+tabId);
		else {
			var $previouslySelected = $selectedCategoryTabs.filter('.selected').first();
			if ($previouslySelected.length > 0) {
				ignoreHashChange = true;
				window.location.hash = "#!/" + $previouslySelected.attr('id');
				$selectedTab = $previouslySelected;
			}
			else $selectedTab = $selectedCategoryTabs.first();
		}

		// Select the actual tab
		if (!$selectedTab.hasClass('selected')) {
			$selectedCategoryTabs.removeClass('selected').fadeOut(settings.fadeSpeed);
		}
		$selectedTab.addClass('selected').fadeIn(settings.fadeSpeed);

		// Select the tab link
		$selectedCategoryTabLinks = $selectedCategory.find('.ips-tabs a');
		selectedTabId = $selectedTab.attr('id');
		$selectedCategoryTabLinks.removeClass('selected');
		$selectedCategory.find('.ips-tabs a[href="#'+selectedTabId+'"]').addClass('selected');
		if (msie) {
			$selectedCategory.find('.ips-tabs a[href="' + window.location.href.replace(window.location.hash, '') + '#'+selectedTabId+'"]').addClass('selected');
		}

		// Init IPS nav links (category and tab links)
		$mainButtonsList.find('a').add('.ips-tabs a').unbind('click.ips').bind('click.ips', function() {
			
			var $a = $(this);
			
			if ($a.hasClass('external')) return;
			
			if (msie && $.browser.version <= 7) {
				window.location.hash = "#!/" + $a.attr('href').split('#')[1];
			}
			else window.location.hash = "#!/" + $a.attr('href').replace('#', '');
			return false;
		});

		setTimeout(function() {
			$selectedTab.find('.ips-product-scroller').productSlider({sliderSpeed: settings.sliderSpeed});
		}, 100);
	}

	$.fn.ips = function(method) {
		if (methods[method]) return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		else if (typeof method === 'object' || !method) return methods.init.apply(this, arguments);
		else $.error('jQuery.ips doesn\'t have a "' + method + '" method');
	};

})(jQuery);



/******************************
  Product Slider Plugin
	by chris.franson@gmail.com
*/
(function($) {

	var $sliderContainer,
		$productsContainer,
		$sliderTable,
		$leftButton,
		$rightButton,
		wDiff = 0,
		wDiffNeg;
		
	var methods = {
		
		init: function(options) {
			
			var settings = {
				sliderSpeed: 6
			};
			
			return this.each(function() {
				
				var $this = $(this);

				if (options) $.extend(settings, options);
				if (settings.sliderSpeed == 0) settings.sliderSpeed = 0.1;
				settings.sliderSpeed = settings.sliderSpeed / 10;
				$this.data('settings', settings);
				
				$sliderContainer = $this;
				$productsContainer = $sliderContainer.find('.ips-products');
				$sliderTable = $productsContainer.find('table');
				$leftButton = $sliderContainer.find('.left-arrow a');
				$rightButton = $sliderContainer.find('.right-arrow a');

				wDiff = $productsContainer.width() - $sliderTable.width();
				wDiffNeg = 0 - Math.abs(wDiff);
				
				if (wDiff < 0) {
					$sliderTable.css('margin', '0px');
					if ($sliderTable.css('left') == '0px') $rightButton.addClass('enabled');

					$leftButton.mousedown(function() {
						$rightButton.addClass('enabled');
						var cssLeft = $sliderTable.css('left'),
							left = (cssLeft == 'auto') ? 0 : parseInt(cssLeft,10),
							distance = 0-left,
							interval = distance * (1/settings.sliderSpeed);

						$sliderTable.animate({ left: '0px' }, interval, 'linear', function() {
							$leftButton.removeClass('enabled');
						});
					});

					$rightButton.mousedown(function() {
						$leftButton.addClass('enabled');
						var cssLeft = $sliderTable.css('left'),
							left = (cssLeft == 'auto') ? 0 : parseInt(cssLeft,10),
							distance = Math.abs(wDiff) + left,
							interval = distance * (1/settings.sliderSpeed);

						$sliderTable.animate({ left: wDiffNeg + 'px' }, interval, 'linear', function() {
							$rightButton.removeClass('enabled');
						});
					});

					$rightButton.add($leftButton).bind('mouseup mouseleave', function() {
						$sliderTable.stop();
					});
				}

				$leftButton.add($rightButton).click(function() {
					return false;
				});

			});
			
		}
		
	};

	$.fn.productSlider = function(method) {
		if (methods[method]) return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		else if (typeof method === 'object' || !method) return methods.init.apply(this, arguments);
		else $.error('jQuery.productSlider doesn\'t have a "' + method + '" method');
	};

})(jQuery);
