var anim_delay = 600;

function setup_ratio(i) {
    var  o = $("#sit").offset();
    var xo = o.left;
    var yo = o.top;

    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName=="Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        else if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
    }

    var xroom = winW - (xo+15); /* these may need tweaking later */
    var yroom = winH - (yo+15);

    var ratio = 1.0;
    while( xroom < i.width*ratio || yroom < i.height*ratio )
        ratio -= 0.01;

    return ratio;
}

function setup_clicks(s) {
    var small = true;

    $("span#note").text("(click image to enlarge)");
    $("img#it").click(function(){
        if( small ) {
            $(this).animate({width: s.orgw, height: s.orgh}, anim_delay);
            small = false;
            $("span#note").text("(click image to shrink)");

        } else {
            $(this).animate({width: s.neww, height: s.newh}, anim_delay);
            small = true;
            $("span#note").text("(click image to enlarge)");
        }
    });
}

$(document).ready(function() {
    var a = $("img#it");

    if( a.length == 0 ) {
        // hopefully jsed... otherwise, what can ya do
        var x = $("#sit").attr('x');
        // jQuery("<img>").load(function(){
        $("<img>").load(function(){
            var ratio = setup_ratio(this);

            var s = {
                orgw: this.width,
                orgh: this.height,
                neww: this.width*ratio,
                newh: this.height*ratio
            };

            $(this).attr({width: 40, height: 30}).appendTo("#sit").animate({width: s.neww, height: s.newh}, anim_delay);

            if( ratio < 1.0 ) {
                setup_clicks(s);
            }

        }).attr({id: 'it', src: x});

    } else {
        a.load(function() {
            var ratio = setup_ratio(this);

            if( ratio < 1.0 ) {
                var s = {
                    orgw: this.width,
                    orgh: this.height,
                    neww: this.width*ratio,
                    newh: this.height*ratio
                };

                setup_clicks(s);
                $(this).animate({width: s.neww, height: s.newh}, anim_delay);
            }
        });
    }

    $("div.inav a").click(function() {
        if( this.innerHTML.match(/Images/) ) return true;

        var h = this.href;
        var x = h.replace(/^.*i=/, "");

        $("div.inav").html("<img style='border: none' src='/loading.gif'>");
        // jQuery("<img>").load(function(){
        var img = document.createElement("img");
        $(img).load(function(){
            $("img#it").animate({width: 40, height: 30}, anim_delay, "", function(){
                location.href = h + ";jsed=1";
            });
        }).attr({src: x});

        return false;
    });


});
