var fotoArray;
var vorige;
var volgende;
/***********************************************
*een submenu openen
***********************************************************/
function open(menu_id, image_id) 
{
	$('#' + menu_id).fadeIn("slow");
	$('#' + image_id).attr('src', 'http://www.luiemotorfiets.nl/images/pijltje_links_17.gif');
}
/********************************** Na loaden *********************************/
function naLaden() {
	menuEvents();
	allesDicht();
	links();
	//tooltips();
	fotogallery();
	$('#csluiten').click(function() {
		toggleCommentaar();
	});
	$('#commentaarHeader').click(function() {
		toggleCommentaar();
	});
	toggleCommentaar();
	$('#verstuur').click(function() {
		valideer();
		return false;
	});
}
function toggleCommentaar() {
	$('#commentaar').toggle();
	if ($('#commentaar').css('display') == 'none') {
		$('#csluiten').attr('src', 'http://www.luiemotorfiets.nl/images/pijltje_rechts_17.gif');
		$('#csluiten').attr('alt', 'Open comments');
		$('#csluiten').attr('title', 'Open comments');
		$('#commentaarHeader').text('Click to view or give Comments');
	}
	else {
		$('#csluiten').attr('src', 'http://www.luiemotorfiets.nl/images/close.png');
		$('#csluiten').attr('alt', 'Close comments');
		$('#csluiten').attr('title', 'Close comments');
		$('#commentaarHeader').text('Comments on this page');
	}
}
/********************************** EventListeners in het menu binden *********************************/
function menuEvents() {
	$('#menu img.menupijl').click(function(){
		var submenu = $(this).next();
		if (submenu.css('display') == 'none')	
		{
			submenu.fadeIn("slow");
			$(this).attr('src', 'http://www.luiemotorfiets.nl/images/pijltje_links_17.gif');
		}
		else 
		{
			submenu.fadeOut("slow");
			$(this).attr('src', 'http://www.luiemotorfiets.nl/images/menupijltje-meer.gif');
		}
		});
	$("#menu img.menupijl").mouseover(function(){
		$(this).attr('style', 'top: -1px; left: -1px');
	});
	$("#menu img.menupijl").mouseout(function(){
		$(this).attr('style', 'top: 0px; left: 0px');
	});
}
/********************************** Het menu dichtklappen*********************************/
function allesDicht() {
	$('#menu img.menupijl').attr('src', 'http://www.luiemotorfiets.nl/images/menupijltje-meer.gif');
	$('#menu ul').not('#menulijst').fadeOut("slow");
}
/*********************************************
* Voeg een target_blank toe aan alle links * die niet naar de site zelf wijzen.
* Voeg een pijltje naar buiten toe achter alle links naar buiten
* Voeg een pijltje naar binnen toe voor alle sitelinks
* Laat alle links een stukje bewegen bij mouseover
***********************************************/
function links() 
{
	$('#verhaal a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}
	).attr('target', '_blank');
	$('#verhaal a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}
	).after('&nbsp;<img src="http://www.luiemotorfiets.nl/images/buitenlink.gif" alt="outbound link">');
	$('#verhaal a').not('.plaatje').filter(function() {
		return this.hostname && this.hostname == location.hostname;
	}
	).before('&nbsp;<img src="http://www.luiemotorfiets.nl/images/binnenlink.gif" alt="inbound link">&nbsp;');
	$('a').mouseover(function(){
		$(this).attr('style', 'top: -1px; left: -1px');
	});
	$('a').mouseout(function(){
		$(this).attr('style', 'top: 0px; left: 0px');
	});
	var tooltiptekst = '<div class=\"tooltip\">' + 
		'You may use a photograph under certain conditions,<br>like mentioning the source. See:<br><br>' +
		'<img src=\"http://www.luiemotorfiets.nl/images/binnenlink.gif\" alt=\"inbound link\">&nbsp;' +
		'<a href=\"http://www.lazymotorbike.eu/site/copyright/#use\">' +
		'Conditions for the use of photographs</a>.<br><br>See for details the:<br> ' +
		'<img src=\"http://www.luiemotorfiets.nl/images/binnenlink.gif\" alt=\"inbound link\">&nbsp;' +
		'<a href=\"http://www.lazymotorbike.eu/site/copyright/\">' +
		'Copyright page</a>.</div>';
	$('a.plaatje').prepend($(tooltiptekst));
	var closecommenttekst = '<img id=\"csluiten\" class=\"links\" ' + 
		'src=\"http://www.luiemotorfiets.nl/images/close.png\" alt=\"sluiten \">';
	$('#commentaar').before($(closecommenttekst));
	if (top.location != location) {
		top.location.href = document.location.href ;
	}
	$('div#bovenkant').click(function() {
		document.location.href='http://www.lazymotorbike.eu/site/about/#safety';
	});
}
function nieuwefoto() {
	$.get('/code/php/nieuwefoto.php' + '?' + Date(),  function(data) {
		var fotogegevens = eval('(' + data + ')');
		$('#plekvoorfoto').css("width", fotogegevens.breedte);
		$('#plekvoorfoto a').attr({ 
			id: 'fotolink', 
			href: fotogegevens.link,
			title: fotogegevens.caption
			});
		$('#plekvoorfoto img').attr({
			id: 'foto',
			src: fotogegevens.foto,
			alt: fotogegevens.alt
			});
		$('#plekvoorfoto h5').html(fotogegevens.caption + '<button id="foto" onclick="nieuwefoto()">Next photo</button>');
	});
}
function rechtermuisknop(src, alt) {
	if (src.search('http') == -1) {
		var url = document.location.href + src;
	}
	else {
		var url = src;
	}
	var codetekst = '<img src=\"' + url +
		'\" \nalt=\"'+ alt + '"><br>\n' +
		'Photograph from: \n<a href='+ 
		'\"http://www.lazymotorbike.eu\">the Lazy Motorbike site.</a>';
	$('#linkcodetext').val(codetekst);
	$('#linkcode').show();
	$('#linkcodetext').focus();
	return false;
}
function fotogallery() {
	fotoArray = $('a.plaatje > img');
	$('#lsluiten').click(function() {
		$('#linkcode').hide();
	});
	$('#gvorige').click(function() {
		openGallery(0);
	});
	$('#gvolgende').click(function() {
		openGallery(1);
	});
	$('#gsluiten').click(function() {
		$('#gallery').hide();
		$('div.tooltip').hide();
		$('linkcode').hide();
		$('body').css('overflow', 'auto');
		return false;
	});
	$('a.plaatje').bind('contextmenu', function(e) {
		var src = $(this).children('img').attr('src');
		var alt = $(this).children('img').attr('alt');
		e.preventDefault();
		rechtermuisknop(src, alt);
		return false;
	});
	$('a.plaatje').mousedown(function(e) {
		if( e && e.button == 2 ){
			var src = $(this).children('img').attr('src');
			var alt = $(this).children('img').attr('alt');
			e.preventDefault();
			rechtermuisknop(src, alt);
			return false;
	     }
	},true);
	$('#gfoto').bind('contextmenu', function(e) {
		var src = $('#gfoto').attr('src');
		var alt = $('#gfoto').attr('alt');
		e.preventDefault();
		rechtermuisknop(src, alt);
		return false;
	});
	$('#gfoto').mousedown(function(e) {
		if( e && e.button == 2 ){
			var src = $('#gfoto').attr('src');
			var alt = $('#gfoto').attr('alt');
			e.preventDefault();
			rechtermuisknop(src, alt);
			return false;
	     }
	},true);
	$('a.plaatje').click(function() {
		$('div.tooltip').hide();
		$('linkcode').hide();
		var headingtext = $(this).next('h5').get(0).innerHTML;
		var volgnummer = fotoArray.index($(this).children('img'));
		//alert("Gebonden bij laden pagina: " + volgnummer);
		var imagesrc = $(this).children('img').attr('src');
		vorige = fotoArray[volgnummer];
		openGallery(0);
		return false; 
	}); 
	$('body').click(function() {
		$('linkcode').hide();
	});
	
}
function openGallery(vorigeOfVolgende) {
	if (vorigeOfVolgende == 0) {
		foto = vorige;
	}
	else {
		foto = volgende;
	}
	var headingtext = $(foto).parent().next('h5').get(0).innerHTML;
	var volgnummer = fotoArray.index($(foto));
	var imagesrc = $(foto).attr('src');
	$.get("/code/php/foto.php", { pad: document.location.href, foto: imagesrc }, function(data) {
		$('#gfoto').attr('src', data);
		$('#gallery').show();
		$('#gheadingtext').html(headingtext);
		//volgende en vorige
		if (volgnummer > 0 ) {
			$('#gvorige').show();
			vorige = fotoArray[volgnummer - 1];
		}
		else {
			$('#gvorige').hide();
		}
		if (volgnummer < (fotoArray.length - 1) ) {
			$('#gvolgende').show();
			volgende = fotoArray[volgnummer + 1];
		}
		else {
			$('#gvolgende').hide();
		}
	});
    $(foto).bind('contextmenu', function(){
		var codetekst = 'Code for a link:\n\n'+ 
			'<img src=\"' + document.location.href + $(foto).attr('src') +
			'\" \nalt=\"'+ $(foto).parent().next('h5').get(0).innerHTML + '"><br>\n' +
			'Photograph from: \n<a href='+ 
			'\"http://www.lazymotorbike.eu\">the Lazy Motorbike site.</a>';
		$('#linkcode').val(codetekst);
		$('#linkcode').show();
		$('#linkcode').focus();
		return false;
	});
	$('body').css('overflow', 'hidden');
	return false; 
}
function tooltips() {
	$('a.plaatje > img').mouseover(function() {
		$(this).parent().children('div.tooltip').fadeIn();	
	});
	$('#gfoto').mouseover(function() {
		$(this).parent().children('div.tooltip').fadeIn();	
	});
	$('div.tooltip').mouseover(function() {
		$(this).fadeIn();	
	});
	$('a.plaatje > img').mouseout(function() {
		$(this).parent().children('div.tooltip').fadeOut();	
	});
	$('#gfoto').mouseout(function() {
		$(this).parent().children('div.tooltip').fadeOut();	
	});
}
/********************************** Reacties *********************************/
/************************************************
* controleren van de velden
************************************************/
function valideer() {
	var formulier = $('#reactieform');
	var naam = $('#naamID');
	var email = $('#eMailID');
	var zelfCC = $('#zelfID');
	var whiteSpace = /^[\s]+$/;
	var inorde = true;
	if (($('#reactieID').val() == '') || whiteSpace.test($('#reactieID').val()) ) {
		alert("Please fill in your comments.");
		inorde = false;
		$('#reactieID').focus();
	}
	else {
		if ( $('#naamID').val() == '' ) {
			alert("Please fill in your name.");
			inorde = false;
			$('#naamID').focus();
		}
	}
	if ($('#reactieID').val().match('viagra') != null) {
		inorde = false;
	}
	if (inorde) {
		verstuur();
	}
}
/********************************** Einde Reacties *********************************/
/**********************************************
* vor het gemak specifiek voor het reactieformulier
************************************************/
function verstuur() {
	var naam = $('#naamID');
	var email = $('#eMailID');
	var reactietekst = $('#reactieID');
	var id = $('#idID');

	var commentaarstr = encodeURIComponent(maakCommentaarItem($('#idID').val(), $('#reactieID').val(), $('#naamID').val()));
	var emailstr = encodeURIComponent($('#eMailID').val());
	var checked = "ja";
	var boodschap = "comCommentaar="+commentaarstr+"&comMail="+emailstr;
	//alert("naar ajaxreactie sturen");
	$.post("/code/php/ajaxreactie.php", boodschap, handleReactie);
	//alert("done");
	return true;
}

function maakCommentaarItem(id, tekst, naam) {
	var commentaarstr = "Page: "+ id + "\n";
	commentaarstr += "Name: " + naam + "\n";
	commentaarstr += "Comments: \n";
	commentaarstr += tekst + "\n";
	return commentaarstr;
}

function handleReactie(http){
	//if ((http.readyState == 4)&& (http.status == 200)) {
		//$('#reactieform').reset();
		$('#reactieID').val(http);
		$('#naamID').val("");
		$('#eMailID').val("");
	//}
	return false;
}