function checkform ()
{
  // see http://www.thesitewizard.com/archive/validation.shtml
	var v = '';
	var f = '';
	var m = '';
	var c= '';

	f = $('#naam');
	v = f.attr('value');
  if (v == "") {
    f.focus();
	  f.select();
    return ("Niet verzonden: 'Naam' is niet ingevuld" );
  }
	f = $('#mail');
	m = f.attr('value');
  if (m == "") {
    f.focus();
	  f.select();
    return ("Niet verzonden: 'Email' is niet ingevuld" );
  }

	f = $('#mail_controle');
	c = f.attr('value');
  if (c == "") {
    f.focus();
	  f.select();
    return ( "Niet verzonden: 'Email (controle)' is niet ingevuld" );
  }
  if (m != c) {
    f.focus();
	  f.select();
    return ( "Niet verzonden: 'Email' en 'Email (controle)' zijn niet gelijk" );
  }
  var str = m;
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    return '';
  }
  f.focus();
  f.select();
  return ("Niet verzonden: ongeldig email adres: " + str);
}


$(document).ready(function() {


  $('div#poWrapper').css('top', $('#container').position().top + 103 + 'px');
  $('div#poWrapper').css('left',$('#col-1').position().left + 244 + 'px');

  var thisImgSeq        = 0;
  var keyTrap           = false;
  var maxCollectionSeq  = 0;
  var whichImgSet       = '';
  
  $('#nwsBrfContainer').dialog({ autoOpen: false, width:540, title: 'Aanmelden voor onze nieuwsbrief' });
	$('#nwsBrfTrigger').click(function(){$('#nwsBrfContainer').dialog('open');return(false)})

	$('#nwsBrfSubmit').click(function(){
		$('#nwsBrfError').text('');
	  var errMsg = checkform();
		if(errMsg != '') {
			$('#nwsBrfError').text(errMsg);
			return(false);
		}
		$.post(
    "nwsBrf.php",
		$("form").serialize(),
		function(response, status, xhr) {
      // NB: IE stikt in console.log als de debugger uit staat!
      // console.log("s " + xhr.status + " " + xhr.statusText);
			if (status == "error") {
        $('#nwsBrfError').text('Systeemfout - probeer het later opnieuw aub.');
			} else {
        $('#nwsBrfError').text('Uw aanmelding is verzonden. Bedankt voor uw aanvraag');
			 $('input[type="text"]').val('');

			}
		},
    "html"
  );

	return(false);
	})

  $(document).bind("keydown", (function(e) {
    if(! keyTrap) {
      return(true);
    }
    if(e.which == 27 || e.which == 13 ) {
      $('a#poClose').click();
      return(true);
    }
    
    maxImg = whichImgset == 'random' ? 5 : maxCollectionSeq;
    minImg = whichImgset == 'random' ? 0 : 100;
    // right arrow or page down key
    if(e.which == 39 || e.which == 34){
      w = thisImgSeq + 1;
      if(w > maxImg) {w = minImg};
      w = 'a#imgSeq-' + w;
      $(w).click();
      return(true);
    }
    // left arrow or page up key
    if(e.which == 37 || e.which == 33){
      w = thisImgSeq - 1;
      if(w < minImg) {w = maxImg};
      w = 'a#imgSeq-' + w;
      $(w).click();
      return(true);
    }
    return(true);
  }));

  $('a.randomImg').each(function(i) {
    //'this' is a DOM node, not a jQuery object!
    this.setAttribute('id','imgSeq-' + i);
    this.setAttribute('name','random');
    });
  
  $('a.collectionImg').each(function(i) {
    //'this' is a DOM node, not a jQuery object!
    maxCollectionSeq = i + 100;
    this.setAttribute('id','imgSeq-' + maxCollectionSeq);
    this.setAttribute('name','collection');
    });

  $('#accordion').css('overflow','hidden');
  //$('#accordion > h2').css({'line-height':'26px','padding-left':'26px'});
  //$('#accordion > div').css({'padding':'8px 8px 8px 24px'});
  $("#accordion").accordion({ header: 'h2', fillSpace: true,autoHeight: false});
  
  
  $('a#poClose').click(function() {
      $('#poWrapper').fadeOut(1000);
      $('#poBody').html('');
      keyTrap = false;
      return(false);
  });

  $('a.thumb').click(function()
    {
      w               = /(.*&)(\w*$)/.exec($(this).attr('href'));

      var toLoad      = "index.php?ajax&" + w[2];
      var toShow      = '';
      
      whichImgset     = $(this).attr('name');
      w               = /(imgSeq-)(\d+)/.exec($(this).attr('id'));
      thisImgSeq      = w[2] * 1;    // make numeric
      
      if($('#poBody').html() == '') {
        toShow = $('#poWrapper');
        loadContent();
      } else {
        toShow = $('#poBody');
        $('#poBody').fadeOut(1000,loadContent);
      }

      function loadContent() {
    	  $('#poBody').load(toLoad,'',showNewContent()) ;
      }

      function showNewContent() {
    	  toShow.fadeIn(1500);
        keyTrap = true;
      }

      return(false);
    });
    var o = $('#col-3').offset();
		var y = o.top + 10;
		var x = o.left + 10;
    // w=425, h=700
    $('#dialogC3').dialog({autoOpen: false,position:[x,y],width: 400,height:600,show: 'slide',hide: 'slide'});

		var o = $('#col-1').offset();
		var y = o.top + 10;
		var x = o.left + 80;
    $('#dialogC4').dialog({autoOpen: false,position:[x,y],width: 665,height:300,show: 'slide',hide: 'slide'});

		$('#moreVanMeel').click(function() {

    	$('#dialogC3').dialog('option','title',$(this).attr('title'));
    	$('#dialogC3').dialog('open');
			return false;
		});

		$('#moreNewly').click(function() {

    	$('#dialogC4').dialog('option','title',$(this).attr('title'));
    	$('#dialogC4').dialog('open');
			return false;
		});
});
