$(document).ready(function(){
	////Menu shadow
	var option = {
		y: 5,
		x: 2
	}

	$("#nav li a").textShadow(option);
	
	//Links with the class external will open in a new window, to get the code valid in xhtml-strict
	$(".external").attr("target","_blank");
	
	//Mailme - Email Spam Protector
    var at = / at /;
    var dot = / dot /g;
    $('span.mailme').each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        $(this)
            .after('<a href="mailto:'+addr+'" class="mailme">'+ addr +'</a>')
            .remove();
    });

	//Validate - Validation of forms
	$("#contactform").validate();

    //slideshows
	$('#slideshow').cycle({
		fx: 'fade'
	});
	$('#about_slides').cycle({
		fx: 'fade'
	});

	//The auto-scrolling function
	$('#client_slider').carousel('#previous', '#next'); 
	//The auto-scrolling function
	function slide(){
	  $('#next').click();
	}
	//Launch the scroll every 2 seconds
	var intervalId = window.setInterval(slide, 3000);
	
	//MENU BG ANIMATION
	if ( $("#nav li:first-child").hasClass("active") ){
		var cur_position = $("#nav li.active").position();
		cur_position.left = cur_position.left+20;
	} else {
		var cur_position = $("#nav li.active").position();
		if(cur_position != null)
			cur_position.left = cur_position.left+55;	
	}
	
	if(cur_position != null)
		$("#nav").stop().animate({backgroundPosition: +cur_position.left+ 'px bottom'});						   
						   
	$("#nav li").hover(			   
	  
	  function () {
		if(!$(this).hasClass("active")){
			var position = $(this).position();
			if ($(this).is(":first-child")){
				position.left = position.left+20;
			} else {
				position.left = position.left+55;	
			}
			$("#nav").stop().animate({backgroundPosition: +position.left+ 'px bottom'});
		}
	  },
	  function () {
		$("#nav").stop().animate({backgroundPosition: +cur_position.left+ 'px bottom'});
	  }
	);
	
	//FANCYBOX TRIGGER
	$("#daily_photos li a").fancybox();
	$("#today_block_image").fancybox();
	
	
	///preview website
	var pathname = $(location).attr('href');
	if (pathname.toLowerCase().indexOf("?preview") >= 0) {
		$('a').each( function() {
			var href = $(this).attr('href');
			
			if(href)
				$(this).attr('href', href + '?preview');
		});
	}
	
	///Alle externe links automatisch nieuw venster
	$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function(){
        $(this).attr('target','_blank');
    });
	
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		queue:true,
		duration:1500
	});
});

//Googlemaps on hte contactpage
$(window).ready(function () {
	if($("#map").length != 0)
	{
		var path = $("#map").html();
		if(!path) path = '';
		
		$.ajax({
			type: "GET",
			url: path+"exe/getaddress.html",
			dataType: "html",
			error: function(){
				alert('Error loading map.');
			},
			success: function(data){
				var address = $(data).filter('#address').html();
				var googlelink = $(data).filter('#googlelink').html();
				
				$("#map").gMap({
					markers: [
						{
							address: address,
							popup: false
						}
					],
					zoom: 14,
					onComplete: function() {
						var data = $('#map').data('gmap');
						google.maps.event.addListener(data.markers[0], 'click', function() {
							window.open( googlelink );
							return false;
						});
					}
				});
			}
		});
	}

});

