$(document).ready(function(){
  if(isInGallery > 0){
	$(".block-core-BreadCrumb a:first").remove();
	$.post("/hemels.php", {}, function(data){
	  $("#menu").html(data);
	  $("#menu ul li a").each(function(){
		val = base64_encode($(this).html());
		$(this).html('').css({'backgroundImage' : 'url(http://www.grandcafehemels.nl/gd/?m=' + val + ')'});
	  });
	}, "html");
  }
  
  $("#menu ul li a").each(function(){
	val = base64_encode($(this).html());
	$(this).html('').css({'backgroundImage' : 'url(../gd/?m=' + val + ')'});
  });

  $("a.single").colorbox({opacity:0.88});

  $("div#form #versturen").click(function(){
	$("div#form").animate({'opacity' : '0.3', 'filter' : 'alpha(opacity=30)'}, 300);
	fN = $("div#form input:eq(0)").val();
	fT = $("div#form input:eq(1)").val();
	fE = $("div#form input:eq(2)").val();
	fO = $("div#form input:eq(3)").val();
	fV = $("div#form textarea").val();
	$.post("/_mail.php", {"naam" : fN, "telefoon" : fT, "mail" : fE, "onderwerp" : fO, "bericht" : fV}, function(data){
	  if(data.done == 0){
		form_message('U heeft geen gegevens ingevuld!');
	  }
	  if(data.done == 1){
		form_message('Fout! Het formulier is niet verzonden!');
	  }
	  if(data.done == 9){
		form_message('Uw telefoonnummer is verplicht');
	  }
	  if(data.done == 2){
		form_message('Bedankt voor uw reactie!',5000);
		$("div#form input").val('')
		$("div#form textarea").val('')
	  }
	}, "json");
  });
  
  $("div#form2 #versturen").click(function(){
	$("div#form2").animate({'opacity' : '0.3', 'filter' : 'alpha(opacity=30)'}, 300);
	fN = $("div#form2 input:eq(0)").val();
	fT = $("div#form2 input:eq(1)").val();
	fE = $("div#form2 input:eq(2)").val();
	fP = $("div#form2 input:eq(3)").val();
	fD = $("div#form2 input:eq(4)").val();
	fH = $("div#form2 input:eq(5)").val();
	fV = $("div#form2 textarea").val();
	$.post("/_mail.php", {"naam" : fN, "telefoon" : fT, "mail" : fE, "personen" : fP, "bericht" : fV, "datum" : fD, "tijd" : fH}, function(data){
	  if(data.done == 0){
		form_message2('U heeft geen gegevens ingevuld!');
	  }
	  if(data.done == 1){
		form_message2('Fout! Het formulier is niet verzonden!');
	  }
	  if(data.done == 9){
		form_message2('Uw telefoonnummer is verplicht');
	  }
	  if(data.done == 3){
		form_message2('Reserveringen < 24 uur van tevoren kunnen wij enkel telefonisch aannemen.');
	  }
	  if(data.done == 2){
		form_message2('Bedankt voor uw reservering! We nemen zo spoedig mogelijk contact met u op.',5000);
		$("div#form2 input").val('')
		$("div#form2 textarea").val('')
	  }
	}, "json");
  });
  
  $("#alternate").datepicker({minDate: new Date(), dateFormat: 'dd-mm-yy',duration: 100, monthNamesShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']}).click(function(){$(this).blur();});
});

function form_message(msg,duration){
  if(!duration) duration = 3000;
  $("div#form").animate({'opacity' : '0.3', 'filter' : 'alpha(opacity=30)'}, 300, function(){
	tW = $("div#form table").width();
	tH = ($("div#form table").height()/2)-80;
	$(this).before('<div id="rmsg" style="padding: 0; margin: 0; position: absolute; text-align: center; color: #ca0000; font-weight: bold; padding-top: ' + tH + 'px; width: ' + tW + 'px; z-index: 99;"><div style="min-width: 1px; width: 215px; margin: 0 auto 0 auto; text-align: center; border: 1px solid #000; padding: 30px; padding-bottom: 24px; color: #ca0000; background-color: #fff;">' + msg + '<br /><div id="okensluit" style="float: right; height: 15px; border: 1px solid #000; color: #000; padding: 0px 5px 0px 5px; cursor: pointer; line-height: 15px; margin-top: 12px;">OK<span style="font-weight: normal;"> &amp; Sluiten</span></div><br clear="both" /></div></div>');
	$("#okensluit").click(function(){
	  $("#rmsg").fadeOut(400, function(){
		$(this).remove();
		$("div#form").animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 300);
	  });
	});
  });
}

function form_message2(msg,duration){
  if(!duration) duration = 3000;
  $("div#form2").animate({'opacity' : '0.3', 'filter' : 'alpha(opacity=30)'}, 300, function(){
	tW = $("div#form2 table").width();
	tH = ($("div#form2 table").height()/2)-80;
	$(this).before('<div id="rmsg" style="padding: 0; margin: 0; position: absolute; text-align: center; color: #ca0000; font-weight: bold; padding-top: ' + tH + 'px; width: ' + tW + 'px; z-index: 99;"><div style="min-width: 1px; width: 215px; margin: 0 auto 0 auto; text-align: center; border: 1px solid #000; padding: 30px; padding-bottom: 24px; color: #ca0000; background-color: #fff;">' + msg + '<br /><div id="okensluit" style="float: right; height: 15px; border: 1px solid #000; color: #000; padding: 0px 5px 0px 5px; cursor: pointer; line-height: 15px; margin-top: 12px;">OK<span style="font-weight: normal;"> &amp; Sluiten</span></div><br clear="both" /></div></div>');
	$("#okensluit").click(function(){
	  $("#rmsg").fadeOut(400, function(){
		$(this).remove();
		$("div#form2").animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 300);
	  });	  
	});
  });
}

function base64_encode(data){
  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
  if(!data){
	return data;
  }

  do{
	o1 = data.charCodeAt(i++);
	o2 = data.charCodeAt(i++);
	o3 = data.charCodeAt(i++);

	bits = o1<<16 | o2<<8 | o3;

	h1 = bits>>18 & 0x3f;
	h2 = bits>>12 & 0x3f;
	h3 = bits>>6 & 0x3f;
	h4 = bits & 0x3f;

	tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
  } while(i < data.length);
  
  enc = tmp_arr.join('');
  
  switch( data.length % 3 ){
	case 1:
      enc = enc.slice(0, -2) + '==';
	break;
	case 2:
	  enc = enc.slice(0, -1) + '=';
	break;
  }
  
  return enc;
}
