<?xml version="1.0" encoding="UTF-8" ?>
<Module>
	<ModulePrefs
		title="Music, Movies, Books Finder"
		title_url="http://tastekid.com"
		directory_title="Music Finder"
		description="Just type in your favorite bands, movies and/or books, and you will get recommendations based on what you like. A gadget that helps you explore your taste. Brought to you by Emmy the Taste Kid."
		screenshot="http://tastekid.com/gadget/files/screenshot.png"
		thumbnail="http://tastekid.com/gadget/files/thumbnail.png"
		height="200"
		scrolling="true"
		author="Felix Oghina"
		author_email="felix.oghina@gmail.com"
		author_affiliation="none"
		author_location="Bucharest, Romania"
		author_photo="http://i80.photobucket.com/albums/j161/znupi/gclock/author_photo.png"
		author_aboutme="I'm a college student at the Faculty of Mathematics and Informatics at the University of Bucharest. I write code for breakfast."
		author_quote="if you think every day that today is the last day of your life, one day you'll certainly be right!"
	>
		<Require feature="dynamic-height" />
		<Require feature="minimessage" /> 
		<Require feature="analytics" />
	</ModulePrefs>
	<Content type="html">
	<![CDATA[
		<style type="text/css">
			* { margin: 0; padding: 0; }
			div.inputDiv {
				text-align: center;
			}
			div.inputDiv input {
				background: url("http://tastekid.com/gadget/files/input.gif") 0 0 no-repeat;
				border: none;
				font: bold 16px "Trebuchet MS", Arial, sans-serif;
				padding: 4px 5px 4px 10px;
				vertical-align: middle;
				width: 80%;
				color: #fff;
			}
			div.inputDiv button {
				background: url("http://tastekid.com/gadget/files/button.gif") 0 0 repeat-y;
				border: none;
				width: 40px;
				height: 30px;
				vertical-align: middle;
				cursor: pointer;
			}
			div.inputDiv button:hover {
				background-position: 0 -30px;
			}
			div.inputDiv button span {
				display: none;
			}
			div#loader {
				position: absolute;
				left: 0;
				top: 0;
				width: 100%;
				height: 100%;
				background: url("http://tastekid.com/gadget/files/load.gif") 50% 50% no-repeat #A5A5A5;
				opacity: 0.5;
				filter: alpha(opacity=50);
				display: none;
			}
			div#expand {
				display: none;
			}
			div#collapse {
				background: url("http://tastekid.com/gadget/files/collapse.gif") 50% 50% no-repeat #F5F5F5;
				height: 30px;
				cursor: pointer;
			}
			div#collapse:hover {
				background-color: #EFEFEF;
			}
			div#content {
				margin: 5px 0;
				min-height: 130px;
			}
			h2 {
				font: bold 16px "Trebuchet MS", "Arial", sans-serif;
				color: #2592A9;
			}
			p {
				font: 12px "Trebuchet MS", "Arial", sans-serif;
			}
			form {
				display: inline;
			}
			a, a:visited {
				color: #00f;
			}
			a:hover {
				text-decoration: none;
			}
		</style>
		<script type="text/javascript">
			_IG_AdjustIFrameHeight(30);
			
			String.prototype.ucwords = function() {
				return (" " + this.toLowerCase()).replace(/\s+\w/g, function(s) {
    			return s.substr(0, s.length-1) + s.charAt(s.length-1).toUpperCase();
				}).substr(1)
			}
			// Internet Explorer is too dumb to know about Array.indexOf, although
			// it's been around since 2005 in most other browsers..
			if (!Array.indexOf) {
				Array.prototype.indexOf = function(obj) {
					var i;
					for (i=0; i < this.length; i++) {
						if (this[i] == obj) return i;
					}
					return -1;
				}
			}
			
			var maxItems = 1000;
			
			var mini = new _IG_MiniMessage(__MODULE_ID__);
			var order = [ ];
			var suggestions = { music: [ ], movies: [ ], shows: [ ], books: [ ], authors: [ ] };
			function inputFocused() {
				if (_gel('q').value == 'Favorite bands/movies/books') _gel('q').value = '';
			}
			function inputBlurred() {
				if (_gel('q').value == '') _gel('q').value = 'Favorite bands/movies/books';
			}
			function makeSmall() {
				_gel('expand').style.display = "none";
				_IG_AdjustIFrameHeight(30);
			}
			function fetchData() {
				_gel('loader').style.display = "block";
				var loc = "http://www.tastekid.com/ask/ws?g=1&q=" + encodeURIComponent(_gel('q').value);
				_IG_FetchXmlContent(loc, handleData);
				return false;
			}
			function handleData(xml) {
				if (xml == null || typeof(xml) != "object" || xml.firstChild == null) {
					mini.createTimerMessage("There was a problem communicating with the server. Please try again in a few moments.", 5, makeSmall);
					_IG_AdjustIFrameHeight(60);
					_gel('loader').style.display = "none";
					return;
				}
				suggestions.music   = [ ];
				suggestions.movies  = [ ];
				suggestions.shows   = [ ];
				suggestions.books   = [ ];
				suggestions.authors = [ ];
				var resources = xml.getElementsByTagName("results").item(0).getElementsByTagName("resource");
				if (!resources.length) {
					html = "<h2>No Recommendations Found</h2><p>Please write only bands, artists, movies or book titles, separated by commas. E.g.: Beethoven, Britney Spears, Trainspotting</p>";
					_gel('content').innerHTML = html;
					_gel('expand').style.display = "block";
					_IG_AdjustIFrameHeight(200);
					_gel('loader').style.display = "none";
					return;
				}
				var rName;
				var rType;
				for (var i=0; i < Math.min(resources.length, 20); i++) {
					rName = resources.item(i).getElementsByTagName("name").item(0).firstChild.nodeValue;
					rType = resources.item(i).getElementsByTagName("type").item(0).firstChild.nodeValue;
					if (order.indexOf(rType) == -1) order.push(rType);
					switch(rType) {
						case "music": suggestions.music.push(rName); break;
						case "movie": suggestions.movies.push(rName); break;
						case "show": suggestions.shows.push(rName); break;
						case "book": suggestions.books.push(rName); break;
						case "author": suggestions.authors.push(rName); break;
					}
				}
				var loc = "http://www.tastekid.com/ask?q=" + encodeURIComponent(_gel('q').value) + "&g=1";
				
				if (suggestions.music.length) {
					var music = "<h2>Recommended Music</h2><p>";
					for (i = 0; i < Math.min(suggestions.music.length, maxItems); i++) {
						music += suggestions.music[i].ucwords() + ", ";
					}
					music = music.substr(0, music.length-2);
					music += " <a href=\"" + loc + "\" target=\"_blank\">More from Emmy &raquo;</a>";
					music += "</p>";
				}
				
				if (suggestions.movies.length) {
					var movies = "<h2>Recommended Movies</h2><p>";
					for (i = 0; i < Math.min(suggestions.movies.length, maxItems); i++) {
						movies += suggestions.movies[i].ucwords() + ", ";
					}
					movies = movies.substr(0, movies.length-2);
					movies += " <a href=\"" + loc + "\" target=\"_blank\">More from Emmy &raquo;</a>";
					movies += "</p>";
				}
				
				if (suggestions.shows.length) {
					var shows = "<h2>Recommended Shows</h2><p>";
					for (i = 0; i < Math.min(suggestions.shows.length, maxItems); i++) {
						shows += suggestions.shows[i].ucwords() + ", ";
					}
					shows = shows.substr(0, shows.length-2);
					shows += " <a href=\"" + loc + "\" target=\"_blank\">More from Emmy &raquo;</a>";
					shows += "</p>";
				}
				
				if (suggestions.books.length) {
					var books = "<h2>Recommended Books</h2><p>";
					for (i = 0; i < Math.min(suggestions.books.length, maxItems); i++) {
						books += suggestions.books[i].ucwords() + ", ";
					}
					books = books.substr(0, books.length-2);
					books += " <a href=\"" + loc + "\" target=\"_blank\">More from Emmy &raquo;</a>";
					books += "</p>";
				}
				
				if (suggestions.movies.length) {
					var authors = "<h2>Recommended Authors</h2><p>";
					for (i = 0; i < Math.min(suggestions.authors.length, maxItems); i++) {
						authors += suggestions.authors[i].ucwords() + ", ";
					}
					authors = authors.substr(0, authors.length-2);
					authors += " <a href=\"" + loc + "\" target=\"_blank\">More from Emmy &raquo;</a>";
					authors	 += "</p>";
				}
				
				if (!order.length) var html = "<h2>No results were found.</h2>";
				else {
					var html = '';
					for (i=0; i < order.length; i++) {
						switch(order[i]) {
							case "music": html += music; break;
							case "movie": html += movies; break;
							case "show": html += shows; break;
							case "book": html += books; break;
							case "author": html += authors; break;
						}
					}
				}
				_gel('content').innerHTML = html;
				_gel('expand').style.display = "block";
				_IG_AdjustIFrameHeight(200);
				_gel('loader').style.display = "none";
			}
			
			_IG_Analytics('UA-2716740-10', '/gadget')
		</script>
		<div id="loader"></div>
		<div class="inputDiv"><form onsubmit="return fetchData()"><input type="text" id="q" value="Favorite bands/movies/books" onfocus="inputFocused()" onblur="inputBlurred()"><button type="submit"><span>Suggest</span></button></form></div>
		<div id="expand">
			<div id="content"></div>
			<div id="collapse" title="Collapse" onclick="makeSmall()"></div>
		</div>
	]]>
	</Content>
</Module>
