$(document).ready(function(){

    var maskHeight = $(document).height();
		var maskWidth = $(document).width();
    
    $("a.popin").click(function(){$("#bgPopin, #popin").fadeIn("slow");})
    
    $("#bgPopin").css({"width":maskWidth+"px","height": maskHeight+"px","opacity":"0.7"});
		$("#popin").css({"top": (maskHeight/2)-172+"px","left": (maskWidth/2)-176+"px"});
		
    $(".popin").click(
		function() {
			$("#inside").load( $(this).attr("href"), function() {
					$.ajax({ type: "GET", url:"js/popin.js", dataType: "script" });
					$("input[type=file]").filestyle({ 
						 image: "img/upload.gif",
						 imageheight : 19,
						 imagewidth : 73,
						 width : 170
					 });
				}
			);
			return false;
		}
	);
    
    $("#popin #close a").click(function(){$("#bgPopin, #popin").fadeOut("slow");})
    
    $(window).resize(function(){
				var maskHeight = $(window).height();
		    var maskWidth = $(window).width();
				$("#bgPopin").css({"width":maskWidth+"px","height": maskHeight+"px","opacity":"0.7"});
				$("#popin").css({"top": (maskHeight/2)-172+"px","left": (maskWidth/2)-176+"px"});
			});
         
        var options = { 
        target:        '#inside',   // target element(s) to be updated with server response 
        success:       showResponse,  // post-submit callback 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
    }; 
 
    $('#contactForm').submit(function() { 
        /*$(this).ajaxSubmit(options); 
        return false; */
        $.post(this.action + '?test=true', $(this).serialize(), function (t) {
			var infos = eval('('+t+')');
			if (infos.type == 'test' && infos.errors.length) {
				var message = '';
				for(index in infos.errors) {
					message += infos.errors[index] + ', ';
				}
				$('.errors').html(message);
			} else if (infos.type == 'test') {
				$('#contactForm')[0].submit();
				$('#iframe').load(mailSent);
			}
        });
        return false;
    });
});
 
function mailSent() {
	myf = $('#iframe')[0];
	myf = myf.contentWindow.document || myf.contentDocument;
	infos = eval('('+myf.body.innerHTML+')');
	if (infos.type == 'test') {
		var message = '';
		for(index in infos.errors) {
			message += infos.errors[index] + ', ';
		}
		$('.errors').html(message);
	} else {
		$('#popin #titre').remove();
		$('#popin #inside').html(infos.message);
		setTimeout('$("#popin #close a").click();', 1500);
	}
}
function showRequest(formData, jqForm, options) { 
} 
function showResponse(responseText, statusText)  { 
     $.ajax({ type: "GET", url:"js/popin.js", dataType: "script" });
} 