// JavaScript Document

lang = $('html').attr('xml:lang');

function backgroundPosition() {
	$('body .wrap').css('backgroundPosition', ($('.content').offset().left-447)+'px 0' );
	$('.colors').remove();
	$('.wrap').append(
		$('<img>').attr('src','/uploads/images/background/colors.gif').addClass('colors').css({
			left: ($('.content').offset().left+734)																		  
		})
	);
}

function navigation(){
	// Menu principal
	$('.menu ul ul').each(function(){
		$(this).width( $(this).find('li').size()*163 );
	});
	
	$('.menu > ul > li').mouseenter(function(){
		$(this).find('a:first').addClass('hover');
		$(this).addClass('hover').find('ul').show();
	});
	$('.menu > ul > li').mouseleave(function(){
		 $(this).find('a:first').removeClass('hover');
		$(this).removeClass('hover').find('ul').hide();
	});
	
	// Navigation interne page statique (fleche de droite)
	if ( $('.static_menu').size() > 0 ) {
		indexPage = $('.static_menu li.active:last').index('.static_menu li');
		nbPage = $('.static_menu li').size();
		
		if( (indexPage+1) == nbPage ) {
			hrefLink = $('.static_menu li:first a').attr('href');
		} else {
			hrefLink = $('.static_menu li:eq('+(indexPage+1)+') a').attr('href');
		}
		
		$('.container').append(
			$('<a>').addClass('page_suivante').attr('href',hrefLink).html(
				$('<img>').attr('src','/uploads/images/fleche_suivante.png')
			)
		);
	}
}

function homepage(){
	$('#choix_type_presta').submit(function(){
		type_presta = $(this).find(':input[name=type_presta] option:selected').val();
		// IF type prestataire n'a pas de critere, la page est un lien de redirect
		if (lang == 'fr') {
			window.location.href = '/'+lang+'/rechercher-un-professionnel/'+type_presta+'.html'; 
		} else {
			window.location.href = '/'+lang+'/find-a-professionnal-in-lille-metropole/'+type_presta+'.html'; 
		}
		return false;
	});
}

function footer() {
	$('.menu_footer li a:last').click(function(){
		$('<div>').attr('id','div_credit').insertAfter('.footer');
		if (lang == 'fr') {
			$('#div_credit').load('/fr/credits.html #credit', function(){
				$('#div_credit').prepend( 
					$('<a>').addClass('close_credit').html(
						$('<img>').attr('src','/uploads/images/background/bg_filtre_1.jpg')
					)
				);
				$('.close_credit').click(function(){
					$('#div_credit').remove();
				});
			});
		} else {
			$('#div_credit').load('/en/copyright.html #credit', function(){
				$('#div_credit').prepend( 
					$('<a>').addClass('close_credit').html(
						$('<img>').attr('src','/uploads/images/background/bg_filtre_1.jpg')
					)
				);
				$('.close_credit').click(function(){
					$('#div_credit').remove();
				});
			});
		}
		return false;
	});
}

