$(document).ready(function()
{
  $('script[src$="fixflash.js"]').each(function() {
    fixFlash = 0;
  });
  //CCL.HasFlash = function() {return false;};

  rotateBanners();  
  setInterval(rotateBanners, 8000);  

  if(!CCL.EditMode)
  {
    var key = CCL.ThisPage.toLowerCase();
    
    if(key == 'amber' || key == 'eric')
      document.location.search = '?' + key;
    
    if(document.location.search != '')
      $('a').each(function(x, oLink)
      {
        if(!/\?/.test(oLink.href))
          oLink.href = oLink.href + document.location.search;
      });
  
    var o = 0;
    if(document.location.search == '?eric' && $('#amber').length > 0)
    {
      o = $('#amber').get(0);
      o.setAttribute('id', 'eric');
    }
    if(document.location.search == '?amber' && $('#eric').length > 0)
    {
      var o = $('#eric').get(0);
      o.get(0).setAttribute('id', 'amber');
    }
    if(o && CCL.IE && CCL.IEVersion <= 7)
      o.outerHTML = o.outerHTML;
  }
  
  $('#dropdown-arrow').click(function()
  {
    $('#dropdown').toggle('slow');
  });

  $('.body li a').mouseover(function()
  {
    var oObj = this.parentNode;
    oObj.style.backgroundImage = 'url(_layout/circle.png)';
    oObj.style.paddingLeft = oObj.style.paddingTop = '17px';
    oObj.style.width = oObj.style.height = '51px';
    oObj.style.marginTop = (oObj.offsetTop - 17) + 'px';
    oObj.style.marginLeft = (oObj.offsetLeft - 17) + 'px';
    oObj.style.zIndex = 100;
    var sClass = oObj.className.replace(/(png | png)/ig, '');
    /*var oZoom = $('.zoom').get(0);
    if(!oZoom.getAttribute('defaultclass'))
      oZoom.setAttribute('defaultclass', oZoom.className);
    oZoom.className = 'zoom zoom-' + sClass;
    $('.zoom li').hide();*/
  });
  
  $('.body li a').mouseout(function()
  {
    var oObj = this.parentNode;
    oObj.style.background = 'url(/_public/trans.gif)';
    oObj.style.width = oObj.style.height = '34px';
    oObj.style.padding = '0px';
    oObj.style.marginTop = (oObj.offsetTop + 17) + 'px';
    oObj.style.marginLeft = (oObj.offsetLeft + 17) + 'px';
    oObj.style.zIndex = 1;
    /*var oZoom = $('.zoom').get(0);
    if(oZoom.getAttribute('defaultclass'))
      oZoom.className = oZoom.getAttribute('defaultclass');
    else
      oZoom.className = 'zoom';
    $('.zoom li').show();*/
  });
  
  $('body.behandelingen #content h1').click(function()
  {
    var obj = this;
    var selected = $(this).hasClass('selected');
    if(!selected)
      $('body.behandelingen #content h1.selected').click();
    $('body.behandelingen #content h1').removeClass('selected');
    if(!selected)
      $(this).addClass('selected');
    var text = $(this).nextAll();
    for(var i=0; i<text.length; i++)
    {
      if(text[i].nodeName == 'H1')
      //if(text[i].nodeName == 'H1' || text[i].nodeName == 'H5')
        break;
      if(selected)
        $(text[i]).hide('slow');
      else
        $(text[i]).show('slow', function()
        {
          obj.scrollIntoView();
        });
    }
  });
  
  $('body.behandelingen #content h1:gt(0)').nextAll(':not(h1)').hide();
  //$('body.behandelingen #content h1:gt(0)').nextAll(':not(h1 h5)').hide();
  $('body.behandelingen #content h1:eq(0)').addClass('selected');
  
});

function rotateBanners()
{
  if(!window.hasRotated)
  {
    window.hasRotated = true;
    $('.banner img.selected').fadeIn('slow');
    $('.banner').each(function()
    {
      $(this).css('height', this.clientHeight);
      $(this).css('width', this.clientWidth);
    });
    $('.banner img').css('position', 'absolute');
    return;
  }
  $('.banner img.selected').fadeOut('slow');
  $('.banner img.selected').each(function()
  {
    $(this).fadeOut('slow');
    $(this).removeClass('selected');
    var siblings = $(this).siblings();
    var newSelect = $(siblings[Math.floor(siblings.length * Math.random())]);
    newSelect.addClass('selected');
    newSelect.fadeIn('slow');
  });
}

function setDirections()
{
  geocoder = new google.maps.ClientGeocoder();
  if(geocoder)
  {
    geocoder.getLocations(toPoint, function(oResults)
    {
      var map = new google.maps.Map2(document.getElementById("map"));
      map.enableScrollWheelZoom();
      map.setMapType(G_NORMAL_MAP);

      $$('directions').innerHTML = "";
      gdir = new google.maps.Directions(map, $$('directions'));
      google.maps.Event.addListener(gdir, "error", handleErrors);
      if (oResults.Status.code = 200)
      {
        // gdir.load("from: " + $$('fromAddress').value + " to: " + oResults.Placemark[0].address,{locale: "nl"});
        gdir.load("from: " + $$('fromAddress').value + " to: Biltstraat 423, Utrecht",{locale: "nl"});
      }
      else
      {
        alert('Helaas kan de bestemming niet nauwkeurig vastgesteld worden.');
      } 
    });
  }
}


function handleErrors()
{
  if(gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
    alert("Adres kon niet gevonden worden. Gelieve postcodes zonder spatie invoeren.\nError code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
    alert("Onbekende fout.\n Error code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
    alert("Geen adres ingevoerd.\n Error code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_BAD_KEY)
    alert("Neem contact op met uw webmaster. \n Error code: " + gdir.getStatus().code);
  else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
    alert("Uw route kon niet berekend worden.\n Error code: " + gdir.getStatus().code);
  else
    alert("Er is een onbekende fout opgetreden.");
  initialize();
}