var imgOrigName="";
var imgOrigWidth=0;
var imgOrigHeight=0;
var imgOrigLeftOffset=0;
var imgSmallWidth=100;
var imgSmallHeight=100;
var imgSmallLeftOffset=0;
var stillAnimating=false;

$(document).ready(function() {
    $("a.showOrHideLink").attr("href","#");
    $("a.showOrHideLink").click( function() {
        var finder = $(this);
        var block = finder.next(".hideable");
        while (!block.length && finder.length) {
            finder = finder.parent();
            block = finder.next(".hideable");
        }
        if (block.length) {
            //initialize
            var showHide='hide';
            var effect=null;
            var duration=200;
            if (block.css('display')=='none') {
                showHide='show';
                if (block.hasClass('hideableBox')) {
                    effect="easeOutBounce";
                    duration=500;
                }
            }
            //switch class of block heading (if exists)
            var blockID=block.get(0).id;
            var heading=$("#"+blockID+"_h");
            //for any found heading (one or none) change class:
            heading.toggleClass("showing", showHide!='hide');
            heading.toggleClass("hidden", showHide=='hide'); 
            //animate block itself           
            block.animate({
                height: showHide,
                opacity: showHide
                }, duration, effect);
        };
        return false;
        //according to http://blog.reindel.com/2006/08/11/a-hrefjavascriptvoid0-avoid-the-void/
        //this should prevent the browser from evaluating the href and jumping to the top.
    });
    //$("a.backtotop").attr("href","#");
    
    /*
    $("a.backtotop").click( function() {
        $("html, body").animate({scrollTop:0},200);
        //bei Safari genügt body, bei FireFox geht's nur mit html.
        return false;
    });*/
    
    $.localScroll({offset:-45});
    
    $("img.hoverToOrigSize").hover(
        function() {
            if (!stillAnimating) {
                if (imgOrigName != $(this).attr("src")) {
                    //preload image
                    imgSmallWidth = $(this).width();
                    imgSmallHeight = $(this).height();  
                    imgSmallLeftOffset = $(this).css("margin-left");              
                    imgOrigName = $(this).attr("src");
                    var img = new Image();
                    var toAnimate = $(this);
                    stillAnimating=true;
                    $(img).load(function() {
                        //event handler to be executed after loading image incl. resizing
                        imgOrigWidth=img.width; imgOrigHeight=img.height; 
                        imgOrigLeftOffset=(imgSmallWidth-imgOrigWidth)/2;
                        delete img;
                        toAnimate.animate({'width':imgOrigWidth, 'height':imgOrigHeight, 'margin-left':imgOrigLeftOffset}, 100, function(){stillAnimating=false;});
                    }).attr({'src': imgOrigName});
                } else {//size is still cached, resize directly 
                    stillAnimating=true;
                    $(this).animate({'width':imgOrigWidth, 'height':imgOrigHeight, 'margin-left':imgOrigLeftOffset}, 100, function(){stillAnimating=false;});
                }
            }
        },
        function() {
            if (!stillAnimating) {
                stillAnimating=true;
                $(this).animate({'width':imgSmallWidth, 'height':imgSmallHeight, 'margin-left':imgSmallLeftOffset},300, function(){stillAnimating=false;});
            }
        }
    ); 

});

