/* Coin arrondis */


str_length = 26;
suggestion = true;
	function lookup(inputString) {
		if(inputString.length == 0 || inputString.length == 1 && suggestion) {
			$('#suggestions').hide();
		} else {
			if(inputString.length <= str_length) {suggestion = true;}
			if(str_length > 0 && suggestion) {
			$.get(fw_uri+'/'+$('html').attr('lang')+"/search/autocompletion/index."+inputString+".html", {/*variable : ""+valeur+""*/}, function(data){
				if(data.length == 0) {
					$('#suggestions').hide();
					suggestion = false;
					str_length = inputString.length;
				}
				if(data.length >1) {
					n = -1;
					suggestion = true;
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
			}	
		}
	} 
	
	function fill(thisValue) {
		$('#search').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}

$(document).ready(function(){

/* Deals */
var COOKIE_NAME = 'pl_ville';
if($.cookie(COOKIE_NAME) == null ) {
     $.cookie(COOKIE_NAME, $('#form_popupdeal').find('#villes_id').val() , { expires: 10 });
     showOverlay();
	 showPopup();	
   }
  
$('#dejamembre').click(function(){
	hidePopup();
	hideOverlay();
});

$('#close_ajax').click(function(){
	hidePopup();
	hideOverlay();
});

/** Submit sur le formulaire */
$('#form_popupdeal').live('submit',function(){
		var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/deals/checkpopup/',
						data : s,
						success : function(msg){						
							
							if(msg == "true")
							{
								var ville = $('#form_popupdeal').find('#villes_id').val();
								window.location = fw_uri+'/'+$('html').attr('lang')+'/deals/index.html?villes_id='+ville+'&n=1';
							}
							else
							{
								$('#form_popupdeal #error').append(msg);
							} 
						},
						complete : function() {
							//refreshpro();
						}
					});
	
	
	
	return false;
	});
	
	
	
   

/* Account */

$('#bloc_photo').hover(function(){$('#update_photo').fadeIn();});

$('#bloc_photo').mouseleave(function(){$('#update_photo').fadeOut();});


	
//	$('form .bloc').corners("5px");


	// Pub
	
	if ($('#header #pub_top').length > 0) {
		
		var classe = $('#header #pub_top').attr('class').split('_');
		if(classe[0] == "dep")
		{
			var departements_id = classe[1];
		}
		if(classe[0] == "reg")
		{
			var regions_id = classe[1];
		}
//	alert('la');
		$.ajax({
			type: 'POST',
			url : fw_uri + '/index.php?lang='+ $('html').attr('lang') +'&p=bannieres&pos=0&departements_id='+departements_id+'&regions_id='+regions_id,
			success : function(msg){
				$('#header #pub_top').append(msg);
			}
		});
	}
	
	if ($('#bannieres_parcsdetail').length > 0) {
		var classe = $('#bannieres_parcsdetail').attr('class').split('_');
		if(classe[0] == "dep")
		{
			var departements_id = classe[1];
		}
		

		$.ajax({
			type: 'POST',
			url : fw_uri + '/index.php?lang='+ $('html').attr('lang') +'&p=bannieres&pos=2&departements_id='+departements_id,
			success : function(msg){
				$('#bannieres_parcsdetail').append(msg);
			}
		});
	}
	
	/*if ($('#pub_right #pub').length > 0) {
		$.ajax({
			type: 'POST',
			url : fw_uri + '/index.php?lang='+ $('html').attr('lang') +'&p=bannieres&pos=1',
			success : function(msg){
				$('#pub_right #pub').append(msg);
			}
		});
	}*/

	// Tweet
	$("#tweet").tweet({
            username: "parcsetloisirs",
            join_text: "auto",
            avatar_size: 32,
            count: 1,
            auto_join_text_default: "Parcs&Loisirs a dit,",
            auto_join_text_ed: "Parcs&Loisirs",
            auto_join_text_ing: "Parcs&Loisirs did",
            auto_join_text_reply: "Parcs&Loisirs avons répondu a",
            auto_join_text_url: "we were checking out",
            loading_text: "Chargement des tweets..."
        });
		
	// Shadow box
	Shadowbox.init();








	$("#actions").live("click",function(){
		if($("#depliant").is(":visible"))
		{
			$("#depliant").slideToggle();
		}
		else
		{
			$("#depliant").slideToggle();
		}
		return false;
	
		
	});
	/**
	 * Initialisation du menu
	 * Il y a un bug avec IE6 sur le deuxième niveau
	 */
	$('#menu').find('ul').parent().each(function(){
		var submenu = $(this).find('ul:eq(0)');
		var alink = $(this).find('a:eq(0)');
		$(this).hover(
			function(){
				submenu.fadeIn('fast');
			},
			function(){
				submenu.fadeOut('fast');
			}
		);
	});
	
	/** Vide les champs pré-remplis **/
	$('#mailings_email').focus(function() {
		$(this).val('');
	});
	
	$('#search').focus(function() {
		$(this).val('');
	});
	
	$('#form_search_barre #parcs_ville').focus(function() {
		$(this).val('');
	});
	
	
	/** Auto-completion **/
	$('#search').attr('autocomplete','off');
	n = -1;
	$('#search').live('keyup', function(event) {
		if(event.keyCode == 40) {
 			if(n+1 < $("#autoSuggestionsList > ul > li").length) {
				n = n + 1;
				$("#autoSuggestionsList > ul > li").eq(n).attr('class','hover').focus();
				if(n > 0 && n < $("#autoSuggestionsList > ul > li").length) {
					$("#autoSuggestionsList > ul > li").eq(n-1).removeClass('hover');
				}
			}
		} else if(event.keyCode == 38) {
			if(n > 0) {
				n = n - 1;
				if(0 <= n <= $("#autoSuggestionsList > ul > li").length) {
					$("#autoSuggestionsList > ul > li").eq(n).attr('class','hover').focus();
				}
				if(n >= 0) {
					$("#autoSuggestionsList > ul > li").eq(n+1).removeClass('hover');
				}
			} 
		}
		else {
			if(event.keyCode != 13) {
				lookup($(this).attr('value'));
			}

		}
	});
	
	$('body').keydown(function(event) {
		if(event.keyCode == 13) {
			$('#form_search').submit(function() {
				if($('#autoSuggestionsList > ul > li.hover').length > 0){
					fill($('#autoSuggestionsList > ul > li.hover').eq(0).html());
					$('#autoSuggestionsList > ul > li.hover').removeClass('hover');
					return false;
				} else {
					return true;
				}
			});
		}
	});
	

	$('#search').blur(function() {
		fill();
	});
	$('.suggestionList > ul > li').live('click',function() {
		fill($(this).html());
	});
	
	
	/** Gestion du formulaire login AJAX */
	$('.link_login').live('click', function(){
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/login/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_login').live('submit',function(){
					$('#login_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/login/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	
	$('.link_loginpro').live('click', function(){
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/loginpro/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_login').live('submit',function(){
					$('#login_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/loginpro/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							refreshpro();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	
	/** Gestion du formulaire mot de passe oublié AJAX */
	$('.link_forgotpassword').live('click', function(){
		$('.ajax_box').remove();
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/forgotpassword/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_forgotpassword').live('submit',function(){
					$('#forgotpassword_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/forgotpassword/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							//refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	
	/** Gestion du formulaire mot de passe oublié AJAX */
	$('.link_forgotpasswordpro').live('click', function(){
		$('.ajax_box').remove();
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/forgotpasswordpro/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_forgotpassword').live('submit',function(){
					$('#forgotpassword_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/forgotpasswordpro/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							//refreshpro();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	/** Gestion du formulaire mot de passe oublié AJAX */
	$('.link_validaccount').live('click', function(){
		$('.ajax_box').remove();
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/validaccount/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_validaccount').live('submit',function(){
					$('#validaccount_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/validaccount/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							//refresh();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	
	$('.link_validaccountpro').live('click', function(){
		$('.ajax_box').remove();
		showOverlay();
		showLoading();
		$.ajax({
			type: 'POST',
			url : fw_uri+'/'+$('html').attr('lang')+'/validaccountpro/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();

				/** Submit sur le formulaire */
				$('#form_validaccount').live('submit',function(){
					$('#validaccount_ajax_box').remove();
					showLoading();
					
					var s = $(this).serialize();
					
					$.ajax({
						type: 'POST',
						url :fw_uri+'/'+$('html').attr('lang')+'/validaccountpro/check/',
						data : s,
						success : function(msg){						
							$('body').append(msg);
							hideLoading();
							setTimeout('showOk()',4000);
						},
						complete : function() {
							//refreshpro();
						}
					});
					return false;
				});
			}
		});
	return false;
	});
	/** Gestion d'ajout au panier en AJAX */
	$('.add_basket').live('submit', function(){
		showOverlay();
		showLoading();
		
		var s = $(this).serialize();
		
		$.ajax({
			type: 'POST',
			data : s,
			url : fw_uri+'/'+$('html').attr('lang')+'/basket/addprod/',
			success : function(msg){
				$('body').append(msg);
				hideLoading();
			},
			complete : function() {
				refresh();
			}
		});
	return false;
	});
});
/** Rafraichissement du bloc "mon panier", "mon compte"  **/
function refresh() {
	$('#basket_content').remove();
	showBasketLoading();
	$.ajax({
		type: 'POST',
		url :fw_uri+'/'+$('html').attr('lang')+'/basket/refresh/',
		success : function(msg) {
			$('#espace_pro').remove();
			$('#basket').append(msg);
			hideBasketLoading();
		}
		
	});
}

function refreshpro() {
	$('#basket_contentpro').remove();
	showBasketLoading();
	$.ajax({
		type: 'POST',
		url :fw_uri+'/'+$('html').attr('lang')+'/basketpro/refresh/',
		success : function(msg) {
			$('#basket').remove();
			$('#espace_pro').remove();
			$('#basketpro').append(msg);
			hideBasketLoading();
		}
	});
}
/** Ferme le bloc ok après quelques secondes et cache l'overlay **/
function showOk() {
	if($('#ok_box').length>0) {
		$('#ok_box').fadeOut('slow');
		hideOverlay();
	}
}
/** Supprime le bloc AJAX **/
$('.close_ajax_box').live('click',function(){
	$(this).parents('.ajax_box').remove();
	hideOverlay();
});









/** Fonctions d'affichage / masquage des loaders et de l'overlay **/
function showOverlay() 		{ $("#overlay").css({'display': 'block','opacity': 0.4});}
function showLoading() 		{ $('#loading').show()}

function showBasketLoading(){ $('#loading_basket').show()}
function showFiltreLoading(){ $('#filtre_loading_ajax').show()}

function hideOverlay() 		{ $("#overlay").css({'display': 'none'});}
function hideLoading() 		{ $('#loading').hide()}
function hideBasketLoading(){ $('#loading_basket').hide()}
function hideFiltreLoading(){ $('#filtre_loading_ajax').hide()}

function showPopup() 		{ $('#popupdeals').show()}
function hidePopup() 		{ $('#popupdeals').hide()}