function ficheProduit(){
	$('.autres_photos').click(function(){
		$('.slider a:first').trigger('click');
		return false;
	});
	$('.tableau_salle tr:odd').css('background','#eee');
	
	$('.descriptif .onglet span').click(function(){
		$('.descriptif .onglet span').removeClass('active');
		$(this).addClass('active');
		ongletId = $('.descriptif .onglet span').index($(this));
		$('.descriptif .contenu p').removeClass('active');
		$('.descriptif .contenu p:eq('+ongletId+')').addClass('active');
	});
	$('.info_adherent .onglet img').click(function(){
		$('.info_adherent .onglet img').removeClass('active');
		$(this).addClass('active');
		$('.info_adherent .onglet img').each(function(){
			$(this).attr('src',$(this).attr('src').replace('_on','_off'));					  
		})
		$(this).attr('src',$(this).attr('src').replace('_off','_on'));
		ongletId = $('.info_adherent .onglet img').index($(this));
		$('.info_adherent .contenu p').removeClass('active');
		$('.info_adherent .contenu p:eq('+ongletId+')').addClass('active');
	});
	
	$('.bouton_fiche .voir_salles').click(function(){
		$('.tableau_salle table').width(730);
		htmlSalle = '<div class="tableau_salle">';
		htmlSalle += $('.tableau_salle').html();
		htmlSalle += '</div>';
		$('.wrap').popup({
			html: htmlSalle
		});
		$('.popup').height('auto');
		return false;
	});
	$('.bouton_fiche .voir_lieux').click(function(){
		$('.tableau_lieux_associes').width(730);
		htmlLieux = '<div class="tableau_lieux_associes">';
		htmlLieux += $('.tableau_lieux_associes').html();
		htmlLieux += '</div>';
		$('.wrap').popup({
			html: htmlLieux
		});
		$('.popup').height('auto');
		return false;
	});

	
	if( $('.info_adherent .contenu p:first a').text() == "" ) {
		$('.info_adherent .contenu p:first, .info_adherent .onglet img:first').remove();
	}
	
	if ( (lang == 'en')&&($('.bouton_fiche .retour_critere').size() > 0) ) {
		
		critere_url = $('.bouton_fiche .retour_critere').attr('href');
		var new_url = "";
		
		if ( critere_url.indexOf('grands-equipements') != -1 ) { new_url = "main-facilities";	} 
		if ( critere_url.indexOf('salles-de-reunion') != -1 ) { new_url = "meeting-rooms";	} 
		if ( critere_url.indexOf('lieux-d-exception') != -1 ) { new_url = "exceptional-sites";	} 
		if ( critere_url.indexOf('lieux-de-reception') != -1 ) { new_url = "reception-sites";	} 
		if ( critere_url.indexOf('prestataires-de-services') != -1 ) { new_url = "service-providers";	} 
		if ( critere_url.indexOf('vie-nocturne') != -1 ) { new_url = "night-life";	} 

		if( new_url != "") {
			$('.bouton_fiche .retour_critere').attr('href', '/en/find-venues-and-services/search-'+new_url+'.html');
		}
	}
	
	$('.retour_resultat').click(function(){
		history.back();
		return false;
	});
	
}

function staticPage() {
	scrollHeight = $(document).height()-$(window).height();
	if ( scrollHeight > 0 ) {
		$('.static_menu').height('auto');
		ecartHauteur = $('.static_menu').height()-(415 - scrollHeight);
		$('.static_menu').height( [ $('.static_menu').height(), (415 - scrollHeight)].sort()[1]);
		
		if( ecartHauteur > 0 ) {
			
			$(window).scroll(function(){
				if ($.browser.msie){
					if( (scrollHeight-$(document).scrollTop())  < ecartHauteur) {
						$('.static_menu').css('margin','-'+(ecartHauteur-(scrollHeight-$(document).scrollTop()) )+'px 25px 0 -194px');
					} else {
						$('.static_menu').css('margin','0 25px 0  -194px');
					}		
				} else {
					if( (scrollHeight-$(document).scrollTop())  < ecartHauteur) {
						$('.static_menu').css('margin','-'+(ecartHauteur-(scrollHeight-$(document).scrollTop()) )+'px 25px 0 0');
					} else {
						$('.static_menu').css('margin','0 25px 0 0');
					}	
				}
						
			});
		}
	}
	
	if( ($('.city.active').size() > 0)&&(window.location.search == "?from=menu") ) {
		getCarteVille();
	}
	
	$('.retour_carte').click(function(){
		getCarteVille();
		return false;
	});
}

