var interval = new Object();

$(document).ready(function()
{
	switch(pagedata.module)
	{
		case 'home':
			$('#homeitems-items').masonry({
				itemSelector : '.portfolio-item'
			});
			$('#portfolio-items').masonry({
				itemSelector : '.portfolio-item'
			});
		
			if(document.location.hash=='')
				fadeIn('.portfolio-item');
			else
				scrollToHash();
			initSliders();
			initHoverOverlayHome();
						
			$('#menuitem_home').click(function() {
			   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $('#portfolio').offset().top}, 700 );
			   return false;
			});
	
			break;
		case 'news':
		case 'work':
			initItem();
			if($('.news-thumb').length>0 && document.location.hash=='')
				fadeIn('.news-thumb');
			if($('#item-detail .images img').length>0)
				fadeIn('#item-detail .images img');
			initHoverOverlayNews();
			break;
		case 'bureau':
			fadeIn('.bureau-item');
			break;
		case 'contact':
			initContact();
			break;
	}
	
	updateTopLink();
	
	$('#toplink').click(function() {
	   $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 700 );
	   return false;
	});
	
	$(window).resize(function()
	{
		updateTopLink();

		$('#homeitems-items').masonry({
			itemSelector : '.portfolio-item'
		});
		$('#portfolio-items').masonry({
			itemSelector : '.portfolio-item'
		});
	});
	
	$(window).scroll(function()
	{
		updateTopLink();
	});
});


function fadeIn(selector)
{
	$(selector).hide();
	$(selector).first().fadeIn(500);
	
	interval.fadeIn = setInterval(function(){fadeInNext(selector);}, 100);

	function fadeInNext(selector)
	{
		var n = $(selector+':hidden').first();

		if(n.length>0)
			n.fadeIn(500);
		else
		{
			clearInterval(interval.fadeIn);
			scrollToHash();
		}
			
		updateTopLink();
	}
}


function initSliders()
{
	$('.images.slider').each(function()
	{
		//$(this).children().not(':first-child').hide();
		$('a img', this).show();
		
		var slider = $(this);
		var inter = setInterval(function(){sliderNext(slider);}, 5000);
		slider.data('interval', inter);
		slider.data('current', 0);

		$(this).css({width: $(this).children().length * $(this).parent().width()});
		$(this).children().css({width: $(this).parent().width()});
		
		var nav = $('<div class="sliderNav">');
		var navButton = $('<a>');
		var buttCount = 0;
		$(this).children().each(function()
		{
			var but = navButton.clone();
			but.attr('href', buttCount);
			if(buttCount==0)
				but.addClass('active');
			nav.append(but);
			but.click(function()
			{
				if(!$(this).hasClass('active'))
					sliderNext(slider, $(this).attr('href'));
				return false;
			});
			
			buttCount++;
		});
		$(this).parent().append(nav);
	});
}

function sliderNext(slider, nextIndex)
{
	if(nextIndex==undefined)
		nextIndex = slider.data('current')+1;
	
	var next = slider.children().eq(nextIndex);
	if(next.length==0)
	{
		next = $(':first-child', slider);
		nextIndex=0;
	}
	
	/*
	next.stop(true,true).fadeIn(500);
	current.stop(true,true).fadeOut(500);
	*/

	var newPos = - nextIndex * next.width();
	slider.animate({marginLeft: newPos}, 800);
	
	$('.sliderNav a.active', slider.parent()).removeClass('active');
	$('.sliderNav a', slider.parent()).eq(nextIndex).addClass('active');
	
	var inter = slider.data('interval');
	clearInterval(inter);
	inter = setInterval(function(){sliderNext(slider);}, 5000);
	slider.data('interval', inter);
	slider.data('current', nextIndex);
	

}


function initHoverOverlayHome()
{
	$('.portfolio-item .images').each(function()
	{
		
		if($('.hover-overlay', $(this).parent()).length)
			return;
			
		var w = $(this).width();
		var h = $(this).height();
		
		var a = $('a', this).eq(0);
		
		if(a.length==0)
			return;
		
		var overlay = $('<a href="'+a.attr('href')+'" class="hover-overlay">');

		overlay.appendTo($(this).parent());
		overlay.css({
			width: w,
			height: h,
			opacity: 0
		});
		
		
		var overlayTitle = $('<a href="'+a.attr('href')+'" class="hover-overlay-title">');
			
		overlayTitle.text('bekijk meer');
		overlayTitle.css({
			width: w-40,
			marginTop: h/2 - 10,
			opacity: 0
		});
		$(this).parent().append(overlayTitle);

		
		overlay.mouseenter(function(e)
		{
			var w = $('.images img', $(this).parent()).width();
			var h = $('.images img', $(this).parent()).height();
		
			$(this).css({
				width: w,
				height: h
			});	
			
			$('.hover-overlay-title', $(this).parent()).css({
				width: w-40,
				marginTop: h/2 - 10
			});
			
			$('.hover-overlay', $(this).parent()).stop(true, true).css({opacity: .85});
			$('.hover-overlay-title', $(this).parent()).stop(true, true).css({opacity: 1});
		});
		
		overlay.mouseout(function(e)
		{
			var $this = $(this);
			var offset = $this.offset();
					
			
			if(e.pageX > offset.left + 20 && e.pageX <= offset.left + $this.width() - 40
				&& e.pageY > offset.top + 20 && e.pageY <= offset.top + $this.height() - 40)
				return;

			
			//$('.hover-overlay-title', $(this).parent()).remove();
			//$('.hover-overlay', $(this).parent()).remove();
			
			$('.hover-overlay', $(this).parent()).animate({opacity: 0});
			$('.hover-overlay-title', $(this).parent()).animate({opacity: 0});
			
		});
		
	});
	



}



