<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[
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="/community/js/jquery-ui/jquery-ui.css"/>
	<link rel="stylesheet" type="text/css" href="/community/js/jquery.qtip2/jquery.qtip.min.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>
	<script type="text/javascript" src="/community/js/jquery/jquery.cookie.js"></script>
	<script type="text/javascript" src="/community/js/vmr-common.js"></script>
	<script type="text/javascript" src="/community/js/jquery.qtip2/jquery.qtip.min.js"></script>

<style type="text/css">
<!--
.qtip {
	max-width:40em ! important;
}

.taskUnitsStats {
        min-width:20em;
        max-height:180px;
        overflow-x:visible;
        overflow-y:auto;
        padding:.5em;
}

.taskUnitsStats td {
        padding-right:.2em;
}




#taskStageChoicesOptions {
	display: table;
	margin-right: auto;
	margin-left: auto;
}
.taskStage {
	padding-left:1em;
	padding-bottom:.5em;
	vertical-align:top;
}

.stageChoiceDisplay,.stageChoice {
	width:2em;
	text-align:center;
	display:inline-block;
	border-radius: 6px;
}

.stageChoice {
	margin:.2em;
	font-size:200%;
}




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;
}
.bottomFooter {
	margin-bottom: 5em;
}
-->
</style>
</head>
<body>

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

<script>

var preferredHeight = 200;
var limitToTaskTypeID = -1;


function imaging(docID, objectPart) {
	VMR.publish("interedition.manuscript.selected", docID);
}

function indexing(docID, objectPart) {
	VMR.publish("interedition.manuscript.selected", docID);
}

function transcribing(docID, objectPart) {
	VMR.publish("interedition.manuscript.selected", { docID : docID, indexContent : objectPart });
}



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


function unassign(docID, taskTypeID) {
	if (confirm('Are you sure you wish to relinquish responsibility for ' + getTaskName(taskTypeID) + ' document ' + docID + '?')) {
	
		var postData = {
			objectID      : docID,
			taskTypeID    : taskTypeID
		};
	
		VMR.serviceRequest('projectmanagement/task/unassign', postData, function(o) {
			var xml = $.parseXML(o.text);
			var error = $(xml).find('error');
			if (error && error.length) {
				alert($(error).attr('message'));
			}
			else {
				loadWorkSummary();
			}
		});
	}
}

function getTaskStages(stageDefsString) {
	var stages = [];

	if (!stageDefsString) stageDefsString = "🔾:transparent|🔘:00FF00";
			
	var stageDefs = stageDefsString.split('|');
	for (var i = 0; i < stageDefs.length; ++i) {
		var stageIcon = stageDefs[i].split(':')[0];
		var stageColor = (stageDefs[i].split(':').length > 1) ? stageDefs[i].split(':')[1] : 'transparent';
		var stageProtected = false;
		if (stageIcon.startsWith('*')) {
			stageProtected = true;
			stageIcon = stageIcon.substring(1);
		}
		stages.push({
			display   : stageIcon,
			color     : stageColor,
			protected : stageProtected
		});
	}
	return stages;
}

function getStageHTML(taskID, taskStage, stageDefsString) {
	var stages = getTaskStages(stageDefsString);

	var t = '';
	if (taskStage < stages.length) {
		t += '<div class="stageChoiceDisplay" title="level: '+taskStage+'" style="background-color:'+stages[taskStage].color+';';
		if (!stages[taskStage].protected) {
			t += 'cursor:pointer;" onclick="changeTaskStage('+taskID+'); return false;';
		}
		t += '">'+stages[taskStage].display+'</div>';
	}
	return t;
}


function changeTaskStage(taskID) {
	var stages = getTaskStages($('tr[data-taskID="'+taskID+'"]').attr('data-projectStageDefinitions'));
	var stage = parseInt($('tr[data-taskID="'+taskID+'"]').attr('data-taskStage'));
	var docID = parseInt($('tr[data-taskID="'+taskID+'"]').attr('data-docID'));
	var taskTypeID = parseInt($('tr[data-taskID="'+taskID+'"]').attr('data-taskTypeID'));
	var taskName = getTaskName(taskTypeID);
	var t = '';
	var divs = [];
	var toggleChoice = 0;
	for (var i = 0; i < stages.length; ++i) {
		if (!stages[i].protected) {
			var div = '<div title="level: '+i+'" class="stageChoice"';
			if (i != stage) {
				div += ' onclick="taskStageSelected('+taskID+', '+i+');return false;"';
				toggleChoice = i;
			}
			div += ' style="'+(i != stage?'cursor:pointer;':'opacity:0.5;')+'background-color:'+stages[i].color+'">'+stages[i].display+'</div>';
			divs.push(div);
		}
	}
	if (divs.length == 2) {
		// we only have 1 choice besides current, just toggle
		taskStageSelected(taskID, toggleChoice);
	}
	else {
		$('#taskStageChoicesDescription').html('Status ' + taskName + ' doc: ' + docID);
		$('#taskStageChoicesOptions').html(divs.join(' '));
		$('#taskStageChoices').dialog({
			resizable: false,
			height: "auto",
			modal: true,
			position: {
				my: "left bottom",
				at: "left top",
				of: $('tr[data-taskID="'+taskID+'"]').find('td.taskStage')
			},
		});
	}
}

