var currentSelectedPhoto='';
function choosePartFromGallery(placeOffset)
{
	//place offset is 1-10 corresponding to which 'square' you are clicking on
	if (placeOffset == 1 && productPlacement[1] == 1)
	{
		turnPreviousButtonOff();
	}
	else
	{
		turnPreviousButtonOn();
	}
	if (productPlacement[placeOffset] == numberOfProducts)
	{
		turnNextButtonOff();
	}
	else
	{
		turnNextButtonOn();
	}
//	if (productPlacement[])
	var productOffset=productPlacement[placeOffset];
	$("#partNameP").html(productNames[productOffset]);
	turnAllPartsGalleryImageOff();
	turnPartsGalleryImageOn(placeOffset);
	currentSelectedPhoto=placeOffset;
//	alert(currentSelectedPhoto);
	return false;
}
function nextPreviousBindHover()
{
	//workaround because other DOM objects have the class 'ro' so can't call the init function for all of them
	$("#nextImageID").unbind('mouseenter mouseleave');
	$("#prevImageID").unbind('mouseenter mouseleave');

	if ($("#nextImageID").hasClass('ro'))
	{
		 $("#nextImageID").hover(
		         function () { $(this).attr( 'src', SITESNOW.rollover.newimage($(this).attr('src')) ); },
		         function () { $(this).attr( 'src', SITESNOW.rollover.oldimage($(this).attr('src')) ); }
		      );
	}
	if ($("#prevImageID").hasClass('ro'))
	{
		 $("#prevImageID").hover(
		         function () { $(this).attr( 'src', SITESNOW.rollover.newimage($(this).attr('src')) ); },
		         function () { $(this).attr( 'src', SITESNOW.rollover.oldimage($(this).attr('src')) ); }
		      );
	}
}
function turnPreviousButtonOff()
{

	if ($("#prevImageID").attr("src") != "/images/previous-off.jpg")
	{
		$("#prevImageID").attr("src","/images/previous-off.jpg");
		$("#prevImageID").removeClass("ro");
		nextPreviousBindHover();

	}
}
function turnPreviousButtonOn()
{

	if ($("#prevImageID").attr("src") != "/images/previous.jpg")
	{
		$("#prevImageID").attr("src","/images/previous.jpg");
		$("#prevImageID").addClass("ro");
		
		nextPreviousBindHover();


	}
}
function turnNextButtonOff()
{

	if ($("#nextImageID").attr("src") != "/images/next-off.jpg")
	{
		$("#nextImageID").removeClass("ro");	
		$("#nextImageID").attr("src","/images/next-off.jpg");
		
		//we need to cancel the hover for this ID
		nextPreviousBindHover();
	}
}
function turnNextButtonOn()
{

	if ($("#nextImageID").attr("src") != "/images/next.jpg")
	{
		$("#nextImageID").addClass("ro");	
		$("#nextImageID").attr("src","/images/next.jpg");
		
		nextPreviousBindHover();
	}
}
function turnPartsGalleryImageOn(placeOffset)
{

	var productOffset=productPlacement[placeOffset];
	$("#image_"+placeOffset).attr("src","/images/partsGallery/"+productBaseImages[productOffset]+"_58.jpg");
}
function turnAllPartsGalleryImageOff()
{
	var productOffset='';
	for (i=0;i<11;i++)
	{
		var productOffset=productPlacement[i];
		$("#image_"+i).attr("src","/images/partsGallery/"+productBaseImages[productOffset]+"_58_dark.jpg")
	}
}
function openProductWindow(placeOffset)
{
	var productOffset=productPlacement[placeOffset];
//	alert(productNames[productOffset]);
//	alert("got here");
	url="/action/getProductInfo.php?productID="+productIDs[productOffset];
	$.get(url,function(data){
		$("#dialogProduct").html(data);
//		$("span.ui-dialogProduct-title").text('Edit this');

		$("#ui-dialog-title-dialogProduct").text('Product Specs - '+productNames[productOffset]);
		$("#dialogProduct").dialog('open');
		
	});
	return false;
}
function galleryMovePrevious()
{
	var previous=currentSelectedPhoto-1;
	if (productPlacement[1] == 1 && (previous == 0 || currentSelectedPhoto == ''))
		return false;
	var firstProduct=productPlacement[1];
	if (firstProduct != 1 && previous == 0)
	{
		//move the whole display to the left
		redrawProductsGallery(productPlacement[10]-1);
		choosePartFromGallery(1);
	}
	else
	{
		//just hightlight the previous one
		choosePartFromGallery(previous);
	}
	//we need to decide if we have to hide the 'previous' button

	//are we at the beginning?
//	alert(productPlacement[1]);
//	alert(previous);
//	currentSelectedPhoto=previous;
//	alert(productPlacement[1]);

	return false;	
}

function galleryMoveNext()
{
	if (currentSelectedPhoto == '')
	{
		//move to the 0 offset
//		alert(currentSelectedPhoto);
		choosePartFromGallery(1);
		currentSelectedPhoto=1;
	}
	else
	{
//		turnPreviousButtonOn();
		var nextOffset=currentSelectedPhoto+1;
//		alert(productPlacement.length);
		if (productPlacement.length-1 < nextOffset)
				return false;
		
//		if (productPlacement[10] == numberOfProducts)
//			return false;
		if (nextOffset > 10)
		{
			//get the next offset from productPlacement
//			alert(productPlacement[10]);
			if (productPlacement[10] == numberOfProducts)
				return false;
			redrawProductsGallery(productPlacement[10]+1);
			
			choosePartFromGallery(10);
		}
		else
		{
			choosePartFromGallery(currentSelectedPhoto+1);
		}
//		currentSelectedPhoto=nextOffset;
	}
	return false;
}
function updateProductPlacement(startValue,endValue)
{
	var z=1;
	for (var i=startValue;i<endValue+1;i++)
	{
		productPlacement[z]=i;
		
		z++;
	}
}
function redrawProductsGallery(currentOffset)
{
//	alert(currentOffset);
	var productOffset=currentOffset-9;
	updateProductPlacement(productOffset,currentOffset);
//	alert('we need to display products '+productOffset+' through '+currentOffset);
//	var lastProductOffset=currentOffset;
	var imageOffset=1;
	for (var i=productOffset;i<currentOffset+1;i++)
	{
//		alert(i);
		//add productOffset to i
//		$("#a_"+imageOffset).attr("onClick","");
//		$("#a_"+imageOffset).attr("onMouseOver","");
		$("#a_"+imageOffset).attr("title",productNames[i]);
//		alert("setting "+imageOffset +" image to " + productBaseImages[i]+"_58_dark.jpg");
		$("#image_"+imageOffset).attr("src","/images/partsGallery/"+productBaseImages[i]+"_58_dark.jpg");
		$("#image_"+imageOffset).attr("alt",productNames[i]);
//		print "<a href=\"\"  id=\"a_{$i}\" onClick=\"return openProductWindow({$productInfo['id']});\" onMouseOver=\"return choosePartFromGallery({$i});\" title=\"" . htmlentities($productInfo['productName'],ENT_QUOTES) . "\"><img id=\"image_{$i}\" src=\"/images/partsGallery/{$baseImage}_58_dark.jpg\" width=\"58\" height=\"58\" alt=\"" . htmlentities($productInfo['productName'],ENT_QUOTES) . "\" class=\"off\" /></a>";
		imageOffset++;
	}
}
