/* diagram_gallery.js   ** Copyright (C) 2008. olivier mai. All Rights Reserved.
**
*/

function ShowDiagramGallery( resourcepath, title, items) {

   $('body').append(''
      +'<div id="thumbnail_layer">'
      +    '<!--<img class="loadingicon" style="display:block; position:absolute; z-index:1" src="/omlib/img/processingbar.gif" />-->'
      +  '<div id="alphashading" class="alphashading" />'
      +  '<div id="thumb_canvas">'
      +  '</div>'
      +'</div>'
      );
    var winW = document.body.offsetWidth-20;
    var winH = document.body.offsetHeight-20;

   //$('.loadingicon').css({ top: Math.round( (winH - 32) /2 )+'px', left: Math.round( (winW - 32)/2 )+'px'});
   //$('.loadingicon').css({ top: Math.round( (winH - 19) /2 )+'px', left: Math.round( (winW - 303)/2 )+'px'});
   $('div#thumbnail_layer').click( function(){$('div#thumbnail_layer').unbind('click').remove()} );
   $('div#thumbnail_layer').css({cursor:'wait'});

       var params = {resourcepath:resourcepath, title:title, items:items};

       $('#thumb_canvas').loadSchematicThumbnails(params);

}

//
//
//

(function($){
   var preloadcache;
   var preloadimageObj;

   function noop(){};

   function finish() {
        // bind each thumbnail to pop-out behavior
        $('img.thumbimg').popOnMouseover();
   }
   function afterLoad(){
              if(pool.seq == 'pre') $('img.loadingicon').remove();
              //
              if(pool.seq='post') {
                $('img.thumbspinner').each(function() {

                        $(this).removeClass('thumbspinner');
                        $(this).attr('src', $(this).attr('uri'));
                });
              }

              // id layerhead imported by loadPage is now available for menu insertion .
              $('table#xmenu').attr('align','center');
              $('table#xmenu').clone(true).prependTo('div#layerhead');

   }
   function loadPage(){
        $("#thumb_canvas").load("/omlib/thumbs.php",
                                {resourcepath:pool.resourcepath, title:pool.title, items:pool.items},
                                function(responseText, textStatus, XMLHttpRequest)
                                {
              this; // dom element
                afterLoad();
                window.setTimeout(finish,250);
                                });
   }
   function specifyNextFilename(src) {
        var fn = String( (src.split(','))[0] );
        fn = fn.replace('.htm','.gif');
        fn = fn.replace('&','%26');
        preloadimageObj.src='/omlib/thumbnail.php?res='+fn;
   }
   function fetchNextImage() {
        var i = preloadcache.pop();
        if (undefined != i) {  specifyNextFilename(i);   return; }
        //
        preloadimageObj.onload=noop;
        loadPage();

   }
   function preloadImages(items) {
        var i;
        if (undefined == preloadimageObj) preloadimageObj = new Image();
        //preloadimageObj = document.createElement('img');
        preloadimageObj.onload = fetchNextImage;
        preloadcache = String(items).split(':');
        fetchNextImage();
   }
   function postloadImages(items) {
        loadPage();
   }
   var pool = {seq:'post' , resourcepath:'', title:'', items:''};
   //////////////
   $.fn.extend({
   //////////////
      loadSchematicThumbnails: function(options){
         pool = $.extend(pool,options);
         return this.each( function(){
                //
                //preloadImages(pool.items);
                //
                postloadImages(pool.items)
                //
         });
      }
   });
})(jQuery);

//
//
//

(function($){

   var active=false;

   function popoutImage(animage) {
      var img = $('img#popi');
      img.css('width','');
      img.css('height','');
      img.attr('src', $(animage).attr('src'));
      $('a#popa').show();
      var w; var h;
      w = img.width()  * 1.1 ;
      h = img.height() * 1.1 ;
      var t; var l;
      t = $(animage).offset().top  - (  (h - $(animage).height())/2 );
      t = t + $('div#thumb_canvas').scrollTop();
      l = $(animage).offset().left - (  (w - $(animage).width() )/2 );

      img.css({
                width:  Math.round(w)+'px',
                height: Math.round(h)+'px'
              });

      $('a#popa').css({ position:'absolute', top:Math.round(t)+'px',left:Math.round(l)+'px'});
   }

   function fitRoundedimage(animage) {
      $('a#popa').show();
      var img = $('img#popi');
      var w; var h;
      w = Math.round( img.width()  * 1.0 );
      h = Math.round( img.height() * 1.0 );

      $('img.roundedgif').css('width', w+8+'px').css('height', h+8+'px');
      $('img.roundedgif').show();

      $('img.roundedshadow').css('width', w+8+'px').css('height', h+8+'px');
      $('a#popa').css({width:w+'px', height:h +'px'});

      var t; var l;
      t = $(animage).offset().top  - (  (h - $(animage).height())/2 );
      t = t + $('div#thumb_canvas').scrollTop();
      l = $(animage).offset().left - (  (w - $(animage).width() )/2 );

      $('a#popa').css({ position:'absolute', top:Math.round(t)+'px',left:Math.round(l)+'px'});
   }

   function fullRoundedThumbnail(animage) {
      $('img#popi').get(0).onload = function(){ fitRoundedimage(animage)} ;
   }

   function imageMouseover(ev) {
      //popoutImage(this);
      var animage = this;
      $('a#popa').attr('href', $(this).parent().parent().attr('href'));
      $('a#popa').mouseout(imageMouseout);
      $('img#popi').get(0).onload = function(){ fitRoundedimage(animage)} ;
      $('img#popi').attr('src', $(this).attr('src'));
   }

   function imageMouseout(ev) {
      $('a#popa').hide();

   }

   //////////////
   $.fn.extend({
   //////////////
      popOnMouseover: function(options){
         //pool = $.extend(pool,options);
         return this.each( function(){
              $(this).mouseover(imageMouseover);
           // $(this).mouseout(imageMouseout);
             // $(this).mousedown(imageMousedown);
             // $(this).mousemove(imageMousemove);
         });
      }
   });
})(jQuery);

//
document.write('<link   type="text/css" rel="stylesheet" href="/omlib/diagram_gallery.css"  />');
document.write('<!--[if IE 6]>');
document.write('<style>');
document.write('.thumbcontainer     { display:inline; }');
document.write('</style>');
document.write('<![endif]-->');
//

