
var map = null;
var geoXml =null;
var KML = null;
var KMLoverlays = new Array();
var KmlParams = null;
var defaultKML = '';

$(document).ready(function(){
						   
	// can we print show the button 

	if(window.print) { 
		$('#print_btn').css({ display: 'inline'});
		$("#print_btn").click( function() { window.print(); return false; });
	}
	
	
	// Add click event to (de-)select all button
	$('#frm-locaties-select-all').click(
		function() {
			if($(this).text() == 'Alles selecteren') {
				$('#frm-locaties input:checkbox').attr('checked','checked');
				$(this).text('Alle selecties ongedaan maken');
			} else {
				$('#frm-locaties input:checkbox').removeAttr('checked');
				$(this).text('Alles selecteren');
			}
			
			clear_KmlParams();
			set_KmlParams($('#frm-locaties').serialize());
			
			_remove_KmlOverlays();
			KMLoverlays['pageDefault'] = load_KML(defaultKML,'pageDefault');
			
			return false;
		}
	);
	
	
	// Add click event to google map options
	$('#frm-locaties input:checkbox').click( function() { 
		clear_KmlParams();
		set_KmlParams($('#frm-locaties').serialize());
		_remove_KmlOverlays();
		KMLoverlays['pageDefault'] = load_KML(defaultKML,'pageDefault');
	})

	// Initialize the carousel
	$(".carousel").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev"
	});
	$(".carousel a").click(function() {
		$('#bigpicture').attr('src' ,$(this).attr('href'));
		return false;
	});
	
	// Check the google map options and set the correct (de-) select button
	check_checkboxes();
	
	/**
	 * #9516 Meten van fontsize (ook door cookie)
	 */	
	// regex voor ophalen juiste class 
	fontsize_regex = /\s[sml]\s|[sml]\s|\s[sml]$/;
	// meten per sessie	
	if ($('.nav-fontsize').length) {
		matches = $('.nav-fontsize.selected').attr('class').match(fontsize_regex);
		try {
			var pageTrackerFont = _gat._getTracker("UA-9036062-2");
			pageTrackerFont._setCustomVar(
		      1,             				// This custom var is set to slot #1
		      "Tekstgrootte (sessie)",	 	// The name of the custom varaible
		      matches[0].replace(' ', ''),	// De waarde (de grootte)
		       2             				// Sets the scope to session-level  
		   	);
			pageTrackerFont._trackPageview();
		} catch(err) {
		}
	};
	// clicks meten
	$('.nav-fontsize').click(function(){
		matches = $(this).attr('class').match(fontsize_regex);
		try {
			pageTrackerFont._trackEvent('Fontsize', 'Change', matches[0]);
		} catch(err) {
			alert(err);
		}
	});
})

function check_checkboxes() {
	allchecked = true;
	$('#frm-locaties input:checkbox').each(function() {
		if(!$(this).attr('checked')) {
			allchecked = false;
		}
	});
	
	if(allchecked) {
		$('#frm-locaties-select-all').text('Alle selecties ongedaan maken');
	} else {
		$('#frm-locaties-select-all').text('Alles selecteren');	
	}
}



function load_KML(UrlOfKml,overlay) {
	if (UrlOfKml != null) {
		KML = UrlOfKml;
		
		//var bounds = map.getBounds();
		var zoom =  map.getZoom();
		//var sW = bounds.getSouthWest(); // south west corner
		//var nE = bounds.getNorthEast();  // north east corner
		var _d = new Date();
		var _d = _d.getMilliseconds();
		
		//var _KmlUrl = KML+"?swLat="+sW.lat()+"&swLng="+sW.lng()+"&neLat="+nE.lat()+"&neLng="+nE.lng()+"&d="+_d+Math.random();
		var _KmlUrl = KML+"?d="+_d+Math.random();
			
		if (KmlParams != null) {
			if(KmlParams.substr(0,1) != "&") { _KmlUrl += "&";	}
			_KmlUrl += KmlParams;
		}
		
		// Open een test window
		//window.open(_KmlUrl, 'kml');
		
		return new GGeoXml(_KmlUrl,function() { _show_KmlOverlay(overlay) } );
	}
}

function set_KmlParams(params) {
	KmlParams = null;	 
	KmlParams = params;
}

function clear_KmlParams() {
	// disable all checkboxes before request
// 	$('#frm-locaties input[type=checkbox]').css('visibility', 'hidden');
	
	KmlParams = null;
}
	
function _show_KmlOverlay(overlay) {
	
	if (KMLoverlays[overlay].loadedCorrectly()) {
		map.addOverlay(KMLoverlays[overlay]);
		
		// enable all checkboxes after request
// 		$('#frm-locaties input[type=checkbox]').css('visibility', 'visible');
		
		if(saveParams){
			saveParams();
		}
	}  else {
		setTimeout(function(){_show_KmlOverlay(overlay)},200 );
	}
}

// remove googlemap Overlays
function _remove_KmlOverlays() {
	
	map.clearOverlays();
}




// FOOTNOTE / TOOLTIPS 

var nvs_footnoteMouseOver = false;
(function($) {
	
	$.fn.footnotes= function(options) { 
		
		var defaults = {
			offsetX	: 15,
			offsetY	: 15
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			$(this).click(function() { this.blur(); return false; });
			$(this).mouseover(_show_footnote);
			$(this).mouseout(_hide_footnote);
		});
		
		function _show_footnote(event) { 
			_instant_hide_footnote();
			
			this.blur();
			if($(this).find('span.tooltip').html()) {
			
			$('<div></div>').attr({
					id: 'nvs_footnote' 
					})
				.css({
					position: 'absolute',
					display: 'block',
					top: -1000,
					left: -1000
					})
				.appendTo('body').mouseover(_prevent_hide).mouseout(_hide_footnote);
		
			$('<div></div>').attr({
					id: 'nvs_footnote_content'
					})
				.css({
					 position: 'relative'
					})
				.appendTo('#nvs_footnote').mouseover(_prevent_hide);
				
				$("#nvs_footnote_content").html($(this).find('span.tooltip').html());
				if($("#nvs_footnote_content").width() > 300) { 
					newWidth = $("#nvs_footnote_content").width() / $("#nvs_footnote_content").height() * 4;
					if(newWidth > 300) { newWidth = 300; }
					$("#nvs_footnote_content").css({width: newWidth});
				}
				$(document).bind('mousemove', _follow_mouse );			
			}
		}
		
		function _follow_mouse(event) {
			var pageCoords = { top: (event.pageY + options.offsetY), left:  (event.pageX + options.offsetX) } ;
			$("#nvs_footnote").css(pageCoords);
		}
		
		function _hide_footnote() { 
			nvs_footnoteMouseOver = window.setTimeout(function() { _instant_hide_footnote()	}, 500); 
		}
		
		function _instant_hide_footnote() {
			window.clearTimeout(nvs_footnoteMouseOver);	
			$(document).unbind('mousemove',_show_footnote._follow_mouse );		
			$('#nvs_footnote').remove();
		}
		
		function _prevent_hide() {
			window.clearTimeout(nvs_footnoteMouseOver);
			nvs_tooltipMouseOver = false; 
		}
	}
	
	
})(jQuery);


jQuery(function($) {
	$('#frm-locaties label').footnotes();
});

