$(document).ready(function() {
	if ($('.scrolltome').length>0) {
		$.scrollTo('.scrolltome',800,{offset: -136});
	}
});


function back_to_top() {
	$('.top').click(function() {
		$.scrollTo('#wrapper',800); 
		return false;
	});
}

$(document).ready(function() {
	back_to_top()
});

function medewerker_close() {
	location.hash = 'over_ovg/mensen/';
	document.title = 'Mensen | OVG re/developers';
	$('#employee_detail').empty();
	$('#employee_detail').css('display','block');
	reinit_psd2html();
	$.scrollTo('#mensen',800,{offset: -136});

	return false;
}
function employee_detail(entry_id) {
	location.hash = 'over_ovg/mensen/'+entry_id;
	url = 'http://www.ovg.nl/ajax/medewerker/'+entry_id;
	$('#employee_detail').load(url, function () {
		document.title = $('#employee_detail h2 span').html() +' | OVG re/developers';
		reinit_psd2html();
		$.scrollTo('#'+entry_id,800,{offset: -136});		
	});
	return false;
}

$(document).ready(function() {
	slideshow(); // start slideshow of individual projects

});


/* make sure that the visuals scale when the browser window resizes */
$(window).resize(function() {
	$('#visuals').css('width','100%');
});

var google_maps = new Array();
var maps_bounds = new Array();

function init_map(entry_id) {
	if ($('#map_'+entry_id).length <1) {
		return false;
	}

	var geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(0, 0);
				
	var myOptions = {
		center: latlng,
		draggable: false,mapTypeControl: false,mapTypeId: google.maps.MapTypeId.ROADMAP,scrollwheel: false,zoom: 16
	}
				
	var coordinates = $("#map_data_"+entry_id+' p.coordinates').html();
	var html = $("#map_data_"+entry_id+' p.popup').html();

	google_maps[entry_id] = new google.maps.Map(document.getElementById("map_"+entry_id), myOptions);
	maps_bounds[entry_id] = new google.maps.LatLngBounds(); 
	//var address = maps_data[entry_id][0];
	geocoder.geocode({ address: coordinates}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var location = results[0].geometry.location;
							
			google_maps[entry_id].setCenter(location);
			var marker = new google.maps.Marker({
				map: google_maps[entry_id],
				position: location,
				title: coordinates
			});
							
			var infowindow = new google.maps.InfoWindow({
				content: html
			});
								
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.close();
				infowindow.open(google_maps[entry_id], marker);
			});
		}
	});
}

/* submenu */
$(document).ready(function() {

	var header_height = $('#header').height();
	var submenu_heights = new Array();

	$('#nav li.nav_item').each(function() {
		if ($(this).find('ul li').length > 2) {
			submenu_heights.push($(this).find('ul li').length*16+96);
		} else {
			submenu_heights.push(header_height);
		}
	});
	$('#nav li.nav_item').hover(
		function() {
			$('#header').height(submenu_heights[$(this).index()]);
		},
		function() {
			$('#header').height(header_height);
		}
	);

});


/* Cufon */
function initCufon() {
	Cufon.replace('ul#nav > li > a', { fontFamily: 'Futura', hover: true });
        Cufon.replace('.lang a', { fontFamily: 'Futura', hover: true });
        Cufon.replace('.blog-box h2 a', { fontFamily: 'Futura', hover: true });
        Cufon.replace('.visual .text h1', { fontFamily: 'Futura'});
        Cufon.replace('.column .heading', { fontFamily: 'Futura'});
        Cufon.replace('.partners .heading', { fontFamily: 'Futura'});
        Cufon.replace('.location strong', { fontFamily: 'Futura'});
        Cufon.replace('.map-heading a', { fontFamily: 'Futura'});
        Cufon.replace('.more', { fontFamily: 'Futura'});
        Cufon.replace('.visual .text .more', { fontFamily: 'Futura'});
        Cufon.replace('.visual .text p', { fontFamily: 'Futura-book'});
        Cufon.replace('#main h3', { fontFamily: 'Futura'});
        Cufon.replace('#main h2', { fontFamily: 'Futura'});
        Cufon.replace('.job-list .sub-title', { fontFamily: 'Futura'});
 }


$(document).ready(function(){
        initCufon();
         $('#nav li').each(function(){
		$(this).mouseenter(function(){
			if(typeof(document.body.style.maxHeight) == 'undefined') $(this).addClass('hover');
			Cufon.replace('#nav > li > a', { fontFamily: 'Futura', hover: true });
		}).mouseleave(function(){
			if(typeof(document.body.style.maxHeight) == 'undefined') $(this).removeClass('hover');
			setTimeout(function(){
				Cufon.replace('#nav > li > a', { fontFamily: 'Futura', hover: true });
			},10)
		})
	});
});



