<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs
	title="Lexicon Lookup Display"
	author_email="scribe777@gmail.com"
	author="CrossWire"
	description="Lexicon Display"
	screenshot="http://crosswire.org/images/crosswire.gif"
	thumbnail="http://crosswire.org/images/crosswire.gif"
	scrolling="false"
   >
<Optional feature="pubsub-2">
  <Param name="topics">
    <![CDATA[ 
    <Topic title="Image Selection" name="interedition.image.selected"
            description="Show an Image" type="string"
            subscribe="true"/>
    ]]>
  </Param>
</Optional>
<Optional feature="dynamic-height"/>
<Require feature="opensocial-0.8"/>
</ModulePrefs>

<UserPref name="height" datatype="enum" display_name="Gadget Height" default_value="200">
	<EnumValue value="200" display_value="Short"/>
	<EnumValue value="400" display_value="Medium"/>
	<EnumValue value="600" display_value="Tall"/>
	<EnumValue value="-1" display_value="Dynamic"/>
</UserPref>


<Content type="html">
<![CDATA[
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

	<link rel="stylesheet" type="text/css" href="/community/js/jquery-ui/jquery-ui.css"/>
	<script type="text/javascript" src="/community/js/jquery/jquery.min.js"></script>
	<script type="text/javascript" src="/community/js/jquery-ui/jquery-ui.min.js"></script>
	<script type="text/javascript" src="/community/js/URI.min.js"></script>

<style type="text/css">
<!--

.ui-widget-content {
	background: white !important;
}

body {
	margin-top: 2px;
	margin-bottom: 2px;
	margin-left: 2px;
	margin-right: 2px;
	font-size: 80%;
	font-family: 'SBL Greek', SymbolGreekU, SymbolGreekTU, SymbolGreekMetU, SymbolGreekTMetU, OdysseaU, OdysseaUBSU, TeubnerLSU, TeubnerLSCU, TeubnerVerticalU, TeubnerVerticalCU, GraecaUBSU, GraecaUBSMetU, GraecaU, HellenicaU, HellenicaCU, GreekSansLSU, GreekSansLSConU, PayneU, UncialLSU;
}

#lexContent {
	margin-left: 10px;
	margin-right: 26px;
}

.copyLine {
	margin: 1em 0px .5em 0px;
	padding: 0px 0px 0px 0px;
	border-top: 1px solid #7F673C;
	font-weight: bold;
	font-style: italic;
}

-->
</style>
</head>
<body>
<div style="overflow:auto; border: none 0px; width:100%;" class="fillPage" id="contentDiv">
<div id="lexContent">
</div>
</div>

</body>
<script>

var viewer = null;
var preferredHeight = 200;
var pageURL = '';
var servicesURL = '';
var imagesURL = '';

var lexModule = 'ls';
var lexModuleBackup = 'StrongsGreek';
var lexLookupURL = 'http://crosswire.org/study/fetchdata.jsp';
var currentKey = '';

function isNumber(n) {
	return !isNaN(parseFloat(n)) && isFinite(n);
}

function loadLexContent(key, mod) {
	if (!mod) mod = lexModule;
	if (!key || key.length < 1 || !isNumber(key)) return;
	$('#lexContent').html('<center style="width:100%;"><image src="'+imagesURL+'/loading.gif"/></center><br/><center><h1>Please wait...</h1></center>');

	var url = lexLookupURL;
	var params = {};
	var postData = {
		mod             : mod,
		key             : key
	};

	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
	params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postData);
	gadgets.io.makeRequest(url, function(o) {
		currentKey = key;
		var t = '';
		t += o.text;
		if (t.length < 50 && mod != lexModuleBackup) {
			loadLexContent(key, lexModuleBackup);
			return;
		}
		$('#lexContent').html(t);

		if (gadgets.util.hasFeature('dynamic-height') && preferredHeight == -1) gadgets.window.adjustHeight();
		setTimeout(function() {
			if (gadgets.util.hasFeature('dynamic-height') && preferredHeight == -1) gadgets.window.adjustHeight();
			expandFillPageClients();
		}, 100);
	}, params);
}


function word_selected_callback(topic, data, subscriberData) {
	if (data.strongNum) loadLexContent(data.strongNum);
}


function loaded() {

	pageURL = (parent.window.location.href.indexOf('?') > -1) ? parent.window.location.href.substring(0, parent.window.location.href.indexOf('?')) : parent.window.location.href;
	servicesURL = URI('../../vmr/api').absoluteTo(gadgets.util.getUrlParameters()['url']);
	imagesURL = URI('../../images').absoluteTo(gadgets.util.getUrlParameters()['url']);

	var prefs = new gadgets.Prefs();
	preferredHeight = parseInt(prefs.getString('height'));
	if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight == -1 ? 200 : preferredHeight);
	$('#contentDiv').css('overflow', (gadgets.util.hasFeature('dynamic-height') && preferredHeight == -1) ? 'visible' : 'auto');

	subId = gadgets.Hub.subscribe("interedition.word.selected", word_selected_callback);

	var req = opensocial.newDataRequest(); 
	var p = {}; 

	p[opensocial.IdSpec.Field.USER_ID] = "VIEWER"; 
	var idSpec = opensocial.newIdSpec(p); 
	req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), "viewer"); 
	req.send(function(data) {
		viewer = data.get("viewer").getData();

		expandFillPageClients();
		$(window).resize(function() {
			expandFillPageClients();
		});
	});
}


if (gadgets.util.hasFeature('pubsub-2')) {
	gadgets.HubSettings.onConnect = function(hum, suc, err) { loaded(); };
}
else gadgets.util.registerOnLoadHandler(loaded);


function clearExpandFillPageClients() {
	$('.fillPage').each(function () {
		$(this).css('height', '');
	});
}

var MARGIN=7;
function expandFillPageClients() {
	$('.fillPage').each(function () {
		$(this).height(gadgets.window.getViewportDimensions().height - $(this).offset().top - MARGIN);
	});
}


</script>


]]>
</Content>
</Module>
