<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs directory_title="AnyCam"
               title="__UP_title__" 
               height="245"
               author="Arsenio Santos"
               author_email="arsenios@creative360.com"
               author_location="San Francisco, CA"
               screenshot="http://www.creative360.com/stuff/google/anycam.jpg"
               singleton="false"
               description="Now, you can easily embed your favorite webcam images within your Google homepage! This module allows you to display any publicly-available image accessible via URL. You can also rotate through a list of multiple cam images within a single frame -- just enter a list of URLs separated by spaces, and let AnyCam do the rest. This module also supports a configurable refresh rate, and optional title, cam homepage URL, and image borders."
  />
  <UserPref name="title" 
            display_name="Title" required="true" default_value="AnyCam"/>
  <UserPref name="imgurl" 
            display_name="Cam Image URLs"
            required="true" />
  <UserPref name="weburl" 
            display_name="Cam Homepage" required="false" />
  <UserPref name="refresh" 
            display_name="Refresh (in minutes)" required="true" 
            default_value="10" />
  <UserPref name="border" 
            datatype="bool" display_name="Image Border?" default_value="1" />
  <UserPref name="uncache" 
            datatype="bool" display_name="Force uncached?" default_value="1" />
   <Content type="html"> 
     <![CDATA[ 
<style type="text/css">
p.anycam {
	font-family: arial,sans-serif;
	font-size: 80%
}
</style>
<div id=anycam__MODULE_ID__ class="anycam"></div>
<script language="JavaScript">
//<!--
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// *  AnyCam, a Google Homepage module                               *
// *  ------- - ------ -------- ------                               *
// *  by Arsenio Santos <arsenios@creative360.com>                   *
// *                                                                 *
// *  Now, you can easily embed your favorite webcam images within   *
// *  your Google homepage! This module allows you to display any    *
// *  publicly-available image accessible via URL. This module also  *
// *  supports a configurable refresh rate, and optional title, cam  *
// *  homepage URL, and image borders.                               *
// *                                                                 *
// *  Currently, AnyCam can only display cams at a pixel resolution  *
// *  of 320x240. Note that you can use cam images of any size you   *
// *  please, but the browser will resize accordingly. Cam images    *
// *  which are already at a size of 320x240 will look the best.     *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//
var prefs__MODULE_ID__ = new _IG_Prefs(__MODULE_ID__);   // User prefs
var camdiv__MODULE_ID__ = _gel('anycam__MODULE_ID__');   // image DIV object

// js_loadImg is the prefetch image object
//
var js_loadImg__MODULE_ID__;

// The rotation variable will help determine which image to display after
// a refresh
var rotation__MODULE_ID__ = 0;

// Now let's take the imgurl parameter and split it apart into multiple
// values, in case there's a space in the string. (If not, it'll just be
// a slightly more costly way of getting the whole value.)
var imgurl__MODULE_ID__ = prefs__MODULE_ID__.getString("imgurl").replace(
					"\"", "\\\"");
imgurl__MODULE_ID__ = imgurl__MODULE_ID__.replace("  ", " ");
var imgurls__MODULE_ID__ = new Array();
imgurls__MODULE_ID__ = imgurl__MODULE_ID__.split(' ');

var imgurldatechars__MODULE_ID__ = new Array();
for (var x = 0; x < imgurls__MODULE_ID__.length; x++) {
	imgurl__MODULE_ID__ = imgurls__MODULE_ID__[x];
	// imgurldatechar is a URL delimiter for support of noncached IMG urls.
	// It's either "?" or "&", depending on whether the imgurl already has
	// a query string component.
	//
	if (imgurl__MODULE_ID__.indexOf("?") > -1) {
		imgurldatechars__MODULE_ID__[x] = "&";
	} else {
		imgurldatechars__MODULE_ID__[x] = "?";
	}
}
//imgurl__MODULE_ID__ = imgurls__MODULE_ID__[0];
////var imgurldatechar__MODULE_ID__ = imgurldatechars__MODULE_ID__[0];

// Here are a few more user prefs, with some data validation logic behind them.
//
var border__MODULE_ID__ = prefs__MODULE_ID__.getInt("border");
var uncache__MODULE_ID__ = prefs__MODULE_ID__.getInt("uncache");
var weburl__MODULE_ID__ = prefs__MODULE_ID__.getString("weburl").replace(
					"\"", "");
var refresh__MODULE_ID__ = prefs__MODULE_ID__.getInt("refresh");
if (refresh__MODULE_ID__ <= 0)
{
	alert("Please put a whole number in the Refresh field.");
	imgurl__MODULE_ID__ = "";
	weburl__MODULE_ID__ = "";
}

// This is as close as we get to a main() entry point: if there's a configured
// imgurl, preload the image in our prefetch object; otherwise, throw control
// to the init() function, which will simply prompt the user for configuration.
//
if (imgurl__MODULE_ID__.length > 0)
{
	js_loadImg__MODULE_ID__ = new Image();
	js_loadImg__MODULE_ID__.src = getImgUrl__MODULE_ID__();
	setTimeout("preloadImg__MODULE_ID__()", 200);
}
else
{
	init_anycam__MODULE_ID__();
}

// The preloadImg() function checks the 'complete' property of the loading
// image every 1/5th of a second. On completion, it kicks control to init()
//
function preloadImg__MODULE_ID__()
{
	if (js_loadImg__MODULE_ID__.complete)
	{
		init_anycam__MODULE_ID__();
	}
	else
	{
		setTimeout("preloadImg__MODULE_ID__()", 200);
	}
}

// The init_anycam() function will look at the imgurl and weburl user prefs,
// and build an appropriate chunk of HTML for displaying the inline cam image.
// Also, the reload timer is started.
//
// If the config is missing, a helpful text block is displayed instead. If
// for some other reason the image doesn't load, a different block of text is
// shown. 
function init_anycam__MODULE_ID__()
{
	var camtext__MODULE_ID__ = "";

	if (weburl__MODULE_ID__.length > 0)
	{
		camtext__MODULE_ID__ += 
			"<a href=\"" + weburl__MODULE_ID__ + "\" "
			+ "target=\"camurl__MODULE_ID__\">";
	}
	if (imgurl__MODULE_ID__.length > 0)
	{
		camtext__MODULE_ID__ +="<img id='anycamimg__MODULE_ID__' src=\""
			+ js_loadImg__MODULE_ID__.src + "\" "
			+ "border=\"" + border__MODULE_ID__ 
			+ "\" width=\"320\" height=\"240\">";
	}
	if (weburl__MODULE_ID__.length > 0)
	{
		camtext__MODULE_ID__ += "</a>";
	}

	// If there's a configured imgurl, show the inline image tag text
	// but ONLY if the preloaded image actually loaded successfully;
	// otherwise, show an error message. Oh, and if there's no imgurl
	// configuration, prompt for one.
	//
	if (imgurl__MODULE_ID__.length > 0
		&& js_loadImg__MODULE_ID__.height > 0)
	{
		camdiv__MODULE_ID__.innerHTML = 
				"<center>" + camtext__MODULE_ID__ + "</center>";
		setTimeout("reloadImg__MODULE_ID__()", 
				(60000 * parseInt(refresh__MODULE_ID__)));
		js_loadImg__MODULE_ID__ = null;
	}
	else
	if (imgurl__MODULE_ID__.length > 0)
	{
		camdiv__MODULE_ID__.innerHTML = "<center>\n<p class=\"anycam\">"
		+ "This image could not be loaded. Please check your "
		+ "configuration information and try again, or click "
		+ "<a href=\"javascript:reloadImg__MODULE_ID__();\">here</a> "
		+ "to try reloading the image.</p>\n"
		js_loadImg__MODULE_ID__ = null;
	}
	else
	{
		camdiv__MODULE_ID__.innerHTML = "<center>\n<p class=\"anycam\">"
		+ "To use AnyCam, click on the <b>edit</b> link above<br />\n" 
		+ "and configure a web cam for viewing.</p>\n<p "
		+ "class=\"anycam\">If you're looking for a webcam to view,"
		+ "<br /><a href=\"http://www.google.com/search?q=webcams\" "
		+ "target=\"googleforcams\">why not ask Google</a>?</p>"
		+ "</center>\n";
	}
}


// The reloadImg function grabs a date-unique'd version of imgurl and loads
// it into the existing image object. It also restarts the reload timer for
// another reload.
//
function reloadImg__MODULE_ID__()
{
	rotation__MODULE_ID__ = rotation__MODULE_ID__ + 1;
	if (rotation__MODULE_ID__ >= imgurls__MODULE_ID__.length) {
		rotation__MODULE_ID__ = 0;
	}
	if (document.images)
	{
		var imgsrc__MODULE_ID__ = getImgUrl__MODULE_ID__();
		var image__MODULE_ID__ = _gel('anycamimg__MODULE_ID__');
		if (image__MODULE_ID__)
		{
			image__MODULE_ID__.src = imgsrc__MODULE_ID__;
			setTimeout("reloadImg__MODULE_ID__()", 
				(60000 * parseInt(refresh__MODULE_ID__)));
		}
	}
}


// The getImgUrl function guarantees a unique, uncached load of the cam image
// for every execution (so long as you only execute the request once per
// second).
//
// Note that this will send an unexpected query string to the remote server.
// My apologies to cam server webmasters everywhere.
//
function getImgUrl__MODULE_ID__()
{
	if (uncache__MODULE_ID__) {
		var now__MODULE_ID__ = new Date;
		return imgurls__MODULE_ID__[rotation__MODULE_ID__]
		    + imgurldatechars__MODULE_ID__[rotation__MODULE_ID__]
		    + now__MODULE_ID__.getTime();
	} else {
		return imgurls__MODULE_ID__[rotation__MODULE_ID__];
	}
}
//-->
</script>
     ]]> 
   </Content> 
</Module>