/* slide fest */
function faq_toggle(entry_id) {
	if ($('#faq_'+entry_id).hasClass('active')) {
		$('#faq_'+entry_id).removeClass('active');
	} else {
		$('#faq_'+entry_id).addClass('active');
	}
	return false;
}

function contact_open_map() {
	$('#contact_map .map.oc-slide').animate({height: '473px'},800, function() {
		$('#contact_map .map.oc-slide').addClass('open');
		reinit_psd2html();
		$.scrollTo('#contact',800,{offset: -136});
	});
	return false;
}

function contact_close_map(entry_id) {
	$('#contact_map .map.oc-slide').animate({height: '119px'},800, function() {
		$('#contact_map .map.oc-slide').removeClass('open');
		init_map('contact');
		reinit_psd2html();
		$.scrollTo('#contact',800,{offset: -136});
	});
	return false;
}

function project_open_map(entry_id) {
	$('#detail_'+entry_id+' .map.oc-slide').animate({height: '473px'},800, function() {
		$('#detail_'+entry_id+' .map.oc-slide').addClass('open');
		reinit_psd2html();
		$.scrollTo('#detail_'+entry_id+' .map-area',800,{offset: -136});
	});
	return false;
}

function project_close_map(entry_id) {
	$('#detail_'+entry_id+' .map.oc-slide').animate({height: '119px'},800, function() {
		$('#detail_'+entry_id+' .map.oc-slide').removeClass('open');
		reinit_psd2html();
		init_map(entry_id);
		goto_visual('projecten/'+entry_id);
	});
	return false;
}

function project_open(entry_id) {
	$('#detail_'+entry_id+' .section.oc-slide').slideDown("slow", function() {
		$('#detail_'+entry_id).addClass('open');
		goto_visual('projecten/'+entry_id);
		init_map(entry_id);
	});
	return false;
}
function project_close(entry_id) {
	$('#detail_'+entry_id+' .section.oc-slide').slideUp("slow", function() {
		$('#detail_'+entry_id).removeClass('open');
		$('#detail_'+entry_id+' .project_more').show();
		$('#detail_'+entry_id+' .project_description_more').hide("slow", function() {
			reinit_psd2html();
			goto_visual('projecten/'+entry_id);
		});
	});
	return false;
}

function project_hide(entry_id) {
	$('#detail_'+entry_id).slideUp("slow", function() {
		goto_visual('projecten/overzicht');
		// $.scrollTo('#thumb_'+entry_id,800,{offset: -136});
		$('#detail_'+entry_id).parent().empty();
		reinit_psd2html();
	});
	return false;
}

function slideshow() {
	$('.slideshow').each(function() {
		var show = $(this);
		show.cycle({
	                fx: 'fade', 
        	        timeout: 4000,
			pause: 1,
			prev:  show.next().find('.prev'),
			next:  show.next().find('.next')
		});
	});
}

/* visuals */

$(document).ready(function() {
	init_visuals($('.visual.active').index());
});

var cycle_initiated = 0;

function init_visuals(startingSlide) {
	var hash = window.location.hash.substr(1);

	if (!cycle_initiated) {
		if (hash) {
			hash_parts = hash.split('/');
			entry_id = hash_parts[0];
			startingSlide = $('#nav_'+entry_id).index();			
		}
		$('#visuals').cycle({
                	fx: 'scrollHorz',
	                timeout:  0,
			fit: 0,
			slideResize: 0,
			startingSlide: startingSlide,
			pause: 1
		});
		cycle_initiated = 1;
	}
	if ($('#visuals').length>0) {
		goto_visual(hash);
	} else {
		$('#main').css('padding-top',166);
	}
}

var block_clicks = 0;