function taskStageSelected(taskID, stage) {
	if ($('#taskStageChoices').hasClass("ui-dialog-content") && $('#taskStageChoices').dialog("isOpen")) {
		$('#taskStageChoices').dialog('close');
	}
	VMR.serviceRequest('projectmanagement/task/setstage', { taskID : taskID, stage : stage }, function(o) {
		var xml = $.parseXML(o.text);
		var task = $(xml).find('task');
		if ($(task).length) {
			stage = $(task).attr('stage');
 			var projectStageDefinitions = $('tr[data-taskID="'+taskID+'"]').attr('data-projectStageDefinitions');
			$('tr[data-taskID="'+taskID+'"]').attr('data-taskStage', stage);
			$('tr[data-taskID="'+taskID+'"]').find('td.taskStage').html(
				getStageHTML(taskID, stage, projectStageDefinitions)
			);
		}
	});
}


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

	var postData = {
		detail       : 'progress'
	};

	if (limitToTaskTypeID > -1) {
		postData.taskTypeID = limitToTaskTypeID;
	}
	VMR.serviceRequest('projectmanagement/task/get', postData, 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 projectID = $(this).attr('projectID');
			var taskID = $(this).attr('taskID');
			var taskTypeID = $(this).attr('taskTypeID');
			var objectPart = $(this).attr('objectPart');
			var taskStage = parseInt($(this).attr('stage'));
 			var projectStageDefinitions = $(this).attr('projectStageDefinitions');
			 
			var taskName = getTaskName(taskTypeID);

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

			t += '<tr style="cursor:pointer;">';
			t += '<td>&#x2022;</td>';
			t += '<td><span onclick="'+taskName+'(' + docID + ',\'' + objectPart + '\');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="'+VMR.imagesURL+'/delete.png"/></a></td><td>&nbsp;</td>';
			t += '</tr>';
			t += '<tr data-projectID="'+projectID+'" data-taskStage="'+taskStage+'" data-docID="'+docID+'" data-taskID="'+taskID+'" data-taskTypeID="'+taskTypeID+'" data-projectStageDefinitions="'+projectStageDefinitions+'" style="cursor:pointer; z-index:-99;">';
			t += '<td style="font-size:10px;"></td><td class="assignmentStatus" colspan="2" onclick="'+taskName+'(' + docID + ',\'' + objectPart + '\');" 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><td class="taskStage">';
			t += getStageHTML(taskID, taskStage, $(this).attr('projectStageDefinitions'));
			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>';
		t += '<div class="bottomFooter">&nbsp;</div>';

		$('#workSummary').html(t);
		$('.assignmentStatus').qtip({
			content: {
				text: function(event, api) {
					$.ajax({
						url: VMR.httpRoot + '/vmr/api/projectmanagement/task/get/'
							+ '?taskID=' + $(this).attr('data-taskID')
							+ '&detail=progress'
					})
					.then(function(content) {
						var m = $(content).attr('completedUnitMap');
						var t = '<div class="taskUnitsHeader">DocID<span style="float:right">'+$(content).find('task').attr('objectID')+'</span></div>';
						t += '<div class="taskUnitsHeader">User<span style="float:right">'+$(content).find('task').attr('assignedTo')+'</span></div>';
						t += '<div class="taskUnitsHeader updateStats"></div>';
						t += '<div class="taskUnitsStats"><table>';
						t += '<thead><tr><th style="text-align:right;">PageID</th><th style="text-align:center;">Transcr Repo</th><th style="text-align:center;white-space:nowrap;">DB Cache &nbsp;&nbsp;<span class="refreshCache" onclick="refreshCache('+$(content).find('task').attr('objectID')+',\''+$(content).find('task').attr('assignedTo')+'\', $(this).parents(\'.taskUnitsStats\').prev(\'.updatestats\')); return false;">&nbsp;</span></th></tr></thead>';
						t += '<tbody>';
						var pages = $(content).find('unit').get();
						pages.sort(function(a,b){return parseInt($(a).attr('unitID'))-parseInt($(b).attr('unitID'));});
						var i = 0;
						$(pages).each(function() {
							t += '<tr><td style="text-align:right;">'+$(this).attr('unitID')+'</td><td style="text-align:center;color:'+($(this).attr('status')==1?'green;">✔':'red;">x')+'</td><td style="text-align:center;color:'+(m && i<m.length ? (m[i]=='#'?'green">✔':'red">x'):'black">')+'</td></tr>';
							++i;
						});
						t += '</tbody></table></div>';
						api.set('content.text', t);
					}, function(xhr, status, error) {
						// Upon failure... set the tooltip content to the status and error value
						api.set('content.text', status + ': ' + error);
					});

					return 'Loading...'; // Set some initial text
				}
			},
			show: {
				delay: 1000
			},
			hide: {
				delay: 1000,
				fixed: true
			},
			position: {
				viewport: true,
				target: 'mouse', // Position at the mouse
//				my: 'bottom left',
//				at: 'top left',
				adjust: {
					mouse: false, // ...but don't follow it!
					method: 'shift shift'
				}
			},
			style : {
				classes: 'qtip-bootstrap',
			}
		});
		VMR.clearExpandFillPageClients();
		VMR.refreshHeight();
	});
}


VMR.init(function() {
	$('#contentDiv').css('overflow', 'auto');
	var prefs = new gadgets.Prefs();
	limitToTaskTypeID = parseInt(prefs.getString('taskTypeID'));
	loadWorkSummary();
}, '../../');

</script>

<div style="display:none;" id="taskStageChoices">
<div id="taskStageChoicesDescription">
</div>
<div id="taskStageChoicesOptions">
</div>
</div>

</body>
</html>
]]>
</Content>
</Module>
