/*   *****************************************************
 *
 *   PAULDELEEUW.NL
 *   Site of Paul de Leeuw / EVA Media
 *
 *   FABRIQUE COMMUNICATIE & DESIGN
 *
 *   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:   JVI
 *
 *   *****************************************************
 */

//  EVA CONFIG START
//
//  Set options for several texts and properties here
//


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



//  INIT: Set up enhancements
//
function evaInit()
{
  // Set up newsletter subscribe form.
  evaSubscribeInit();
  
  // Set up sharing block.
  evaShareInit();  

  // Set up folding of blog comments.
  evaCommentsFoldInit();

  // Set up TwitPic popups
  evaTwitPic();
}



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




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



//  SUBSCRIBE: Add an extra usecue in input fields.
//
function evaSubscribeInit()
{
  if (!$("newsletterform")) return false;

  $$("#newsletterform input").each(function(field)
  {
    if (field.getProperty("value") == "")
    {
      field.setProperty("value", field.getProperty("title"));
    }
    field.addEvent("focus", function(e)
    {
      if ($(this).getProperty("value") == $(this).getProperty("title"))
      {
        $(this).setProperty("value", "");
      }
    });
    field.addEvent("blur", function(e)
    {
      if ($(this).getProperty("value") == "")
      {
        $(this).setProperty("value", $(this).getProperty("title"));
      }
    });
  });
}



//  SHARING: Show/hide sharing popup.
//
function evaShareInit()
{
  if (!$("shareblock")) return false;

  var closeButton = new Element("a", 
  {
    'id': 'closeshareblock',
    'events':
    {
      'click': function()
      {
        $("shareblock").removeClass("popup");
      }
    }
  });
  closeButton.inject($("shareblock"));
  $("shareblock").getElement("h2").addEvent('click', function()
  {
    $(this).getParent().addClass("popup");
  });
}



//  FOLDING: Fold comments in and out.
//
function evaCommentsFoldInit()
{
  if (!$("commentsblock")) return false;
  
  var commentsHeading = $("commentsblock").getElement("h3");
  var commentsBlock = commentsHeading.getNext("ul");
  commentsBlock.fold = new Fx.Morph(commentsBlock, { 'duration': 1000 });
  commentsBlock.originalHeight = parseInt(commentsBlock.getSize().y);

  var foldButton = new Element("span", 
  {
    'html': 'Reacties inklappen',
    'class': 'open',
    'events':
    {
      'click': function()
      {
        if ($(this).hasClass("closed"))
        {
          // Open comments.
          $(this).set('text', 'Reacties inklappen').removeClass('closed').getParent().getNext("ul").fold.start({ 'height': commentsBlock.originalHeight });
        }
        else
        {
          // Close comments.
          $(this).set('text', 'Reacties uitklappen').addClass('closed').getParent().getNext("ul").fold.start({ 'height': 0 });
        }
      }
    }
  });
  
  commentsHeading.empty();
  foldButton.inject(commentsHeading);
}

// Popup Twitpic images
//
function evaTwitPic()
{
    $(document.body).getElements('a.twitpic img').setStyle('height', '20px');
    $(document.body).getElements('a.twitpic').addEvents({
        'mouseenter': function(){
            var img = $(this).getElement('img');
            img.setStyles({
                position: 'absolute',
                height: '150px',
                marginLeft: '-75px',
                marginTop: '-75px'
            });
        },
        'mouseleave': function(){
            var img = $(this).getElement('img');
            img.setStyles({
                position: 'static',
                height: '20px',
                marginLeft: '0px',
                marginTop: '0px'
            });
        }
    });
}
