function too_high (items, targetHeight)                       
  { var maxHeight    = 0;                                     
    for( var i=0; i<items.length; i++)                        
      { var height= parseInt( items[i].offsetHeight, 10);     
        if( height > maxHeight) { maxHeight= height; }        
      }                                                       
    if( maxHeight > targetHeight)
      { return true; }
    else
      { return false; }
  }

function resizeMenu (div)
  { var targetHeight = 50;
    var fontSize     = 1.3;
    var initial_pos  = 130; // how far down is the button box

    var buttons      = div.getElementsByTagName( "a");
    var spans        = div.getElementsByTagName( "span");

    var items= [];
    var i;
    for( i=0; i<buttons.length; i++) { items.push( buttons[i]); }
    for( i=0; i<spans.length; i++)   { items.push( spans[i]); }

    while( (fontSize >= 0.8) && too_high(items, targetHeight))
      { fontSize= fontSize - 0.1;
        var style= fontSize.toPrecision(2) + "em";
        for( i=0; i<items.length; i++)
          { items[i].style.fontSize= style; }
      }

    if( initial_pos + (items.length * targetHeight) > $(window).height())
      { var new_initial_pos=  $(window).height() - (items.length * targetHeight);
        if( new_initial_pos < 0) { new_initial_pos= 20; }
        $('div.buttonscontainer').css({ position: "absolute", top: new_initial_pos + 'px' });
      }
  }

function more (that)
  { that.parent('div').prev( 'div').slideToggle( 'normal');
    that.toggleClass( 'more').toggleClass( 'hide');
    if( that.hasClass( 'more' ))
      { that.text( 'continue reading »');
        var to_show= that.parent( 'div').parent( 'div').parent( 'div');
        $('html,body').animate({scrollTop: $(to_show).offset().top - 20},500);
      }
    else
      { that.text( 'hide «'); }
    return false;
  }


var ORIGINAL_WIDTH    =  850;
var RESIZE_UNDER      = 1280;
var MIN_WIDTH         =  400;
var MARGIN_LEFT       =  155;
var MARGIN_RIGHT      =  100;
var MARGIN_RIGHT_BLOG =  200;
var SSHOW_WIDTH       =  420;
var HALFCOL_WIDTH     =  380;

$(document).ready( function() 
  { $('img').each( set_osize); 
    set_width(); 

    // put all tn's in a gallery
    $('a.tn').attr( 'rel', 'prettyPhoto[g]'); 

    // put all tn's in blog stories in galleries, one per story
    var i=1;
    $('div.story_body').each( function() { g = 'prettyPhoto[g' + i++ +']';
                                           $(this).find('a.tn').attr( 'rel', g);
                                         }
                            );
    $('a[rel^="prettyPhoto"]').prettyPhoto({theme: 'dark_rounded',slideshow:5000, autoplay_slideshow:true, overlay_gallery: true, default_width: 600, default_height: 400});
    
  } 
) ;

$(window).resize( set_width ) ;

function set_width()
  { var old_center_width= document.xwidth || ORIGINAL_WIDTH ;
    
    var new_center_width= set_new_width();
    resizeMenu( document.getElementById( 'leftmenu'));
    //alert( 'window width: ' + $(window).width() + ' center_width: ' + center_width);
    if( old_center_width != new_center_width)
      { $( '#center_column').width( new_center_width);
        $( '#bg').width( new_center_width);
        $( '#bg').removeAttr( 'height'); 
        resize_strips( new_center_width);
        resize_tns( new_center_width);
        resize_sshow( new_center_width);
        document.xwidth= new_center_width;
      }
  }

function resize_strips( center_width)
  { $('p.strip').each( function(i) { var tns= $(this).children( 'a.tn');
                                     var new_size= Math.round( (center_width - ((tns.size() -1) * 30) - 80 ) / tns.size());
                                     //alert( 'nb img: ' + tns.size() + ' new size: ' + new_size);
                                     tns.children( 'img').each( function(i) { $(this).width( new_size)
                                                                                     .height( new_size);
                                                                            }
                                                 );
                                   }
                     );
  }
             
      
function add_links_to_sshow()
  { $('#sshow').children( 'a').each( function(i) { var a= $(this);
                                                   var title = a.children( 'img').first().attr( 'alt');
                                                   a.addClass( 'tn').attr( 'title', title);
                                                   }
                                     );
    $('.tn').lightBox();
  }

function file2title( file)
  { var title= file.replace( /^.*\//, '')             // remove path info (up until the last /)
                   .replace( /_(full|tn)?\.jpg$/, '') // remove suffix and extension
                   .replace( /_/g, ' ')                // replace underscore by space
                   .replace( /-/g, ' - ')              // add spaces around -
                   ;
    return title;
  }

function resize_sshow( center_width)
  { var sshow= $('#sshow');
    if( sshow)
      { var new_sshow_size  = Math.round( SSHOW_WIDTH * center_width / ORIGINAL_WIDTH);
        sshow.width( new_sshow_size);
        sshow.children( 'a').children( 'img').width( new_sshow_size).height( new_sshow_size);
        sshow.css( 'background-image', 'none');
        var new_halfcol_width=  center_width - new_sshow_size - 50;
        $('.halfcolumn').width( new_halfcol_width);
      }
  }

function set_new_width()
  { var width = $(window).width();
    var new_width;
    var margin;
    if( document.URL.match( '/blog/'))
      { margin= MARGIN_LEFT + MARGIN_RIGHT_BLOG;
        $('div.right_column').width( MARGIN_RIGHT_BLOG);
      }
    else
      { margin= MARGIN_LEFT + MARGIN_RIGHT;
      }

    if( width < RESIZE_UNDER)       { new_width= width - margin; }

    if( new_width > ORIGINAL_WIDTH) { new_width= ORIGINAL_WIDTH; }
    if( new_width < MIN_WIDTH)      { new_width= MIN_WIDTH;      }

    $('html').width( new_width + margin + 20);

    return new_width;
  }                                                                                                                                   
        
function set_osize (i)
  { this.owidth= $(this).width(); 
    this.oheight=$(this).height(); 
  }

function resize_tns( center_width)
  { // alert( 'resize_tns ' + center_width); 
    $('img.right').each(  function(i) { resize_img( this, center_width); });
  }

function resize_img (img, center_width)
  { var pwidth     = img.owidth / ORIGINAL_WIDTH;          // fraction of the width used by the img
    var new_width  = Math.round( center_width * pwidth);   // img takes up the same fraction of the new width
    var new_height = Math.round( img.oheight * new_width / img.owidth); // img.owidth); # keep width height ration constant
    //alert( 'current center width: ' + center_width + ', pwidth: ' + pwidth + ', new width: ' + new_width + ', new_height: ' + new_height + ', original width: ' + img.owidth + ', original height: ' + img.oheight);
    $(img).width( new_width);
    $(img).height( new_height);
  }


