$.fn.productsNav = function(options){
	
	var el = this;
	var block = false;
	
	var options = $.extend({
		activeItem : "",
		leftArrow : "",
		rightArrow : "",
		speed : 100,
		dots: true
	}, options);
	
	var itemsWidth = ($(el).find("ul:first").find("li").width() * $(this).find("ul").find("li").length);
	var numberOfItems = $(el).find("ul:first").find("li").length;
	var numberOftemsInFrame = Math.floor($(el).width() / $(this).find("ul:first").find("li").width());
	var numberOfFrames = Math.ceil(numberOfItems / numberOftemsInFrame);
	var currentFrame = 0;
	var activeItemFrame = 1;
	
	$(el).find("ul:first").width(itemsWidth);
	
	$(el).find("ul:first").find("li").each(function(){
		var itemId = $(this).find("a").attr("id");
		if (options.activeItem && itemId.match(options.activeItem)) {
			activeItemFrame = Math.ceil(($(this).index() + 1) / numberOftemsInFrame);
		}
	});
	
	currentFrame = activeItemFrame;
	
	$(el).find("ul:first").css("left", "-" + ($(this).width() * (activeItemFrame - 1)) + "px");
	
	var resetNav = function(){
		if (currentFrame == 1) {
			$(options.leftArrow).fadeOut("fast");
		}
		else {
			$(options.leftArrow).fadeIn("fast");
		}
		if (currentFrame == numberOfFrames) {
			$(options.rightArrow).fadeOut("fast");
		}
		else {
			$(options.rightArrow).fadeIn("fast");
		}
		if (options.dots) {
			$(el).find("ul:last").find("li").find("a").removeClass("active");
			$(el).find("ul:last").find("li:eq(" + (currentFrame - 1) + ")").find("a").addClass("active");
		}
	};
	
	$(options.rightArrow).click(function(){
		if (block) return false;
		block = true;
		currentFrame++;
		resetNav();
		var position = $(el).find("ul").position();
		$(el).find("ul:first").animate({ left: (position.left - $(el).width()) }, options.speed, function(){ block = false; });		
		return false;
	});
	
	$(options.leftArrow).click(function(){
		if (block) return false;
		block = true;
		currentFrame--;
		resetNav();
		var position = $(el).find("ul").position();
		$(el).find("ul:first").animate({ left: (position.left + $(el).width()) }, options.speed, function(){ block = false; });		
		return false;
	});
	
	if (options.dots) {
		$(el).append("<ul class=\"dots\"></ul>");
		for (i=1; i<=numberOfFrames; i++) {
			$(el).find("ul:last").append("<li><a href=\"#\"></a></li>");
		}
		$(el).find("ul:last").width($(el).find("ul:last").find("li").width() * numberOfFrames);
		$(el).find("ul:last").find("li").find("a").click(function(){
			if (block) return false;
			block = true;
			$(el).find("ul:first").animate({ left: (($(this).parent().index() * $(el).width()) * -1) } , options.speed, function(){ block = false; });
			currentFrame = ($(this).parent().index() + 1);
			resetNav();
			return false;
		});
	}
	
	resetNav();
};

$(document).ready(function(){
	$("#pageTop").click(function(){
		$("html, body").animate({ scrollTop: 0 }, 1000);
		return false;
	});
	
	$(".badge").click(function(){
		var el = this;
		_gaq.push(['_trackEvent', 'Product', 'Buy', $(el).attr("rel")]);
		$.ajax({
			type: "GET",
			url: "/api.php",
			data: "method=webLog&action=buy&product=" + $(el).attr("rel") + "&source=productpage",
			complete: function(){
				window.location = $(el).attr("href");
			}
		});
		return false;
	});
});
