var BatchTagEdit = {
	busySmallURL: 'images/loading_small.gif',
								
	busyURL: 'images/loading.gif',

	emptyImageURL: 'images/image_place_holder.gif',
								
	activeBatch: 0, 

	addToBatchButtons: new Array,
							 
	rowIndex: new Array,
					
	baseImageWidth: new Array,

	imagePadding: new Array,
									
	baseDivWidth: new Array,

	baseDivHeight: new Array,

	imagesPerPage: new Array,

	numRows: new Array,

	maxImageWidth: new Array,

	currentTableShift: new Array,

	currentTableVShift: new Array,

	notBase: new Array,

	imageSelect: new Array,

	pageNum: new Array,

	totalPages: new Array,

	nextPage: new Array,

	nextPageFileIDs: new Array,

	prevPage: new Array,

	prevPageFileIDs: new Array,

	currentPageFileIDs: new Array,

	selectDisabled: new Array,

	commonTerms: new Array,

	toAddTerms: new Array,

	toRemoveTerms: new Array,

	revisionNumber: new Array,

	disambigEmptyXML: '',
	
	disambigXML: new Array,
	
	disambigTerms: new Array,

	disambigRemovedTerms: new Array,

	draggables: new Array,

	navBarVars: new Object(),

	failed: function(req){
		$('BatchEditLoading').style.visibility = 'hidden';
		alert('An error occured while communicating with the server. Please try again.');
	},

	silentFailed: function(req){
		//alert('drawing dis' + req.getAllResponseHeaders() + "\n\n" + req.responseText);
		$('BatchEditLoading').style.visibility = 'hidden';
	},

	initializeImage: function(
			batchID, 
			baseImageWidth, 
			imagePadding, 
			baseDivWidth, 
			baseDivHeight, 
			imagesPerPage, 
			maxImageWidth, 
			pageNum, 
			totalPages,
			numRows
			){

		this.activeBatch = batchID;

 		rowID = this.rowIndex.indexOf(batchID);
		if(rowID == -1){
			this.rowIndex[this.rowIndex.length] = batchID;
	 		rowID = this.rowIndex.indexOf(batchID);
		}

		this.baseImageWidth[rowID] = baseImageWidth;
		this.imagePadding[rowID] = imagePadding;
		this.baseDivWidth[rowID] = this.getBaseDivWidth(batchID);
		this.baseDivHeight[rowID] = baseDivHeight;
		this.imagesPerPage[rowID] = imagesPerPage;
		this.maxImageWidth[rowID] = maxImageWidth;
		this.currentTableShift[rowID] = 0;
		this.currentTableVShift[rowID] = 0;
		this.notBase[rowID] = new Array;
		this.imageSelect[rowID] = new Array;
		this.pageNum[rowID] = pageNum;
		this.totalPages[rowID] = totalPages;
		this.nextPage[rowID] = null;
		this.prevPage[rowID] = null;
		this.nextPageFileIDs[rowID] = new Array;
		this.prevPageFileIDs[rowID] = new Array;
		this.currentPageFileIDs[rowID] = new Array;
		this.selectDisabled[rowID] = false;
		this.numRows[rowID] = numRows;

		this.loadNextPage(batchID);
		return;
		this.loadPrevPage(batchID);
	},

	initFileID: function(batchID, index, fileID){
 		rowID = this.rowIndex.indexOf(batchID);

		this.currentPageFileIDs[rowID][index] = fileID;
	},

	initAddToBatchButton: function(fileID){
		//id="batchEditAddButton_'.$FileID.'"'
		this.addToBatchButtons.push(fileID);
	},

	getBaseDivWidth: function(batchID){
		div = $('batchImageTool_' + batchID);
		if(typeof(div) == 'object'){
			return div.offsetWidth;
		}else{
			return false;
		}
	},
									 
	imageHover: function(e, batchID){
		rowID = this.rowIndex.indexOf(batchID);

		if(!this.baseDivWidth[rowID]){
			this.baseDivWidth[rowID] = this.getBaseDivWidth(batchID);
			return;
		}

		var layerx = e.layerX;
	
		if(typeof(layerx) != 'number'){
			layerx = e.x;
		}
		
		//adjust the x to the table shift, and the border
		var x = (layerx + this.currentTableShift[rowID]) - 1;
		
		var imagePartial = (x/this.baseDivWidth[rowID]) * this.imagesPerPage[rowID];
		var imageIndex = Math.round(imagePartial-0.5);
		imagePartial = (imagePartial - imageIndex) - 0.5;
		
		if(imageIndex < 0){
			imageIndex = 0;
		}else	if(imageIndex >= this.imagesPerPage[rowID]){
			imageIndex = this.imagesPerPage[rowID] - 1;
		}
		
		if(imagePartial < -0.5){
			imagePartial = -0.5;
		}else if(imagePartial > 0.5){
			imagePartial = 0.5;
		}
		
		var imageStrength = 1 - (Math.abs(imagePartial));
		var imageWidth = ((this.maxImageWidth[rowID] - this.baseImageWidth[rowID])*imageStrength);
		var	otherIndex = -1;
		
		if(imageIndex == 0){
			if(imagePartial < 0){
				otherIndex = -1;
				imageWidth = ((this.maxImageWidth[rowID] - this.baseImageWidth[rowID])*(imageStrength-0.5)*2);
			}else{
				otherIndex = 1;
			}
		}else if(imageIndex == (this.imagesPerPage[rowID] - 1)){
			if(imagePartial > 0){
				otherIndex = -1;
				imageWidth = ((this.maxImageWidth[rowID] - this.baseImageWidth[rowID])*(imageStrength-0.5)*2);
			}else{
				otherIndex = imageIndex - 1;
			}
		}else{
			if(imagePartial < 0){
				otherIndex = imageIndex - 1;
			}else{
				otherIndex = imageIndex + 1;
			}
		}
	
		if(this.notBase[rowID].length > 0){
			this.notBase[rowID].each(
				(
					function(imageID, index){
						BatchTagEdit.resetImage(imageID, batchID);
					}
				).bind(this)
			);
			this.notBase[rowID] = new Array;
		}

		$('imageRow' + imageIndex + '_' + batchID).style.width = (imageWidth + this.baseImageWidth[rowID]) + 'px';
		this.notBase[rowID].push(imageIndex);

		if(otherIndex >= 0){
			otherStrength = 1 - imageStrength;
			otherWidth = ((this.maxImageWidth[rowID] - this.baseImageWidth[rowID])*otherStrength);
			
			$('imageRow' + otherIndex + '_' + batchID).style.width = (otherWidth + this.baseImageWidth[rowID]) + 'px';
			this.notBase[rowID][this.notBase[rowID].length] = otherIndex;
		}else{
			otherWidth = 0;
		}
		
		var table = $('imageRow_' + batchID);

		var tableWidth = table.offsetWidth;
		var tableShift = -Math.round((x/this.baseDivWidth[rowID]) * (tableWidth - this.baseDivWidth[rowID]));

		//causes jitter, this can be fixed with faster growing of primary image
		//tableShift = -((imageWidth/2) + otherWidth);

		var tableHeight = table.offsetHeight;

		this.currentTableShift[rowID] = tableShift;
		
		table.style.left = tableShift + 'px';
	},

	addImage: function(imageID){
		batchID = this.activeBatch;

 		rowID = this.rowIndex.indexOf(batchID);

		this.showAddToBatchButton(false, imageID);

		//if there is a blank, busy it
		if(this.currentPageFileIDs[rowID].length < this.imagesPerPage[rowID]){
			$('imageRow' + this.currentPageFileIDs[rowID].length + '_' + batchID).src = this.busyURL;
		}

		var url = "ajax_class_creator.php";
		var params = "ajax_action=addImage&ajax_class=tagedit" +
			"&image=" + imageID +
			"&pageSize=" + this.imagesPerPage[rowID] +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.showPage.bind(this),
				onFailure: this.silentFailed.bind(this)
			}
		);
	},
							 
	resetImage: function(imageID, batchID){
		rowID = this.rowIndex.indexOf(batchID);

		$('imageRow' + imageID + '_' + batchID).style.width = this.baseImageWidth[rowID] + 'px';
	},
	
	resetImages:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		for (i=0; i < this.imagesPerPage[rowID]; i++) {
			this.resetImage(i, batchID);
		}
		$('imageRow_' + batchID).style.left = '0px';
	},
							 
	selectImage:function(batchID, imageIndex){
		rowID = this.rowIndex.indexOf(batchID);

		if(this.selectDisabled[rowID] || imageIndex >= this.currentPageFileIDs[rowID].length){
			//alert('disabled at: ' + batchID + ' ' + this.selectDisabled[rowID]);
			return;
		}
		imageID = this.currentPageFileIDs[rowID][imageIndex];
		foundAt = this.imageSelect[rowID].indexOf(imageID);
		
		if(foundAt > -1){
			this.imageSelect[rowID] = this.imageSelect[rowID].without(imageID);
			$('imageRow' + imageIndex + '_' + batchID).style.backgroundColor = '';
		}else{
			this.imageSelect[rowID].push(imageID);
			$('imageRow' + imageIndex + '_' + batchID).style.backgroundColor = '#ff9900';
		}

		this.showSelectCount(batchID);
	},

	showSelectCount: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		
		var count = this.imageSelect[rowID].length;

		var selectCount = $('BatchEditSelectCount');

		if(count > 1){
			selectCount.innerHTML = '(' + count + ' files selected)';
		}else if(count > 0){
			selectCount.innerHTML = '(' + count + ' file selected)';
		}else{
			selectCount.innerHTML = '';
		}
	},

	loadPrevPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		this.prevPage[rowID] = null;
		this.prevPageFileIDs[rowID] = null;
		
		//alert('loadPrev');
		if(this.pageNum[rowID] <= 0){
			
			return;
		}

		var url = "ajax_class_creator.php";
		var params = "ajax_action=getBatchPage&ajax_class=tagedit" +
			"&page=" + (this.pageNum[rowID] - 1) +
			"&pageSize=" + this.imagesPerPage[rowID] +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.storePrevPage.bind(this),
				onFailure: this.silentFailed.bind(this)
			}
		);
	},

	storePrevPage: function(req){
		//alert('storePrev');
		//alert(req.getAllResponseHeaders() + "\n\n" + req.responseText);

		var data = AjaxUtil.receiveArray(req.responseText);

		if(data.error == 1) {
			this.failed();
			return;
		}

		var batchID = data.batchID;
		var rowID = this.rowIndex.indexOf(batchID);
		var thePrevPage = new Array;
		var thePrevPageFileIDs = new Array;

		if(data.fileIDs.length > 0){
			data.fileIDs.each(
				(
					function(fileID, index){
						var anImage = new Image();
						anImage.src =	data.fileThumbs[index]; 
						anImage.onmouseover = function(){
							showtrailBatch(data.fileThumbsBig[index],data.fileTitles[index],data.fileTypes[index]);
						}
						thePrevPageFileIDs[index] = fileID;
						thePrevPage[index] = anImage;
					}
				).bind(this)
			);
		}

		this.prevPage[rowID] = thePrevPage;
		this.prevPageFileIDs[rowID] = thePrevPageFileIDs;
		//this.showPrevButton(batchID);
		this.refreshRecentBatches(batchID,data.recentBatches);
	 	return;
	},

	showPrevPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		if(this.pageNum[rowID] < 1){
			this.showPageOfPage(batchID);
			return;
		}

		if(this.prevPage[rowID] == null){
			this.loadPage(batchID, this.pageNum[rowID] - 1);
			return;
		}
		
		//alert('showPrev');
		//$('batchImageToolPrevPage_' + batchID).style.visibility = 'hidden';
		//$('batchImageToolNextPage_' + batchID).style.visibility = 'hidden';
		this.selectDisabled[rowID] = true;

		this.nextPage[rowID] = new Array;
		this.nextPageFileIDs[rowID] = new Array;
		theCurrentFileIDs = new Array;
		
		if(this.prevPage[rowID].length > 0){
			this.prevPage[rowID].each(
				(
					function(image, index){
						var imgElement = $('imageRow' + index + '_' + batchID);
						var onmouseoverAttr = imgElement.onmouseover;
						if(index < this.currentPageFileIDs[rowID].length){
							this.nextPage[rowID][index] = new Image();
							this.nextPage[rowID][index].src = imgElement.src;
							this.nextPage[rowID][index].onmouseover = onmouseoverAttr;
							this.nextPageFileIDs[rowID][index] = this.currentPageFileIDs[rowID][index];
						}
						imgElement.onmouseover = image.onmouseover;
						imgElement.src = this.busyURL;
						imgElement.src = image.src;
						if(this.imageSelect[rowID].indexOf(this.prevPageFileIDs[rowID][index]) > -1){
							imgElement.style.backgroundColor = '#ff9900';
						}else{
							imgElement.style.backgroundColor = '';
						}
						theCurrentFileIDs[index] = this.prevPageFileIDs[rowID][index];
					}
				).bind(this)
			);
		}

		for (i = this.prevPage[rowID].length; i < this.imagesPerPage[rowID]; i++){
			imgElement = $('imageRow' + i + '_' + batchID);
			onmouseoverAttr = imgElement.onmouseover;
			if(i < this.currentPageFileIDs[rowID].length){
				this.nextPage[rowID][i] = new Image();
				this.nextPage[rowID][i].src = imgElement.src;
				this.nextPage[rowID][i].onmouseover = onmouseoverAttr;
				this.nextPageFileIDs[rowID][i] = this.currentPageFileIDs[rowID][i];
			}
			imgElement.onmouseover = null;
			imgElement.src = 'images/image_place_holder.gif';
			imgElement.style.backgroundColor = '';
		}

		this.pageNum[rowID] = this.pageNum[rowID] - 1;
		this.currentPageFileIDs[rowID] = theCurrentFileIDs;
		this.selectDisabled[rowID] = false;
		this.showPageOfPage(batchID);
		this.loadPrevPage(batchID);
		
		return;
	},

	showPrevButton:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		if(this.pageNum[rowID] > 0){
			$('batchImageToolPrevPage_' + batchID).style.visibility = 'visible';
		}else{
			$('batchImageToolPrevPage_' + batchID).style.visibility = 'hidden';
		}
	},

	loadNextPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		//alert('loadNext');

		this.nextPage[rowID] = null;
		this.nextPageFileIDs[rowID] = null;

		var url = "ajax_class_creator.php";
		var params = "ajax_action=getBatchPage&ajax_class=tagedit" +
			"&page=" + (this.pageNum[rowID] + 1) +
			"&pageSize=" + this.imagesPerPage[rowID] +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.storeNextPage.bind(this),
				onFailure: this.silentFailed.bind(this)
			}
		);
	},

	storeNextPage: function(req){
		//alert('storeNext');
		//alert(req.getAllResponseHeaders() + "\n\n" + req.responseText);

		var data = AjaxUtil.receiveArray(req.responseText);

		if(data.error == 1) {
			this.failed();
			return;
		}

		var batchID = data.batchID;
		var rowID = this.rowIndex.indexOf(batchID);
		var theNextPage = new Array;
		var theNextPageFileIDs = new Array;

		if(data.fileIDs.length > 0){
			data.fileIDs.each(
				(
					function(fileID, index){
						var anImage = new Image();
						anImage.src =	data.fileThumbs[index]; 
						anImage.onmouseover = function(){
							showtrailBatch(data.fileThumbsBig[index],data.fileTitles[index],data.fileTypes[index]);
						}
						theNextPageFileIDs[index] = fileID;
						theNextPage[index] = anImage;
					}
				).bind(this)
			);
		}

		this.nextPage[rowID] = theNextPage;
		this.nextPageFileIDs[rowID] = theNextPageFileIDs;
		//this.showNextButton(batchID);

		this.refreshRecentBatches(batchID,data.recentBatches);

	 	return;
	},

	showNextPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		if(this.pageNum[rowID] > (this.totalPages[rowID] - 1)){
			this.showPageOfPage(batchID);
			return;
		}

		if(this.nextPage[rowID] == null){
			this.loadPage(batchID, this.pageNum[rowID] + 1);
			return;
		}

		//$('batchImageToolNextPage_' + batchID).style.visibility = 'hidden';
		//$('batchImageToolPrevPage_' + batchID).style.visibility = 'hidden';
		this.selectDisabled[rowID] = true;

		this.prevPage[rowID] = new Array;
		this.prevPageFileIDs[rowID] = new Array;
		theCurrentFileIDs = new Array;
		
		if(this.nextPage[rowID].length > 0){
			this.nextPage[rowID].each(
				(
					function(image, index){
						var imgElement = $('imageRow' + index + '_' + batchID);
						var onmouseoverAttr = imgElement.onmouseover;
						if(index < this.currentPageFileIDs[rowID].length){
							this.prevPage[rowID][index] = new Image();
							this.prevPage[rowID][index].src = imgElement.src;
							this.prevPage[rowID][index].onmouseover = onmouseoverAttr;
							this.prevPageFileIDs[rowID][index] = this.currentPageFileIDs[rowID][index];
						}
						imgElement.onmouseover = image.onmouseover;
						imgElement.src = this.busyURL;
						imgElement.src = image.src;
						if(this.imageSelect[rowID].indexOf(this.nextPageFileIDs[rowID][index]) > -1){
							imgElement.style.backgroundColor = '#ff9900';
						}else{
							imgElement.style.backgroundColor = '';
						}
						theCurrentFileIDs[index] = this.nextPageFileIDs[rowID][index];
					}
				).bind(this)
			);
		}

		for (i = this.nextPage[rowID].length; i < this.imagesPerPage[rowID]; i++){
			imgElement = $('imageRow' + i + '_' + batchID);
			var onmouseoverAttr = imgElement.onmouseover;
			if(i < this.currentPageFileIDs[rowID].length){
				this.prevPage[rowID][i] = new Image();
				this.prevPage[rowID][i].src = imgElement.src;
				this.prevPage[rowID][i].onmouseover = onmouseoverAttr;
				this.prevPageFileIDs[rowID][i] = this.currentPageFileIDs[rowID][i];
			}
			imgElement.onmouseover = null;
			imgElement.src = this.emptyImageURL;
			imgElement.style.backgroundColor = '';
		}

		this.pageNum[rowID] = this.pageNum[rowID] + 1;

		this.currentPageFileIDs[rowID] = theCurrentFileIDs;
		this.selectDisabled[rowID] = false;
		
		this.showPageOfPage(batchID);
		//this.showPrevButton(batchID);
		this.loadNextPage(batchID);
		
		return;
	},

	showNextButton:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		if(this.pageNum[rowID] < (this.totalPages[rowID]-1)){
			$('batchImageToolNextPage_' + batchID).style.visibility = 'visible';
		}else{
			$('batchImageToolNextPage_' + batchID).style.visibility = 'hidden';
		}
	},

	loadPage:function(batchID, pageNum){
		$('BatchEditLoading').style.visibility = 'visible';
		rowID = this.rowIndex.indexOf(batchID);
		pageNum = Math.max(0, Math.min((this.totalPages[rowID]-1), pageNum));

		var url = "ajax_class_creator.php";
		var params = "ajax_action=getBatchPage&ajax_class=tagedit" +
			"&page=" + pageNum +
			"&pageSize=" + this.imagesPerPage[rowID] +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.showPage.bind(this),
				onFailure: this.silentFailed.bind(this)
			}
		);
	},
	
	showPage: function(req){
		$('BatchEditLoading').style.visibility = 'hidden';
		//alert(req.getAllResponseHeaders() + "\n\n" + req.responseText);
		
		var data = AjaxUtil.receiveArray(req.responseText);
		
		if(data.error == 1) {
			this.failed();
			return;
		}
	
		var batchID = data.batchID;
		var rowID = this.rowIndex.indexOf(batchID);
		//$('batchImageToolNextPage_' + batchID).style.visibility = 'hidden';
		//$('batchImageToolPrevPage_' + batchID).style.visibility = 'hidden';
		this.selectDisabled[rowID] = true;
		this.nextPage[rowID] = new Array;
		this.nextPageFileIDs[rowID] = new Array;
		this.prevPage[rowID] = new Array;
		this.prevPageFileIDs[rowID] = new Array;
		this.currentPageFileIDs[rowID] = new Array;

		if(typeof(data.fileIDs.length) == 'undefined'){
			data.fileIDs.length = 0;
		}

		if(data.fileIDs.length > 0){
			data.fileIDs.each(
				(
					function(fileID, index){
						var imgElement = $('imageRow' + index + '_' + batchID);
						imgElement.src =	data.fileThumbs[index]; 
						imgElement.onmouseover = function(){
							showtrailBatch(data.fileThumbsBig[index],data.fileTitles[index],data.fileTypes[index]);
						}
					 
						this.currentPageFileIDs[rowID][index] = fileID;
						if(this.imageSelect[rowID].indexOf(fileID) > -1){
							imgElement.style.backgroundColor = '#ff9900';
						}else{
							imgElement.style.backgroundColor = '';
						}
						this.currentPageFileIDs[rowID][index] = fileID;
					}
				).bind(this)
			);
		}
		
		for (i = data.fileIDs.length; i < this.imagesPerPage[rowID]; i++){
			imgElement = $('imageRow' + i + '_' + batchID);
			imgElement.onmouseover = null;
			imgElement.src = 'images/image_place_holder.gif';
			imgElement.style.backgroundColor = '';
		}
	
		if(data.addToBatchButtons && data.addToBatchButtons.length > 0){
			data.addToBatchButtons.each(
				(
					function(id, index){
						this.showAddToBatchButton(true, id);
					}
				).bind(this)
			);
		}

		this.pageNum[rowID] = Number(data.pageNum);
		this.totalPages[rowID] = data.totalPages;
		
		this.selectDisabled[rowID] = false;
		this.showPageOfPage(batchID);
	
		//this.showNextButton(batchID);
		//this.showPrevButton(batchID);
		this.loadNextPage(batchID);
		this.loadPrevPage(batchID);
		
		this.refreshRecentBatches(batchID,data.recentBatches);
		
		return;
	},

	showPageOfPage: function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		pageOfPage = $('batchImageToolPage_' + batchID);
		pageOfPage.innerHTML = pageOfPage.innerHTML.replace(/^(\D*)\d+(\D+)\d+(\D*)$/,'$1' + Math.max(Number(1),(Number(this.pageNum[rowID]) + Number(1))) + '$2' + Math.max(1,this.totalPages[rowID]) + '$3');
		this.showPrevButton(batchID);
		this.showNextButton(batchID);

		this.rememberBatch(batchID);
		this.showSelectCount(batchID);
	},

	rememberBatch: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);

		if(batchID > 0){
			page = this.pageNum[rowID];
		}else{
			page = 0;
		}

		var url = "ajax_class_creator.php";
		var params = "ajax_action=rememberBatch&ajax_class=tagedit" +
			"&page=" + page +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.saveRememberBatch.bind(this)
			}
		);
	},

	saveRememberBatch: function(req){
		//alert(req.responseText);
	},
	
	jumpPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		var newPageControl = $('batchImageToolJumpPage_' + batchID);
		var newPage = newPageControl.value;
		
		newPageControl.value = '';
		
		if(isNaN(newPage)){
			alert('please enter a number');
		}else if(Number(newPage) < 1 || Number(newPage) > this.totalPages[rowID]){
			alert('That page does not exist');
		}else{
			newPage = Number(newPage);
			this.loadPage(batchID, newPage - 1);
		}
	},

	showFirstPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		this.loadPage(batchID, 0);
	},

	showLastPage:function(batchID){
 		rowID = this.rowIndex.indexOf(batchID);

		this.loadPage(batchID, this.totalPages[rowID]-1);
	},

	moveSelected:function(batchID, targetType){
		$('BatchEditLoading').style.visibility = 'visible';
 		rowID = this.rowIndex.indexOf(batchID);

		if(targetType == 'New'){
			target = $('batchImageToolNew_' + batchID).value;
			target = encodeURI(target);
	    target = target.replace(/&/g, '%26');
	    target = target.replace(/=/g, '%3D');
		}else{
			target = $('batchImageToolRecent_' + batchID).value;
			if(isNaN(target)){
				return;
			}
		}

		this.imageSelect[rowID].each(
			(
				function(id, index){
					this.showAddToBatchButton(true, id);
				}
			).bind(this)
		);
		
		var url = "ajax_class_creator.php";
		var params = "ajax_action=moveTo&ajax_class=tagedit" +
			"&selected=" + this.imageSelect[rowID] +
			"&target=" + target +
			"&targetType=" + targetType +
			"&page=" + this.pageNum[rowID] +
			"&pageSize=" + this.imagesPerPage[rowID] +
			"&batchID=" + batchID;

		this.imageSelect[rowID] = new Array;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.showPage.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	createBatch: function(title){
		$('BatchEditLoading').style.visibility = "visible";
		var title = $('batchImageToolNew').value;

		var url = "ajax_class_creator.php";
		var params = "ajax_action=createNew&ajax_class=tagedit" +
			"&title=" + encodeURIComponent(title) +
			"&buttons=" + this.addToBatchButtons;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.initToBatch.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},
	
	useBatch: function(batchID, pageNum){
		$('BatchEditLoading').style.visibility = 'visible';
		if(!batchID){
			var batchID = $('batchImageToolRecent').value;
		}

		if(!pageNum){
			pageNum = 0;
		}

		var url = "ajax_class_creator.php";
		var params = "ajax_action=loadBatch&ajax_class=tagedit" +
			"&batchID=" + batchID +
			"&page=" + pageNum +
			"&buttons=" + this.addToBatchButtons;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.initToBatch.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	initToBatch: function(req){
		//alert(req.responseText);

		var data = AjaxUtil.receiveArray(req.responseText);
		
		if(data.error == 1) {
			this.failed();
			return;
		}
	
		$('BatchEditLoading').style.visibility = 'hidden';

		if(data.problem && data.problem == 'batch deleted'){
			//alert('deleted');
			return;
		}
		
		var BatchEditTitle = $('BatchEditTitle');
		BatchEditTitle.innerHTML = data.title;

		var BatchEditDiv = $('BatchEditTabs');
		BatchEditDiv.innerHTML = data.tools;

		var batchID = data.batchID;
		this.initializeImage(
				data.initImage[0],
				data.initImage[1],
				data.initImage[2],
				data.initImage[3],
				data.initImage[4],
				data.initImage[5],
				data.initImage[6],
				data.initImage[7],
				data.initImage[8],
				data.initImage[9]
			);

		data.initImageIDs.each(
			(
				function(ID, index){
					if(ID != 'empty'){
						this.initFileID(batchID, index, ID);
					}
				}
			).bind(this)
		);

		var rowID = this.rowIndex.indexOf(batchID);

		this.initializeOps(
				data.initOps[0],
				data.initOps[1],
				data.initOps[2]
			);
		
		data.initOpsTerms.each(
			(
				function(termOpPair, index){
					if(termOpPair[0] == 'toAdd'){
						this.initToAddTerm(batchID, termOpPair[1]);
					}else if(termOpPair[0] == 'toRemove'){
						this.initToRemoveTerm(batchID, termOpPair[1]);
					}else if(termOpPair[0] == 'common'){
						this.initCommonTerm(batchID, termOpPair[1]);
					}
				}
			).bind(this)
		);
		
		data.initTabs.each(
			(
			 	function(tabID, index){
					if(tabID != 'empty'){
						if(index == 0){
							Tabs.switchToTab(tabID);
						}else{
							Tabs.switchFromTab(tabID);
						}
					}
				}
			).bind(this)
		);
		
		this.showSelectCount(batchID);
		
		BatchEditDiv.style.display = 'inline';
		
		this.showBatchEdit(true);

		this.showAddToBatchButtons(true, data.addableIDs);
	},
	
	selectPage: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);

		this.currentPageFileIDs[rowID].each(
			(
				function(fileID, index){
					if(this.imageSelect[rowID].indexOf(fileID) == -1){
						this.selectImage(batchID, index, fileID);
					}
				}
			).bind(this)
		);
	},

	deleteSelected: function(batchID){
		$('BatchEditLoading').style.visibility = 'visible';
		rowID = this.rowIndex.indexOf(batchID);

		if(!confirm('Delete selected images from this batch')){
			return;
		}

		target = '';
		targetType = 'delete';

		var url = "ajax_class_creator.php";
		var params = "ajax_action=moveTo&ajax_class=tagedit" +
			"&selected=" + this.imageSelect[rowID] +
			"&target=" + target +
			"&targetType=" + targetType +
			"&page=" + this.pageNum[rowID] +
			"&pageSize=" + this.imagesPerPage[rowID] +
			"&batchID=" + batchID;

		this.imageSelect[rowID] = new Array;
		
		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.showPage.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	refreshRecentBatches: function(batchID, data){
		var recentSelect = $('batchImageToolRecent');
		var recentSelectBatch = $('batchImageToolRecent_' + batchID);

		for (var i = recentSelect.length; i > 0; i--) {
			recentSelect.remove(i);
		}

		for (var i = recentSelectBatch.length; i > 0; i--) {
			recentSelectBatch.remove(i);
		}

 		data.each(
			(
				function(option, index){
					if(option[1] != batchID){				
						var newOption = document.createElement('option');
						newOption.text = option[0];
						newOption.value = option[1];
						
						var newOption2 = document.createElement('option');
						newOption2.text = option[0];
						newOption2.value = option[1];

						try {
							recentSelectBatch.add(newOption, null); // standards compliant; doesn\'t work in IE
							recentSelect.add(newOption2, null); // standards compliant; doesn\'t work in IE
						}catch(ex) {
							recentSelect.add(newOption); // IE only
							recentSelectBatch.add(newOption2); // IE only
						}
					}
				}
			).bind(this)
		);
	},

	initializeOps: function(batchID, emptyDisambigXML, revisionNumber){
		this.activeBatch = batchID;

 		rowID = this.rowIndex.indexOf(batchID);
		if(rowID == -1){
			this.rowIndex[this.rowIndex.length] = batchID;
	 		rowID = this.rowIndex.indexOf(batchID);
		}

		this.commonTerms[rowID] = new Array;
		this.disambigXML[rowID] = '';
		this.disambigEmptyXML = emptyDisambigXML;
		this.disambigTerms[rowID] = new Array;
		this.disambigRemovedTerms[rowID] = new Array;
		this.toAddTerms[rowID] = new Array;
		this.toRemoveTerms[rowID] = new Array;
		this.revisionNumber[rowID] = revisionNumber;
	},
	
	initCommonTerm: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);
		this.commonTerms[rowID].push(termID);

		if(this.toAddTerms[rowID].indexOf(termID) == -1 && this.toRemoveTerms[rowID].indexOf(termID) == -1){
			this.displayTerm(batchID, termID, true, 'common');
		}
	},

	initToAddTerm: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);
		this.toAddTerms[rowID].push(termID);

		this.displayTerm(batchID, termID, true, 'add');
		this.displayTerm(batchID, termID, false, 'common');
	},

	initToRemoveTerm: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);
		this.toRemoveTerms[rowID].push(termID);

		this.displayTerm(batchID, termID, true, 'remove');
		this.displayTerm(batchID, termID, false, 'common');
	},

	getMoreCommon: function(batchID){
		$('BatchEditLoading').style.visibility = 'visible';
		var url = "ajax_class_creator.php";
		var params = "ajax_action=getCommonTerms&ajax_class=tagedit" +
			"&common=" + this.commonTerms[rowID] +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.storeCommonTerms.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	storeCommonTerms: function(req){
    //alert('storeCommonTerms');
    //alert(req.getAllResponseHeaders() + "\n\n" + req.responseText);

    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;
		rowID = this.rowIndex.indexOf(batchID);

		var container = $('batchOpsToolCommon_' + batchID);
		html = container.innerHTML;
		container.innerHTML = html + data.newCommonDisplay;
		
		if(data.newCommonTerms && data.newCommonTerms.length > 0){
			data.newCommonTerms.each(
				(
					function(termID, index){
						this.commonTerms[rowID].push(termID);
						if(this.toAddTerms[rowID].indexOf(termID) == -1 
							&& this.toRemoveTerms[rowID].indexOf(termID) == -1){
							this.displayTerm(batchID, termID, true, 'common');
						}
					}
				).bind(this)
			);
		}else if(data.newCommonTerms) {
			termID = data.newCommonTerms[1];
			
			this.commonTerms[rowID].push(termID);
			if(this.toAddTerms[rowID].indexOf() == -1 && this.toRemoveTerms[rowID].indexOf(termID) == -1){
				this.displayTerm(batchID, termID, true, 'common');
			}
		}
		$('BatchEditLoading').style.visibility = 'hidden';
	},

	setAddMoreCommon: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		rawKeysBox = $('batchOpsToolAddMoreTags_' + batchID);

		rawKeysBox.value = TagDisambiguation.cleanKeys(rawKeysBox.value);

		//get the lang code
		rootTag = this.disambigEmptyXML.match(TagDisambiguation.assetTagListRegex);

		//use regex to make the CSV of tags into html and inject it into the xml (sweet, eh?)
		tagStart = "<tag tagid=\"\" lang=\"\" string=\"";
		tagEnd = "\"></tag>\n";

		csvRegex = "\\s*,\\s*";
		csvRegex = new RegExp(csvRegex, "gi");

		keys = rawKeysBox.value;

		keys = TagDisambiguation.entityEncode(keys);

		newTags = tagStart + keys.replace(csvRegex, tagEnd + tagStart) + tagEnd;

		//get the tag list
		this.disambigXML[rowID]= this.disambigEmptyXML.replace(TagDisambiguation.assetTagListRegex, rootTag[0] + '\n' + newTags);
		
		//get the new disambiguation form
		this.addMoreCommon(batchID);
	},
					 
	addMoreCommon: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		
		$('batchOpsToolAddMoreBusy_' + batchID).style.visibility = 'visible';
		var url = "ajax_class_creator.php";
		var params = "ajax_action=addCommonTerms&ajax_class=tagedit" +
			"&common=" + this.commonTerms[rowID] +
			"&disambigXML=" + encodeURIComponent(this.disambigXML[rowID]) +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.disambigCommonTerms.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	disambigCommonTerms: function(req){
		//alert(req.responseText);
    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;
		rowID = this.rowIndex.indexOf(batchID);

		$('batchOpsToolAddMoreBusy_' + batchID).style.visibility = 'hidden';

		this.disambigXML[rowID] = data.disambigXML;
		this.disambigTerms[rowID] = data.disambigTerms;
		this.disambigRemovedTerms[rowID] = new Array;
		this.showDisambig(batchID, data.disambigRows);
	},

	showDisambig: function(batchID, rows){
		opsDisambig = $('batchOpsToolDisambig_' + batchID);
		opsDisambigRows = $('batchOpsToolDisambigRows_' + batchID);
		opsTermsTable = $('batchOpsToolTermsTable_' + batchID);

		opsTermsTable.style.display = 'none';

		opsDisambigRows.innerHTML = rows;
		opsDisambig.style.display = 'block';
	},

	hideDisambig: function(batchID){
		opsDisambig = $('batchOpsToolDisambig_' + batchID);
		opsDisambigRows = $('batchOpsToolDisambigRows_' + batchID);
		opsTermsTable = $('batchOpsToolTermsTable_' + batchID);

		opsDisambigRows.innerHTML = '';
		opsDisambig.style.display = 'none';
		opsTermsTable.style.display = 'block';
		
		//$('12345').style.width = opsTermsTable.offsetWidth + 'px';	
	},

	cancelDisambig: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);

		this.disambigXML[rowID] = '';
		this.disambigTerms[rowID] = '';
		this.disambigRemovedTerms[rowID] = '';

		this.hideDisambig(batchID);
	},

	saveDisambig: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
			
		this.disambigTerms[rowID].each(
			(
				function(termID, index){
          regexTerm = '<term((\\s+termid\\s*=\\s*("|\')' +
            termID +
            '\\3)|(\\s+chosen\\s*=\\s*("|\')(true|false)\\5)){2}\\s*(\\/)?\\>(.*?)\\</term\>\\s*';
						
				  regexTerm = new RegExp(regexTerm, "i");

					matches = this.disambigXML[rowID].match(regexTerm);

					if(matches != null && 
							this.toAddTerms[rowID].indexOf(termID) == -1 &&
							this.toRemoveTerms[rowID].indexOf(termID) == -1){
						this.addToCommmon(batchID, termID, matches[8]);
					}
				}
			).bind(this)
		);

		this.disambigXML[rowID] = '';
		this.disambigTerms[rowID] = '';
		this.disambigRemovedTerms[rowID] = '';

		this.hideDisambig(batchID);
	},

	disambigRemoveButton: function(batchID, tagid){
		rowID = this.rowIndex.indexOf(batchID);
 
    //use regex to find and remove the tag from the xml
    tagRegex = "\\<tag\\s*tagid=\"" + tagid + "\"\\s*lang=\"[^\"]*\"\\s*string=\"[^\"]*\"\\>(.|\\n)*?\\<\\/tag\\>";
    tagRegex = new RegExp(tagRegex, "gi");

		this.disambigRemovedTerms[rowID][tagid] = this.disambigXML[rowID].match(tagRegex);

		this.disambigXML[rowID] = this.disambigXML[rowID].replace(tagRegex, '');

		//strike out the row
		tagTD = $('minusTD_' + tagid);
		tagTD.innerHTML = '<img src="images/buttons/plussign.gif" onclick="BatchTagEdit.disambigUnremoveButton(' + batchID + ',\'' + tagid + '\');" style="cursor: pointer;">';

		tagTD = $('tagTD_' + tagid);
		tagTD.innerHTML = '<div style="text-decoration: line-through;">' + tagTD.innerHTML + '</div>';

		tagTD = $('langTD_' + tagid);
		tagTD.innerHTML = '<div style="text-decoration: line-through;">' + tagTD.innerHTML + '</div>';

		tagTD = $('termsTD_' + tagid);
		tagTD.style.visibility = 'hidden';
	},

  disambigUnremoveButton: function(batchID, tagid){
		rowID = this.rowIndex.indexOf(batchID);
		
		//get the lang code
		rootTag = this.disambigXML[rowID].match(TagDisambiguation.assetTagListRegex);

		this.disambigXML[rowID] = this.disambigXML[rowID].replace(TagDisambiguation.assetTagListRegex, rootTag[0] 
				+ '\n' + this.disambigRemovedTerms[rowID][tagid]);

		divRegex = /\<div style="text-decoration: line-through;?"\>/i;
		divRegex2 = /\<\/div\>/;

		tagTD = $('minusTD_' + tagid);
		tagTD.innerHTML = '<img src="images/buttons/minussign.gif" onclick="BatchTagEdit.disambigRemoveButton(' +
			batchID + ',\'' + tagid + '\');" style="cursor: pointer;">';

		tagTD = $('tagTD_' + tagid);
		html = tagTD.innerHTML
		html = html.replace(divRegex, '');
		html = html.replace(divRegex2, '');
		tagTD.innerHTML = '';
		tagTD.innerHTML = html;

		tagTD = $('langTD_' + tagid);
		html = tagTD.innerHTML
		html = html.replace(divRegex, '');
		html = html.replace(divRegex2, '');
		tagTD.innerHTML = html;

		tagTD = $('termsTD_' + tagid);
		tagTD.style.visibility = 'visible';
	},

	disambigSpellingChange:function(batchID, tagid, newTag){
		//alert('change: ' + tagid + ' to: ' + newTag);

		rowID = this.rowIndex.indexOf(batchID);

		rootTag = this.disambigXML[rowID].match(TagDisambiguation.assetTagListRegex);

		//get the tag and replace the id, and the string
		tagRegex = "\\<tag\\s*tagid=\"" + tagid + "\"\\s*lang=\"[^\"]*\"\\s*string=\"([^\"]*)\"\\>(.|\\n)*?\\<\\/tag\\>";
		tagRegex = new RegExp(tagRegex, "i");

		match = this.disambigXML[rowID].match(tagRegex);

		//see if this tag is to be added for sure, or if this is just a spelling change
		if(typeof(match) == 'object' && match[1] == newTag){
			//this tag is to be added
			addTag = " newtag=\"true\"";
		}else{
			addTag = "";
		}

		tagStart = "<tag tagid=\"\" " + addTag + " lang=\"" + rootTag[1] + "\" string=\"";
		tagEnd = "\"></tag>\n";

		this.disambigXML[rowID] = this.disambigXML[rowID].replace(tagRegex, tagStart + TagDisambiguation.entityEncode(newTag) + tagEnd);

		//get the new disambiguation form
		this.addMoreCommon(batchID);
	},

	disambigLangSelect: function(batchID, tagid, lang){
		//alert('for: ' + tagid + ' use: ' + lang);

		rowID = this.rowIndex.indexOf(batchID);

		//get the tag and replace the id, and the string
		tagRegex = "(\\<tag\\s*tagid=\"" + tagid + "\"\\s*lang=\")[^\"]*";
		tagRegex = new RegExp(tagRegex, "i");

		this.disambigXML[rowID] = this.disambigXML[rowID].replace(tagRegex, '$1' + lang);

		//get the new disambiguation form
		this.addMoreCommon(batchID);
	},

	disambigBoxCheck: function(batchID, tagid, termid, checked){
		//alert('set: ' + tagid + '_' + termid + ' to ' + checked);

		chosen = 'false';
		if(checked){
			chosen = 'true';
			this.disambigTerms.push(termid);
		}else{
			this.disambigTerms[rowID].splice(this.disambigTerms[rowID].indexOf(termid), 1);
		}

		//get the tag and replace the id, and the string
		tagRegex = "(\\<tag\\s*tagid=\"" + tagid + "\"\\s*lang=\"[^\"]*\"\\s*string=\"[^\"]*\"\\>(.|\\n)*?\\<term\\s*termid=\"" +
			termid + "\"\\s*chosen=\")(true|false)";
		
		tagRegex = new RegExp(tagRegex, "i");

		this.disambigXML[rowID] = this.disambigXML[rowID].replace(tagRegex, '$1' + chosen);
	},

	delToAdd: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);

		if(this.displayTerm(batchID, termID, false, 'add')){
			this.toAddTerms[rowID].splice(this.toAddTerms[rowID].indexOf(termID), 1);
			this.opsUpdate(batchID, 'delToAdd', termID);
		}

		inRemove = this.toRemoveTerms[rowID].indexOf(termID);
		
		if(inRemove == -1){
			this.displayTerm(batchID, termID, true, 'common');
		}
	},

	delToRemove: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);

		if(this.displayTerm(batchID, termID, false, 'remove')){
			this.toRemoveTerms[rowID].splice(this.toRemoveTerms[rowID].indexOf(termID), 1);
			this.opsUpdate(batchID, 'delToRemove', termID);
		}

		inAdd = this.toAddTerms[rowID].indexOf(termID);
		if(inAdd == -1){
			this.displayTerm(batchID, termID, true, 'common');
		}
	},

	addToCommmon: function(batchID, termID, prefSyn){
		if(!this.displayTerm(batchID, termID, true, 'common')){
			var newTerm = '<div ' +
				'id="batchOpsToolCommonTerm' + termID + '_' + batchID + '" ' +
					'class="left" ' +
					'style="' +
						'width: 150px; ' +
						'display: block; ' +
				'">' +
				'<img src="images/buttons/minussign.gif" ' +
					'onclick="BatchTagEdit.addToRemove(' + batchID + ',\'' + termID + '\')"' +
				'>' +
				'<img src="images/buttons/plussign.gif" ' +
					'onclick="BatchTagEdit.addToAdd(' + batchID + ',\'' + termID + '\')"' +
				'>' +
				'<div ' +
					'id="batchOpsToolCommonLabel' + termID + '_' + batchID + '" ' +
					'style="' +
						'display: inline; ' +
					'"' +
				'>' +
				prefSyn +
				'</div>' +
				'</div>';

			var commonDiv = $('batchOpsToolCommon_' + batchID);

			commonDiv.innerHTML = newTerm + commonDiv.innerHTML;
		}
	},

	addToAdd: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);

		this.displayTerm(batchID, termID, false, 'common');

		if(!this.displayTerm(batchID, termID, true, 'add')){
			var lable = $('batchOpsToolCommonLabel' + termID + '_' + batchID).innerHTML;
			var toAddDiv = $('batchOpsToolToAdd_' + batchID);
			
			existingAdd = '<div ' +
				'id="batchOpsToolToAddTerm' + termID + '_' + batchID + '" ' +
				'class="left" style="width: 150px;">' + 
				'<img src="images/buttons/minussign.gif" ' + 
				'onclick="BatchTagEdit.delToAdd(' + batchID + ',\'' + termID + '\')">' +
				lable + '</div>';

			toAddDiv.innerHTML = toAddDiv.innerHTML + existingAdd;
		}
		
		this.toAddTerms[rowID].push(termID);

		this.opsUpdate(batchID, 'addToAdd', termID);

		this.delToRemove(batchID, termID);
	},

	addToRemove: function(batchID, termID){
		rowID = this.rowIndex.indexOf(batchID);

		this.displayTerm(batchID, termID, false, 'common');

		if(!this.displayTerm(batchID, termID, true, 'remove')){
			var lable = $('batchOpsToolCommonLabel' + termID + '_' + batchID).innerHTML;
			var toRemoveDiv = $('batchOpsToolToRemove_' + batchID);
			
			existingRemove = '<div ' +
				'id="batchOpsToolToRemoveTerm' + termID + '_' + batchID + '" ' +
				'class="left" style="width: 150px;">' + 
				'<img src="images/buttons/minussign.gif" ' + 
				'onclick="BatchTagEdit.delToRemove(' + batchID + ',\'' + termID + '\')">' +
				lable + '</div>';

			toRemoveDiv.innerHTML = toRemoveDiv.innerHTML + existingRemove;
		}

		this.toRemoveTerms[rowID].push(termID);

		this.opsUpdate(batchID, 'addToRemove', termID);

		this.delToAdd(batchID, termID);
	},

	displayTerm: function(batchID, termID, show, group){
		var divName = 'batchOpsTool';
		if(group == 'common'){
			divName = divName + 'CommonTerm';
		}else if(group == 'add'){
			divName = divName + 'ToAddTerm';
		}else if(group == 'remove'){
			divName = divName + 'ToRemoveTerm';
		}else{
			return false;
		}

		divName = divName + termID + '_' + batchID;

		var theDiv = $(divName);

		if(typeof(theDiv) != 'object' || theDiv == null){
			return false;
		}

		if(show){
			theDiv.style.display = 'block';
		}else{
			theDiv.style.display = 'none';
		}

		return true;
	},

	opsUpdate: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		
		this.revisionNumber[rowID]++;

		var url = "ajax_class_creator.php";
		var params = "ajax_action=opsUpdate&ajax_class=tagedit" +
			"&toAdd=" + this.toAddTerms[rowID] +
			"&toRemove=" + this.toRemoveTerms[rowID] +
			"&revisionNumber=" + this.revisionNumber[rowID] +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.opsUpdated.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},
	
	opsUpdated: function(req){
		//alert(req.responseText);

		var data = AjaxUtil.receiveArray(req.responseText);
		
		if(data.error == 1) {
			this.failed();
			return;
		}
	},

	save: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		
		var url = "ajax_class_creator.php";
		var params = "ajax_action=saveBatch&ajax_class=tagedit" +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.resetBatchMode.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	deleteBatch: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		
		var url = "ajax_class_creator.php";
		var params = "ajax_action=deleteBatch&ajax_class=tagedit" +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.resetBatchMode.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	resetBatchMode: function(req){
		//alert(req.responseText);

		var data = AjaxUtil.receiveArray(req.responseText);

		if(data.error == 1){
			this.failed;
			return;
		}

		$('BatchEditTabs').innerHTML = data.tools;
		$('BatchEditTitle').innerHTML = data.title;

		rowID = this.rowIndex.indexOf(data.batchID);

		this.activeBatch = 0; 
		this.rowIndex[rowID] = new Array;
		this.imageSelect[rowID] = new Array;
		this.pageNum[rowID] = 0;
		this.totalPages[rowID] = 0;
		this.nextPage[rowID] = new Array;
		this.nextPageFileIDs[rowID] = new Array;
		this.prevPage[rowID] = new Array;
		this.prevPageFileIDs[rowID] = new Array;
		this.currentPageFileIDs[rowID] = new Array;
		this.selectDisabled[rowID] = false;
		this.commonTerms[rowID] = new Array;
		this.toAddTerms[rowID] = new Array;
		this.toRemoveTerms[rowID] = new Array;
		this.revisionNumber[rowID] = 0;

		this.addToBatchButtons.each(
			(
				function(id, index){
					this.showAddToBatchButton(false, id);
				}
			).bind(this)
		);
	},

	cancel: function(){
		this.showBatchEdit(false);
	},

	endBatchMode: function(req){
		//alert(req.responseText);

		var data = AjaxUtil.receiveArray(req.responseText);
		
		if(data.error == 1) {
			this.failed();
			return;
		}

		$('BatchEditTabs').innerHTML = data.tools
	},

	showTitleTool: function(batchID){
		$('batchInfoToolTitle_' + batchID).style.display = 'none';
		$('batchInfoToolTitleTool_' + batchID).style.display = 'inline';
	},

	saveTitle: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		titleText = $('batchInfoToolTitleToolText_' + batchID);
		
		var url = "ajax_class_creator.php";
		var params = "ajax_action=saveBatchTitle&ajax_class=tagedit" +
			"&title=" + encodeURIComponent(titleText.value) +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.saveTitleResponse.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	saveTitleResponse: function(req){
    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;

		var infoTitle = $('batchInfoToolTitle_' + batchID);
		var title = $('BatchEditTitle');
		
		infoTitle.innerHTML = data.title;
		title.innerHTML = data.title;
		
		
		this.hideTitleTool(batchID);
	},

	hideTitleTool: function(batchID){
		var title = $('batchInfoToolTitle_' + batchID);
		var titleTool = $('batchInfoToolTitleTool_' + batchID);
		var titleText = $('batchInfoToolTitleToolText_' + batchID);

		titleText.value = title.innerHTML;
		title.style.display = 'inline';
		titleTool.style.display = 'none';
	},
	
	showCommentsTool: function(batchID){
		$('batchInfoToolComments_' + batchID).style.display = 'none';
		$('batchInfoToolCommentsTool_' + batchID).style.display = 'inline';
	},

	saveComments: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		commentsText = $('batchInfoToolCommentsToolText_' + batchID);
		
		var url = "ajax_class_creator.php";
		var params = "ajax_action=saveBatchComments&ajax_class=tagedit" +
			"&comments=" + encodeURIComponent(commentsText.value) +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.saveCommentsResponse.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	saveCommentsResponse: function(req){
    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;

		var comments = $('batchInfoToolComments_' + batchID);
		comments.innerHTML = data.comments;

		this.hideCommentsTool(batchID);
	},

	hideCommentsTool: function(batchID){
		var comments = $('batchInfoToolComments_' + batchID);
		var commentsTool = $('batchInfoToolCommentsTool_' + batchID);
		var commentsText = $('batchInfoToolCommentsToolText_' + batchID);

		commentsText.value = comments.innerHTML;
		comments.style.display = 'inline';
		commentsTool.style.display = 'none';
	},

	showNotifyTool: function(batchID){
		$('batchInfoToolNotify_' + batchID).style.display = 'none';
		$('batchInfoToolNotifyTool_' + batchID).style.display = 'inline';
	},

	saveNotify: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		notifyText = $('batchInfoToolNotifyToolSelect_' + batchID);
		
		var url = "ajax_class_creator.php";
		var params = "ajax_action=saveBatchNotify&ajax_class=tagedit" +
			"&notify=" + encodeURI(notifyText.value) +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.saveNotifyResponse.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	saveNotifyResponse: function(req){
    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;

		var notify = $('batchInfoToolNotify_' + batchID);
		notify.innerHTML = data.notifyLabel;

		this.hideNotifyTool(batchID);
	},

	hideNotifyTool: function(batchID){
		var notify = $('batchInfoToolNotify_' + batchID);
		var notifyTool = $('batchInfoToolNotifyTool_' + batchID);
		var notifyText = $('batchInfoToolNotifyToolSelect_' + batchID);

		notifyText.value = notify.innerHTML;
		notify.style.display = 'inline';
		notifyTool.style.display = 'none';
	},

	showStatusTool: function(batchID){
		$('batchInfoToolStatus_' + batchID).style.display = 'none';
		$('batchInfoToolStatusTool_' + batchID).style.display = 'inline';
	},

	saveStatus: function(batchID){
		rowID = this.rowIndex.indexOf(batchID);
		statusText = $('batchInfoToolStatusToolSelect_' + batchID);

		var url = "ajax_class_creator.php";
		var params = "ajax_action=saveBatchStatus&ajax_class=tagedit" +
			"&status=" + encodeURI(statusText.value) +
			"&batchID=" + batchID;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.saveStatusResponse.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	saveStatusResponse: function(req){
		//alert(req.responseText);
    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;

		var status = $('batchInfoToolStatus_' + batchID);
		status.innerHTML = data.statusLabel;

		this.hideStatusTool(batchID);
	},

	hideStatusTool: function(batchID){
		var status = $('batchInfoToolStatus_' + batchID);
		var statusTool = $('batchInfoToolStatusTool_' + batchID);
		var statusText = $('batchInfoToolStatusToolSelect_' + batchID);

		statusText.value = status.innerHTML;
		status.style.display = 'inline';
		statusTool.style.display = 'none';
	},

	showBatchEdit: function(show){
		if(show){
			$('BatchEdit').style.display = 'inline';
			Droppables.add('BatchEdit', {onDrop:BatchTagEdit.imageDrop})
			this.rememberBatch(this.activeBatch);
			this.showAddToBatchButtons(true);
			this.loadAddableButtons(this.activeBatch);
		}else{
			Droppables.remove('BatchEdit');
			$('BatchEdit').style.display = 'none';
			this.rememberBatch(-1);
			this.showAddToBatchButtons(false);
		}
	},

	loadAddableButtons: function(batchID){
		if(batchID < 1){
			return;
		}

		var url = "ajax_class_creator.php";
		var params = "ajax_action=getAddable&ajax_class=tagedit" +
			"&batchID=" + batchID +
			"&buttons=" + this.addToBatchButtons;

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.showAddableButtons.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	showAddableButtons: function(req){
		//alert(req.responseText);

    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		batchID = data.batchID;

		this.showAddToBatchButtons(true, data.addableIDs);
	},

	imageDrop: function(imageElement){
		matches = imageElement.id.match(/subimageThumb_(\d+)/);
		if(matches[1] != null){
			BatchTagEdit.addImage(matches[1]);
		}
	},
						 
	showAddToBatchButtons: function(show, buttonIDs){
		this.addToBatchButtons.each(
			(
				function(id, index){
					element = $('addToLightBoxButton_' + id + '__');
					if(element){
						if(show){
							element.style.display = 'none';
						}else{
							element.style.display = 'inline';
						}
					}
				}
			).bind(this)
		);

		if(show){
			$('BatchEditStart').style.display = 'none';	
		}else{
			$('BatchEditStart').style.display = 'inline';	
		}

		if(show){
			if(buttonIDs && buttonIDs.length > 0){
				this.addToBatchButtons.each(
					(
						function(id, index){
							if(buttonIDs.indexOf(id) > -1){
								this.showAddToBatchButton(true, id);
							}else{
								this.showAddToBatchButton(false, id);
							}
						}
					).bind(this)
				);
			}
		}else{
			addToBatchButtons = document.getElementsByClassName('batchEditAdd');

			addToBatchButtons.each(
				(
					function(button, index){
						button.style.display = 'none';
					}
				).bind(this)
			);
		}
	},

	showAddToBatchButton: function(show, buttonID){
		var button = $('batchEditAddButton_' + buttonID);

		if(button){
			if(show){
				button.style.display = 'inline';
			}else{
				button.style.display = 'none';
			}
		}
	},

	initDraggables: function(delay){
		return;
		if(delay){
			if(this.draggables.length > 0){
				this.draggables.each(
					(
						function(elementID, index){
							new SubsDraggable(elementID,{dragelement:getDragElement,change:hidetrail});
						}
					).bind(this)
				);
			}
		}else{
			window.setTimeout('BatchTagEdit.initDraggables(false)',6000);
		}
	},
									
	initDraggable: function(id){
		return;
		this.draggables.push(id);
	},

	initNavBar: function(
			Items_Total,
			Items_Per_Page,
			CurrentPage,
			NavLink){
		
		this.navBarVars.Items_Total = Items_Total;
		this.navBarVars.Items_Per_Page = Items_Per_Page;
		this.navBarVars.CurrentPage = CurrentPage;
		this.navBarVars.NavLink = NavLink;
	},

	getNavBar: function(){
		var url = "ajax_class_creator.php";

		var cleanNavLink = this.navBarVars.NavLink;

		cleanNavLink = encodeURIComponent(cleanNavLink);
    cleanNavLink = cleanNavLink.replace(/&/g, '%26');
	  cleanNavLink = cleanNavLink.replace(/=/g, '%3D');
	  cleanNavLink = cleanNavLink.replace(/\?/g, '%3F');

		var params = "ajax_action=getNavBar&ajax_class=tagedit" +
			"&Items_Total=" + this.navBarVars.Items_Total +
			"&Items_Per_Page=" + this.navBarVars.Items_Per_Page +
			"&CurrentPage=" + this.navBarVars.CurrentPage +
			"&NavLink=" + cleanNavLink + '';

		var req = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: this.showNavBar.bind(this),
				onFailure: this.failed.bind(this)
			}
		);
	},

	showNavBar: function(req){
    var data = AjaxUtil.receiveArray(req.responseText);

    if(data.error == 1) {
	  	this.failed();
		  return;
		}

		var navBarDivs = document.getElementsByClassName('navBar');
		navBarDivs.each(
			(
				function(aNavBarDiv, index){
					aNavBarDiv.innerHTML = data.navBar;
				}
			).bind(this)
		);
	},

	showIDs: function(batchID){
	  rowID = this.rowIndex.indexOf(batchID);
    alert(this.currentPageFileIDs[rowID]);
	},

	dummyFunction: function(){
		alert('called');
	}
}

