/*   *****************************************************
 *
 *   DACTIQUE
 *
 *   
 *   Fabrique Design & Communicatie
 *   
 *   Behaviour layer: javascript enhancements
 *
 *   This should comply with Dutch web guidelines
 *   See: http://www.webrichtlijnen.nl/
 *   
 *   DEPENDS ON MOOTOOLS LIBRARY v2
 *   Uses: core, class, native,
 *   element, window. effects
 *
 *   location: /behaviour/dactique.js
 *   version:  0.1
 *   date:     03/01/2010
 *
 *   *****************************************************
 */


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


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



//  INIT: Set up enhancements
//
function dactiqueInit()
{
  // Init form inputs and labels. 
  dactiqueFormsInit();

  // Init print function. 
  dactiquePrintInit();
  
  // Init external links function. 
  dactiqueExternalLinksInit();

  // Init bookmark function. 
  dactiqueBookmarkInit();

  // Init email this function. 
  dactiqueEmailInit();

  // Init Contactmap this function. 
  dactiqueContactmapInit();
  
  // Init news blocks height in IE
  if (Browser.Engine.trident) 
  {
    dactiqueNewsBlocksIeInit();
  }
}



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



//  FORM ENHANCEMENTS: Add presentation for focus.
//
function dactiqueFormsInit()
{
  var formFields = $$("#search input.toggle", 
                      "#footer input.toggle");
  if (formFields.length == 0) return;
  
  formFields.each(function(elem, index)
  {
    if (elem.value != "")
    {
      elem.getPrevious().setStyle('opacity',0);
    }
    elem.addEvent('focus', function()
    {
      if ($(this).value == "")
      {
        $(this).getPrevious().tween('opacity',1,0);
      }
    });
    elem.addEvent('blur', function()
    {
      if ($(this).value == "")
      {
        $(this).getPrevious().tween('opacity',0,1);
      }
    });
  });
}



//  PRINT FUNCTION: Add print listitem with click event.
//
function dactiquePrintInit()
{
  var articleActions = $$(".article-actions ul");
  if (articleActions.length == 0) return;
  
  var printAction = new Element("li",
  {
    'class': 'action-print',
    'html': '<a href="#">Print</a>',
    'events':
    {
      'click': function(evt)
      {
        if (window && window.print) window.print();
        evt.preventDefault();
      }
     }
  });
  printAction.inject(articleActions[0]);
}



//  EXTERNAL LINKS FUNCTION: make external links open in a new window.
//
function dactiqueExternalLinksInit()
{
  var externalLinks = $$("a.extern");
  if (externalLinks.length == 0) return;
  
  externalLinks.each(function(elem, index)
  {
    elem.addEvent('click', function(evt)
    {
      if (typeof popupWin !== 'undefined') popupWin.close();
      popupWin = window.open(this.href, "", "");
      evt.preventDefault();
    });
  });
}



//  BOOKMARK FUNCTION: add bookmark link and function if applicable.
//
function dactiqueBookmarkInit()
{
  var articleActions = $$(".article-actions ul li");
  if (articleActions.length == 0) return;
  
  if (Browser.Engine.webkit || Browser.Engine.presto) return;

  // Fetch the URL and title
  var url = window.location.href || "";
  var title = document.title || "";
  if (url == "" || title == "") return false;
  
  var bookmarkAction = new Element("li");
  var bookmarkLink = new Element("a",
  {
    'href': url,
    'title': title,
    'html': 'Bookmark',
    'class': 'bookmark',
    'events':
    {
      'click': function(evt)
      {
        if (window.sidebar)
        {
          // Firefox
        	window.sidebar.addPanel(this.title, this.href, "");
        }
        else if (window.external)
        {
          // Microsoft's implementation
        	window.external.AddFavorite(this.href, this.title);
        }
        evt.preventDefault();
      }
     }
  });
  bookmarkLink.inject(bookmarkAction);
  bookmarkAction.inject(articleActions[3], 'before');
}


//  EMAIl THIS FUNCTION: show or hide popup with email form.
//
function dactiqueEmailInit()
{
  var emailForm = $("email-form");
  var emailToggle = $$(".article-actions a.email-form-open")[0];
  if (!emailForm || !emailToggle) return;
  
  var emailCloseButton = new Element("a",
  {
    'class': 'email-form-close',
    'href': '#',
    'html': 'X&nbsp; <span>Sluiten</span>',
    'events':
    {
      'click': function(evt)
      {
        $("email-form").tween('opacity',1,0);
        evt.preventDefault();
      }
     }
  });
  emailCloseButton.inject(emailForm.getElement("form"));
  
  emailForm.tween('opacity',1,0);

  if (emailForm.getElement("form").hasClass('contains_errors'))
  {
    emailFormShow();
  }
  emailToggle.addEvent('click', function(evt)
  {
    emailFormShow();
    evt.preventDefault();
  });
}

function emailFormShow()
{
  // Fetch form and dimensions.
  var emailForm = $("email-form");
  var viewportScroll = window.getScroll();
  var viewportSize = window.getSize();
  var headerSize = $("header").getSize();
  var emailFormSize = emailForm.getSize();
  
  // Calculate new position.
  var emailFormLeft = parseInt((viewportSize.x - emailFormSize.x) / 2);
  var emailFormTop = parseInt(viewportScroll.y - headerSize.y + ((viewportSize.y - emailFormSize.y) / 2));

  // Set new position.
  emailForm.setStyles(
  {
    //'left': emailFormLeft,
    'top': emailFormTop
  });
  
  // Fade in email form.
  emailForm.tween('opacity',0,1);
}


//  NEWS BLOCK HEIGHT FIX: set height on news items based on visual.
//
function dactiqueNewsBlocksIeInit()
{
  var newsBlocks = $$(".home .main .listing-item", ".home .main .listing-item-large");
  if (newsBlocks.length == 0) return;

  newsBlocks.each(function(elem, index)
  {
    var visualSize = parseInt(elem.getElement("img").getProperty('height'));
    var elemSize = parseInt(elem.getProperty('height'));
    if (elemSize < visualSize) elem.setStyle('height', visualSize);
  });
}


//  Map for Contactpage
//
function dactiqueContactmapInit()
{
  var mapContainer = $('map');
  if (!mapContainer || !GBrowserIsCompatible()) {
    return false;
  }
  var map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng(52.235268, 6.8516995), 15);
  var point = new GLatLng(52.235268, 6.8516995);
  map.addOverlay(new GMarker(point));
}