function getCarteVille(){
	
	$('.map_arrow_bottom').live('click',function(){
		currentTop = parseInt($('.carte').css('top'));
		if( currentTop > -384 ) {
			$('.carte').css('top', currentTop-96);
			currentTop -= 96;
		} 
		$('.map_arrow_top').fadeTo(50,1);
		if( currentTop == -384 ) {
			$(this).fadeTo(50,0.6);
		}
	});
	$('.map_arrow_top').live('click',function(){
		currentTop = parseInt($('.carte').css('top'));
		if( currentTop < 0 ) {
			$('.carte').css('top', currentTop+96);
			currentTop += 96;
		} 
		$('.map_arrow_bottom').fadeTo(50,1);
		if( currentTop == 0 ) {
			$(this).fadeTo(50,0.6);
		}
	});
	
	if (lang == 'fr') {
		$('<div>').load('/fr/popup-carte-interactive.html #html_popup_carte', function(data){
			htmlMap = ($(this).html())
			$('body').popup({
				html: htmlMap
			});
			$('.carte').css('top', -192);
			$('.map_arrow_top').fadeTo(50,1);
		});
	} else {
		$('<div>').load('/en/popup-interactive-map.html #html_popup_carte', function(data){
			htmlMap = ($(this).html())
			$('body').popup({
				html: htmlMap
			});
			$('.carte').css('top', -192);
			$('.map_arrow_top').fadeTo(50,1);
		});
	}
}

function autoScroll() {
	if( $('.bloc_actu ul li').size() > 1 ) {
				
		$('.bloc_actu').unbind();
		$('.bloc_actu').bind('mouseenter', function(){
			$('.bloc_actu ul').stop(true);
		});
		$('.bloc_actu').bind('mouseleave', function(){
			autoScroll();
		});
		
		
		currentPos = parseInt($('.bloc_actu ul').css('top'));
		tempTime = currentPos*7500/(-145);
		time = 7500 - tempTime;
		$('.bloc_actu ul').animate({top:'-145px'},time,function(){
			$('.bloc_actu ul li:first').detach().appendTo($('.bloc_actu ul'));
			$('.bloc_actu ul').css('top',0);
			autoScroll();
		});
	}
}
function makeSlide() {
	if( $('.bloc_actu').height() < $('.bloc_actu>:first').height() ) {
		
		$('<div>').addClass('slider_scroll').height($('.bloc_actu').outerHeight()).prependTo('.bloc_actu');
		$('<img>').addClass('slider_top').attr('src','/uploads/images/slider_arrow_top.jpg').prependTo('.slider_scroll');
		$('<img>').addClass('slider_bottom').attr('src','/uploads/images/slider_arrow_bottom.jpg').prependTo('.slider_scroll');
		
		$('.slider_top').mouseenter(function(){
			count_slider();
			$('.bloc_actu>:first-child').next().stop().animate( { top:0 } , vitesse_up );
		});
		$('.slider_bottom').mouseenter(function(){
			count_slider();
			$('.bloc_actu>:first-child').next().stop().animate( { top: -hauteur	} , vitesse_down );
		});
		$('.slider_top, .slider_bottom').mouseleave(function(){
			$('.bloc_actu>:first-child').next().stop();
		});
	}
}
function count_slider() {
	
	//!\\ Si aucune valeur de top n'est attribuée IE renvoie 0. Préciser top:0, dans le css pour un bon fonctionnement 
	
	hauteur = $('.bloc_actu>:first-child').next().outerHeight() - $('.bloc_actu').height();
	vitesse_up = ( parseInt( $('.bloc_actu>:first-child').next().css("top")) )*6;
	vitesse_down = ( $('.bloc_actu>:first-child').next().height() - parseInt( $('.bloc_actu>:first-child').next().css("top")) )*6;
		
	if (vitesse_up < 0 ) vitesse_up =  vitesse_up*-1;
	if (vitesse_down < 0 ) vitesse_down =  vitesse_down*-1;
}

