function ScriptableTask(taskId, contextPath, mapId) {
	this.taskId = taskId;
	this.contextPath = contextPath;
	this.mapId = mapId;
}

function PersonalisableTask(taskId, contextPath, mapId, userId) {
	this.inheritsFrom(new ScriptableTask(taskId, contextPath, mapId));
	this.userId = userId;
}

function OpenMapCompsTask(taskId, contextPath, mapId, userId) {
	this.inheritsFrom(new PersonalisableTask(taskId, contextPath, mapId, userId));
	var displayDescriptionUponRerender = false;
	
	this.setDisplayDescriptionUponRerender = function(display) {
		displayDescriptionUponRerender = display;
	}
	
	this.getDisplayDescriptionUponRerender = function() {
		return displayDescriptionUponRerender;
	}
	
   	this.confirmDeleteMapComposition = function(confirmMsg) {
   		var mapSelectBox = document.getElementById(this.taskId + '_param_maps');
   		var mapName = this.findMapName(mapSelectBox.value);
   	
   		confirmMsg = confirmMsg.replace("{name}", mapName);
   		
   		if (confirm(confirmMsg)) {
   			return true;
   			
   			//Call to delete left inside task XSL due to number
   			//of XSL parameters that needed to be passed
   			//return deleteMapComposition();
   		}
   		
   		return false;
   	}	
   	
    this.onSortChange =	function(sortBox) {
   		var sortBtn = document.getElementById('button_' + this.taskId + '_action_sort');
   		
   		if (sortBtn) {
   			this.setDisplayDescriptionUponRerender(true);
   			
   			sortBtn.click();
   		} 
   	}  
   	
   	this.findMapName = function(selectedMapId) {
   		var mapSelectBox = document.getElementById(this.taskId + '_param_maps');
		
		for (i = 0; i < mapSelectBox.options.length; i++) {
			if (mapSelectBox.options[i].value == selectedMapId) {
				return mapSelectBox.options[i].text;
			}
		}
		    		
		return "";
   	}   
   	
    this.findMapDescription = function(selectedMapId) {
		var descSelectBox = document.getElementById(this.taskId + '_param_mapDescriptions');
		
		for (i = 0; i < descSelectBox.options.length; i++) {
			if (descSelectBox.options[i].value == selectedMapId) {
				return descSelectBox.options[i].text;
			}
		}
		    		
		return "";
   	}   		
   	
   	this.onMapSelectionChange = function(selectBox) {
   		var selectedMapId = selectBox.value;
   		var descriptionText = this.findMapDescription(selectedMapId);
   		//var descriptionUI = document.getElementById(this.taskId + '_description');
   		//descriptionUI.innerText = descriptionText;
   		
   		$("#"+this.taskId+"_description").text(descriptionText);
   		
   		this.updatePreviewImage(selectedMapId);   		
   	}
   	
   	this.updatePreviewImage = function (selectedMapId) {
        if (selectedMapId == null || selectedMapId == '') {
            return;
        }
        
        var previewImage = new JitkImagePreview(this.taskId);
        previewImage.init(document.getElementById(this.taskId + '_fieldset_preview'));
        previewImage.showPreviewImage(this.contextPath + 'maps/preview/' + this.userId + '/' + selectedMapId);   	    
   	}  
   	
   	this.openMapTaskUpdateListener = function () {
   		var openFlag = document.getElementById("cb_openMapCompFlag");
   	
   		// if the maps have just been sorted, then display the description
   		// field for the selected map
   		if (mvsOpenMapCompsTask.getDisplayDescriptionUponRerender()) {
   			var mapSelectBox = document.getElementById(mvsOpenMapCompsTask.taskId + '_param_maps');
   			var mapDescription = mvsOpenMapCompsTask.findMapDescription(mapSelectBox.value);
   			
   			$("#" + mvsOpenMapCompsTask.taskId + "_description").text(mapDescription);
   			
   			// set the flag to false so this code is not called upon every
   			// rerender of the task
   			mvsOpenMapCompsTask.setDisplayDescriptionUponRerender(false);
   		}
   		
   		// if the user has just opened a map composition, close the task window
   		if (openFlag.checked) {
   			openFlag.checked = false;
   			
   			var taskWin = taskWindowManager.windows["win_EsriTaskCell_" + mvsOpenMapCompsTask.taskId];
   			taskWin.hide();
   		}
   	}		
}

var mvsOpenMapCompsTask = null;

function jitkInitMapCompsTask(taskId, contextPath, mapId, userId) {
	if (mvsOpenMapCompsTask == null) {
		mvsOpenMapCompsTask = new OpenMapCompsTask(taskId, contextPath, mapId, userId);
		
		var task = EsriControls.tasks[taskId];
		
		if (task) {
			task.addUpdateListener("openMapTaskUpdateListener", mvsOpenMapCompsTask.openMapTaskUpdateListener);
		}
	}
}

