//-*- javascript -*-

/*const*/ PAGE_VIEW  = 0;
/*const*/ IMAGE_VIEW = 1;

/*const*/ SHIFT  =  256;
/*const*/ CTRL   =  512;
/*const*/ ALT    = 1024;

/*const*/ ENTER  = 13;
/*const*/ SPACE  = 32;
/*const*/ HOME   = 36;
/*const*/ END    = 35;
/*const*/ LEFT   = 37;
/*const*/ RIGHT  = 39;
/*const*/ DELETE = 46;

/*const*/ I_LC  = 73;
/*const*/ Z_LC  = 90;

/*const*/ SOURCE = "/~ingram/icon/";

/*const*/ FORWARD_ICON        = "forward.png";
/*const*/ FORWARD_GRAYED_ICON = "forward-grayed.png";
/*const*/ BACK_ICON           = "back.png";
/*const*/ BACK_GRAYED_ICON    = "back-grayed.png";
/*const*/ UP_ICON             = "up.png";
/*const*/ EDIT_ICON           = "edit.png";
/*const*/ DELETE_ICON         = "editdelete.png";
/*const*/ INFO_ICON           = "info.png";
/*const*/ ZOOM_ICON           = "viewmag.png";
/*const*/ LOGIN_ICON          = "personal.png";
/*const*/ HELP_ICON           = "help.png";
/*const*/ START_ICON          = "start.png";
/*const*/ START_GRAYED_ICON   = "start-grayed.png";
/*const*/ FINISH_ICON         = "finish.png";
/*const*/ FINISH_GRAYED_ICON  = "finish-grayed.png";

/*const*/ PLAY_ICON           = "play.png";
/*const*/ PLAY_GRAYED_ICON    = "play-grayed.png";
/*const*/ STOP_ICON           = "stop.png";
/*const*/ STOP_GRAYED_ICON    = "stop-grayed.png";

page  = 0;
image = 0;

slideshow_playing = 0;
slideshow_timeout = 5000;

function getkey(event)
{
    if (!event.keyCode) 
	return 0;

    key = event.keyCode;

    if (event.shiftKey) key += SHIFT; 
    if (event.ctrlKey) 	key += CTRL; 
    if (event.altKey) 	key += ALT; 

    return key;
}

function page_view_keyboard_handler(event)
{
    key = getkey(event);

    switch(key) {
    case HOME   : first_page();    break;
    case LEFT   : previous_page(); break;
    case RIGHT  : next_page();     break;
    case END    : last_page();     break;
    case DELETE : remove_page();   break;

    case ENTER :
	location = "index.php?op=view&image=0";
	break;

	//default :
	//alert(event.type + "=" + key);
    }
}

function image_view_keyboard_handler(event)
{
    key = getkey(event);

    switch(key) {
    case HOME   : first_image();    break;
    case LEFT   : previous_image(); break;
    case ENTER  :
    case RIGHT  : next_image();     break;
    case END    : last_image();     break;
    case DELETE : remove_image();   break;
    case I_LC   : view_info();      break;
    case Z_LC   : zoom_view();      break;

    case SPACE  :
	slideshow(!slideshow_playing);
	break;

	//default :
	//alert(event.type + "=" + key);
    }
}

function view_page(nr)
{
    page = nr;
    location.replace("index.php?op=index&page=" + page);
}

function first_page()    { if (page > 0)          view_page(0);          }
function previous_page() { if (page > 0)          view_page(page-1);     }
function next_page()     { if (page+1 < nr_pages) view_page(page+1);     }
function last_page()     { if (page+1 < nr_pages) view_page(nr_pages-1); }

function view_image(nr)
{
    image = nr;
    location.replace("index.php?op=view&image=" + image);
    return;

    var e;

    if (image == 0) {
	document.start.src = SOURCE + START_ICON;
	document.back.src  = SOURCE + BACK_ICON;
	document.start.title = "First Image";
	document.back.title  = "Previous Image";
    } else if (image+1 == nr_images) {
	document.forward.src = SOURCE + FORWARD_ICON;
	document.finish.src  = SOURCE + FINISH_ICON;
	document.forward.title = "Next Image";
	document.finish.title  = "Last Image";
    }

    if (nr == 0) {
	document.start.src = SOURCE + START_GRAYED_ICON;
	document.back.src  = SOURCE + BACK_GRAYED_ICON;
	document.start.title = "";
	document.back.title  = "";
    } else if (nr+1 == nr_images) {
	document.forward.src = SOURCE + FORWARD_GRAYED_ICON;
	document.finish.src  = SOURCE + FINISH_GRAYED_ICON;
	document.forward.title = "";
	document.finish.title  = "";
    }

    image = nr;
    document.photo.src = picture[image];
    //document.photo.src = SOURCE + "ksplash.png";
    //sleep(10);
    e = document.getElementById('caption');
    e.replaceChild(document.createTextNode(caption[image]), e.firstChild);

}

function first_image()    { if (image > 0)           view_image(0);           }
function previous_image() { if (image > 0)           view_image(image-1);     }
function next_image()     { if (image+1 < nr_images) view_image(image+1);     }
function last_image()     { if (image+1 < nr_images) view_image(nr_images-1); }

function cache_next(nr)
{
    if (nr >= nr_images)
	nr = 0;

    if (cached_image[nr] == null) {
	cached_image[nr] = new Image;
	cached_image[nr].src = picture[nr];
    }
}

function next_slide()
{
    if (slideshow_playing) {
	if (image+1 < nr_images)
	    //document.photo = cached_image[image+1];
	    view_image(image+1);
	else
	    //document.photo = cached_image[0];
	    view_image(0);
	//cache_next(image+1);
	setTimeout('next_slide()', slideshow_timeout);
    }
}

function slideshow(play)
{
    slideshow_playing = play;

    if (play) {
	//cache_next(image);
	document.play.src = SOURCE + PLAY_GRAYED_ICON;
	document.stop.src = SOURCE + STOP_ICON;
	//document.photo = cached_image[image];
	//cache_next(image+1);
	//first_image();
	setTimeout('next_slide()', slideshow_timeout);
    } else {
	document.play.src = SOURCE + PLAY_ICON;
	document.stop.src = SOURCE + STOP_GRAYED_ICON;
    }
}
	
function zoom_view()
{
    location = "index.php?op=zoom&image=" + image;
}

function view_info()
{
    //alert("info");
    location = "index.php?op=info&image=" + image;
}

function edit_caption()
{
    location = "index.php?op=editcaption&image=" + image;
}

function remove_image()
{
    if (confirm("Do you want to remove image '" + picture[image] + "' from the Gallery?"))
	location.replace("index.php?op=delete&image=" + image);
}

function rotate_image()
{
	location.replace("index.php?op=rotate&image=" + image);
}

function gallery_help()
{
    location = "index.php?op=help";
}

function goto_parent()
{
    location = "..";
}

function goto_index()
{
    location.replace("index.php?op=index");
}

function main(view, current) 
{
    if (view == PAGE_VIEW) {
	page = current;
	document.onkeydown = page_view_keyboard_handler;
    } else if (view == IMAGE_VIEW) {
	image = current;
	document.onkeydown = image_view_keyboard_handler;
    }
}


