var currentNode;

$(document).ready(function() {
    $("#links a").mouseover( function() { highlight(this); } );
    $("#highlight").mouseout( function() { $("#highlight").hide() } );
    $("#highlight").click( function() { window.location.href = currentNode.href; } );
    initializeImageGallery();
});

var imagesToLoaded = 0;
var imagesLoaded = 0;
var imageSelected = false;

function initializeImageGallery() {
    $("#image_gallery img").click(imageClick);
//    currentImage = findFirstImage();
    selectImage( findFirstImage(), false );
}

function findFirstImage() {
    var images = $("#image_gallery img");
    return images[0];
}

function imageClick() {
    selectImage(this, this != currentImage || !imageSelected);
}

var currentImage;

function nextImage() {
    var next = $(currentImage.parentNode).next()[0];
    if (next && next.firstChild) {
        selectImage(next.firstChild, imageSelected);
    }
}

function prevImage() {
    var prev = $(currentImage.parentNode).prev()[0];
    if (prev && prev.firstChild) {
        selectImage(prev.firstChild, imageSelected);
    }
}

function closeImage() {
    selectImage(currentImage, false);    
}

function selectImage(node, select) {
    currentImage = node;
    imageSelected = select;

    if (hasNextImage()) {
        $("#nextImageIcon").css("opacity", "1"); 
        $("#nextImageIcon").css("cursor", "pointer"); 
    } else {
        $("#nextImageIcon").css("opacity", "0.4"); 
        $("#nextImageIcon").css("cursor", "default"); 
    }

    if (hasPrevImage()) {
        $("#prevImageIcon").css("opacity", "1"); 
        $("#prevImageIcon").css("cursor", "pointer"); 
    } else {
        $("#prevImageIcon").css("opacity", "0.4"); 
        $("#prevImageIcon").css("cursor", "default"); 
    }

    //$("#prevImageIcon").hide();
    $("#closeImage").hide();
    //$("#nextImageIcon").hide();
    $("#image_gallery img").css("height", "158px");	
    if (node) {
        centerImage(node);    
    }
}

function centerImage(node) {
    var gallery = document.getElementById("image_gallery");
    var desiredLeft = ($("#image_container").width() - $(node).width()) / 2;
	//desiredLeft = Math.max(320, desiredLeft);
	//var desiredLeft = 330;
    adjustX = Math.round(desiredLeft - $(node).offset().left);
    adjustNode = gallery; 
    increment = adjustX > 0 ? 10 : -10;
    delay = 20;
    acceleration = 1.3;
    adjustLeft();
}

var adjustNode;
var adjustX;
var increment;
var delay;
var acceleration;


function adjustLeft() {
    var node = adjustNode;
    var xIncrement = 0;
    if (adjustX > 0) {
        xIncrement = Math.min(increment, adjustX);
    } else if (adjustX < 0) {
        xIncrement = Math.max(increment, adjustX);
    }
    if (Math.abs(xIncrement) > 0) {
        adjustX -= xIncrement;
        $(node).css("left", $(node).position().left + xIncrement);        
        increment = Math.round(increment * acceleration);
        setTimeout(adjustLeft, delay);
    } else {
        if (imageSelected) {
            resizeImage(currentImage);	
            $("#closeImage").show();
            $("#closeImage").css("position", "absolute");
            $("#closeImage").css("top", $(currentImage).offset().top - 30);
            $("#closeImage").css("left", $(currentImage).offset().left + $(currentImage).width() - 30);
        }
    }
}

function hasNextImage() {
    var next = $(currentImage.parentNode).next()[0];
    return (next && next.firstChild);
}

function hasPrevImage() {
    var prev= $(currentImage.parentNode).prev()[0];
    return (prev && prev.firstChild);
}

function resizeImage(node) {
    $(node).css("height", "286px");
}

function highlight(node, on) {
    currentNode = node;
    var highlightNode = document.getElementById("highlight");
    copyPosition(currentNode, highlightNode);
    $(highlightNode).show();
}

function centerNode(node) {
    var left = (920 - $(node).width()) / 2;
    var top = (920 - $(node).height()) / 2 + $(window).scrollTop();
    $(node).css("top", top);
    $(node).css("left", left);
}

function showBios() {
    $("#hide_bios").fadeOut();
}

function hideBios() {
    $("#hide_bios").fadeIn();
}

function toggleBios() {
    $("#hide_bios").toggle("fast");
}

function showBioEnglish() {
    showDialog("bio_english");
}

function showBioJapanese() {
    showDialog("bio_japanese");
}

function showContact() {
    showDialog("contact_dialog");
}

function showWork() {
    showDialog("work_dialog", function() {        
        centerImage(currentImage);
    });    
}

function resetMenu() {
    hideBios();
}

function copyPosition(from, to) {
   $(to).css('top', $(from).offset().top);
   $(to).css('left', $(from).offset().left);
   $(to).width($(from).width());
   $(to).height($(from).height());
}