$(document).ready(function(){
  /* Preload hover images */
  preloadImages(['../images/medewerker-h2-active.png', '../images/medewerker-p-active.png', '../images/submenu-active.png', '../images/menu-active.png']);

  /* Cufon */
  Cufon.replace('#menu li a')('h1')('#adres');

  /* Sfeerbeelden */
  if($('.sfeerbeelden').length > 0) {
    $('.sfeerbeeld img').click(function() {
      var newSrc = $(this).attr('src').replace('_thumb', '');
      var ondr = $(this).attr('alt');
      $('#sfeerbeeld-big').fadeOut(200, function() {
        $('img', this).attr('src', newSrc)
        $('#onderschrift').html(ondr);
        $(this).fadeIn();
      });
    });
  }

  /* Medewerker effects */
  if($('#content .medewerker').length > 0) {
    $('#content .medewerker').click(function() {
      $('#content .medewerker .medewerker-more').slideUp();
      $('#content .medewerker .medewerker-info').removeClass('active');
      var newSrc = $('.medewerker-info img', this).attr('src').replace('_thumb', '');
      $('#medewerker-big').fadeOut(200, function() {
        $(this).attr('src', newSrc).fadeIn();
      });
      $('.medewerker-more', this).slideDown();
      $('.medewerker-info', this).addClass('active');
    });
  }

  /* Start sidebar slideshow */
  if($('#sidebarimg-holder').length > 0) {
    slideshowIndex = $('#sidebarimg-holder img').length - 1;
    setTimeout(startSidebarSlideshow, 5000);
  }

  /* Directions */
  if($('#address').length > 0) {
    $('#gmAddress').keypress(function(e) {
  	  var c = e.which ? e.which : e.keyCode;
  	  if(c == 13) {
  	    getDirections($('#gmAddress').val() + ', ' + $('#gmCity').val());
  	  }
  	});
  	$('#gmCity').keypress(function(e) {
  	  var c = e.which ? e.which : e.keyCode;
  	  if(c == 13) {
  	    getDirections($('#gmAddress').val() + ', ' + $('#gmCity').val());
  	  }
  	});
  	$('#address img').click(function() {
  	  getDirections($('#gmAddress').val() + ', ' + $('#gmCity').val());
  	});
  }

  /* Submenu effects */
  $('#menu > li').each(function() {
    if($(' > .submenu-holder', this).length > 0) {
      var liW = $(this).outerWidth(true);
      if(liW < $(' > .submenu-holder > .submenu', this).outerWidth(true)) {
        liW = $(' > .submenu-holder > .submenu', this).outerWidth(true);
      }
      $(' > .submenu-holder', this).css('width', liW + 18);
      $(' > .submenu-holder > .submenu', this).css('width', liW + 18);
      $(' > .submenu-holder > .submenu a', this).css('width', liW + 18);
      $(' > .submenu-holder', this).css({height:'0px'});
    }
  });
  $('#menu > li > .submenu-holder > .submenu').each(function() {
    $(this).css({opacity:0, top:0 - $(this).height()});
  });
  $('#menu > li').hover(function() {
    $(' > .submenu-holder > .submenu', this).stop().animate({opacity:1, top:'0px'}, 200);
		$(' > .submenu-holder', this).stop().animate({height:$(' > .submenu-holder > .submenu', this).height()}, 200);
  }, function() {
    $(' > .submenu-holder > .submenu', this).stop().animate({opacity:0, top:0 - $(' > .submenu-holder > .submenu', this).height()}, 200);
		$(' > .submenu-holder', this).stop().animate({height:'0px'}, 200);
  });
});

function scrollTo(id) {
  $(id + ' .holder').slideDown();
  $('html, body').animate({scrollTop:$(id).offset().top - 25}, {queue:false, duration:1200, easing:'easeInOutCubic'});
}
var slideshowIndex = 0;
function startSidebarSlideshow() {
  $('#sidebarimg-holder img').eq(slideshowIndex).fadeOut(1500, function() {
    slideshowIndex--;
    if(slideshowIndex < 1) {
      setTimeout(resetSidebarSlideshow, 5000);
    } else {
      setTimeout(startSidebarSlideshow, 5000);
    }
  });
}
function resetSidebarSlideshow() {
  slideshowIndex = $('#sidebarimg-holder img').length - 1;
  $('#sidebarimg-holder img').eq(slideshowIndex).fadeIn(1500, function() {
    $('#sidebarimg-holder img').show();
    setTimeout(startSidebarSlideshow, 5000);
  });
}

var directionDisplay;
var map;
var startLat;
var startMarker;
function doGoogleMaps() {
  directionsDisplay = new google.maps.DirectionsRenderer({draggable:true, suppressMarkers:true});
  var geocoder = new google.maps.Geocoder();
  if(geocoder) {
    geocoder.geocode({'address':'Eyckholtstraat 1, Roosteren'}, function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
        var gLat = startLat = results[0].geometry.location.lat();
        var gLng = results[0].geometry.location.lng();
      }
      var latlng = new google.maps.LatLng(gLat, gLng);
      var myOptions = {zoom:17, disableDefaultUI:true, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP};
      map = new google.maps.Map(document.getElementById("googlemaps"), myOptions);
      new google.maps.Marker({position:latlng, map:map});
      directionsDisplay.setMap(map);
      directionsDisplay.setPanel(document.getElementById("directions"));
    });
  }
}

function getDirections(address) {
  var directionsService = new google.maps.DirectionsService();
  directionsService.route({origin:address, destination:'Eyckholtstraat 1, Roosteren', provideRouteAlternatives:false, travelMode:google.maps.DirectionsTravelMode.DRIVING, unitSystem:google.maps.DirectionsUnitSystem.METRIC}, function(result, status) {
    if(status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
      var geocoder = new google.maps.Geocoder();
      if(geocoder) {
        geocoder.geocode({'address':address}, function(results, status) {
          if(status == google.maps.GeocoderStatus.OK) {
            var gLat = results[0].geometry.location.lat();
            var gLng = results[0].geometry.location.lng();
          }
  	      $('#address').slideUp();
          var latlng2 = new google.maps.LatLng(gLat, gLng);
          new google.maps.Marker({position:latlng2, map:map})
          $('#directions').slideDown();
        });
      }
    } else {
      alert('Het adres kan niet worden gevonden. Controleer het door u opgegeven adres op mogelijke typfouten.'); 
    }
  });
}

function preloadImages(imgs) {
  for(var i in imgs) {
    var oImg = new Image();
    oImg.src = imgs[i];
  }
}
function backgroundResize() {
  if($('#bg-img').length > 0) {
    var oImg = new Image();
    oImg.src = $('#bg-img').attr('src');
    $('#bg-img').ready(function (e) {
      var orgW = oImg.width;
      var orgH = oImg.height;
      if(orgW != 0 && orgH != 0) {
      	var h = $(window).height();
      	var w = $(window).width();
      	var ratioW = w / orgW;
      	var ratioH = h / orgH;
      	if(ratioW > ratioH) {
      		$('#bg-img').css({'height':(w * orgH / orgW), 'width':w});
      	} else {
      		$('#bg-img').css({'height':h, 'width':(h * orgW / orgH)});
      	}
      	$('#bg-holder').css({'display':'block'});
    	}
  	});
    setTimeout(function() {
      $('#bg-holder').css({'display':'block'});
    }, 3000);
	}
}