function listPagination() {
	
	var page_en_cours = 1;
	var nbParPage = 8;
	
	$('.liste_resultat li').each(function(){
		elementPage = Math.floor($(this).index()/nbParPage);
		$(this).addClass('page_'+(elementPage+1))
	});
	$('.liste_resultat li:not(.page_1)').hide();
	
	nbElement = $('.liste_resultat li').size();
	if( Math.floor(nbElement/nbParPage) == (nbElement/nbParPage) ) {
		nbPage = Math.floor(nbElement/nbParPage); 
	} else {
		nbPage = Math.floor(nbElement/nbParPage)+1; 
	}
	
	for( i=2; i<=nbPage; i++) {
		$('<a>').attr('href','#').html(i).insertBefore('.pagination a:last')
	}
	
	if( nbPage == 1 ) {
		$('.pagination').hide();
	
	} else {
	
		if (lang == 'fr') {
			libelleVoirTout = 'Voir tous les prestataires';
			libelleRetourPagination = 'Revenir &agrave; la pagination';
		} else {
			libelleVoirTout = 'See all';
			libelleRetourPagination = 'Back to pages';
		}
		
		voir_tout = $('<a>').attr('href','#').addClass('voir_tout').html(libelleVoirTout);
		$('.pagination').prepend(voir_tout);
	
		$('.pagination .voir_tout').live('click',function(){
			$('.liste_resultat').height('auto');
			$('.liste_resultat li').show();
			$('<a>').attr('href','#').html(page_en_cours).insertAfter('.pagination span');
			$('.pagination span').remove();
			$('.pagination *:not(.voir_tout)').hide();
			$(this).removeClass('voir_tout').addClass('retour_pagination').html(libelleRetourPagination);
			return false;
		});
		
		$('.pagination .retour_pagination').live('click',function(){
			$('.liste_resultat').height(370);
			$('.pagination *:not(.voir_tout)').show();
			$(this).removeClass('retour_pagination').addClass('voir_tout').html(libelleVoirTout);
			$('.pagination a:contains(1)').trigger('click');
			return false;
		});
		
		$('.pagination a:not(.voir_tout,.retour_pagination)').live('click',function(){
			
			$('<a>').attr('href','#').html(page_en_cours).insertAfter('.pagination span');
			$('.pagination span').remove();
			
			if ( $(this).is('.pagination_suivant') ) {
				page_en_cours++;
				$('.liste_resultat li').hide();
				$('.liste_resultat li.page_'+page_en_cours).show();
				
			} else if ( $(this).is('.pagination_precedent') ) {
				page_en_cours--;
				$('.liste_resultat li').hide();
				$('.liste_resultat li.page_'+page_en_cours).show();
			
			} else {
				page_en_cours = $(this).html();
				$('.liste_resultat li').hide();
				$('.liste_resultat li.page_'+page_en_cours).show();
			}
			
			if ( page_en_cours > 1 ) {
				$('.pagination .pagination_precedent').show();	
			} if ( page_en_cours < nbPage ) {
				$('.pagination .pagination_suivant').show();
			} if ( page_en_cours == 1 ) {
				$('.pagination .pagination_precedent').hide();	
			} if ( page_en_cours == nbPage ) {
				$('.pagination .pagination_suivant').hide();
			}
			
			$('<span>').html(page_en_cours).insertAfter('.pagination a:contains('+page_en_cours+')');
			$('.pagination a:contains('+page_en_cours+')').remove();
			
			return false;
		});
	}	
}

function tracking() {
	$('.aside_link a:eq(1)').click(function(){
		_gaq.push(['_trackEvent', 'PDF', 'Open']);
	});
	
	$('.info_adherent .onglet img').click(function(){
		_gaq.push(['_trackEvent', 'Fiche_adherent', $(this).attr('alt'), $(this).closest('.entete_adherent').find('h2').text() ]);
	});
}



function afficheDate()// fonction pour afficher la date et l'heure
{
    mois = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

    dateNow = new Date();
    dateannee = dateNow.getFullYear();
    datejour = dateNow.getDate();
    heure = dateNow.getHours();
    minute = dateNow.getMinutes();
    if (dateNow.getDate() < 10) {
		datejour = "0" + datejour;
	}
    if (heure < 10) heure = "0" + heure;
    if (minute < 10) minute = "0" + minute;
    
	$('#date').html(datejour + "/" + mois[dateNow.getMonth()] + "/" + dateannee + " &nbsp;#&nbsp; " + heure + ":" + minute + " &nbsp;#&nbsp; ");
   
	setTimeout("afficheDate()", 60000);
}

