$(function() {
	$('.datetimefield').datetimepicker({
		timeOnlyTitle: 'Kies een tijdstip',
		timeText: 'Tijd',
		hourText: 'Uur',
		minuteText: 'Minuut',
		currentText: 'Nu',
		closeText: 'Ok',
		hourGrid: 4,
		minuteGrid: 10,
		prevText: '<Vorige',
		nextText: 'Volgende>',
		monthNames: ['Januari','Februari','Maart','April','Mei','Juni',
		'Juli','Augustus','September','Oktober','November','December'],
		monthNamesShort: ['jan','feb','maa','apr','mei','jun',
  		'jul','aug','sep','okt','nov','dec'],
  		dayNames: ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'],
  		dayNamesShort: ['Zo','Ma','Di','Wo','Do','Vr','Za'],
  		dayNamesMin: ['Zo','Ma','Di','Wo','Do','Vr','Za'],
  		dateFormat: 'dd-mm-yy'

	});
	
	$('.timefield').timepicker({
		timeOnlyTitle: 'Kies een tijdstip',
		timeText: 'Tijd',
		hourText: 'Uur',
		minuteText: 'Minuut',
		currentText: 'Nu',
		closeText: 'Ok',
		hourGrid: 4,
		minuteGrid: 10

	});
	$('.datefield').datepicker({
		closeText: 'Ok',
		prevText: '<Vorige',
		nextText: 'Volgende>',
		currentText: 'Nu',
		monthNames: ['Januari','Februari','Maart','April','Mei','Juni',
		'Juli','Augustus','September','Oktober','November','December'],
		monthNamesShort: ['jan','feb','maa','apr','mei','jun',
  		'jul','aug','sep','okt','nov','dec'],
  		dayNames: ['Zondag','Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag','Zaterdag'],
  		dayNamesShort: ['Zo','Ma','Di','Wo','Do','Vr','Za'],
  		dayNamesMin: ['Zo','Ma','Di','Wo','Do','Vr','Za'],
  		dateFormat: 'dd-mm-yy'

	});
	
	if(jQuery('iframe').length > 0) {
		jQuery('iframe').iframeAutoHeight();
	}
	
	//$("#tabs").tabs();
	/*
	var $tabs = $("#tabs").tabs({
        show: function(e, ui) {
            //var thistab = ui;
            //runMethod(thistab.index);
			//alert('resize');
			jQuery('iframe').iframeAutoHeight();
        }
    });
*/
	
	showEmailForm = function () {
		$('.emailform').show('slow');
		$('.hide-email-form').show('slow');
		$('.mail-to-friend').hide('slow');
	}
	
	hideEmailForm = function () {
		//alert('test');
		$('.emailform').hide('slow');
		$('.hide-email-form').hide('slow');
		$('.mail-to-friend').show('slow');
	}
	
	isValidEmail = function (strEmail){
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;				
		// search email text for regular exp matches
		if (strEmail.search(validRegExp) == -1) {
			return false;
		} 
		return true; 
	}
	
	sendImageEmail = function () {
		name = $('#name').val();
		email = $('#email').val();
		message = $('#message').val();
		emailreceiver = $('#emailreceiver').val();
		image = $('#fancybox-img').attr("src");
		error = '';
		if(name == '') {
			error += 'Uw Naam is een verplicht veld!\n';
		}
		
		if(email == '') {
			error += 'Uw Email is een verplicht veld!\n';
		} else if(!isValidEmail(email)) {
			error += 'Uw email is een ongeldig e-mail adres!\n';
		}
		
		if(emailreceiver == '') {
			error += 'Email ontvanger is een verplicht veld!\n';
		} else if(!isValidEmail(emailreceiver)) {
			error += 'Email ontvanger is een ongeldig e-mail adres!\n';
		}
		
		if(error != '') {
			alert(error);
		} else {
			//alert($('#fancybox-img').attr("src"));
			//alert(name+' - '+email+' - '+message+' - '+emailreceiver);
			http.open("GET", '../../../modules/gallery/sendImageEmail.php?name='+escape(name)+'&email='+escape(email)+'&message='+escape(message)+'&emailreceiver='+escape(emailreceiver)+'&image='+escape(image) , true);
			http.onreadystatechange = handleHttpResponse;
			http.send(null);
			hideEmailForm();
		}
	}
	
	handleHttpResponse = function () {
		if (http.readyState == 4) {
			alert(http.responseText);
		}
	}
	
	getHTTPObject = function () {
		var xmlhttp;
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			try {
				xmlhttp = new XMLHttpRequest();
			} catch (e) {
				xmlhttp = false;
			}
		}
		return xmlhttp;
	}
	http = getHTTPObject();
});

