(function($) {
	$.log = function(){
		if (window.console && window.console.log && window.console.firebug) {
			if ($.logFirebug) {
				console.log.apply(window.console, arguments);
			}
		}
	};
	$.fn.log = function() {
		$.log(this);
		return this;
	};
})(jQuery);

$(document).ready(function() {

if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i))) {
   $('body').attr('id','iPad');
   $.iPad = true;
}
	$.logFirebug = true;
	$.DetailFixed = false;
    var myLatlng = new google.maps.LatLng(52.19447,5.94270);
    var myLatlng1 = new google.maps.LatLng(52.19700,5.94270);
    var myOptions = {
      zoom: 13,
      center: myLatlng1,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var image = '/resources/images/layout/icons/favicon.png';
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        icon: image,
        myURL: 'http://maps.google.nl/maps?daddr=Schumanpark+35,+Brouwersmolen,+Apeldoorn&hl=nl&sll=52.469397,5.509644&sspn=7.760344,10.568848&mra=ls&z=7'
    });
	google.maps.event.addListener(marker, "click", function ()
	{
		window.open(this.myURL,"_blank");
	}); 
	$(".blank").each(function(){
		$(this).attr('target', '_blank');
	});


	var map_active = false;

	$('#map_resize').click(function() {
		if(map_active == false){
			$('#map_canvas').height(400);
			$('#map').animate({height:'400px'},500, function() {$('#map_resize').toggleClass('resized');});
			$('html, body').animate({scrollTop: '5000px'}, 500);
			var myLatlng = new google.maps.LatLng(52.19447,5.94270);
			var myLatlng1 = new google.maps.LatLng(52.20700,5.94270);
			map.setCenter(myLatlng1);
			google.maps.event.trigger(map, 'resize');
			map_active = true;
		} else {
			//$('#map_canvas').animate({height:'100px'},500);
			$('#map_canvas').height(100);
			google.maps.event.trigger(map, 'resize');
			$('#map').animate({height:'100px'},400, function() {$('#map_resize').toggleClass('resized');});
			$('html, body').animate({scrollTop: '5000px'}, 400);
			var myLatlng = new google.maps.LatLng(52.19447,5.94270);
			var myLatlng1 = new google.maps.LatLng(52.19700,5.94270);
			map.setCenter(myLatlng1);
			map_active = false;
			
		}
	});
	if($('#portfolio').length > 0) {
		$.firstItemClicked = false;
		$(document).keydown(function(e) {

			switch(e.keyCode) { 
				case 37:
					$('#previous').click();
				break;
				case 39:
					$('#next').click();
				break;
			}
		});
		$.projectList = '';
	
		$.ajax({
			type: "GET",
			url: '/resources/projects.json',
			dataType: 'json',
			success: function(data){
	
				$.projectList = data.projects;
				$.portfolioNavigation();
	
	
			},
			error: function(){
				alert('error');
			}
		});

		$('#portfolio_right').height($('#portfolio_list').height());
		$(window).scroll(function() {	
		var offset = $('#footer').offset();
//$.log($(window).scrollTop() + '--'+offset.top + '--' + $('#portfolio_details').height()+ '--' + $('#footer').height() + '--' + $('#map').height());

			if($(window).scrollTop() > 280) {
			
				if($.iPad == true) {
						$('#portfolio_details').animate({ top: ($(window).scrollTop() - 320)});
				}

			if(!$.iPad == true) {	$('#top_shadow, #bottom_shadow').fadeIn('fast'); }
				
	
				
			} else {
				
			if(!$.iPad == true) {	$('#top_shadow, #bottom_shadow').fadeOut('fast'); }
			}
			if($(window).scrollTop() > ($('#portfolio_details').height()+ $('#footer').height()+ $('#map').height())) {
				$('#portfolio_details').removeClass('fixed').addClass('hold');
				
			}
			if(($('#portfolio_list').height() + 290 - $('#portfolio_details').height()) < + $(window).scrollTop()) {
				//$('#portfolio_details').removeClass('fixed').addClass('hold');
			}
			else if($(window).scrollTop() > 340) {
				$('#portfolio_details').removeClass('hold').addClass('fixed');

			} else {
				$('#portfolio_details').removeClass('fixed');
			}

		 });
		 
		 
		$('.item a').hover(function(){
			if($(this).has('div.overlay').length == 0){
				$(this).append('<div class="overlay"></div>');
				var zoomicon = $(this).children('div.overlay').first();
				$(zoomicon).hide();
			}
			var zoomicon = $(this).children('div.overlay').first();
			$(zoomicon).stop().fadeTo('fast', 1);
		},function(){
			var zoomicon = $(this).children('div.overlay').first();
			$(zoomicon).stop().fadeTo('slow', 0);
		});
		$('#portfolio_list .item a').each(function() {
			if($(this).has('div.title').length == 0){
				//$(this).append('<div class="title"><strong>' + $(this).attr('title') + '</strong></div>');
				//var titlediv = $(this).children('div.title').first();
				//$(titlediv).hide();
			}
		});
		$('#portfolio_list .item a').hover(function(){

			var titlediv = $(this).children('div.title').first();
			$(titlediv).stop().fadeTo('fast', 0.8);
		},function(){
			if(!$(this).parents('.item').hasClass('current')) {
				var titlediv = $(this).children('div.title').first();
				$(titlediv).stop().fadeTo('fast', 0);
			}
		});
		 
	 }
	 if($('#team').length > 0) {
	 	$('#specialisten_in a').hover(function() {
	 		var className = $(this).attr('class');
	 		//$.log(className);
	 		$('#team .item').stop().animate({opacity: 0.5},'fast');
	 		$('#team .item.' + className).stop().animate({opacity: 1},'fast');
			$('.werkwijze_item.' + className + ' h2').addClass('red');
	 		
	 	},
	 	function() {
	 		$('#team .item').stop().animate({opacity: 1},'fast');
	 		$('.werkwijze_item h2').removeClass('red');
	 	});
	 	$('.werkwijze_image a').fancybox({
	 		overlayColor: '#706969'
	 	});
	 	



	 }
	 if($('#contact').length > 0){

			var myLatlng = new google.maps.LatLng(52.19447,5.94270);
			var myOptions = {
			zoom: 13,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			var smallMap = new google.maps.Map(document.getElementById("smallMap"), myOptions);
			var image = '/resources/images/layout/icons/favicon.png';
			var marker = new google.maps.Marker({
				position: myLatlng, 
				map: smallMap,
				icon: image
			});
			google.maps.event.addListener(marker, 'click', function() {
				$('#map').animate({height:'400px'},400);
				//$('#map_resize').click();
			});

	 }
$(".klanten .jCarouselLite").jCarouselLite({
  auto: 1000,
    speed: 2000,
    visible: 8,
    circular: true, 
    pauseOnHover: true 
});

});


