function centerThis(element) {
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var elementHeight = $(element).height();
	var elementWidth = $(element).width();

	var elementTop, elementLeft;

	if (windowHeight <= elementHeight) {
		elementTop = $(window).scrollTop();
	} else {
		elementTop = ((windowHeight - elementHeight) / 2) + $(window).scrollTop();
	}

	if (windowWidth <= elementWidth) {
		elementLeft = $(window).scrollLeft();
	} else {
		elementLeft = ((windowWidth - elementWidth) / 2) + $(window).scrollLeft();
	}

	$(element).css({
		'top': elementTop,
		'left': elementLeft
	});
}

function maxThis(element) {
		$(element).css({
		'height': $(window).height(),
		'width': $(window).width(),
		'opacity': 0.5
	});
}
$(document).ready(function(){
	  /*  centerThis('#pop');
		maxThis('#blackscreen');
		centerThis('#blackscreen');
	  	
	
		$(window).bind('resize', function()
		{
	   centerThis('#pop');
	   maxThis('#blackscreen');
		});
		$(window).bind('scroll', function()
		{
	 	 centerThis('#pop');
	   	  maxThis('#blackscreen');
	     centerThis('#blackscreen');
		});
*/
    
	  	
	  	$('.iframe').hide();
	  	
	/*	$('#blackscreen').click(function(){
		$('#blackscreen').remove();
		$('#pop').remove();
		$('.iframe').show();
		});
		$('#closeid').click(function(){
		$('#blackscreen').remove();
		$('#pop').remove();
		$('.iframe').show();
		});
		$('#pop').fadeIn('slow');*/

		 $('#envoicp').hide();
		 $('#envoicp2').hide();
		/* $('#popupcp').bind('keyup',function()
		 {
		 	//alert($(this).val().length);
		 	if($(this).val().length == 4)
		 	{
			    $(this).autocomplete('/index.php?page=ajax_commune&action=recherche_cp', {
					multiple: false,
					matchSubset: false,
					dataType: "json",
					delay:500,
					max:100,
					highlight:function(value, term){
						return value;
					},
					
					parse: function(data) {
						return $.map(data, function(row) {
							return {
								data: row,
								code_insee: row.code_insee,
								nom_commune: row.nom_commune,
								code_postal: row.code_postal
							}
						});
					},
					
					formatItem: function(data, i, total) {
						return data.nom_commune+' ('+data.code_postal+')';
					}
				}).result(function(e, item) {
					 $('#popupcp').val(item.nom_commune+' ('+item.code_postal+')');
					 $('#popuphidden').val(item.code_insee);
					 $('#envoicp').show();
				});
			}
		});
		
		$('#popupcp').click(function(){
			if($(this).val()=='code postal')
				$(this).val('');
		});*/
		
		$('#departement').bind('change',function()
		{
			$.get('/index.php?page=ajax_commune&action=getCodePostaux&dpt='+$('#departement').val(),function(data){
					$('#pcodepostal').html('<select name="code_postal" id="code_postal">'+data+'</select>');
				}
			);
		}
		);
		
});


$('#code_postal').live('change',function()
{
	$.get('/index.php?page=ajax_commune&action=getCommunes&code_postal='+$('#code_postal').val(),function(data){
			$('#pcommune').html('<select name="commune" id="commune">'+data+'</select>');
		}
	);
}
);

$('#commune').live('change',function()
{
	$('#popuphidden').val($(this).val());
	$('#popuphiddendepot').val('false');
	$('#envoicp').show();

	$('#popup_retrait').css({'visibility':'hidden'});
	
}
);

$('#depot').live('change',function()
{
	$('#popuphidden').val($(this).val());
	$('#popuphiddendepot').val('true');
	$('#envoicp2').show();
	
	$('#popup_livraison').css({'visibility':'hidden'});
}
);
