var selectedPhotoId;
var removableSign = true;

function selectActions(){
    if (selectedActions){
        for(var i=0; i < selectedActions.length; i++){
            var obj = document.getElementById(selectedActions[i]);
            if (obj){
                obj.className = 'action_narrow';
            }
        }
    }
    if (disabledActions){
        for(var i=0; i < disabledActions.length; i++){
            var obj = document.getElementById(disabledActions[i]);
            if (obj){
                obj.className = 'actionDisabled_narrow';
            }
        }
    }
}

function selectPhotoActions(){
    if (selectedPhotoActions){
        for(var i=0; i < selectedPhotoActions.length; i++){
            var obj = document.getElementById(selectedPhotoActions[i]);
            if (obj){
                obj.className = 'action_narrow';
            }
        }
    }
}

function selectTreeItem(id, newStyle, oldStyle, removeable) {
    if (selectedId!=null){
        document.getElementById(selectedId).className = oldStyle;
    }

    selectedId = id;
    if (!document.getElementById(selectedId)) return;

    document.getElementById(selectedId).className = newStyle;
    enableDeleteCategory(removeable);
    removableSign = removeable;
    selectActions();
}

function selectimg(idim){
    if (selectedPhotoId != null) {
        document.getElementById(selectedPhotoId).className = "imglink";
    }
    selectedPhotoId = idim;
    document.getElementById(selectedPhotoId).className = "imgselectlink";

    selectPhotoActions();
}

function goToCategory(categoryId) {
    document.location.href="/servlet/virtualtour?id=" + parseInt(categoryId);
}

function showImage(photoId) {
    showDialog("/servlet/viewphoto?photoId="+parseInt(photoId),640,480);
}


function view_photo_album(){
    alert('View Photo Album');
}
function view_category(){
    alert('View Category');
}
function view_photo(){
    alert('View Photo');
}
function add_category(){
    if (selectedId == null) {
        alert('Please, select a category first.');
        return;
    }
    showDialog("/servlet/addcategory?parentId="+parseInt(selectedId),500,300);
}
function edit_category(){
    if (selectedId == null) {
        alert('Please, select a category.');
        return;
    }
    showDialog("/servlet/editcategory?id="+parseInt(selectedId),500,300);
}
function delete_category(){
    if (!removableSign) return;
    if (selectedId == null) {
        alert('Please, select a category first.');
        return;
    }
    if (confirm("This will permanently delete this category from the Images Repository. If the category has any subcategories, they will be deleted as well. There will not be an undo function. Are you absolutely sure you want to delete the category?")) {
        document.location.href="/servlet/deletecategory?id="+parseInt(selectedId);
    }
}
function upload_photo(){
    if (selectedId == null) {
        alert('Please, select a category first.');
        return;
    }
    showDialog("/servlet/uploadphoto?categoryId="+parseInt(selectedId),500,300);
}
function replace_photo(){
    if (selectedPhotoId == null) {
        alert('Please, select an image first.');
        return;
    }
    showDialog("/servlet/replacephoto?photoId=" + parseInt(selectedPhotoId),500,300);
}
function edit_photo_description(){
    if (selectedPhotoId == null) {
        alert('Please, select an image first.');
        return;
    }
    showDialog("/servlet/editphotodescription?photoId=" + parseInt(selectedPhotoId),500,450);
}
function delete_photo(){
    if (selectedPhotoId == null) {
        alert('Please, select an image first.');
        return;
    }
    if (confirm("This will permanently delete this picture from the Images Repository. There will not be an undo function. Are you absolutely sure you want to delete the picture?")){
        document.location.href="/servlet/deletephoto?id=" + parseInt(selectedId) + "&photoId=" + parseInt(selectedPhotoId);
    }
}

function viewDescription(photoId) {
    showDialog("/servlet/viewdescription?id=" + parseInt(photoId),500,300);
}

function moveUpCategory() {
    if (getId() > 0) {
        document.location.href = '/servlet/upcategory?id='+getId();
    }
}

function moveDownCategory() {
    if (getId() > 0) {
        document.location.href = '/servlet/downcategory?id='+getId();
    }
}

function changeCategory(photoId) {
    showDialog("/servlet/changecategory?photoId="+parseInt(photoId),400,400);
}


function find(array, string) {
    if (array){
	    for (var i = 0; i < array.length; i++) if (array[i] == string) return i;
    }
    return -1;
}


function enableDeleteCategory(enable) {
    if ((selectedActions) && (disabledActions)) {
	    var action5 = 'action5';
	    var index = find(selectedActions, action5);
	    if (index > -1) selectedActions.splice(index, 1);
	    index = find(disabledActions, action5);
	    if (index > -1) disabledActions.splice(index, 1);
	    if (enable) selectedActions.push(action5);
	    else disabledActions.push(action5);
    }
}