/* VARIABLES */
var curr_infw;
var geocoder;
var map;
var locationRef = new Array();
var geocounter = 0;

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

	// high contrast
		var hc = readCookie('hc');
		if (hc == 'yes' || hc == 'yesundefined') {
			$('body').addClass('hc');
			$('.high a span').html('Normaal contrast');
		} else {
			$('body').removeClass('hc');
			$('.high a span').html('Hoger contrast');
		}
		$('.high a').attr('href', '#top');
		$('.high').click(function() {
			if ($('body').hasClass('hc')) {
				$('body').removeClass('hc');
				$(this).find('a span').html('Hoger contrast');
				eraseCookie('hc');
				createCookie('hc', 'no');
			} else {
				$('body').addClass('hc');
				$(this).find('a span').html('Normaal contrast');
				eraseCookie('hc');
				createCookie('hc', 'yes');
			}
		});

		// smooth scrolling
		enableSmoothScroll();

		/*
		// start google map
		if ($('#map_canvas').width() > 0) {
			startGoogleMap();
		}
		*/


		// input fields
		inputFieldEvents();

		// columns
		if ($.browser.msie) {
			createTwoColumns('.twoCol:not(#faq_list)');
		}
		createTwoColumnsFaq('#faq_list');

		// first-children
		if ($.browser.msie) {
			$('#content *:first-child').addClass('first-child');
		}

		// nav
		if ($.browser.msie
				&& ($.browser.version == '6.0' || $.browser.version == '7.0')) {
			$('#header ul.navigation li').hover(function() {
				navItem = $(this)
				navItem.siblings().css('overflow', 'hidden');
				navItem.css('overflow', 'visible');
			});
			$('#content').hover(function() {
				$('#header ul.navigation li').css('overflow', 'hidden');
			});
		}

		$('a.back').hover(function() {
			fixFooterPosition();
			if ($.browser.msie && $.browser.version == '6.0') {
				$('h3').css('marginTop', '35px');
				$('.large .dtstart').css('top', '65px');
			}
		});

		// tinymce
		$('#topic_content, #comment_content').tinymce( {
			script_url : baseUrl + 'js/tiny_mce/tiny_mce.js',
			theme : "advanced",
			plugins : "safari,spellchecker",
			theme_advanced_buttons1 : "bold,italic,link",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left"
		});
		
	    setTimeout(fixFooterPosition,1000);


	});

$(window).resize(function() {

	fixFooterPosition();

});

/* FUNCTIONS */

function inputFieldEvents() {

	/*
	 * $('input:text').each(function(){ inp = $(this);
	 * inp.attr('original',inp.attr('value')); });
	 * 
	 * $('input#login_email, input#login_pass, input#term').focus(function(){
	 * inp = $(this); if(inp.attr('value') == inp.attr('original')){
	 * inp.attr('value',''); } });
	 * 
	 * $('input#login_email, input#term').blur(function(){ inp = $(this);
	 * if(inp.attr('value') == ''){ inp.attr('value',inp.attr('original')); }
	 * });
	 */
}

function fixFooterPosition() {

	$('#content').css('height', 'auto');
	footerOffset = $('#footer').offset();
	$('#footer div').hide();
	if ($.browser.msie
			&& ($.browser.version == '6.0' || $.browser.version == '7.0')) {
		minimalContentHeight = $(window).height()
				- ($('#footer').height() + $('#header').height());
	} else {
		minimalContentHeight = $(window).height()
				- ($('#footer').height() + $('#header').height()) - 23;
	}
	contentHeight = footerOffset.top - $('#header').height();
	if (contentHeight < minimalContentHeight) {
		$('#content').height(minimalContentHeight);
		$('#footer div').css('position', 'relative');
		$('#footer div').css('top', '1px');
		$('#footer div').css('top', '0');
	}
	$('#footer div').show();
}

