<?xml version="1.0" encoding="UTF-8"?>
<Module>
    <ModulePrefs title="The New Hanoian" author_email="jeremi23@gmail.com" description="The New Hanoian Gadget">
        <Require feature="opensocial-0.7"/>
        <Require feature="minimessage"/>
        <Require feature="dynamic-height" />
    </ModulePrefs>
    <Content type="html"><![CDATA[
<style>
    a {
    	color: #343434;
    }

    p {
    	margin: 0px;
    }


    .descriptionHighlight .title {
    	border-bottom:1px solid #e3e3e3;
    	background: #eeeeee;
    	margin-bottom: 2px;
    	padding: 3px;
    }

    .descriptionHighlight .title a {
    	color: #3C7EB2
    }

    .title {
    	margin: 3px 0px;
    	padding: 3px;
    	font-weight: bold;
    }

    .item {
    	border-bottom: 1px dotted #b7b7b7;
    }

    .item .more {
    	color:#000000;
    	display:block;
    	font-size: 11px;
    	width:100%;
    	padding: 0px;
    }

    .item .date {
    	top: 0px;
    	left: 0px;
    	padding: 2px 20px 0px;
    }


    .item .more .desc {
    	background: #f5f5f5;
    	padding: 4px 20px 10px;
    }

    .item .date {
    	background: #f5f5f5;
    }

    .item .more .link {
    	background:#eeeeee none repeat scroll 0 0;
    	border-top: 1px solid #e3e3e3;
    	margin-top: 2px; padding: 0px 5px 0px 0px;
    	color: #fe7000;
    	font-weight: bold;
    	text-align: right;
    }

    .item .more .link a {
    	color: #fe7000;
    }

    .descriptionHighlight {
    	border:none;
    	margin: 3px 0px;
    	padding:0px;
    }
</style>
<script type="text/javascript">

function makeGetRequest(url, callback) {
var params = {};

params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
gadgets.io.makeRequest(url, callback, params);
}

function searchPlace() {
var q = document.getElementById("q").value;
makeGetRequest("http://newhanoian.xemzi.com/entity/search_json?ssquery=" + escape(q), searchCallback);
}

function displayPlaces(data) {
  var bullet = "<img src='" + getFavicon("http://newhanoian.xemzi.com/") + "' alt='' border=0 align='absmiddle' style='height:16;width:16;' onerror='this.style.visibility=\"hidden\";'>&nbsp;&nbsp;";
  var resEl = _gel("results"); 
  window.places = data;
  resEl.innerHTML = "";
  for (var i = 0; i < data.length; i++) {
    var place = data[i];
    var id = place.xemzi_entity_id;
    var itemEl = document.createElement('div');
    var item_title = document.createElement('div');
    var item_more = document.createElement('div');
    var item_desc = document.createElement('div');
    var item_link = document.createElement('div');

    itemEl.id = 'item_'+id;
    item_title.id = 'title_'+id;
    item_more.id = 'more_'+id;
    item_more.style.display='none';
    item_desc.id = 'desc_'+id;
    item_link.id = 'link_'+id;


	itemEl.className = 'item';
    item_title.className = 'title';
    item_more.className = 'more';
    item_desc.className = 'desc';
    item_link.className = 'link';

    item_title.innerHTML = bullet + "<a id='link_title_"+id+"' class='titlelink' href='" + place.xemzi_url + "' onclick='toggleDescription("+id+");return false;'>" + place.name + "</a>";


	item_desc.innerHTML = place.short_description;

    item_link.innerHTML =  "<a href='javascript:share(" + i + ")'>Share</a> | <a href='" + place.xemzi_url + "' target='_blank'>view link &raquo;</a>";


    item_more.appendChild(item_desc);
    item_more.appendChild(item_link);


    itemEl.appendChild(item_title);
    itemEl.appendChild(item_more);

    resEl.appendChild(itemEl);
  }
  gadgets.window.adjustHeight();
}

function toggleDescription(elmnt_id) {
    if (_gel('more_'+elmnt_id).style.display == 'none') {
        _gel('more_'+elmnt_id).style.display = '';
        _gel('item_'+elmnt_id).className = 'item descriptionHighlight';
    } else {
        _gel('more_'+elmnt_id).style.display = 'none';
        _gel('item_'+elmnt_id).className = 'item';
    }
    gadgets.window.adjustHeight();
}

function share(entryId) {
  var place = window.places[entryId];
  var activity = opensocial.newActivity({ 'title' : place.name,
    'body' : "share the place " + place.name + ": " + place.short_description,
	'url': place.xemzi_url});

  opensocial.requestCreateActivity(activity, "HIGH", shared);
}

function shared() {
  var miniMsg = new gadgets.MiniMessage();
  miniMsg.createTimerMessage('Successfully shared.', 7);
}

function searchCallback(resp) {
  displayPlaces(resp.data);
}

getFavicon = function(feedurl) {
    var favicon = feedurl.match( /:\/\/(www\.)?([^\/:]+)/ );
    favicon = favicon[2]?favicon[2]:'';
    favicon = "http://"+favicon+"/favicon.ico";
    return favicon;
}

</script>
  <form>
<input type="text" name="q" id="q" />
</form>
<a href="#" onclick="searchPlace();return false;">Search</a>
<div id="results">
    
</div>

]]></Content>
</Module>

