<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs  
    title="Engadget Mobile"
    title_url="http://www.engadgetmobile.com/"
    directory_title="Engadget Mobile latest story titles"
    description="Get the latest story titles from EngadgetMobile.com on your iGoogle page, along with a thumbnail of the story picture."
    screenshot="http://www.ljmsite.com/google/gadgets/engadget/engadgetmobile_screenshot.png"
    thumbnail="http://www.ljmsite.com/google/gadgets/engadget/engadgetmobile_thumbnail.png"
    author="Jerome Mouton"
    author_email="igoogle_gadgets@ljmsite.com"
    author_location="Huntsville, AL, USA"
    author_affiliation="LjmSite"
    author_aboutme="Google API gadget and web application developer, tech project manager in globally dispersed team environment, computer geek on call, etc. Come check me out at LjmSite.com and contact me to discuss gadget projects or opportunities."
    author_link="http://www.ljmsite.com/"
    author_photo="http://www.ljmsite.com/google/gadgets/jerome.png"
    singleton="false"
    height="300">
    <Require feature="analytics"/>
    <Require feature="dynamic-height"/>
  </ModulePrefs>
  <UserPref name="nbstories" display_name="Number of stories" datatype="enum" default_value="5">
    <EnumValue value="1"/>
    <EnumValue value="2"/>
    <EnumValue value="3"/>
    <EnumValue value="4"/>
    <EnumValue value="5"/>
    <EnumValue value="6"/>
    <EnumValue value="7"/>
    <EnumValue value="8"/>
    <EnumValue value="9"/>
  </UserPref>
  <UserPref name="fontsize" display_name="Text size" datatype="enum" default_value="14px">
    <EnumValue value="12px" display_value="Small" />
    <EnumValue value="14px" display_value="Medium" />
    <EnumValue value="16px" display_value="Large" />
  </UserPref>
  <Content type="html">  
<![CDATA[

<style type="text/css">
* {
   font-family:"trebuchet ms",trebuchet,arial,helvetica,sans-serif;
   text-align:left;
   vertical-align:middle;
   font-size:__UP_fontsize__;
   color:#000000;
   background-color:#ffffff;
}
a, a:link, a:visited {
    color:#008EB4;
    text-decoration:none;
}
a:hover {
    background-color:#008EB4;
    color:#FFFFFF;
}
.image {
   text-align:center;
}
img {
   max-width:70px;
   overflow:hidden;
}
</style>

<div id="content" />

<script type="text/javascript">

// Global variables to track the success of fetching the feed
var fetchSuccessfull = false;

function getSubstring(data, pre, post) {
        var val = '';
        var start = data.indexOf(pre);
        if(start != -1) {
                start += pre.length;
                var data2 = data.substring(start);
                var end = data2.indexOf(post);
                if(end != -1) {
                        val = data2.substring(0, end);
                }
        }
        return val;
}

function getWidth() {
        var w;
        if(self.innerHeight) {
                w = self.innerWidth;
        }
        else if(document.documentElement && document.documentElement.clientHeight) {
                w = document.documentElement.clientWidth;
        }
        else if(document.body) {
                w = document.body.clientWidth;
        }
        return w;
}

function getHtmlOneStory(title, url, imageUrl, imageWidth) {
        var storyHtml = '<tr><td class="image"><a href="' + url + '" target="_blank">'
                      + '<img src="' + imageUrl + '" border="0" height="50" alt="' + title + '" />'
                      + '</a></td><td><a href="' + url + '" target="_blank">' + title + '</a></td></tr>';

        return storyHtml;
}


function displayStories(feed) {
        var storiesHtml = '<table border=0>';

        // Set the thumbnail size to 1/3 of the gadget width, but no bigger than 70 pixels
        var imageWidth = getWidth() / 3;
        if(imageWidth > 70) {
                imageWidth = 70;
        }

        var storyId = 0;
        for(var i = 0; (i < feed.Entry.length) && (storyId < __UP_nbstories__); i++) {

                // Find the URL of the story image for the thumbnail
                var imageUrl = getSubstring(feed.Entry[i].Summary, 'src="', '"');

                // Default picture in case none is to be found
                if(imageUrl == '') {
                        imageUrl = 'http://www.ljmsite.com/google/gadgets/engadget/engadget_logo.gif';
                }

                // Get the image cached at Google
                imageUrl = _IG_GetImageUrl(imageUrl);

                var title = feed.Entry[i].Title.replace(/"/g, '&quot;');

                storiesHtml += getHtmlOneStory(title, feed.Entry[i].Link, imageUrl, imageWidth);
                
                storyId++;
        }

        storiesHtml += '</table>';
        _gel("content").innerHTML = storiesHtml;

        // Adjust height but do it again in 5 seconds in case some image are slow to come
        setTimeout('_IG_AdjustIFrameHeight()', 5000);
        _IG_AdjustIFrameHeight();

        return;
}

// Top level fetcher which initiate the assynchronous function
function fetchStories() {
        _IG_FetchFeedAsJSON('http://www.engadgetmobile.com/rss.xml', function (response) {
                if (response == null || response.Entry == null) {
                        _gel("content").innerHTML = "<i>Error fetching data.<br />Stand by as we retry...</i>";
                }
                else {
                        fetchSuccessfull = true;
                        displayStories(response);
                }
                return;
        }, __UP_nbstories__ + 5, true);
}

// Wrapper to the fetchStories function which manage the retries
// every 10 seconds in case _IG_FetchFeedAsJSON() fails
function fetchStoriesWrapper() {
        if(!fetchSuccessfull) {
                fetchStories();
                setTimeout('fetchStoriesWrapper()',10000);
        }
        return;
}

function init() {
        _gel("content").innerHTML = "<i>Getting latest stories...</i>";
        fetchStoriesWrapper();
        setTimeout('_IG_Analytics("UA-1189083-1", "/EngadgetMobile/1")', 6000);
        return;
}

_IG_RegisterOnloadHandler(init);

</script>

]]> 
  </Content>
</Module>