function enableSmoothScroll() {

	$('a[href*=#]').click(
			function() {
				if (location.pathname.replace(/^\//, '') == this.pathname
						.replace(/^\//, '')
						&& location.hostname == this.hostname) {
					var $target = $(this.hash);
					$target = $target.length && $target
							|| $('[name=' + this.hash.slice(1) + ']');
					if ($target.length) {
						var targetOffset = $target.offset().top;
						$('html,body').animate( {
							scrollTop : targetOffset
						});
						return false;
					}
				}
			});

}
/*
function getMapData() {

	$('.vcard').each(
			function() {

				var vcard = $(this);
				var org = vcard.find('.org').html();
				var name = vcard.find('.given-name').html();
				var details = vcard.find('.details');
				var street = vcard.find('.street-address').html();
				var city = vcard.find('.locality').html();
				var postalcode = vcard.find('.postal-code').html();
				var country = vcard.find('.country-name').html();

				locationRef.push( [
						name + ',' + street + ',' + postalcode + ' ' + city
								+ ',' + country, '', details.html(), , , , ,
						org ]);

				// locationRef.push([street + ',' + postalcode]);

				details.hide();

			});

}

function startGoogleMap() {

	getMapData();

	var content = '<div id="images">';

	for ( var i = 0; i < locationRef.length; i++) {

		if (locationRef[i][1]) {

			var iconImage = 'images/icons/' + locationRef[i][1] + '.png';
			var shadowImage = 'images/icons/' + locationRef[i][1] + '_shadow.png'

			content += '<img class="ic n' + i + '" src="' + iconImage + '"/>';
			content += '<img class="ch n' + i + '" src="' + shadowImage + '"/>';

		}

	}

	content += '</div>';
	$('body').append(content);

	var imageDiv = $('#images');
	imageDiv.css( {
		position : 'absolute',
		bottom : '0',
		width : '1px',
		overflow : 'hidden'
	});

	$(window).load(function() {

		$('#images img.ic').each(function() {

			iconTemp = $(this);
			i = iconTemp.removeClass('ic').attr('class').substring(1);
			locationRef[i][3] = iconTemp.width();
			locationRef[i][4] = iconTemp.height();
			iconTemp.remove();

		})

		$('#images img.ch').each(function() {

			iconTemp = $(this);
			i = iconTemp.removeClass('ch').attr('class').substring(1);
			locationRef[i][5] = iconTemp.width();
			locationRef[i][6] = iconTemp.height();
			iconTemp.remove();

		})

		imageDiv.remove();
		googleMapInit();

	});

}

function googleMapInit() {

	var canvas = $('#map_canvas');
	canvas.height('400px');

	var myOptions = {
		zoom : 11,
		center : new google.maps.LatLng(51.305621, 4.36114),
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		backgroundColor : 'white'
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	geocoder = new google.maps.Geocoder();

	setMarkers(locationRef);

}

function setMarkers(locations) {

	
	while(geocounter<locations.length){
		setMarker(locations[geocounter], geocounter);
		geocounter++;
	}

}

function setMarker(locationRef, i) {

	if (locationRef[1]) {

		var iconImage = 'images/icons/' + locationRef[1] + '.png';
		var shadowImage = 'images/icons/' + locationRef[1] + '_shadow.png'
		var iconImageWidth = locationRef[3];
		var iconImageHeight = locationRef[4];
		var shadowImageWidth = locationRef[5];
		var shadowImageHeight = locationRef[6];

		var image = new google.maps.MarkerImage(iconImage,
				new google.maps.Size(iconImageWidth, iconImageHeight),
				new google.maps.Point(0, 0), new google.maps.Point(
						iconImageWidth / 2, iconImageHeight))

		var shadow = new google.maps.MarkerImage(shadowImage,
				new google.maps.Size(shadowImageWidth, shadowImageHeight),
				new google.maps.Point(0, 0), new google.maps.Point(
						iconImageWidth / 2, shadowImageHeight));

		var shape = {
			coord : [ 0, 0, 0, iconImageHeight, iconImageWidth,
					iconImageHeight, iconImageWidth, 0 ],
			type : 'poly'
		};

	}

	var infowindow = new google.maps.InfoWindow( {
		content : locationRef[2],
		maxWidth : 350,
		maxHeight : 350
	});
	
	// set delay
	$(document).oneTime(5000, function() {

		// request geocode via JSON
			$.getJSON(baseUrl + "js/geo.php?location=" + locationRef[0],

			function(data) {

				// convert coordinates to floats
					var lat = parseFloat(data.lat);
					var lng = parseFloat(data.lng);

					// create LatLng object
					var myLatlng = new google.maps.LatLng(lat, lng);

					// put markers on map
					var marker = new google.maps.Marker( {
						position : myLatlng,
						map : map,
						icon : image,
						shadow : shadow,
						shape : shape,
						title : locationRef[7]
					});

					// make them clickable
					google.maps.event.addListener(marker, 'click', function() {
						if (curr_infw) {
							curr_infw.close();
						}
						curr_infw = infowindow;
						infowindow.open(map, marker);
					});

				});

		});

	fixFooterPosition();

}
*/
function createTwoColumns(contentName) {

	var cont = $(contentName);
	var totalHeight = $(contentName).height();
	var countHeight = 0;
	var divs = '<div id="firstCol"></div><div id="secondCol"></div>'

	cont.append(divs);

	$.fn.reverse = [].reverse;

	$(contentName + '>p, ' + contentName + '>table').reverse().each(function() {
		p = $(this);
		height = parseInt(p.height()) + parseInt(p.css('marginBottom'));
		countHeight += height;
		if (countHeight > (totalHeight / 2)) {
			$('#firstCol').prepend(p);
		} else {
			$('#secondCol').prepend(p);
		}
	});

	fixFooterPosition();

}

function createTwoColumnsFaq(contentName) {

	var cont = $(contentName);
	var totalHeight = $(contentName).height();
	var countHeight = 0;
	var divs = '<div id="firstCol" class="container"></div><div id="secondCol" class="container"></div>'

	cont.append(divs);

	$.fn.reverse = [].reverse;

	$(contentName + ' div:not(.container)').reverse().each(function() {
		p = $(this);
		height = parseInt(p.height()) + parseInt(p.css('marginBottom'));
		countHeight += height;
		if (countHeight > (totalHeight / 2)) {
			$('#firstCol').prepend(p);
		} else {
			$('#secondCol').prepend(p);
		}
	});

	fixFooterPosition();

}

function createCookie(name, value, path, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	if (!path) {
		path = '/';
	} else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=" + path;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for ( var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function checkoutcheck() {
	if(confirm('Bent u zeker dat u dit document wilt uitchecken?')==true) {
		return true;
	} else {
		return false;
	}
}

