<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <!--
Copyright 2008 lassytouton@gmail.com
-->
  <ModulePrefs
    title="Cheeky Quotes"
    title_url="http://www.CheekyQuotes.com/"
    description="Get random funny quotes every day with Cheeky Quotes ... updates on every page visit. Click on a quote to send it to your twitter account. Visit www.CheekyQuotes.com for more humor, wit and sarcasm from a variety of famous personalities."
    thumbnail="http://hosting.gmodules.com/ig/gadgets/file/112764733758979090903/cheekyquote-thumbnail.png"
    screenshot="http://hosting.gmodules.com/ig/gadgets/file/112764733758979090903/cheekyquote-screenshot.png"
    directory_title="Funny Quotes, Comedy and Jokes"
    category="lifestyle"
    category2="funandgames"
    width="320"
    height="200"
    scrolling="true"
    author="lassytouton"
    author_email="webmaster@cheekyquotes.com"
    author_photo="http://hosting.gmodules.com/ig/gadgets/file/112764733758979090903/lassytouton.png"
    author_aboutme="I used to have super powers but the psychiatrist took them all away."
    author_link="http://www.CheekyQuotes.com/"
    author_quote="If I agreed with you we'd both be wrong. (Anonymous)">

    <Require feature="opensocial-0.8"/>
    <Require feature="skins"/>
  </ModulePrefs>

  <Content type="html">
    <![CDATA[

<!--
Cheeky Quotes. You may also want to check out these related items: Funny, Quote, Quotes, Joke, Jokes, Daily, Day, Humor, Humour, Famous, Comedy

Copyright 2008 lassytouton@gmail.com
-->

<div id="cheekyquote_content"></div>
<script type="text/javascript">

var iconBaseURL = "http://hosting.gmodules.com/ig/gadgets/file/112764733758979090903/";

function getGadgetHost()
{
  var gadgetHost = "iGoogle";

  try
  {
    if (location.hostname.search(/blogger/i) != -1)
    {
      gadgetHost = "blogger";
    }
  }
  catch(e)
  {
    gadgetHost = "iGoogle";
  }

  return gadgetHost;
}

function wrapPNG(image)
{
  var wrappedPNG = image;

  var srcRegEx = new RegExp("src=[\"'](.*?)[\"']", "g");

  var srcMatches = srcRegEx.exec(image);
  
  if (srcMatches)
  {
    if (srcMatches.length == 2)
    {
      if (srcMatches[1].match(/\.png/i))
      {
        wrappedPNG = "<!--[if IE]><![if gt IE 6]><![endif]-->";

        wrappedPNG = wrappedPNG + image;

        wrappedPNG = wrappedPNG + "<!--[if IE]><![endif]><![endif]-->";

        wrappedPNG = wrappedPNG + "<!--[if lte IE 6]>";

        var iconBlank = iconBaseURL + "blank.gif";
  
        var parameters = {};
  
        parameters[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 60 * 60 * 24;

        var srcReplace = "src=\"" + encodeURI(gadgets.io.getProxyUrl(iconBlank, parameters)) + "\" style=\"filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + srcMatches[1] + "', sizingMethod='image');\"";

        wrappedPNG = wrappedPNG + image.replace(srcRegEx, srcReplace);

        wrappedPNG = wrappedPNG + "<![endif]-->";
      }
    }
  }
  
  return wrappedPNG;
}

function skinsColor(skinsItem, defaultColor)
{
  var color = defaultColor;

  try
  {
    color = gadgets.skins.getProperty(skinsItem);
  }
  catch(e)
  {
    color = defaultColor;
  }
  
  return color;
}

function update(response)
{
//  var cachedResponse = response.text;
//  
//  var regex = new RegExp("(img |\\()src=[\"'](.*?)[\"']", "g");
//
//  var match;
//
//  // use an associative array to eliminate duplicate image filenames
//  var images = new Array();
//
//  while ((match = regex.exec(cachedResponse)) != null)
//  {
//    if (match.length > 2)
//    {
//      images[match[2]] = null;
//    }
//  }
//
//  var parameters = {};
//  
//  parameters[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 60 * 60 * 24;
//
//  for (var image in images)
//  {
//    cachedResponse = cachedResponse.split(image).join(encodeURI(gadgets.io.getProxyUrl(image, parameters)));
//  }
//
//  _gel("cheekyquote_content").innerHTML = cachedResponse;

  var cachedResponse = response.data;

  var TWITTER_MAX_MESSAGE_LENGTH = 140;
             
  var icons = new Array({"url":iconBaseURL + "cheekyquote-cm-75x75.png", "link":"http://homepage.usask.ca/~cdm518/", "text":"Carl McCrosky's Photos"},
                        {"url":iconBaseURL + "cheekyquote-tl-75x75.png", "link":"http://blog.thomaslaupstad.com/", "text":"Photos from Northern Norway"});
                                      
  var icon = icons[Math.floor(Math.random() * icons.length)];
  
  var html = "<div id=\"watermark\" style=\"z-index: 1; float: right; padding-top: 5px; padding-right: 10px;\"><a href=\"" + encodeURI(icon.link) +  "\" rel=\"nofollow\" target=\"_blank\">";
  
  var parameters = {};
  
  parameters[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 60 * 60 * 24;

  var image = "<img src=\"" + encodeURI(gadgets.io.getProxyUrl(icon.url, parameters)) + "\" alt=\"" + icon.text + "\" title=\"" + icon.text + "\" width=\"75\" height=\"75\" style=\"border: 0;\"/>";
    
  html = html + wrapPNG(image);
    
  html = html + "</a></div><div id=\"quote\" style=\"z-index: 2; font-size: 13px; padding: 10px;\">";
  
  var quoteSearchTags = new Array(" www.CheekyQuotes.com funny quotes",
                                  " www.CheekyQuotes.com quotes",
                                  " www.CheekyQuotes.com funny",
                                  " www.CheekyQuotes.com",
                                  " funny quotes");

  var quoteLength = cachedResponse.quote.length;
    
  var authorCitation = "";
    
  if ((quoteLength + 3 + cachedResponse.author.length) <= TWITTER_MAX_MESSAGE_LENGTH)
  {
    authorCitation = " ~ " + cachedResponse.author;
      
    quoteLength += (3 + cachedResponse.author.length);
  }
    
  var quoteSearchTag = "";
  
  var rndQuoteSearchTag = Math.floor(Math.random() * 2);

  if (rndQuoteSearchTag == 0)
  {
    for(var idx = 0; idx < quoteSearchTags.length; idx++)
    {
      if ((quoteLength + quoteSearchTags[idx].length) <= TWITTER_MAX_MESSAGE_LENGTH)
      {
        quoteSearchTag = quoteSearchTags[idx];
          
        break;
      }
    }
  }
  else
  {
    if ((quoteLength + quoteSearchTags[quoteSearchTags.length - 1].length) <= TWITTER_MAX_MESSAGE_LENGTH)
    {
      quoteSearchTag = quoteSearchTags[quoteSearchTags.length - 1];
    }
  }

  var quoteAnchor = "";
  
  if (quoteLength <= TWITTER_MAX_MESSAGE_LENGTH)
  {
    quoteAnchor = "<a id=\"quoteLink\" href=\"http://twitter.com/home?status=" + encodeURIComponent(cachedResponse.quote + authorCitation + quoteSearchTag) + "\" title=\"Twitter this quote.\" style=\"text-decoration: none; color: #005696;\" target=\"_blank\" rel=\"nofollow\">" + cachedResponse.quote + "</a>";
  }
  else
  {
    quoteAnchor = "<a id=\"quoteLink\" href=\"\" onclick=\"event.returnValue=false; return false;\" title=\"Quote too long for twitter.\" style=\"text-decoration: none; color: #005696;\">" + cachedResponse.quote + "</a>";
  }
                 
  html = html + quoteAnchor;
  
  html = html + "<br/><br/><div id=\"author\" style=\"color: black;\"><b style=\"font-size: 12px;\">" + cachedResponse.author + "</b></div><br clear=\"all\"/></div><div id=\"linkbar\" style=\"z-index: 3; font-size: 12px; padding-top: 2px; padding-bottom: 4px;\"><div><span style=\"float: left; padding-left: 10px;\"><a id=\"authorQuotesLink\" href=\"http://www.CheekyQuotes.com/google/authorquotes/author/" + cachedResponse.author.replace(" ", "%20") + "/\" style=\"text-decoration: underline; color: #005696;\" target=\"_blank\">author&nbsp;quotes</a></span></div><div><span style=\"float: left; padding-left: 10px;\"><a id=\"randomQuotesLink\" href=\"http://www.CheekyQuotes.com/\" style=\"text-decoration: underline; color: #005696;\" target=\"_blank\">random&nbsp;quotes</a></span></div></div>";

  _gel("cheekyquote_content").innerHTML = html;
  
  document.body.style.backgroundColor = skinsColor("CONTENT_BG_COLOR", "#FFFFFF");

  _gel("author").style.color = skinsColor("CONTENT_TEXT_COLOR", "#000000");

  var linkColor = skinsColor("CONTENT_LINK_COLOR", "#005696");

  _gel("quoteLink").style.color = linkColor;
  _gel("authorQuotesLink").style.color = linkColor;
  _gel("randomQuotesLink").style.color = linkColor;
}

//var contentURL = "http://www.CheekyQuotes.com/google/update.php";
var contentURL = "http://www.CheekyQuotes.com/google/updateJSON.php";

contentURL += "?randomize=" + Math.floor(Math.random() * 100).toString();

//contentURL += "&consumer=google";

//contentURL += "&analyticsCode=UA-9804151-3";

//contentURL += "&gadgetHost=" + getGadgetHost();

var parameters = {};

//parameters[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
parameters[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
parameters[gadgets.io.RequestParameters.REFRESH_INTERVAL] = 60 * 30;

// Fetch fresh content every half hour
gadgets.io.makeRequest(contentURL, update, parameters);

</script>
<!-- Google Analytics -->
<script type="text/javascript">

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

</script>
<script type="text/javascript">

var pageTracker = null;

try
{
  pageTracker = _gat._getTracker("UA-9804151-3");
}
catch(err)
{
  pageTracker = null;
}

function trackPageview(page)
{
  if (pageTracker)
  {
    try
    {
      pageTracker._trackPageview(page);
    }
    catch(err)
    {
    }
  }
}

trackPageview("/CheekyQuotes/");

</script>
]]>

  </Content>
</Module>
