<?xml version="1.0" encoding="UTF-8" ?>
<Module>

<ModulePrefs
title="Deals on Amazon"
height="350"
width="280"
description="You won't miss a deal on Amazon.com! This gadget updates you with
latest deals from Amazon's Goldbox, Amazon's top seller books, Amazon's
top DVD sellers, Amazon's top toy sellers, Amazon's top music
sellers, Amazon's top electronics sellers, Amazon's top toys sellers.
More categories will be added later. The contents of this gadget will be updated with
Amazon's official RSS feeds.You could submit to Amazon's official RSS
feeds using the following URLs. Amazon's Goldbox:
http://rssfeeds.s3.amazonaws.com/goldbox/; Amazon's top book sellers:
http://rss.amazon.com/TopSeller/cat/books/associate/igildeals-books-20;
Amazon's top DVD sellers:
http://rss.amazon.com/TopSeller/cat/dvd/associate/igildeals-dvd-20;
Amazon's top toy sellers:
http://rss.amazon.com/TopSeller/cat/toys/associate/igildeals-toys-20;
Amazon's top music sellers:
http://rss.amazon.com/TopSeller/cat/music/associate/igildeals-music-20;
Amazon's top electronics: sellers:
http://rss.amazon.com/TopSeller/cat/electronics/associate/igildeals-elec-20;
"
screenshot="http://hosting.gmodules.com/ig/gadgets/file/105645083724165793766/amazon_deals.PNG"
thumbnail="http://hosting.gmodules.com/ig/gadgets/file/105645083724165793766/goldboxthumbnail.gif"
author="Obama Wood"
author_email="wendy4wood@gmail.com"
author_affiliation="igotilike.com"
author_location="Portland, OR, USA"
title_url="http://www.amazon.com/?tag=igildeals-20"
directory_title="Amazon Deals"
>
<Require feature="tabs" />
</ModulePrefs>

<Content type="html">

