// Routines for using the API for fleshing out the listings
function getItem(itemid)
{
	var bodyTag = document.getElementsByTagName("body").item(0);
	var scriptTag = document.createElement("script");
	scriptTag.src = "http://open.api.ebay.com/shopping?version=515&callname=GetSingleItem&ItemID=" + itemid + "&responseencoding=JSON&appid=JoshuaMa-0866-49fb-ae1f-dceacedb49ed&callback=true&IncludeSelector=Details,ItemSpecifics,ShippingCosts";
	bodyTag.appendChild( scriptTag );
}

function _cb_GetSingleItem(root)
{
	var item = root.Item;
	var html = [];
	var sText = '';
	var sShipping = "";
	
	if (null == item) return;
	if (null != item.ShippingCostSummary.ShippingType) 
		sShipping = item.ShippingCostSummary.ShippingType;
	if (sShipping == "Flat") {
	    if (null != item.ShippingCostSummary.ShippingServiceCost) {
			var s2 = item.ShippingCostSummary.ShippingServiceCost.Value;
			if ((s2 == 0) || (s2 == "0.0")) {
				sShipping = "Free"
			} else {
				sShipping = 'US $' + s2;
			}
		}	
	}

	sText = '<span style="font-weight:normal"><u>Seller:</u> ' + item.Seller.UserID ;
    if (null != item.Seller.FeedbackScore) {
		sText = sText +	" (" + item.Seller.FeedbackScore + ") " + item.Seller.PositiveFeedbackPercent + "%<br />";
	}
	if (sShipping != "")
		sText = sText +	"<u>Shipping:</u> " + sShipping  + "<br />" ;
    if ((null != item.Location) && (null != item.Country)) {
		sText = sText +	"<u>Location:</u> " + item.Location + ", " + item.Country + "</span>";
	}	
//	alert(item.ItemID);
	      
	// Find the links in the item
	var links = document.getElementsByTagName("a");
	for (var i = 0; i<links.length; i++) {
		if (links[i].href.search(item.ItemID) > -1) {
		    // Change the item text
		    var str = links[i].innerHTML
			str = str.replace(/<span><\/span>/i, '<br />' + sText);
			links[i].innerHTML = str;
			// Keep looping because there are multiple auctions with the same id
		}
	}
}

function FixLinks()
{
	// Find the links in the item
	var links = document.getElementsByTagName("a");
	for (var i = 0; i<links.length; i++) {
		if (links[i].href.search(document.domain+"/item-") > -1) {
			aUrl = links[i].href.split("_");
			itemid = aUrl[1];
			getItem(itemid);
		}
	}
} // FixLinks

