var swatchPos = 0;
var num_swatches = 0;
var swatchAnimating = false;
var floor_id = 0;
	
function Swatcher(num_swatches) {
	var swatchWidth = 73 + 2; // + 2 to account for the border
	
	$("a.mapto_thumb").fancybox({hideOnContentClick: false,
								frameWidth: 435,
								frameHeight: 382
								});
	
	$("#info_popup.png").pngFix();

	$("#left_arrow").click(function () {
		hideRollover();									 
		swatchAnimating=true;
		swatchPos += swatchWidth*5;
		
		if(swatchPos >= 0) {
			swatchPos = 0;
			$("#left_arrow").animate({marginLeft: "-30px"},200);
		}
		if(swatchPos > -(swatchWidth*num_swatches)+$("#swatchList").width()) {
			$("#right_arrow").animate({right: "0px"},200);	
		}
		$("#swatches").animate({marginLeft: swatchPos + "px"},500,function() {swatchAnimating=false;});
	});

	$("#right_arrow").click(function () {
		hideRollover();
		swatchAnimating=true;
		swatchPos -= swatchWidth*5;
		if(swatchPos <= -(swatchWidth*num_swatches)+$("#swatchList").width()) {
			swatchPos = -(swatchWidth*num_swatches)+$("#swatchList").width();
			$("#right_arrow").animate({right: "-50px"},200);		
		}
		if(swatchPos < 0) {
			$("#left_arrow").animate({marginLeft: "0px"},200);
		}
		$("#swatches").animate({marginLeft: swatchPos + "px"},500,function() {swatchAnimating=false;});
	});
	
	// check to see if there is not enough swatches to even HAVE the right arrow
	if(swatchWidth*num_swatches < $("#swatchList").width()) {
		$("#right_arrow").animate({right: "-50px"},0);
	}	
}

function showRollover(target, floor_species, floor_color, id) {
	if(!swatchAnimating) {
		floor_id = id;
		var leftOffset = document.getElementById("swatchList").offsetLeft + (document.getElementById("swatches").offsetLeft) - 40;
		var topOffset = document.getElementById("swatchList").offsetTop - 60;
	
		var infoLeft = leftOffset + target.offsetLeft;
		var infoTop = topOffset + target.offsetTop;
		
		if($.browser.msie) {
			switch(jQuery.browser.version*1) {
				case 8:
					infoTop += -12;
					break;
				default:
					infoTop += 40;
			}
		}
		if((infoLeft + 40) >= document.getElementById("swatchList").offsetLeft && 
			(leftOffset + target.offsetLeft) < (document.getElementById("swatchList").offsetLeft + $("#swatchList").width() - 50)
			) {
			var htmlText = "<strong>Species / Color:</strong><br />" + floor_species + " /\n";
			htmlText += floor_color + "<br />\n";
			$("#info_popup").html(htmlText);
			infoTop -= $("#info_popup").height();
			$("#info_popup").css({top: infoTop});
			$("#info_popup").css({left: infoLeft});	
			$("#info_popup").stop(true,true);
			$("#info_popup").fadeIn(250);
		}
	}
}

function hideRollover() {
	$("#info_popup").hide();
}
