/*---------------------------------------------------*/
/*---------------Product Popup Function--------------*/
/*---------------------------------------------------*/
$(function() {
	$('#productpopup').width($(window).width());
	$('#productpopup').height($(window).height());
	centerWindow('#productpopupinfo', true, true);
	
	$('.productitem a').click(function() {		  
		$('#productpopupiframe iframe').attr('src', $(this).attr('href')+'?popup=true');
		$('#productpopup').fadeIn();
		$('#productpopupinfo').fadeIn();
		return false;
	});
	
	$('#closebutton').click(function() {
		$('#productpopup').fadeOut();
		$('#productpopupinfo').fadeOut();
		//alert('popupclosed');
		$.ajax({
		  url: "closepopup.php",
		  success: function(){
		    //alert('success');
		  },
		  error: function(){
		    //alert('error');
		  },
		  complete: function(){
		    //alert('complete');
		  }
		});
		//alert('popupclosed');
	});
	
	if(parent.$('#productpopup').is(':visible')) {
		var pathname = location.pathname;
		if(pathname == '/') {
			parent.$('#productpopup').fadeOut();
			parent.$('#productpopupinfo').fadeOut();
		}
	}
});

$(function()
{
	/*var settings = {
		showArrows: true
	};
	var pane = $('#productpopupbody');
	pane.jScrollPane(settings);
	var api = pane.data('jsp');*/
});

function centerWindow(id, setcenter, setmiddle) {
	spaceleftright = ($(window).width()-$(id).width());
	spacetopbottom = ($(window).height()-$(id).height());
	if(setcenter) {
		if(spaceleftright >= 0) {
			$(id).css('left', spaceleftright/2);
		} else {
			$(id).css('left', 10);
		}
	}
	if(setmiddle) {
		if(spacetopbottom >= 0) {
			$(id).css('top', spacetopbottom/2);
		} else {
			$(id).css('top', 10);
		}
	}
}

/*---------------------------------------------------*/
/*---------------Gallery Email Image-----------------*/
/*---------------------------------------------------*/
$(function() {
	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() > 195) {
			var scale = $(this).width()/195;
			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');
});
