var fotoArray;
var vorige;
var volgende;
if (top != self) {top.location = location;}
/***********************************************
*een submenu openen
***********************************************************/
function open(menu_id, image_id) {
	$('#' + menu_id).fadeIn("slow");
	$('#' + image_id).attr('src', '/images/pijltje_links_17.gif');
}
/********************************** Na loaden *********************************/
function naLaden() {
	links();
	allesDicht();
	menuEvents();
	fotogallery();
	$('#verstuur').click(function() {
		valideer();
		return false;
	});
}
/********************************** 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', '/images/pijltje_links_17.gif');
		}
		else 
		{
			submenu.fadeOut("slow");
			$(this).attr('src', '/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', '/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() {
	$('article a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}
	).attr('target', '_blank');
	$('article a').not('.plaatje, .geenpijl').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}
	).after('&nbsp;<img src="/images/buitenlink.gif" width="12" height="12" alt="outbound link">');
	$('article a').not('#paginamenu a, .plaatje, .geenpijl').filter(function() {
		return this.hostname && this.hostname == location.hostname;
	}
	).before('&nbsp;<img src="/images/binnenlink.gif" width="12" height="12" alt="inbound link">&nbsp;');
	$('a').mouseover(function(){
		$(this).attr('style', 'top: -1px; left: -1px');
	});
	$('a').mouseout(function(){
		$(this).attr('style', 'position: relative; top: 0px; left: 0px');
	});
	if (top.location != location) {
		top.location.href = document.location.href ;
	}
}
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' +
		'Foto afkomstig van: \n<a href='+ document.location.href + '">de Luie Motorfiets 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();
		$('linkcode').hide();
		$('body').css('overflow', 'auto');
		return false;
	});
	$('a.plaatje').bind('contextmenu', function(e) {
		var lmsrc = $(this).children('img').attr('src');
		var lmalt = $(this).children('img').attr('alt');
		e.preventDefault();
		rechtermuisknop(lmsrc, lmalt);
		return false;
	});
	$('#gfoto').bind('contextmenu', function(e) {
		var lmsrc = $('#gfoto').attr('src');
		var lmalt = $('#gfoto').attr('alt');
		e.preventDefault();
		rechtermuisknop(lmsrc, lmalt);
		return false;
	});
	$('#gfoto').mousedown(function(e) {
		if( e && e.button == 2 ){
			var lmsrc = $('#gfoto').attr('src');
			var lmalt = $('#gfoto').attr('alt');
			e.preventDefault();
			rechtermuisknop(lmsrc, lmalt);
			return false;
	     }
	},true);
	$('a.plaatje').click(function(e) {
		e.preventDefault();
		$('linkcode').hide();
		var headingtext = $(this).next('div.caption').get(0).innerHTML;
		var volgnummer = fotoArray.index($(this).children('img'));
		var imagesrc = $(this).children('img').attr('src');
		vorige = fotoArray[volgnummer];
		openGallery(0);
		return false; 
	}); 
	$('body').click(function() {
		$('linkcode').hide();
	});
	
}
function openGallery(vorigeOfVolgende) {
	var foto;
	if (vorigeOfVolgende == 0) {
		foto = vorige;
	}
	else {
		foto = volgende;
	}
	var headingtext = $(foto).parent().next('div.caption').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();
		// foto in het midden zetten
		/* var fotohelft = (new String($('#gfoto').width()).replace('px', ''))/2;
		var dochelft = (new String($(document).width()).replace('px', ''))/2;
		$('#gfoto').css('left', (dochelft - fotohelft)); */
		$('#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 voor een link:\n\n'+ 
			'<img src=\"' + document.location.href + $(foto).attr('src') +
			'\" \nalt=\"'+ $(foto).parent().next('div.caption').get(0).innerHTML + '"><br>\n' +
			'Foto afkomstig van: \n<a href='+ 
			'\"http://www.luiemotorfiets.nl\">de Luie Motorfiets site.</a>';
		$('#linkcode').val(codetekst);
		$('#linkcode').show();
		$('#linkcode').focus();
		return false;
	});
	$('body').css('overflow', 'hidden');
	return false; 
}
/********************************** 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("Je hebt je reactie nog niet geschreven.");
		inorde = false;
		$('#reactieID').focus();
	}
	else {
		if ( $('#naamID').val() == '' ) {
			alert("Je hebt je naam nog niet opgegeven.");
			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 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 = "Pagina: "+ id + "\n";
	commentaarstr += "Naam: " + naam + "\n";
	commentaarstr += "Commentaar: \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;
}