function initReviewTabs(){

  if ($('review-meta')) {
    // Set height to highest tab
    var tabDetailsDimensions = $('tab-details').getSize();
    var tabLocationDimensions = $('tab-location').getSize();
    var padding = 20;

    // Activate tabs
    $('h2-details').addClass('active');
    $('h2-details').addClass('tabbed');
    $('h2-location').addClass('tabbed');
    $('tab-details').addClass('tabbed');
    $('tab-location').addClass('tabbed');
    $('tab-location').addClass('inactive');

    if (tabDetailsDimensions.y > tabLocationDimensions.y){
      $('tab-location').setStyle('height', tabDetailsDimensions.y - padding);
    }else if (tabLocationDimensions.y > tabDetailsDimensions.y){
      $('tab-details').setStyle('height', tabLocationDimensions.y - padding);
    }

    // Layout hackfix for maps tab
    //showLocationTab();

    // Go to details-tab
    $('h2-details').addEvent('click', function(){
      showDetailsTab();
    });

    // Go to location-tab
    $('h2-location').addEvent('click', function(){
      showLocationTab();
    });
  }

}



/***
 * Show locationtab on postpage
 */
function showLocationTab() {
  var h2Location = $('h2-location');
  var h2Details = $('h2-details');
  var tabLocation = $('tab-location');
  var tabDetails = $('tab-details');

  if(!h2Location || !h2Details || !tabLocation || !tabDetails)
    return false;
  
  h2Location.addClass('active');
  h2Details.removeClass('active');
  tabLocation.removeClass('inactive');
  tabDetails.addClass('inactive');
}


/***
 * Show detailstab on postpage
 */
function showDetailsTab() {
  var h2Details = $('h2-details');
  var h2Location = $('h2-location');
  var tabDetails = $('tab-details');
  var tabLocation = $('tab-location');

  if(!h2Location || !h2Details || !tabLocation || !tabDetails)
    return false;
  
  h2Details.addClass('active');
  h2Location.removeClass('active');
  tabDetails.removeClass('inactive');
  tabLocation.addClass('inactive');
}

function initPostblocks(){
  $$('.postblock').getElement('.details').setStyle('display', 'none');
  $$('.postblock').setStyle('cursor', 'pointer');

  $$('.postblock').addEvent('mouseenter', function(){
    this.getElement('.details').setStyle('display', 'block');
    this.getChildren('div').set('tween', {duration: 'short'});
    this.getChildren('div').tween('height', '280px');
  });

  $$('.postblock').addEvent('mouseleave', function(){
    this.getElement('.details').setStyle('display', 'none');
    this.getChildren('div').set('tween', {duration: 'short'});
    this.getChildren('div').tween('height', '96px');
  });

  $$('.postblock').addEvent('mouseup', function(){
    window.location = this.getElement('.postlink').getAttribute('href');
  });
}

/**
 * Initialize tip 10
 */
function initTip10(){
  $('nav-tip10').addEvent('click', function(){
    $('tip10').toggleClass('hidden');
    this.toggleClass('active');
    return false;
  });
}

/**
 * Initialize LMTS banner
 */
function initLMTS(){
  
  if ($$('.lmtsblock')){
    $$('.lmtsblock').setStyle('cursor', 'pointer');
    $$('.lmtsblock').addEvent('click', function(){
      window.location = 'http://www.lastminuteticketshop.nl';
    });
  }
}

/**
 * Initialize archive-selector
 */
function initArchiveSelector(){
  if ($('archiveselect_date')){
    $('archiveselect_date').addEvent('change', function(){
      this.getParent('form').submit();
    });
  }
}

/***
 * Initialize Googlemaps blocks
 */
var places = new Array();
function initMaps() {
  var map = $('map');
  if(!GBrowserIsCompatible() || !map)
    return false;
  
  

  // Set Amsterdam in centre as default http://maps.google.nl/?ie=UTF8&ll=52.371826,4.898529&spn=0.038935,0.077248&z=14
  window['map'] = new GMap2($("map"));
  var tabLocation = $('tab-location');
  if(!tabLocation) {
    window['map'].addControl(new GLargeMapControl());
    setMapCenter(52.371826, 4.898529);
  } else {
    setMapCenter(52.376018, 4.886513);
  }
  processPlaces.delay(1000);
}

/**
 * Init dagagenda genre en dag selectie
 */
function initAgenda() {
  
  if(!$('agenda')) {
    return false;
  }
  $('agenda').getElements('ul').each(function(item) {
    $(item).addClass('hidden'); 
  })
  
  $('agenda').getElements('a[class=tab]').each(function(item) {
    $(item).addEvent('click', function() {
      $(this.getParent().getParent()).getElement('ul').toggleClass('hidden')
      this.toggleClass('active')
    })
  })
}


/** 
 * Focus map to given coordinates
 *
 * @param  lat      latitude coordinate
 * @param  lng      longitute coordinate
 */
function setMapCenter(lat, lng, pan) {
  if(!window['map'])
    return false;

  window['map']['center'] = new GLatLng(lat, lng);
  if(typeof pan == 'undefined') {
    window['map'].setCenter(window['map']['center'], 14);
  } else {
    window['map'].panTo(window['map']['center']);
  }
}


/** 
 * Prepare a place to be set on the map
 *
 * @param  oPlace      Placeobject
 */
function addPlace(oPlace) {
  //alert(oPlace);
  places.push(oPlace);
}


/** 
 * Process places
 */
function processPlaces() {
  var i = places.length; 
  while(i--) {
    paintPlace(places[i].name, places[i].lat, places[i].lng, places[i].address, places[i].city, places[i].image);
  }
}


/** 
 * Paint place
 */
function paintPlace(pName, pLat, pLng, pAddress, pCity, pImage) {
  var gMarker = new GMarker(new GLatLng(pLat, pLng));

  GEvent.addListener(gMarker, "click", function() {
    var placeContent = "<div class=\"info-window\"> \n";
    placeContent += "  <h3>"+pName+"</h3> \n";
    placeContent += "  <p>"+pAddress+"</p> \n";
    placeContent += "  <p>"+pCity+"</p> \n";

    if(pImage != '')
      placeContent += "  <img src=\"/imageserver/server.php?image=" + pImage + "&template=maps\" alt=\"\" /> \n";

    placeContent += "</div> \n";
    gMarker.openInfoWindowHtml(placeContent);
  });
  
  window['map'].addOverlay(gMarker);
}

/**
 * AjaxForm helpers
 */
function serializeForm(el) {
  var elems = $(el).getElements('input, textarea, select');
  var postData = new Element('form', {action: 'null'});
  elems.each(function(item, index) {
    if(item.get('name') != null) { 
      if((item.get('type') == 'checkbox' && item.get('checked') == true) || item.get('type') != 'checkbox') {
        postData.grab(new Element('input', {name: item.get('name'), value: item.get('value')}));
      }
    }
  });
  //alert(postData.toQueryString());
  return postData.toQueryString();
}

function post_remote(uri, data, elemToUpdate) {
  var r = new Request.HTML({
    'method': 'post', 
    'url': uri,
    'update': $(elemToUpdate),
    'data': data
    }).send();
}


window.addEvent('domready', function(){
  initMaps();
  initReviewTabs();
  initPostblocks();
  initTip10();
  initLMTS();
  initArchiveSelector();
  initAgenda();
});
