<?xml version="1.0" encoding="UTF-8" ?>

<%
response.setContentType("text/xml");
%>
<Module>
  <ModulePrefs
	title="My Work Summary"
	author_email="scribe777@gmail.com"
	author="CrossWire"
	description="My Work Summary"
	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="taskTypeID" datatype="enum" display_name="Task Type" default_value="-1">
	<EnumValue value="-1" display_value="All"/>
	<EnumValue value="1" display_value="Imaging"/>
	<EnumValue value="2" display_value="Indexing"/>
	<EnumValue value="3" display_value="Transcribing"/>
</UserPref>

<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/jquery/jquery.cookie.js"></script>
	<script type="text/javascript" src="/community/js/URI.min.js"></script>

<style type="text/css">
<!--
body {
	margin-top: 10px;
	margin-bottom: 10px;
	margin-left: 10px;
	margin-right: 10px;
	background-color: #aaa;
	font-family: Verdana, Arial, Helvetica, sans-serif; 
	font-size:14px;
}

a img {
	border: 0;
}
-->
</style>
</head>
<body>

<div style="overflow:auto; border: none 0px; width:100%;min-height:300px;" class="fillPage" id="contentDiv">
<div id="workSummary">
</div>
</div>

</body>
<script>

var viewer = null;
var preferredHeight = 200;
var limitToTaskTypeID = -1;
var pageURL = '';
var servicesURL = '';
var imagesURL = '';


function imaging(docID) {
	if (gadgets.util.hasFeature('pubsub-2')) gadgets.Hub.publish("interedition.manuscript.selected", docID);
}

function indexing(docID) {
	if (gadgets.util.hasFeature('pubsub-2')) gadgets.Hub.publish("interedition.manuscript.selected", docID);
}

function transcribing(docID) {
	if (gadgets.util.hasFeature('pubsub-2')) gadgets.Hub.publish("interedition.manuscript.selected", docID);
}



function getTaskName(taskTypeID) {
	switch (parseInt(taskTypeID)) {
		case 1 : return 'imaging';
		case 2 : return 'indexing';
		case 3 : return 'transcribing';
	}
}


function unassign(docID, taskTypeID) {
	var unassignTaskURL = servicesURL + '/projectmanagement/task/unassign/';
	if (confirm('Are you sure you wish to relinquish responsibility for ' + getTaskName(taskTypeID) + ' document ' + docID + '?')) {
	
		var params = {};
	
		var postData = {
			sessionHash   : $.cookie('ntvmrSession'),
			objectID      : docID,
			taskTypeID    : taskTypeID
		};
	
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postData);
		gadgets.io.makeRequest(unassignTaskURL, function(o) {
			var xml = $.parseXML(o.text);
			var error = $(xml).find('error');
			if (error && error.length) {
				alert($(error).attr('message'));
			}
			else {
				loadWorkSummary();

				// announce activity
				var params = {};  
				params[opensocial.Activity.Field.TITLE] = 'Assignment removed for Indexing Manuscript: '+docID+' from '+viewer.getDisplayName();
				var activity = opensocial.newActivity(params); 
				opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH);

			}
		}, params);
	}
}



function loadWorkSummary() {
	$('#workSummary').html('<tr><td colspan="5"><center><image src="'+imagesURL+'/loading.gif"/></center><br/><center><h1>Loading your assignments.  Please wait...</h1></center></td></tr>');

	var url = servicesURL + '/projectmanagement/task/get/';
	var params = {};
	var postData = {
		sessionHash  : $.cookie('ntvmrSession'),
		detail       : 'progress'
	};

	if (limitToTaskTypeID > -1) {
		postData.taskTypeID = limitToTaskTypeID;
	}
	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) {
		var xml = $.parseXML(o.text);
		var t = '';
		t += '<div style="">';

		t += '<span style="margin:15px 0px 5px 0px; font-weight:bold;">My Assignments</span>';
		if (limitToTaskTypeID == 2) {
			t += ' <span style="font-size:x-small;"> (<a href="/indexingstatus" target="_blank">see what\'s available</a>)</span>';
		}

		t += '<table style="margin:0px 5px 0px 30px;width:80%">';

		$(xml).find('task').each(function () {

			var docID = $(this).attr('objectID');
			var gaNum = $(this).attr('objectName');
			var taskTypeID = $(this).attr('taskTypeID');
			var taskName = getTaskName(taskTypeID);

			var percentageComplete = $(this).attr('percentageComplete');

			t += '<tr style="cursor:pointer;">';
			t += '<td>&#x2022;</td>';
			t += '<td><span onclick="'+taskName+'(' + docID + ');return false">Assigned '+taskName+ ' ' + docID + ' (' + gaNum + ')</span></td>';
			t += '<td style="text-align:right;"><a href="#" onclick="unassign('+docID+', ' + taskTypeID + '); return false;"><img height="16" src="'+imagesURL+'/delete.png"/></a></td>';
			t += '</tr>';
			t += '<tr onclick="'+taskName+'(' + docID + ');" style="cursor:pointer; z-index:-99;">';
			t += '<td style="font-size:10px;"></td><td colspan="2" style="white-space: nowrap; font-size:10px;">';
			t += '<div style="padding: 0pt; border:1px solid black;margin: 0px 0px 10px 0px; width: 100%; white-space: nowrap; text-align: center; position: relative; z-index:0;">';
			t += '&nbsp;' + percentageComplete + '%';
			if (percentageComplete > 0) { // for stupid ie 
				t += '<div style="position: absolute; top:0px; left:0px; height:10px; margin: 0pt; padding: 0pt; background: transparent url(/community/images/statusbar_green.gif) repeat-x scroll 0% 0%; overflow: visible; width: ' + percentageComplete + '%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; vertical-align: middle; filter:alpha(opacity=40); -khtml-opacity: 0.40; -moz-opacity: 0.40; opacity:0.40; z-index:-1;">';
				t += '&nbsp;';
				t += '</div>';
			} // for stupid ie
			t += '</div>';
			t += '</td></tr>';
		});
		if ($(xml).find('task').length < 1) {
			t += '<tr><td>none.</td></tr>';
		}
		t += '</table>';
		t += '</div>';

		t += '<div>&nbsp;</div>';

		t += '<div>';
		t += '<span style="font-weight:bold; margin:15px 0px 5px 0px;">My Unsubmitted Work</span>';
		t += '<table style="margin:0px 5px 0px 30px;width:80%">';
		t += '<tr><td>none.</td></tr>';
		t += '</table>';
		t += '</div>';

		$('#workSummary').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 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'));
	limitToTaskTypeID = parseInt(prefs.getString('taskTypeID'));
	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.transcription.saved", transcription_saved_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();
		loadWorkSummary();
	});
}


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>
