/*   *****************************************************
 *
 *   PLASTICISLAND.ORG
 *   Site of the Plastic Island project 
 *   An initiative of Eilander Architects
 *
 *   Behaviour layer: javascript enhancements
 *
 *   This should comply with Dutch web guidelines
 *   See: http://www.webrichtlijnen.nl/
 *   
 *   DEPENDS ON MOOTOOLS LIBRARY V2 CORE + MORE
 *   
 *   location: /behaviour/eva.js
 *   version:  1.0
 *   date:     27/08/2009
 *   author:   VIZI | VORM GEVEN AAN INHOUD
 *
 *   *****************************************************
 */

//  CONFIG START
//
//  Set options for several texts and properties here
//
var plasticOpen = 433;
var plasticClosed = 122;



//
//
//  CONFIG END
//
//  NO USER EDITABLE OPTIONS BEYOND THIS POINT !!!! (No, really)



//  INIT: Set up enhancements
//
function plasticInit()
{
	// Build slider functionality.
  plasticSlidersInit();
}



//  DOMREADY: Attach init routine to domready event
//
window.addEvent("domready", plasticInit);




//  UTILITY FUNCTIONS: Handy stuff for all other enhancements.
//
function plasticOver()
{
	this.addClass("highlight");
}
function plasticOut()
{
	this.removeClass("highlight");
}




//  SLIDERS: Init sliders
//
function plasticSlidersInit()
{
  // Remember minimum slider height
  var minSlider = 0;

  // Keep track of ongoing tweens.
  $("main").busy = false;

  $$(".slider").each(function(elem, index)
  {
    // Check if this slider is the tallest
    if (parseInt(elem.getSize().y) > minSlider)
    {
      minSlider = parseInt(elem.getSize().y);
    }
    
    // Set up slider tween
    elem.squeeze = new Fx.Tween(elem,
    {
      duration: 500,
      onComplete: function()
      {
        elem.open = !elem.open;
        elem.busy = false;
        $("main").busy = ($("slider-1").busy || $("slider-2").busy || $("slider-3").busy || $("slider-4").busy);
      }
    });

    // Set initial status.
    elem.open = (elem.hasClass("slider-open"));
    
    if (elem.open && index == 0)
    {
      $$("#slider-1 div.visual a").slimbox();
    }

    // Add event handlers.
    elem.addEvent('click', function()
    {
      if ($(this).open || $("main").busy)
      {
        return;
      }

      // Close open slider first.
      plasticSliderClose($$(".slider-open")[0]);

      // Open this slider.
      plasticSliderOpen(elem);
    });

    // Prevent default link or form actions on closed sliders
    elem.getElements("a").each(function(link)
    {
      link.addEvent('click', function(evt)
      {
        if ($(this).getParent("div.slider").open) return true;
        $(this).blur();
        evt.preventDefault();
      });
    });
  });

  $$(".slider").each(function(elem)
  {
    elem.setStyle('height', minSlider+100);
  });
}


//  OPEN SLIDER: single animation
//
function plasticSliderOpen(slider)
{
  $(slider).busy = true;
  $(slider).tween('letter-spacing', '-4px', '0');
  $(slider).getElements('h3').each(function(elem)
  {
    elem.tween('letter-spacing', '-5px', '0');
  });
  if (slider.getElementById("content-item-big"))
  {
    var contentBig = $(slider.getElementById("content-item-big"));
    contentBig.tween('letter-spacing', '-10px', '0');
    contentBig.getElement('h3').tween('letter-spacing', '-12px', '0');
    contentBig.getElement('p.morelink').tween('letter-spacing', '-6px', '0');
  }
  $(slider).squeeze.start('width',plasticClosed,plasticOpen);
  if (!slider.hasClass('slider-open')) slider.addClass('slider-open');
  
  if (slider.getProperty("id") == 'slider-1')
  {
    $$("#slider-1 div.visual a").slimbox();
  }
}


//  CLOSE SLIDER: single animation
//
function plasticSliderClose(slider)
{
  $(slider).busy = true;
  $(slider).tween('letter-spacing', '0', '-4px');
  $(slider).getElements('h3').each(function(elem)
  {
    elem.tween('letter-spacing', '0', '-5px');
  });
  if (slider.getElementById("content-item-big"))
  {
    var contentBig = $(slider.getElementById("content-item-big"));
    contentBig.tween('letter-spacing', '0', '-10px');
    contentBig.getElement('h3').tween('letter-spacing', '0', '-12px');
    contentBig.getElement('p.morelink').tween('letter-spacing', '0', '-6px');
  }
  $(slider).squeeze.start('width',plasticOpen,plasticClosed);
  slider.removeClass('slider-open');

  if (slider.getProperty("id") == 'slider-1')
  {
    $$("#slider-1 div.visual a").each(function(visual)
    {
      visual.removeEvents('click');
      visual.addEvent('click', function(evt)
      {
        if ($(this).getParent("div.slider").open) return true;
        $(this).blur();
        evt.preventDefault();
      });

    });
  }
}