/* Portfolio Navigatie

  aan de hand van de lijst met portfolio items navigeren door de verschillende projecten;

*/

$.portfolioNavigation = function () {

	$.totalProjects = $('#portfolio_list .item').length -1;

	//anne edit
	$.currentProject = 0;
	if ($('#current_project').length > 0){
		$.currentProject = $('#current_project').html();
		highlightProject($.currentProject);
		scrollToProject($.currentProject);
	}

	if($.currentProject == 0) {
		$('#previous').addClass('disabled');
		highlightProject($.currentProject);
	}
	
	$('#previous').click(function() {
		if($.currentProject>=0){
			if($.currentProject!=0){
				$.currentProject--;
				highlightProject($.currentProject);
				
			} 
		}
		if($.currentProject == 0) {
		//	$('#previous').addClass('disabled');
		//	return false;
		}

		if($.currentProject < $.totalProjects) {
	//		$('#next').removeClass('disabled');
		}
		return false;
	});
	
	$('#next').click(function() {
		if(($.totalProjects) != $.currentProject){
	//		$('#previous').removeClass('disabled');
			$.currentProject++;
			highlightProject($.currentProject);

		}
		if(($.totalProjects) == $.currentProject) {
	//		$(this).addClass('disabled');
	//		return false;
		}
		return false;
	});	

	function highlightProject(projectId) {
		$('#portfolio_list .item.current').removeClass('current');
		$('#portfolio_list .item a .title').hide();
		$('#smallscreens a.current').removeClass('current');
		$('#portfolio_list .item:eq('+(projectId)+')').addClass('current');
		$('#smallscreens a:eq(0)').addClass('current');
		
		
		$('#previous').attr({'href': $('#portfolio_list .item:eq('+(projectId-1)+') a').attr('href')});
		$('#next').attr({'href':$('#portfolio_list .item:eq('+(projectId)+') a').attr('href')});
		
		$('#link').hide();
		if ($.projectList[projectId].Url != ''){
			$('#link').show();
			$('#link').attr('href', $.projectList[projectId].Url);
		}
	
		$('#portfolio_list .item:eq('+(projectId)+') a .title').css({opacity:0.8}).show();


		
		$('#portfolio_details h2').text($.projectList[projectId].Title);
		$('#portfolio_details p').text($.projectList[projectId].Description);
		if($.projectList[projectId].Images != undefined) {
			$('#smallscreens').remove();
			$('#bigscreen').empty();
			$('#bigscreen').cycle('destroy');
			var currentThumb = 0;
	
			for(i=0;i<=$.projectList[projectId].Images.length-1;i++){
				currentThumb = i;
				$('<img id="screenshot'+(i+1)+'" src="'+$.projectList[projectId].Images[i].ImageName+'" />').appendTo($('#bigscreen'));
			}
		}
		
		
   		$('#bigscreen').after('<div id="smallscreens">').cycle({
			fx: 'fade',
			delay: 2000,
			timeout: 3000,
			pager:  '#smallscreens',
			fit: 1,
			width: '100%',
			height: 'auto'
		});
		$('#portfolio_details').mouseover(function() {
			$('#bigscreen').cycle('pause');
		})
		.mouseout(function(){
			$('#bigscreen').cycle('resume');
		});
		if($.currentProject > 0) {
			$('#previous').removeClass('disabled');
		} else {
			$('#previous').addClass('disabled');
		}
		if($.currentProject < $.totalProjects) {
			$('#next').removeClass('disabled');
		} else {
			$('#next').addClass('disabled');
		}
		var offset = $('#portfolio_list .item.current').offset();
		var offset1 = $('#portfolio_details').offset();
		var offset2 = $('#portfolio_list').offset();
		if($.firstItemClicked == true) {
			if((offset.top) > (($('#portfolio_list').height() + offset2.top)-$('#portfolio_details').height())) {
				$('html, body').stop().animate({scrollTop: (($('#portfolio_list').height() + offset2.top) -$('#portfolio_details').height()-100) }, 700);
			} else {
				scrollToProject(projectId);
			}
		} else {
		$.firstItemClicked = true;
		}

		
	}
	function scrollToProject(projectId) {

		var offset = $('#portfolio_list .item.current').offset();
				if($.iPad == true) {
						$('#portfolio_details').animate({ top: (offset.top - 320)});
				}
		$('html, body').stop().animate({scrollTop: (offset.top - 37)}, 700, function(){ 
			
		});
		return false;
	}

	for(i=0;i<=$.totalProjects;i++){
		$('#portfolio_list .item:eq('+i+') a').attr({'rel':(i)});
		$('#portfolio_list .item:eq('+i+') a').attr({'href':'#'+$('#portfolio_list .item:eq('+i+') a').attr('href').replace('/portfolio/','')});
	}
	
	$('#portfolio_list .item a').click(function() {

		$.currentProject = $(this).attr('rel');

		
		highlightProject($.currentProject);

		return false;
	});
	
}

vergroot_werkwijze = function() {
	$('.overlay, .werkwijze_large').fadeIn('fast');
	$('.werkwijze_large a.close').click(function() {
		$('.overlay, .werkwijze_large').fadeOut('fast');
		return false;
	});
}
