// GALLERY IMAGES ROTATION

      var galleryDelay=5000;              // default delay before the nex transition
      var faultGalleryDelay=300;          // delay before the next try if transition fails

      var pageGallery=new Array();        // array with URLs of the gallery images
      var galleryImages=new Array();      // array with image objects
      var galleryRecent=-1;               // index of recently displayed gallery image
      var galleryCurrent=-1;              // index of current gallery image
      var galleryNext=-1;                 // index of next gallery image
      var galleryPrev=-1;                 // index of previous gallery image
      var galleryFirstLoaded=-1;          // index of first loaded image ???
      var galleryInterval=0;              // variable for setTimeout (in order to cancel previously set one)
      var galleryStarted=false;           // gallery initialisation flag ???
      var galleryPath='./pics/g_p/';      // relative path to the gallery images

      var imgWidth=528;                   // width of the image area
      var imgHeight=320;                  // height of the image area
      var colMax=0;                       // max height of the 3 columns

      function startGallery(idx)
      {
        var arl=arguments.length;
        if (arl>0)
        {
          if (galleryInterval!=0) clearTimeout(galleryInterval);
        }
        var idx=Number((arl>0)?arguments[0]:galleryRecent+1);
        if ($(galleryImages).eq(idx).attr('loaded')==0) // next image is not loaded
        {
          galleryInterval=setTimeout('startGallery('+idx+')', faultGalleryDelay);
        }
        else
        {
          var max=galleryImages.length-1;
          galleryCurrent=idx;
          galleryNext=idx+1;
          galleryPrev=idx-1;
          if (galleryCurrent>max) galleryCurrent=0;
          if (galleryNext>max) galleryNext=(max>0)?0:-1;
          if (galleryPrev<0) galleryPrev=(max>0)?max:-1;
          if (galleryRecent!=-1) $('#gallery_layer_'+galleryRecent).fadeOut("slow");
          if (galleryCurrent!=-1) $('#gallery_layer_'+galleryCurrent).fadeIn("slow", function(){galleryRecent=galleryCurrent;});
          if (galleryNext!=-1)
          {
            galleryInterval=setTimeout('startGallery()', galleryDelay);
          }
        }
      }

      function galleryPrevImage()
      {
        if (galleryPrev!=-1) startGallery(galleryPrev);
      }

      function galleryNextImage()
      {
        if (galleryNext!=-1) startGallery(galleryNext);
      }

      function setupImage(idx, img)
      {
        var gidx=galleryImages.length;
        var iobj=document.createElement('img');
        var imgw=0;
        var imgh=0;
        var neww=0;
        var newh=0;
        var szsteps=4;
        var i;
        var szratio=0;
        var itop=0;
        var ileft=0;
        img=galleryPath+img;
        $(iobj).attr('id', 'gallery_image_'+idx);
        $(iobj).attr('loaded', '0');
        $(iobj).attr('index', gidx);
        $(iobj).load(function(){
          var idiv=document.createElement('div');
          var gidx=$(this).attr('index');
          $(this).attr('loaded', '1');
          imgw=$(this).attr('width');
          imgh=$(this).attr('height');
          for (i=1; i<=szsteps; i++)
          {
            szratio=1/szsteps*i;
            neww=Math.round(szratio*imgw);
            newh=Math.round(szratio*imgh);
            if (neww>=imgWidth&&newh>=imgHeight) break;
          }
          itop=-Math.round((newh-imgHeight)/2);
          ileft=-Math.round((neww-imgWidth)/2);
          var html='<div id="gallery_layer_'+idx+'" class="imgLayer" style="top: '+itop+'px; left: '+ileft+'px"><img src="'+$(this).attr('src')+'" width="'+neww+'" height="'+newh+'" border="0"></div>';
          $('#gallery-image').append(html);
          if (galleryFirstLoaded==-1 && gidx==0)
          {
            galleryFirstLoaded=gidx;
            startGallery(gidx);
          }
        });
        iobj.src=img;
        galleryImages[gidx]=iobj;
      }

      function preloadImages()
      {
        if (pageGallery.length>0)
        {
          $(pageGallery).each(function(index, value){
            setupImage(index, value);
          });
        }
      }
/*
      function utf8_decode(aa)
      {
        var bb = '', c = 0;
        for (var i = 0; i < aa.length; i++)
        {
          c = aa.charCodeAt(i);
          if (c > 127)
          {
            if (c > 1024)
            {
              if (c == 1025)
              {
                c = 1016;
              }
              else if (c == 1105)
              {
                c = 1032;
              }
              bb += String.fromCharCode(c - 848);
            }
          }
          else
          {
            bb += aa.charAt(i);
          }
        }
        return bb;
      }
*/

      function uDecode(utftext)
      {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length )
        {
          c = utftext.charCodeAt(i);
          if (c < 128)
          {
            string += String.fromCharCode(c);
            i++;
          }
          else if((c > 191) && (c < 224))
          {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
          }
          else
          {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
          }
        }
        return string;
      }
    
      function setNewHeights(h)
      {
        var pollTitle=parseInt($('#home-col-3 > div > h1').css('height'), 10)+8;
        colMax=Math.max(colMax, h);
        $('#home-col-1').css('height', (colMax+pollTitle)+'px');
        $('#home-col-2').css('height', (colMax+pollTitle)+'px');
        $('#home-col-3').css('height', (colMax+pollTitle)+'px');
        $('#pollframe').css('height', h+'px');
      }
      
    
      function equalNews()
      {
        var col1=$('#home-col-1').css('height');
        var col2=$('#home-col-2').css('height');
        var col3=$('#home-col-3').css('height');
        colMax=Math.max(parseInt(col1, 10), parseInt(col2, 10), parseInt(col3, 10));
        $('#home-col-1').css('height', colMax+'px');
        $('#home-col-2').css('height', colMax+'px');
        $('#home-col-3').css('height', colMax+'px');
        $('#home-col-3').css('padding-right', '0px');
/*        
        if (!pollLoaded)
        {
          pollLoaded=$('button[id="OPP-poll-vote-button"]').length;
          if (pollLoaded)
          {
            pollBlock=$('#home-col-3 > div > div').first();
            colmax=Math.max(parseInt(colmax, 10), parseInt(pollBlock.css('height'), 10));
            pollTitle=parseInt($('#home-col-3 > div > h1').css('height'), 10)+10;
            $('#home-col-1').css('height', (colmax+pollTitle)+'px');
            $('#home-col-2').css('height', (colmax+pollTitle)+'px');
            $('#home-col-3').css('height', (colmax+pollTitle)+'px');
          }
          else
            setTimeout('equalNews()', 1000);
        }
*/        
      }

      function homeStartup()
      {
        equalNews();
        preloadImages();
      }