/*---------------------------------------------------*/
/*-----------Gallery Slideshow Function--------------*/
/*---------------------------------------------------*/
$(function() {
	//width=195px height=146px
	//var count = 1;
	$('div.photogallery div a img').each(function(index) {
		//if(count < 3) {
			//alert($(this).width()+' : '+$(this).height());
		//}
		if($(this).width() > 176) {
			var scale = $(this).width()/176;
			var oldheight = $(this).height();
			var newheight = Math.floor(oldheight/scale);
			$(this).height(newheight);
			var paddingtop = Math.floor((oldheight-newheight)/2);
			$(this).css('padding-top', paddingtop);
			$(this).css('padding-bottom', paddingtop);
		}
		//count++;
	});
});

$(document).ready(function() {
	$("a[rel=example_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			titleFormatVar = '	<div class="emailform">';
			titleFormatVar +='		Om deze afbeelding te versturen vragen wij om uw e-mail adres en naam in te vullen zodat de ontvanger weet wie het bericht verstuurd heeft. U kunt ook nog een persoonlijke boodschap toevoegen en uiteraard hebben we het e-mail adres van de persoon nodig waar u de afbeelding naartoe wil sturen.<br/>';
			titleFormatVar +='		<br/>';
			titleFormatVar +='		<table cellpadding="0" cellspacing="0" border="0">';
			titleFormatVar +='		<tr><td>Uw Naam: </td><td><input type="text" id="name" name="name" /></td></tr>';
			titleFormatVar +='		<tr><td>Uw E-mail: </td><td><input type="text" id="email" name="email" /></td></tr>';
			titleFormatVar +='		<tr><td>Bericht: </td><td><textarea name="message" id="message"></textarea></td></tr>';
			titleFormatVar +='		<tr><td>Email ontvanger: </td><td><input type="text" id="emailreceiver" name="emailreceiver" /></td></tr>';
			titleFormatVar +='		<tr><td>&nbsp;</td><td align="right"><button onclick="sendImageEmail();">Verzenden</button></td></tr>';
			titleFormatVar +='		</table>';
			titleFormatVar +='	</div>';
			titleFormatVar +='	<div id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '<div class="mail-to-friend"><a onclick="showEmailForm()" style="cursor: pointer;">Email afbeelding naar een vriend</a></div><div class="hide-email-form"><a onclick="hideEmailForm()" style="cursor: pointer;">Verberg email formulier</a></div></div>';
			return titleFormatVar;
		}
	});
});

$(document).ready(function() {
    $('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
});

/*---------------------------------------------------*/
/*-----------Gallery Slideshow Function--------------*/
/*---------------------------------------------------*/
$(function() {
	//alert($('.slideshow').height());
	maxthumbwidth = $('.slideshow').width()-5;
	maxthumbheight = $('.slideshow').height()-130;
	
	//alert(maxthumbwidth+' : '+maxthumbheight);
	
	$('.slideshow').children().each(function(index) {
		//.slideshow a img
		child = $(this);
		child.children().each(function(index) {
			//.slideshow a img (eerste is altijd a)
			subchild = $(this);
			thumbwidth = subchild.width();
			thumbheight = subchild.height();
			
			if(thumbwidth > maxthumbwidth && ((maxthumbwidth/thumbwidth)*thumbheight) <= maxthumbheight) {
				thumbwidth = maxthumbwidth;
				thumbheight = maxthumbheight/(thumbwidth/maxthumbwidth);
			} else if(thumbheight > maxthumbheight && ((maxthumbheight/thumbheight)*thumbwidth) <= maxthumbwidth) {
				thumbwidth = (maxthumbheight/thumbheight)*thumbwidth;
				thumbheight = maxthumbheight;
			}
			subchild.css('width', thumbwidth+'px');
			subchild.css('height', thumbheight+'px');
		});
	});
	
	//$('.slideshow img').css('width', ($('.slideshow').width()-5)+'px');
	//$('.slideshow img').css('height', ($('.slideshow').height()-130)+'px');
});
