var is_ie = false;
var photos = new Array();
var zoomed_photo_id = false;
var blackout_fade = false;

preload_img = new Image(); 
preload_img.src = 'img/black_20.png';

function zoom_photo(id) {
    zoomed_photo_id = id;
    target_photo_id = id;
    
    $('zoomed_photo').src = 'img/black_20.png';
    $('zoomed_photo').style.width  = photos[id].width;
    $('zoomed_photo').style.height = photos[id].height;
    $('zoomed_photo').src = photos[id].photo_url;

    // $('reblog_link').href = photos[id].reblog_url;
    
    $('caption').innerHTML = photos[id].caption;
    
    if (photos[zoomed_photo_id].link_label) {
        $('photo_link').innerHTML = photos[zoomed_photo_id].link_label;
        $('photo_link').href      = photos[zoomed_photo_id].link_url;
        Element.show('photo_link_container');
    } else {
        Element.hide('photo_link_container');
    }
    
    Element.show('photo_container');
    if (is_ie) {
        Element.show('blackout');
    } else {
        if (blackout_fade) blackout_fade.cancel();
        blackout_fade = new Effect.Appear('blackout', { duration: 0.5 });
    }
    
    if (photos[zoomed_photo_id].prev) {
        Element.show('prev_button');
    } else {
        Element.hide('prev_button');
    }
    
    if (photos[zoomed_photo_id].next) {
        Element.show('next_button');
    } else {
        Element.hide('next_button');
    }
}

function blur_photo() {
    zoomed_photo_id = false;
    Element.hide('photo_container');
    if (is_ie) {
        Element.hide('blackout');
    } else {
        if (blackout_fade) blackout_fade.cancel();
        blackout_fade = new Effect.Fade('blackout', { duration: 0.5 });
    }
}

function prev_photo() {
    if (zoomed_photo_id && photos[zoomed_photo_id] && photos[zoomed_photo_id].prev) {
        zoom_photo(photos[zoomed_photo_id].prev);
    }
}

function next_photo() {
    if (zoomed_photo_id && photos[zoomed_photo_id] && photos[zoomed_photo_id].next) {
        zoom_photo(photos[zoomed_photo_id].next);
    }
}

document.onkeydown = function(e) { 
    if (! e) var e = window.event;
    var code = e.charCode ? e.charCode : e.keyCode;
    if (! e.shiftKey && ! e.ctrlKey && ! e.altKey && ! e.metaKey) {
	    if (code == Event.KEY_LEFT && Element.visible('photo_container')) {
            prev_photo();
	    } else if (code == Event.KEY_RIGHT && Element.visible('photo_container')) {
	        next_photo();
	    } else if (code == Event.KEY_ESC) {
	        blur_photo();
	    } else if (code == 32 /* SPACE */) {
	        if (Element.visible('photo_container')) {
	            blur_photo();
	        } else {
	            zoom_photo(target_photo_id);
	        }
	    }
    }
}