function SaveMapCompTask (taskId, contextPath, mapId, userId) {
	this.inheritsFrom(new PersonalisableTask(taskId, contextPath, mapId, userId));
	
	var self = this;
	this.taskId = taskId;
	this.updatePreviewFlag = true;
	
	this.initPreview = function () {
		if (!self.updatePreviewFlag) {
			return;
		}
		var previewImg = new JitkImagePreview(self.taskId);
        previewImg.init(document.getElementById(self.taskId + '_previewDiv'));
        var cacheControl = Math.random();
        previewImg.showPreviewImage(self.contextPath + 'maps/current/' + self.mapId + '.png?exportGraphics=false&amp;cachecontrol=' + cacheControl);
        self.updatePreviewFlag = false;
	}
	
	this.updateListener = function () {
		self.updatePreviewFlag = true;
		
		var win = self.getTaskWindow();
		if (win && !win.closed) {
			self.initPreview();
		}
	}
	
	this.getTaskWindow = function () {
		return taskWindowManager.windows["win_EsriTaskCell_" + self.taskId];
	}
}

var mvsSaveMapCompTask = null;

function jitkInitSaveMapCompTask (taskId, contextPath, mapId, userId) {
	if (mvsSaveMapCompTask == null) {
		mvsSaveMapCompTask = new SaveMapCompTask(taskId, contextPath, mapId, userId);
		
		var task = EsriControls.tasks[taskId];
		
		if (task) {
			task.addUpdateListener("saveMapCompTaskUpdateListener", mvsSaveMapCompTask.updateListener);
		}
		
		var map = EsriControls.maps[mapId];
		if (map) {
			map.addUpdateListener("saveMapCompTaskUpdateListener", mvsSaveMapCompTask.updateListener);
		}
	}
}

function jitkToggleWindowSaveMapComp (taskId, mapId, args) {
	toggleWindow(taskId, mapId, args);
	if (mvsSaveMapCompTask) {
		mvsSaveMapCompTask.initPreview();
	}
	return false;
}

function NewMapCompTask (taskId, contextPath, mapId, userId) {
	this.inheritsFrom(new PersonalisableTask(taskId, contextPath, mapId, userId));
	
	var self = this;
	this.initUI = function () {
	
      	var chkSaveCurrentMap = document.getElementById(self.taskId + '_param_saveCurrentMap');
      	var saveForm = document.getElementById(self.taskId + '_saveForm');
      	var btnCreateMap = document.getElementById('button_' + self.taskId + '_action_create');
   
      	if (chkSaveCurrentMap.checked) {
      		EsriUtils.setElementStyle(saveForm, "display:block");
      		EsriUtils.setElementStyle(btnCreateMap, "display:none");
      	} else {
      		EsriUtils.setElementStyle(saveForm, "display:none");
      		EsriUtils.setElementStyle(btnCreateMap, "display:block");
      	}
      	
      	chkSaveCurrentMap.onclick = mvsNewMapCompTask.onSaveCurrentMapClick;
	}
	
    this.onSaveCurrentMapClick = function () {
       var chkSaveCurrentMap = document.getElementById(self.taskId + '_param_saveCurrentMap');
       var saveForm = document.getElementById(self.taskId + '_saveForm');
       var btnCreateMap = document.getElementById('button_' + self.taskId + '_action_create');
     	
       if (chkSaveCurrentMap.checked) {
     		EsriUtils.setElementStyle(saveForm, "display:block");
     		EsriUtils.setElementStyle(btnCreateMap, "display:none");
     		self.initPreview();
       } else {
     		EsriUtils.setElementStyle(saveForm, "display:none");
     		EsriUtils.setElementStyle(btnCreateMap, "display:block");
       }
     	
       return true;
	}
	
	this.initPreview = function () {
		var previewImg = new JitkImagePreview(self.taskId);
	    previewImg.init(document.getElementById(self.taskId + '_previewDiv'));
	    var cacheControl = Math.random();
        previewImg.showPreviewImage(self.contextPath + 'maps/current/' + self.mapId + '.png?exportGraphics=false&amp;cachecontrol=' + cacheControl);
    }
}

var mvsNewMapCompTask = null;

function jitkInitNewMapCompTask (taskId, contextPath, mapId, userId) {

	if (mvsNewMapCompTask == null) {
		mvsNewMapCompTask = new NewMapCompTask(taskId, contextPath, mapId, userId);
		
		var task = EsriControls.tasks[taskId];
		
		if (task) {
			task.addUpdateListener("initUI", mvsNewMapCompTask.initUI);
        	task.addUpdateListener("initPreview", mvsNewMapCompTask.initPreview);
		}
	}
}
			