<![CDATA[
<style type="text/css">
.priceLabel{
font-weight: normal;
font-size: 0.4em;
}
.price{
font-weight: normal;
font-size: 0.8em;
}

</style>

<script language="JavaScript" type="text/javascript">

var tabs;
var detailsFrame;
var frameDataFilled="F";
var tabDatas = new Array();
var urls = new Array();;
var tabName2Ind = new Array(); // from tab name to tab index
var fetchSucceeded = new Array();

/////////////////////////
function initialize (){
detailsFrame = createIframe ("Details", "99%", "96%");
//alert("List Frame width: " + listFrame.width);
urls["GoldBox"]="http://rssfeeds.s3.amazonaws.com/goldbox";
urls["Books"] = "http://rss.amazon.com/TopSeller/cat/books/associate/igildeals-books-20";
urls["DVDs"] = "http://rss.amazon.com/TopSeller/cat/dvd/associate/igildeals-dvd-20";
urls["Toys"] = "http://rss.amazon.com/TopSeller/cat/toys/associate/igildeals-toys-20";
urls["Music"] = "http://rss.amazon.com/TopSeller/cat/music/associate/igildeals-music-20";
urls["Electronics"] = "http://rss.amazon.com/TopSeller/cat/electronics/associate/igildeals-elec-20";

for (var tabName in urls) {
fetchSucceeded[tabName] = false;
fetchDataWrapper(tabName);
}
//alert ("finished fetch?");
tabs = new _IG_Tabs(__MODULE_ID__, "Categories");
var ind=0;
for (var tabName in urls) {
tabs.addTab(tabName, {
contentContainer: _gel(tabName),
tooltip: tabName,
callback: tabCallback
});
tabName2Ind[tabName] = ind;
ind++;
}
tabs.setSelectedTab(1);
}

//////////////////
function createIframe (iframeName, w, h) {
var iframe;
if (document.createElement && (iframe = document.createElement('iframe'))) {
iframe.name = iframe.id = iframeName;
iframe.width = w;
iframe.height = h;
}
document.body.appendChild(iframe);
// Depending on browser platform get the iframe's document, this is only
// available if the iframe has already been appended to an element which
// has been added to the document
if(iframe.contentDocument)
// Firefox, Opera
iframe.doc = iframe.contentDocument;
else if(iframe.contentWindow)
// Internet Explorer
iframe.doc = iframe.contentWindow.document;
else if(iframe.document)
// Others?
iframe.doc = iframe.document;

// If we did not succeed in finding the document then throw an exception
if(iframe.doc == null)
throw "Document not found, append the parent element to the DOM before creating the IFrame";

// Create the script inside the iframe's document which will call the
iframe.doc.open();
iframe.doc.close();
return iframe;
}


function writeToFrame (iframe, content) {
iframe.doc.open();
iframe.doc.write("<html><body>"+content + "<\/body><\/html>");
iframe.doc.close();
}


/////
function tabCallback(tabId) {
// Get selected tab
var selectedTab = tabs.getSelectedTab();
var tabName = selectedTab.getName();
//alert("to change tab: " + tabName);
if(tabDatas[tabName]) {
//alert("change tab");
writeToFrame(detailsFrame, tabDatas[tabName] );
} else {
fetchDataWrapper(tabName)
}
}

/////
function fetchDataWrapper(tabName) {
if(!fetchSucceeded[tabName]) {
fetchData(tabName);
setTimeout('fetchDataWrapper("' + tabName + '");', 30000); // unit: milli-seconds
}
}

function getPrice(des, priceName) {
var price = des.match("[^ ]" + priceName + ":[^0-9\\$]*\\$[0-9\.]*");

if(price) {
return price[0].match("\\$[0-9\.]*");
}
else
return null;
}
function getImgLink(des) {
var link = des.match('img src="[^"]*"');
if(link) {
var l = link[0].match('"[^"]*"');
return l[0].replace(/\"/g, "").replace(/_SL.*_.jpg/, "_AA70_.jpg");
}
return null;
}

function createDescription(itemTitle, itemLink, imgLink, listPrice, dealPrice, youSave) {
var desc="";
desc += "<table><tr>"
desc += "<td>" + '<a href="' + itemLink
+ '" target="_blank"> <img src="' + imgLink + '" alt="Product Image" border="0"></a>'
+ "</td>";
desc += "<td>";

desc += "<table>";
if(itemTitle)
desc += "<tr><td>" + '<a href="' + itemLink
+ '" target="_blank">'+ itemTitle + "</a></td></tr>";
if(listPrice )
desc += '<tr><td>' + 'List Price: ' + '<Strike>' + listPrice+ '</Strike>'+"</td></tr>";
if(dealPrice && youSave)
desc += "<tr><td>" + "Deal Price: " + dealPrice+ "</td></tr>";
if(youSave)
desc += "<tr><td>" + "You Save: " + youSave+ "</td></tr>";
desc += "</table>";
desc += "</td>";
desc += "</tr></table>";
return desc;
}


/////
function fetchData(tabName) {
//alert("To fetch URL: " + urls[tabName]);
_IG_FetchXmlContent(
urls[tabName],
function (response) {
if (response == null || typeof(response) != "object" ||
response.firstChild == null) {
//alert("failed to fetch");
writeToFrame(detailsFrame, "<i>Invalid data.</i>");
return;
}
fetchSucceeded[tabName] = true;
var itemDescriptions;
var itemList = response.getElementsByTagName("item");

itemDescriptions = "<table border='0'>";
var i=0;
var itemDescription="";
var newDesc = "";
var itemLink;
var itemTitle;
for (i = 0; i < itemList.length ; i++) {
var nodeList = itemList.item(i).childNodes;
for (var j = 0; j < nodeList.length ; j++) {
var node = nodeList.item(j);
if (node.nodeName == "title") {
itemTitle = node.firstChild.nodeValue;
}
if (node.nodeName == "link") {
itemLink= node.firstChild.nodeValue.replace(/tag=rssfeeds-20/g, "tag=igildeals-goldbox-20");
itemLink = itemLink.replace(/\/ref=xs_gb_rss_[0-9]*/g, "");
}
if (node.nodeName == "description") {
itemDescription = node.firstChild.nodeValue.replace(/tag=rssfeeds-20/g, "tag=igildeals-goldbox-20");
itemDescription = itemDescription.replace(/\/ref=xs_gb_rss_[0-9]*/g, "");
//alert("new" + itemDescription);
var listPrice = getPrice(itemDescription, "List Price");
var dealPrice = getPrice(itemDescription, "Deal Price");
if(!dealPrice)
dealPrice = getPrice(itemDescription, "Price");
var youSave = getPrice(itemDescription, "You Save");
var imgLink = getImgLink(itemDescription);
// alert(imgLink);
newDesc = createDescription(itemTitle, itemLink, imgLink, listPrice, dealPrice, youSave);
//alert(newDesc);
}
}
itemDescriptions += "<tr>" + newDesc + "</tr>";
}
itemDescriptions += "</table>";
tabDatas[tabName] = itemDescriptions;
if(frameDataFilled == "F") {
tabs.setSelectedTab(tabName2Ind[tabName]);
frameDataFilled = "T";
} else if (tabName == "GoldBox") {
tabs.setSelectedTab(tabName2Ind[tabName]);
}
},
{
refreshInterval: 600000 // unit millisec
}
);
}

_IG_RegisterOnloadHandler(initialize);

</script>
]]>
</Content>
</Module>


