var lastParallelOver;
var last_id = 0;

/* Stop Parallel effects running */
function stopParallelEffects(Paralleleffect) {
  if(Paralleleffect != null) {
    Paralleleffect.cancel();
  }
}

document.observe('dom:loaded', function() {

      var buttons  = new Array();
      var images   = new Array();
      var comments = new Array();
      
      $$('#media_navigation .button_image').each(function(el) {
          buttons.push(el);
          if(el.hasClassName('over'))
            el.setOpacity(0.0);
      });      
      
      $$('.subsite_selector img').each(function(el) {
        if(el.hasClassName('image')) {
          images.push(el);
        }
      });
      
      $$('.subsite_selector div.subsite_comment').each(function(el) {
          el.setOpacity(0.0);
          comments.push(el);
      });
      
/*  $$('.button_background').each(function(el) {
    el.observe('click', function click(event) {
      event.stop();          
      window.location.href = event.target.up().up().href ;
    });
  });      
*/
   
  $$('img.button_image, img.image, div.text_news').each(function(el) {
  
    el.observe('mouseover', function obsMouseOver(event) {
      event.stop();              
//      alert(event.target.id);
      filterId = event.target.id;
      id       = filterId.substring( (filterId).indexOf("_")+1, filterId.length);
      if(id == last_id) {
        return;
      }
      last_id = id;
//			$('article_content').innerHTML = $('article_content').innerHTML + "<br />" + id;

      // Effects: fade all other images and comments, 
      // give full opacity to the current image and show the comment
      // The button over appears and the button off fades
      var effects = new Array();
      
      images.each(function(i) {
        if(i.id != "img_"+id)
          effects.push(new Effect.Opacity(i, { sync: true, to: 0.5}));
      });
      
      buttons.each(function(b) {
        
        if(b.hasClassName('off')) {
          if(b.id == 'buttonoff_'+id) {
            effects.push(new Effect.Opacity(b, { sync: true, to: 0.0}));
          } else {
            if(b.getStyle('opacity') <= 0.95)
              effects.push(new Effect.Opacity(b, { sync: true, to: 0.99}));
          }
          
        } else {
          if(b.id == 'buttonover_'+id) {
            if(b.getStyle('opacity') <= 0.95)
              effects.push(new Effect.Opacity(b, { sync: true, to: 0.99}));
          } else {
            if(b.getStyle('opacity') >= 0.1)            
            effects.push(new Effect.Opacity(b, { sync: true, to: 0.0}));
          }        
        }
      });
      
      comments.each(function(c) {
        if(c.id != "comment_"+id && c.getStyle('opacity') != 0.0) {
          effects.push(new Effect.Opacity(c, { sync: true, to: 0.0}));
        }
      });
            
      if($("comment_"+id) && $("comment_"+id).getStyle('opacity') <= 0.99 )
        effects.push(new Effect.Opacity("comment_"+id, { sync: true, to: 0.99}));
        
      if ($("img_"+id) && $("img_"+id).getStyle('opacity') <= 0.99 )
        effects.push(new Effect.Opacity("img_"+id, { sync: true, to: 0.99}));   
           
      stopParallelEffects(lastParallelOver);
      lastParallelOver = new Effect.Parallel(effects, { duration: 0.3 } );

    });
  });
  
    $$('body')[0].observe('mouseover', function obsMouseOver(event) {
      event.stop();
      
      last_id = 0;
            
      // Effects: Full opacity to all images
      // comments hidden
      var effects = new Array();
      
      images.each(function(i) {
        effects.push(new Effect.Opacity(i, { sync: true, to: 0.99}));
      });
      
      comments.each(function(c) {
        effects.push(new Effect.Opacity(c, { sync: true, to: 0.0}));
      });
      
      buttons.each(function(b) {
        if(b.hasClassName('off')) {
          effects.push(new Effect.Opacity(b, { sync: true, to: 0.99}));
        } else {
          effects.push(new Effect.Opacity(b, { sync: true, to: 0.0}));
        } 
      });      
      stopParallelEffects(lastParallelOver);            
      lastParallelOver = new Effect.Parallel(effects, { duration: 0.7} );
    });
    
});