function initHoverOverlayNews()
{
	$('#news-thumbs a').mouseenter(function()
	{	
		if($('#container').width()<900)
			return;
		
		var $this = $(this);
		var w = $('img', $this).width();
		var h = $('img', $this).height();
		
		var overlay = $('<div class="hover-overlay">');

		overlay.appendTo($(this));
		overlay.css({
			width: w,
			height: h,
			opacity: 0.85
		});
		
		var overlayTitle = $('<div class="hover-overlay-title">');
			
		overlayTitle.text('lees meer');
		overlayTitle.css({
			width: w-40,
			marginTop: h/2 - 10,
			marginLeft: 20
		});
		$(this).append(overlayTitle);
	});
	
	$('#news-thumbs a').mouseleave(function(e)
	{						
		$('.hover-overlay-title', this).remove();
		$('.hover-overlay', this).remove();
	});
}



function initItem()
{
	$('.images').imagefit();
	
	initFixedDetails();
}


function initFixedDetails()
{
	var $details = $('.details');
	var offset = $details.offset();
	
	$(window).scroll(function()
	{
		updateFixedDetails($details, offset);
	});
	$(window).resize(function()
	{
		offset = $details.offset();
		updateFixedDetails($details, offset);
	});
}

function updateFixedDetails($details, offset)
{
	if($('#content').innerWidth() > 768 && $(window).scrollTop() > offset.top - 20)
	{
		$details.css({
			position: 'fixed',
			top: 20,
			left: offset.left
		});
	}
	else
	{
		$details.css({
			position: 'relative',
			top: 'auto',
			left: 'auto'
		});
	}
}


function initContact()
{
	loadMapCoordinates();
	
	$('.newsletter-subscribe label').inFieldLabels({
    	fadeOpacity:0
    });

}

function loadMapCoordinates()
{
	/* Get coordinates from address */
	var geocoder = new google.maps.Geocoder();
	
	geocoder.geocode({'address':$('#map').text()}, function(results, status)
	{
		if (status == google.maps.GeocoderStatus.OK)
		{
			showMap(results[0].geometry.location);
		}
	});
	$('#map').text('');
}

function showMap(location)
{
	/* INIT GOOGLE MAP */
    var zoomLevel = 14;
    
    var center = new google.maps.LatLng(location.lat()-0.005, location.lng());
		
	var map = new google.maps.Map($('#map')[0], {
	    zoom: zoomLevel,
	    center: center,
	    mapTypeId: google.maps.MapTypeId.ROADMAP,
	    navigationControlOptions: {
      		style: google.maps.NavigationControlStyle.SMALL
    	},
    	styles: [
		  {
		    stylers: [
		      { saturation: -100}
		    ]
		  }
		]
	});
	
	var markerIcon = {
		url: '/images/mapMarker.png'
	};

	var markerOptions = {
		title: 'skinn',
		position: location,
		icon: markerIcon,
		map: map
	}
	var marker = new google.maps.Marker(markerOptions);
}

function updateTopLink()
{
	if($(window).width()<767)
	{
		$('#toplink').show();
		return;	
	}

	switch(pagedata.name)
	{
		case 'home':
		case 'bureau':
		case 'contact':
			var pos = $(window).height() - (( $('#newsitems').height() + $('#newsitems').offset().top) - $(window).scrollTop());
			
			$('#toplink').css({left: $('#newsitems').offset().left});
			break;
		case 'news':
		case 'work':
			if($('.details').length>0)
			{
				var pos = $(window).height() - (( $('.details').height() + $('.details').offset().top) - $(window).scrollTop());
				
				$('#toplink').css({left: $('.details').offset().left});
			}
			else
			{
				var pos = 100;
				
				$('#toplink').css({left: $('#news-thumbs').width() + $('#news-thumbs').offset().left});
			}
			
			break;
		
	}
	

	if(($(document).height() > $(window).height()) && pos > 80 && $(window).scrollTop() > 120)
		$('#toplink').fadeIn();
	else
		$('#toplink').fadeOut();
		
		
	
}


function scrollToHash(){
	if(document.location.hash=='')
		return;
		
	var target_offset = $(document.location.hash).offset();
	var target_top = target_offset.top;

	$('html, body').animate({scrollTop:target_top}, 200);
}

