var lastParallel = null;
var lastParallelMenu = null;
var lastParallelDescr = null;


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

function movePhoto(direction) {
  if($('works_gallery_slider'))
    var w = $('works_gallery_slider').down().getWidth();
  else
    var w = $('exhibitions_gallery_slider').down().getWidth();  
    
  var distance = 180;          
  if($('works_gallery_slider'))
    distance = 180;
  else if('exhibitions_gallery_slider')
    distance = 110;    
    
  var photo_counter = parseInt($('photo_counter').innerHTML);
  var dist_photo  = 0;
  var dist_li     = 0;
  
  var effects = new Array();
  var effectsDescr = new Array();
  
  if(direction == 'next') {
    
    if(photo_counter < parseInt($('photo_total').innerHTML)) {
      var actual_photo = photo_counter;      
      $('photo_counter').innerHTML = photo_counter + 1;
      
      var list_item = $('photos_descriptions_list').down();
//      dist_li    += list_item.getHeight();                

/*
      for(var i = 0; i < actual_photo - 1; i++) {
        list_item = list_item.next();
      dist_li    += list_item.getHeight();        
      dist_li    = list_item.cumulativeScrollOffset().top;        
      }      
*/  
      if($('photo_'+(photo_counter+1))) {
        
        $$('div.img_container').each(function(c) {
          if(c.id == 'photo_'+(photo_counter+1))
            effects.push(new Effect.Appear('photo_'+(photo_counter+1), {sync: true}));      
          else if(c.id == 'photo_'+photo_counter)
            effects.push(new Effect.Fade('photo_'+photo_counter, {sync: true}));              
          else
            effects.push(new Effect.Fade(c.id, {sync: true}));
        });        
      }
//      dist_li    = list_item.getHeight();
//      effects.push(new Effect.Move('works_gallery_slider', {x: dist_photo, y: 0, mode:'absolute', sync: true}));

      effectsDescr.push(new Effect.Move('photos_descriptions_list', {x: 0, y: -distance, mode:'relative', sync: true}));
    } 
  } else {
    
    if(photo_counter > 1) {    
      photo_counter--;
      $('photo_counter').innerHTML = photo_counter;
      dist_photo = (w * photo_counter + 14 * (photo_counter-1)) - w;
      dist_photo = (w * photo_counter) - w;      
      if(photo_counter == 1) {
        dist_li = 0;
      } else {
        var list_item = $('photos_descriptions_list').down();
        dist_li = 0;        
        dist_li   += list_item.getHeight();                
        
        for(var i = 1; i < photo_counter - 1; i++) {
          list_item = list_item.next();
          dist_li   += list_item.getHeight();                
        }      
      }     
      if($('photo_'+(photo_counter))) {
        $$('div.img_container').each(function(c) {
          if(c.id == 'photo_'+(photo_counter+1))
            effects.push(new Effect.Fade('photo_'+(photo_counter+1), {sync: true}));      
          else if(c.id == 'photo_'+photo_counter)
            effects.push(new Effect.Appear('photo_'+photo_counter, {sync: true}));              
          else
            effects.push(new Effect.Fade(c.id, {sync: true}));
        });                
      }//      effects.push(new Effect.Move('works_gallery_slider', {x: -dist_photo, y: 0, mode:'absolute', sync: true}));
      effectsDescr.push(new Effect.Move('photos_descriptions_list', {x: 0, y: distance, mode:'relative', sync: true}));
    }
    
  }
    var container = $('photo_descr');

    var actual_photo = parseInt($('photo_counter').innerHTML);
    var list_item = $('photos_descriptions_list').down();

    for(var i = 0; i < actual_photo - 1; i++) {
      list_item = list_item.next();
    }

    container.setStyle({"height": list_item.getHeight()+"px"});  
    effectsDescr.push(new Effect.Morph(container, {style: "height:"+list_item.getHeight()+"px;"}));  
  
  if(effects.length > 0) {
    stopParallelEffects(lastParallel);
    lastParallel = new Effect.Parallel(effects, {duration: 0.3});
    
    stopParallelEffects(lastParallelDescr);    
    lastParallelDescr = new Effect.Parallel(effectsDescr, {duration: 0.0});    
  }
}