function goto_visual(hash) {

	if (block_clicks) {
		return false;
	}
	if (hash) {
		window.location.hash = hash;

		if (window.location!='http://www.ovg.nl/#'+hash) {
			/*
				clean up URL and reload page
				'projecten/#over_ovg' translates to '#over_ovg'
				'blog/OVG_herontwikkelt_op_geboortegrond_van_Cofely#projecten/cofely' translates to '#projecten/cofely'
			*/
			window.location = 'http://www.ovg.nl/#'+hash;
			return false;
		}
		hash_parts = hash.split('/');
		entry_id = hash_parts[0];
		if ($('#nav_'+entry_id+' cufontext').html()) {
			title = '';
			$('#nav_'+entry_id+' cufontext').each(
				function() { title+= $(this).text()+' ';}
			);
			document.title = title+' | OVG re/developers';
		} else {
			document.title = $('#nav_'+entry_id+' > a').html()+' | OVG re/developers';
		}

		_gaq.push(['_trackPageview', hash]);
		url = 'http://www.ovg.nl/'+entry_id+ ' #ajax_canvas';
	}
	else {
		return true;
	}

	block_clicks=1;

	if ($('#nav_'+entry_id).hasClass('active')) {
		goto_submenu(hash);
		block_clicks = 0;
		//initIas();
		return false;
	}

	$.scrollTo('#wrapper'); // scroll back to top

	$('.nav_item').removeClass('active');
	$('#nav_'+entry_id).addClass('active');
	$('#visuals').cycle($('#nav_'+entry_id).index());

	$('#ajax_canvas').css('visibility','hidden');
	if (!$.browser.msie) {
		$('#ajax_canvas').css('opacity',0);
	}

	// update active menu item
	window.setTimeout(function() {
		$('#ajax_canvas').load(url, function() {
			$('#ajax_canvas').css('visibility','visible');
			if (!$.browser.msie) {
				$('#ajax_canvas').animate({opacity:1},1000);
			}
			$("body").css("cursor", "progress");
			reinit_psd2html();

			if (entry_id=='home') {
				$('#footer').hide();
			} else {
				$('#footer').show();
			}

			$("body").css("cursor", "auto");
			block_clicks = 0;
			initIas();
			goto_submenu(hash);
		});
	},1000);
	return false;
}

function goto_action(hash) {
	hash_parts = hash.split('/');
	submenu = hash_parts[1];
	action = hash_parts[2];
	group = hash_parts[3];
	if (action && submenu) {
		if (submenu=='mensen') {
			employee_detail(action);
		}
	}
}

function goto_submenu(hash) {
	hash_parts = hash.split('/');
	entry_id = hash_parts[0];
	submenu = hash_parts[1];
	action = hash_parts[2];
	if (submenu) {
		if (action) {
			goto_action(hash);
		} else {
			if (entry_id=='projecten') {
				goto_project(submenu);
			} else {
				$.scrollTo('#'+submenu,800,{offset: -136});
			}
		}
	}
}


function goto_project(entry_id) {
	if (entry_id=='overzicht' || entry_id=='uitgelicht') {
		$.scrollTo('#'+entry_id,800,{offset: -136});
		return false;
	}

	var title = '';
	// scrolls page to details of a project
	// project is not visible

	if ($('#detail_'+entry_id).length>0) {
		// project is already visible on page
		// just scroll to it
		$.scrollTo('#detail_'+entry_id,800,{offset: -136});
		$('#detail_'+entry_id+' .article-box h2 cufontext').each(function() {
			title+=$(this).html();
		});
		document.title = title +' | OVG re/developers';
	} else {
		// check if thumb is part of 'overzicht'
		if ($('#thumb_'+entry_id).length>0) {
			// thumb is present in 'overzicht'
			// get the row of the thumb in 'overzicht'
			var row = $('#thumb_'+entry_id).parents('ul').attr('id').substr(6);
			url = 'http://www.ovg.nl/ajax/project_detail/'+entry_id+'/overzicht_detail';
			$('.overzicht_detail').hide();
			$('#overzicht_detail_'+row).load(url, function () {
				$('#overzicht_detail_'+row).css('display','block');
				$('#overzicht_detail_'+row).show();
				init_map(entry_id);
				$.scrollTo('#detail_'+entry_id,800,{offset: -136});
				reinit_psd2html();
				$('#detail_'+entry_id+' .article-box h2 cufontext').each(function() {
					title+=$(this).html();
				});
				document.title = title +' | OVG re/developers';
			});
		}
	}
	return false;
}

/* tabs */
/*
$(document).ready(function() {
	init_tabs();
});

function init_tabs() {
	return false;
	$('.tabs-holder > .tab').hide();
	$('#uitgelicht_tabs-area .tabs-holder > .tab:first').show();
	$('#inontwikkeling_tabs-area .tabs-holder > .tab:first').show();
}
*/

function reinit_psd2html() {
	back_to_top();
	initCufon();
	slideshow();
	init_map('contact');
	FB.XFBML.parse();
}

