var itsFeaturedProducts;
var itsFeaturedStart = 0;
var itsFeaturedMax = 0;
var itsFeaturedIndex = 0;

function addHandler(inTarget, inEventName, inHandler)
{
	if (inTarget.addEventListener)
	{
		inTarget.addEventListener(inEventName, inHandler, false);
	}
	else if (inTarget.attachEvent)
	{
		inTarget.attachEvent("on" + inEventName, inHandler);
	}
	else
	{
		inTarget["on" + inEventName] = inHandler;
	}
}

function getEl(inId)
{
	return document.getElementById(inId);
}

function updateFeaturedDD(inSlide, inItemNumber, inItem)
{
	var theHtml = "";
	var theHighlight = "";
	var theDd = document.getElementById("featured_dd_" + inSlide);
	if (inItemNumber == itsFeaturedIndex)
	{
		var theImage = "<a class='slider' href='#' onclick='return false;' style='cursor: default; border: 1px solid black;'><img src='" + inItem.thumbimageurl + "' alt='" + inItem.name + "' width='95' height='59' id='PRODUCT_THUMBNAIL_IMAGE_" + inSlide + "'/></a><br/>";
		theHtml = theImage + " " + inItem.name;
	}
	else
	{
		var theImage = "<a class='slider' href='javascript:showProduct(" + inItemNumber + ")'><img src='" + inItem.thumbimageurl + "' alt='" + inItem.name + "' width='95' height='59' id='PRODUCT_THUMBNAIL_IMAGE_" + inSlide + "'/></a><br/>";
		theHtml = theImage + "<a href='javascript:showProduct(" + inItemNumber + ")' id='PRODUCT_THUMBNAIL_LINK_" + inSlide + "'>" + inItem.name + "</a>";
	}
	theDd.innerHTML = theHtml;
}

function featuredUpdate() {
	for (var theLoop = 0; (theLoop < 4); theLoop++)
	{
		var theItem = itsFeaturedStart + theLoop;
		if (theItem > itsFeaturedMax)
		{
			theItem = theItem - (itsFeaturedMax + 1);
		}
		updateFeaturedDD(theLoop, theItem, itsFeaturedProducts.items[theItem]);
	}
}

function getLinkDD(inLabel, inUrl)
{
	return "<dd><a href='" + inUrl + "'>" + inLabel + "</a></dd>";
}

function showProduct(inWhich)
{
	if (!itsFeaturedProducts) return;
	if (!(itsFeaturedProducts.items)) return;
	getEl("VEHICLE_NAME").innerHTML = itsFeaturedProducts.items[inWhich].name;
	var theState = itsFeaturedProducts.items[inWhich].state;
	getEl("VEHICLE_TESTED").innerHTML = theState;
	var theRoadTest = "Road test";
	if (theState == "(First drive)")
	{
		theRoadTest = "First drive";
	}
	var theSummary = itsFeaturedProducts.items[inWhich].summary;
	theSummary = theSummary.replace(/&quot;/g,'"');
	theSummary = theSummary.replace(/&lt;/g,"<");
	theSummary = theSummary.replace(/&gt;/g,">");
	getEl("VEHICLE_SUMMARY").innerHTML = theSummary;
	cueVideo(itsFeaturedProducts.items[inWhich].videoid);
	getEl("VEHICLE_BLURB").innerHTML = itsFeaturedProducts.items[inWhich].name;
	getEl("VEHICLE_LINKBOX").innerHTML =
		"<div class='header'><a href='" +
		itsFeaturedProducts.items[inWhich].overviewurl +
		"'>Learn more</a></div><dl>" +
		getLinkDD("Overview", itsFeaturedProducts.items[inWhich].overviewurl) +
		getLinkDD("Ratings &amp; Specs", itsFeaturedProducts.items[inWhich].ratingsurl) +
		getLinkDD(theRoadTest, itsFeaturedProducts.items[inWhich].roadtesturl) +
		getLinkDD("Reliability", itsFeaturedProducts.items[inWhich].reliabilityurl) +
		"</dl><dl class='second'>" +
		getLinkDD("Owner satisfaction", itsFeaturedProducts.items[inWhich].ownersatisfactionurl) +
		getLinkDD("Prices &amp; Cost", itsFeaturedProducts.items[inWhich].pricesurl) +
		getLinkDD("Photos &amp; Videos", itsFeaturedProducts.items[inWhich].photosurl) +
		"</dl>";
	itsFeaturedIndex = inWhich;
	featuredUpdate();
}

function featuredLeftClick()
{
	if (itsFeaturedStart <= 0)
	{
		itsFeaturedStart = itsFeaturedMax;
	}
	else
	{
		itsFeaturedStart--;
	}
	featuredUpdate();
}

function featuredRightClick()
{
	if (itsFeaturedStart >= itsFeaturedMax)
	{
		itsFeaturedStart = 0;
	}
	else
	{
		itsFeaturedStart++;
	}
	featuredUpdate();
}

function recently_reviewed_callback(inData)
{
	itsFeaturedProducts = inData;
	itsFeaturedMax = itsFeaturedProducts.items.length - 1;
	if (itsFeaturedProducts.items.length > 4)
	{
		var theLeftArrow = getEl("FEATURED_LEFT");
		var theRightArrow = getEl("FEATURED_RIGHT");
		var theLeftArrowImage = getEl("FEATURED_LEFT_IMAGE");
		var theRightArrowImage = getEl("FEATURED_RIGHT_IMAGE");
		if (theLeftArrow && theRightArrow)
		{
			theLeftArrow.style.visibility = "visible";
			theLeftArrow.style.cursor = "pointer";
			addHandler(theLeftArrowImage, "click", featuredLeftClick);
			theRightArrow.style.visibility = "visible";
			theRightArrow.style.cursor = "pointer";
			addHandler(theRightArrowImage, "click", featuredRightClick);
		}
	}
}