function beginSlideshow() {
  
  setTimeout(function() {
    
    $$('#home_slideshow figure img').each(
      function(i){
        if(i.visible()) {
          i.fade({duration: 0.4});
          
          if(i.up() && i.up().next() && i.up().next().down() && i.up().next().down() && i.up().next().down().tagName == "IMG") {
            i.up().next().down().appear({duration: 0.4});
            
            $$('#slideshow_indicator li.selected').each(
              function(li) {
                if(li.hasClassName('selected')) {
                  li.removeClassName('selected');
                  li.next().addClassName('selected');
                }
              }
            );          
          } else {
            $('home_slideshow').down().down().appear();
            $$('ul#slideshow_indicator li').each(function(si) {
              si.removeClassName('selected');
            });
            $('slideshow_indicator').down().addClassName('selected');      
          }
        }
      });
      beginSlideshow();
      }, 3000);
}

function resizeDescription() {
  var container = $('photo_descr');
  var actual_photo;
  
	if($('photo_counter'))
  	actual_photo = parseInt($('photo_counter').innerHTML);
    var list_item = $('photos_descriptions_list').down();
  
  for(var i = 0; i < actual_photo - 1; i++) {
    list_item = list_item.next();
  }
  
//  container.setStyle({"height": list_item.getHeight()+"px"});  
  new Effect.Morph(container, {style: "height:"+list_item.getHeight()+"px;", duration: 0.1});
}

  
  function menuAnim(el) {
    var effects = new Array();              
    if(el == null) {

      $$('ul.sub-nav').each(function(elAnim) {
          if(elAnim.visible())
            effects.push(new Effect.BlindUp(elAnim, {sync: true}));          
        });     
         
    } else {

      effects.push(new Effect.BlindDown(el, {sync: true}));  
      $$('ul.sub-nav').each(function(elAnim) {
        
        if(elAnim.id != el.id) {
          if(elAnim.visible())
            effects.push(new Effect.BlindUp(elAnim, {sync: true}));          
        }
        
      });      
    }

    stopParallelEffects(lastParallelMenu);    
    lastParallelMenu = new Effect.Parallel(effects, {duration: 0.3});    
  }


// Observe some elements in the page
document.observe('dom:loaded', function() {
  
  if($('photo_descr')) {
    resizeDescription();
  }
  
  if($('home_slideshow')) {
	  beginSlideshow();
	}
	
  if($('footer')) {
    $('footer').absolutize();
  
    if(document.viewport.getHeight() > ($('footer').cumulativeOffset().top + $('footer').getHeight())) {
      $('footer').setStyle({'top': document.viewport.getHeight() - 3 - $('footer').getHeight() + "px"});
    }
  }
  
  if($('works_thumbs')) {
    $$('#works_thumbs img.thumb').each(function(th) {
      
      th.observe('click', function() {
        var split = (th.id).split("_");
        var n = split[1];

        $$('img.work_photo').each(function(im) {
          im.up().hide();
        });

        if($('photo_'+n)) {
          $('photo_'+n).appear({duration: 0.2}); 
          $('photo_counter').innerHTML = n;
          
          var distance = 180;          
          if($('works_gallery_slider'))
            distance = 180;
          else if('exhibitions_gallery_slider')
            distance = 110;
          
        $('photos_descriptions_list').setStyle({top: -(distance * (n-1))+"px"})
          
        } 
      });
    });
  }
  
  if($('photo_nav'))
    $('photo_nav').writeAttribute({"unselectable": "on"});

  if($('photo_volgende')) {
    $('photo_volgende').observe('click', function(event) {
    	event.stop();
    	movePhoto('next');
    	});
  }
  
  if($('photo_vorige')) {
    $('photo_vorige').observe('click', function(event) {
      event.stop();
      movePhoto('prev');
    });  
  }
  
  $$('ul.sub-nav').each(function(el) {
    el.up().observe('mouseover', function(event) {
      event.stop();   
      if(!el.visible())   
        menuAnim(el);
    });
    
  });      

  $$('#works, #exhibitions_media, #exhibitions_menu').each(function(el){
    el.observe('mouseover', function(event){
      event.stop();
      menuAnim(null);
    });    
  });
  
/*
  $$('img.off').each( function(el) {
    el.observe('mouseover', function(event) {
      event.stop();
      mouseOver(el);
    });
    
    el.observe('mouseout', function(event) {
      event.stop();
      mouseOut(el);
    });            
  });
*/    
});