function meteo() {
	$.simpleWeather({
		location: 'Lille, FR',
		unit: 'c',
		success: function (weather) {
			html = weather.temp+'&deg; '+weather.units.temp;
			html += '<img src="'+weather.thumbnail+'">';
			$('.date_meteo').append(html);
		}
	});
}

function formClass() {
	$(":text").addClass("input-text"); 
	$(":radio").addClass("input-radio");
	$(":image").addClass("input-image");
	$(":checkbox").addClass("input-checkbox");
}

function phototheque(){
	if( $('#select_theme_photo').size() > 0 ) {
		$('#select_theme_photo').change(function(){
			$('ul.gallery').hide();
			if( $(this).val() != "" ) {
				$('ul.gallery.'+$(this).val()).show();
			}
		});
	
	
		$('.btn_photo:first').click(function(){
			$('body').popup({
				html: $('.saisie_mdp').html()
			});
			$('.popup').height('auto');
			$('.popup').width(500);
			$('.popup').css('margin','0 0 0 -250px');
			
			
			dateNow = new Date();
			dateannee = dateNow.getFullYear();
			dateannee = dateannee.toString().split("").reverse().join("");
			mdp = "pass_photo*"+dateannee;

			$('form[name=saisie_mdp]').submit(function(){
				$(this).find('.erreur').remove();
				
				if( $('.popup input[name=mdp_photo]').val() == mdp ) {
				
					window.open('/uploads/images/phototheque/phototheque.zip');
					$('.close_popup').trigger('click');
				
				} else {
					if (lang == 'fr') {
						$('<li>').addClass('erreur').html('Mot de passe incorrect').insertBefore('form[name=saisie_mdp] .validation');
					} else {
						$('<li>').addClass('erreur').html('Wrong password').insertBefore('form[name=saisie_mdp] .validation');
					}
				}
				return false;
			});
			return false;
		});
		$('.btn_photo:last').click(function(){
			$('.content .demande_mdp').show();
			$('.content p, .content .gallery').hide();
			return false;
		});
		
		if( $('.demande_mdp .champ_erreur').size() > 0 ) {
			$('.content .demande_mdp').show();
			$('.content p, .content .gallery').hide();	
		}
		if( $('.valid_phototheque').size() > 0 ) {
			$('.valid_phototheque').show();
			$('.content p, .content .gallery').hide();	
		}
		
	}
}

function centraleReservation(){
	
	// Gestion du scrolling
	var wrapHeight = $('.reservation_centrale .wrap').height();	
	var contentHeight = $('.reservation_centrale .content').height();	
	if( wrapHeight > (contentHeight+109)) {
		$('.reservation_centrale .content').height(wrapHeight-109);
	} else {
		$('.reservation_centrale .wrap').height(contentHeight+109);
	}
	
	// Champ dates à vider
	$('#date_arrivee,#date_depart').focus(function(){$(this).val('')});
	
	
}

(function ($) {
	//Dfinition du plugin
	$.fn.popup = function(options) {
		
		var defaults = {
			html: '',
			bgColor: '#fff'
		}
		var opt = $.extend(defaults, options);
		
		bgFilter = $("<div>").addClass('bg_filter').width($(document).width()).height($(document).height()).css({
			background: opt.bgColor,
			position: 'absolute',
			top: 0,
			left: 0,
			zIndex: 100,
			opacity: 0.6
		});
		
		PopupContainer = $("<div>").addClass('popup').css({
			top: $(document).scrollTop() + 100
		}).html(opt.html).append('<a href="#" class="close_popup"><img src="/uploads/images/background/bg_filtre_1.jpg" alt="" /></a>');
		
		PopupContainer.find('.close_popup').click(function () {
			bgFilter.fadeOut(500, function () {
				$(this).remove();
			});
			PopupContainer.fadeOut(500, function () {
				$(this).remove();
			});
			return false;
		});
		
		
		
	function popup() {
		bgFilter.appendTo(this);
		PopupContainer.appendTo(this);
	}
		
	$(this).each(popup);
		
	// interface fluide
	return $(this);
};
})(jQuery);
