var cellBlockId;
var photoCache = {};

function monitorInit(id, status) {
	suppressErrors();
	cellBlockId = id;
	context = status;
    setRefreshContentsMethod(monitorRefreshContents);
    setGetPhotosMethod(monitorGetPhotos);
	setShowThumbVidOverlayMethod(monitorShowThumbVidOverlay);
    setGetTotalMethod(monitorGetTotal);
    setThumbMunger(monitorThumbMunger);
    setAcceptDivMunger(monitorAMunger);
    setRightDivMunger(monitorRMunger);
    updatePopUpMenu();
	initContext();
    updateMonitor(id);
}

function monitorThumbMunger(thumb, pic, img, index) {
	img.ondblclick = function(){if(locked!=true){launchMugShotPopup(pic.ID);}};
	img.onclick = function(e){
        if(locked!=true){
            var evtobj = getEventObj(e);
            toggleSelection(evtobj, pic.ID, index);
        }
    };

	thumb.oncontextmenu = showmenuie5;
	thumb.setAttribute("MUGID", pic.ID);
	thumb.setAttribute("CELLBLOCKID", pic.CELLBLOCK_ID);
	thumb.setAttribute("POSITION", pic.POSITION);
}

function monitorRMunger(right, pic) {
	var del = _div("delete");
	var i = image("cellblock/trash.png");
	del.appendChild(i);
	del.onclick = createDeleteHandler(pic.ID);
	del.onmouseover = function(){overlib("Delete",DELAY, thumbDelay);};
	del.onmouseout = function(){ nd(); };
	right.appendChild(del);
    changeCursorArray.push(del);
}

function monitorAMunger(accept, pic) {
	if(context == PENDING){
		var i = image("cellblock/accept.png");
		accept.appendChild(i);
		accept.onmouseover = function(){ overlib("Move to Accepted & Make Viewable",DELAY, thumbDelay); };
		accept.onmouseout = function(){ nd(); };
		accept.onclick = createApproveHandler(pic.ID);
	}else if( context == ACCEPTED){
		var checkBox = document.createElement("input");
		checkBox.checked = pic.VISIBLE;
		checkBox.setAttribute("type", "checkbox");
		checkBox.setAttribute("id", "xb" + pic.ID);
		checkBox.onclick = createSetVisibleHandler(pic.ID);
		checkBox.onmouseover = function(){ overlib("Make Viewable",DELAY, thumbDelay); };
		checkBox.onmouseout = function(){ nd(); };
		accept.appendChild(checkBox);
        changeCursorArray.push(checkBox);
	}
}

function createApproveHandler(id){
	return function() {
		if(	!inSelection(id)){
			clearAllSelections();
			setSelection(id);
		}
		approveMultiple(selectionArray);
	}
}

function changeContext(newContext){
	context = newContext;
	currentPage = 0;
	refreshContents();
	refreshThumbs();
    updatePopUpMenu();
}

function updatePopUpMenu() {
    if (context == PENDING) {
        pendingMenu();
    } else {
        acceptedMenu();
    }
}

function acceptCallback(data) {
	acceptedPhotos.splice(acceptedPhotos.length, 0, data);
	initEverything();
}

function initEverything() {
	refreshContents();
	refreshThumbs();
}

function getAccepted(){
	var tab = getElement("tabAccepted");
	if(tab == null){
		tab = getElement("tabAcceptedBold");
	}
	return tab;
}

function getAcceptedPhotos(id){
	cellBlockController.getApprovedMugShots(id, currentPage*perPage, perPage, {
		callback:function(data) {
			//alert("In getAcceptedPhotos  " + DWRUtil.toDescriptiveString(data, 2));
			acceptedPhotos = data
		},
		async:false
	});
}

function getAcceptedPhotoCount(id){
	cellBlockController.getApprovedMugShotCount(id, {
		callback:function(data) {
			acceptedTotalCount = data
		},
		async:false
	});
}

function getSubmittedMugShots(id){
	cellBlockController.getSubmittedMugShots(id, currentPage*perPage, perPage,{
		callback:function(data) {
			pendingPhotos = data
		},
		async:false
	});
	getAcceptedPhotoCount(id);
}

function getSubmittedPhotoCount(id){
	cellBlockController.getSubmittedMugShotCount(id, {
		callback:function(data) {
			pendingTotalCount = data
		},
		async:false
	});
}

function getPending(){
	var tab = getElement("tabPending");
	if(tab == null){
		tab = getElement("tabPendingBold");
	}
	return tab;
}

function monitorRefreshContents(){
	getAcceptedPhotos(cellBlockId);
	getAcceptedPhotoCount(cellBlockId);	
	getSubmittedMugShots(cellBlockId);
	getSubmittedPhotoCount(cellBlockId);
}

function monitorGetPhotos() {
    if (context == PENDING) {
        return pendingPhotos;
    } else {
        return acceptedPhotos;
    }
}

function monitorGetTotal() {
    if (context == PENDING) {
        return pendingTotalCount;
    } else {
        return acceptedTotalCount;
    }
}

function monitorShowThumbVidOverlay(pic, thumbWrap1, index) {
	var overlayRight = _div(null, "overlayRight");
	var overlay = _div(null, "overlay");
	var vidOverlayImg = image("cellblock/vidIcon7.png");
	createThumbHover(overlayRight, pic.USER, pic.CAPTION);
	overlayRight.ondblclick = function(){if(locked!=true){launchMugShotPopup(pic.ID);}};
	overlayRight.onclick = function(e){
        if(locked!=true){
            var evtobj = getEventObj(e);
            toggleSelection(evtobj, pic.ID, index);
        }
    };
	thumbWrap1.appendChild(overlayRight);
	overlayRight.appendChild(overlay);
	overlay.appendChild(vidOverlayImg);
}

function iehackery(pic, context) {
	if( context == ACCEPTED) {
		var xbox = getElement("xb" + pic.ID);
		if (pic.VISIBLE) {
			xbox.checked = true;
		} else {
			xbox.checked = false;
		}
	}
}

function updateMonitor(id){
	showThumbPane();
	setTimeout("updateMonitor('"+id+"')", updateTimer);
}

function pendingMenu() {
    var items = [ ["aprv", "Accept"], ["del", "Delete"] ];
    updatePopupMenu(items);
}

function acceptedMenu() {
    var items = [ ["copy", "Copy"], ["cut", "Cut"], ["paste", "Paste"], ["del", "Delete"], ["visible", "Make Visible"], ["notVisible", "Make Not Visible"], ["rotRight", "Rotate Right"], ["rotLeft", "Rotate Left"]];
    updatePopupMenu(items);
}

function updatePopupMenu(data) {
    var menuobj=document.getElementById("ie5menu");
    killChildren(menuobj);
    
    for (var i = 0; i < data.length; i++) {
        var row = data[i];
        var div = _div(row[0], "menuitems");
        div.appendChild(textNode(row[1]));
        menuobj.appendChild(div);
    }
}
