//
// ZEN IMAGE GALLERY
// Written by Blain Hosford
// Copyright 2009, Zen Webware
// www.zenwebware.com
//
var image_no = 0;
var images = [];
var image_target_x = 0;
var image_slide_timer;
var total_width = 0;

document.onkeydown = function(e) {
    var key = e.keyCode;
    if(key == 37 || key == 39) {
        return false;
    }
}


function add_image(width) {
    var image_gallery = document.getElementById("image_gallery");
    if(image_gallery.style.width == 0) image_gallery.style.width = "0px";
    if(image_gallery.style.left == 0) image_gallery.style.left = "0px";
    images[images.length] = total_width;
    //
    // NOTE:
    // Add an extra pixel to the width to account for IE bug.
    // Also keep track of the true total width so subsequent images are correctly positioned.
    //
    image_gallery.style.width = parseInt(image_gallery.style.width) + width + 1 + "px";
    total_width += width;
}


function home_image() {
    image_no = 0;
    image_target_x = -images[image_no];
    clearTimeout(image_slide_timer);
    image_slide_timer = setInterval("slide_images()", 10);
}


function next_image() {
    if(image_no < images.length - 1) {
        var image_gallery = document.getElementById("image_gallery");
        image_no++;
        image_target_x = -images[image_no];
        clearTimeout(image_slide_timer);
        image_slide_timer = setInterval("slide_images()", 10);
    }
}

    
function previous_image() {
    if(image_no > 0) {
        var image_gallery = document.getElementById("image_gallery");
        image_no--;
        image_target_x = -images[image_no];
        clearTimeout(image_slide_timer);
        image_slide_timer = setInterval("slide_images()", 10);
    }
}
    
    
function slide_images() {
    var image_gallery = document.getElementById("image_gallery");
    var x = parseInt(image_gallery.style.left);
    x = Math.round(x + (image_target_x - x) / 4);
    if(Math.abs(image_target_x - x) <= 4) {
        x = image_target_x;
        clearTimeout(image_slide_timer);
    }
    image_gallery.style.left = x + "px";
}
