// Popup voor het mailen
// © 2010 Itass

$(document).ready(function() {
	$('.mail').click(function(e) {	
		$content = $('<div />');
		$content.attr("id", "popupcontent");
		$('object').css("visibility", "hidden"); // Hier verstoppen we de flash
		
		// Formuliertje van de server trekken...
		$.ajax({
			url: '/mailfriends.php',
			success: function(data) {
				$content.html(data);
			}
		});
		
		$popup = $('<div />');
		$popup.attr("id", "popup");
		$popup.html($content);
				
		$div = $('<div />');
		$div.attr("id", "popupwrapper");
		$div.html($popup);
			
		$div.appendTo('body');	
	});
	
	$(window).scroll(function(e) {
		$('#popupwrapper').css('top', $(window).scrollTop());
	});
	
	$('#submit').live('click', function(e) { 
		// Eventjes wat data versturen en valideren
		
		$('#errors').addClass('load');
				
		$.ajax({
			url: '/mailfriends.php?link=' + window.location,      
			type: "POST",      
			data: "submit=true&txtmename=" + $('#txtmename').val() + 
					"&txtmeemail=" + $('#txtmeemail').val() +
					"&txttoname=" + $('#txttoname').val() +
					"&txttoemail=" + $('#txttoemail').val() +
					"&txtomschrijving=" + $('#txtomschrijving').val() +
					"&txtonderwerp=" + $('#txtonderwerp').val(),
			success: function(data) { 
				$('#errors').removeClass('load');
				if(data != "") {
					// We krijgen fouten terug...
					$("#errors").html(data);
				} else {									
					$('#popupwrapper').remove();
					$('object').css("visibility", "inherit");
					//$('#contents').html('Uw bericht is met succes verzonden. Klik <a href="#" class="close">hier</a> om dit venster te sluiten.');
				}
			}						
		});
	});
	
	// Beetje hele lompe manier om een window te sluiten
	// Kan echter niet anders, want niet alle browsers snappen de z-index. Beetje jammer
	$('#popupwrapper').live('click', function(e) {	
		var p = $('#popup').offset();
	
		if($(p).length > 0) {
			if(e.clientX < p.left || e.clientX > (p.left + $('#popup').width())) {	
				$(this).remove();
				$('object').css("visibility", "inherit");
			} else if(e.clientY < p.top || e.clientY > (p.top + $('#popup').height())) {		
				$(this).remove();
				$('object').css("visibility", "inherit");
			}
		}
	});	
	
	$('.close').live('click', function(e){ 
		$('#popupwrapper').remove();
		$('object').css("visibility", "inherit");
	});
});

function popUp(URL,width,height) {
	day = new Date();
	id = day.getTime();

	var winLeft = (screen.width - width) / 2;
	var winUp = (screen.height - height) / 2;

	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+',left='+winLeft+',top='+winUp);");
}

