<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs height="235" width="325" 
  title="Lenovo Promotions" 
  title_url = "http://www.lenovo.com"
  author= "Lenovo" 
  author_email = "LenovoGadgets@gmail.com"
  description = "Discounts, promotions, and other offerings from Lenovo"
  thumbnail= "http://hosting.gmodules.com/ig/gadgets/file/116568160537417525399/icon.png"
  screenshot="http://hosting.gmodules.com/ig/gadgets/file/116568160537417525399/screenshot.jpg"/>

  <Content type="html" preferred_width="325" preferred_height="235">
  <![CDATA[
	<script type="text/javascript" src="http://www.google.com/jsapi"></script>
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
	<script type="text/javascript" src="http://hosting.gmodules.com/ig/gadgets/file/116568160537417525399/slideshow.js"></script>
	<link rel="stylesheet" type="text/css" href="http://hosting.gmodules.com/ig/gadgets/file/116568160537417525399/withStyle.css" />
    <script type="text/javascript">
	// Version 1.0
	
	// Settings
	var secondsToCacheManifest = 60*60*12; 	// 12 hours
	var secondsToCacheImages = 60*60*12; 	// 12 hours
	var dayOfMonth = (new Date()).getDate();
	var cacheBust = "?i=" + dayOfMonth; // Force cache bust each day
	var manifestPattern = "http://www.lenovo.com/gadgets/Google/Think/Content/{profile}/offers.json"		// {profile} will be replaced with the region
	
	// Entry point
	function initialize()
	{
		try {
			$("#slider").hide();
			$("#error").hide();
			fetchLocalData();
		}
		catch(e){friendlyError(e);}
	}

	// Transforms list into slideshow
	// Called after data is fetched and transformed
	function startTheShow()
	{
		try {
			$("#slider").easySlider({
				auto: true,
				continuous: true,
				pause:  7000,
				width: 325,
				height: 225
			});    
			$("#slider").fadeIn();  
		}
		catch(e){friendlyError(e);}
	}
  
	// Determines the user's current language
	// Returns first two letters (language)
	// Except for pt-BR, zh-*
	function getCurrentCulture()
	{
		try {
			var language = "";
			var browserLanguage = window.navigator.userLanguage || window.navigator.language;
			browserLanguage = browserLanguage.toLowerCase();
			
			// Grab first two chars
			language = browserLanguage.substr(0, 2);
			
					// Preserve specific
			if(browserLanguage == "pt-br")	
			{
				language = "pt-br";
			}
			else 	// Map to traditional
			if(browserLanguage == "zh-hk" || browserLanguage == "zh-tw")	
			{
				language = "cht";
			}
			else 	// Map to simplified
			if(language == "zh")		
			{
				language = "chs";
			}
			return language;
		}
		catch(e){friendlyError(e);}
	}
	
	// Forms a URL to where the manifest should be
	// BOOL useLocalized = Whether or not to use the browsers language 
	function getPathToManifest(useLocalized)
	{
		try {
			var profile = "default";
			if(useLocalized == true)
			{
				profile = getCurrentCulture();
			}
			return manifestPattern.replace("{profile}", profile) + cacheBust;
		}
		catch(e){friendlyError(e);}
	}

  
	// Makes a requst for content in the user's language
	function fetchLocalData() {
		try {
			var params = {};
			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
			url = getPathToManifest(true);
			makeCachedRequest(url, responseLocal, params, secondsToCacheManifest);
		}
		catch(e){fetchDefaultData();}
	}
  
	// Makes a request for the default, non geo-targetted content
	function fetchDefaultData() {
		try {
			var params = {};
			params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
			url = getPathToManifest(false);
			makeCachedRequest(url, responseDefault, params, secondsToCacheManifest);
		}
		catch(e){friendlyError(e);}
	}
	
	// Response from fetching localized content
	
	function responseLocal(obj) {
		try {
			if(obj.data == undefined) {
				fetchDefaultData();
			}
			else
			{
				processJson(obj.data);
			}
		}
		catch(e){friendlyError(e);}
	};
   
	// Response from fetching generic content
	function responseDefault(obj) {
		try {
			if(obj.data == undefined) {
				friendlyError(e);
			}else
			{
				processJson(obj.data);
			}
		}
		catch(e){friendlyError(e);}
	};
  
	// Processes the json data and transforms into unordered list
	function processJson(json) {
		try {
			var list = document.createElement('ul');
			$.each(json.offers, function(index, item)
			{
				var listItem = document.createElement('li');
				list.appendChild(listItem);

				//var image = document.createElement('img');
				//image.src = item.img_url;
				var image = _IG_GetImage(item.img_url + cacheBust, { refreshInterval: secondsToCacheImages });

				var link = document.createElement('a');
				link.href = item.link_url;
				link.target = "_BLANK";
				link.appendChild(image);

				listItem.appendChild(link);

				var para = document.createElement("p");
				var text = document.createTextNode(item.note);
				para.appendChild(text)
				listItem.appendChild(para);
			});
			// Insert list into #slider div
			$("#slider").append(list);
			// Start slideshow in 1 second
			var t = setTimeout(startTheShow, 1000);
		}
		catch(e){friendlyError(e);}
		
	}
	
	// Performs a request using Google's Cache
	function makeCachedRequest(url, callback, params, refreshInterval)
	{
		var ts = new Date().getTime();
		var sep = "?";
		if (refreshInterval && refreshInterval > 0) {
			ts = Math.floor(ts / (refreshInterval * 1000));
		}
		if (url.indexOf("?") > -1) {
			sep = "&";
		}
		url = [ url, sep, "nocache=", ts ].join("");
		gadgets.io.makeRequest(url, callback, params);
	}
	
	// Last resort.  Insteady of displaying error, show content
	function friendlyError(exception)
	{
		$("#error").fadeIn('slow');
		// Throw so it can be collected by error console
		setTimeout("throw new Error('Error: "+(exception.message ? exception.message : exception)+"')",0); 
	}
	
	gadgets.util.registerOnLoadHandler(initialize);
	</script>
	
	<div id="error">
		<a href="http://www.lenovo.com/accessories" target="_BLANK"><img border="0" src="http://hosting.gmodules.com/ig/gadgets/file/116568160537417525399/default.jpg" width="325" height="225" /></a>
	</div>

	<!-- Content will be injected into #slider -->
	<div id="content">
		<div id="slider"></div>
	</div>
  
  ]]>
  </Content>
</Module>