
/*var JQY = jQuery.noConflict();*/
var maxH = 0;
var lockV = 0;
var lockColor = '#ccc';
$(document).ready(function() {					   
  	
	generatePictedImg('.content img');
	generateQuotedtxt('.bloc-temoignage .citation');
	verticalAlignCenter('#submenu2 ul li a');
	verticalAlignLink('a.bloc-link-more');
	verticalAlignBloc('.inside-bloc-tiers-page .inside-content');
	
	equalizeBlocks('.inside-bloc-tiers-page .inside-content');
	equalizeBlocks('.inside-bloc-tiers-page .csc-textpic-text');
	equalizeBlocks('.bloc-demi-page .inside-content ul');
	equalizeBlocksImg('.inside-bloc-normal .picto-image');
	listenSearchInput('.agency-search input');
	centerElementsByParent('.bloc-demi-page .global-btn');
	centerHElementsByParent('.onglet-agence li','h2',4);
	centerHElementsByParent('.mainmenu ul li a','span',0, 'margin-top');
	centerVertical('.inside-bloc-partenaire .picto-image');
	resizeHoraires('.horaires-container table');
	initOnglets('ul.onglet-agence li','.content-container');
	initFormDevis();
	
	addConseilTitle('.bloc-conseil');
	
	$('a.demande-devis, a.espace-affilies-link, a.fancy').fancybox({'titleShow'		:	false});
	
	jQuery.datepicker.setDefaults(jQuery.datepicker.regional['fr']);
	$('.date_picker').datepicker({
		dateFormat:'dd/mm/yy'/*,
		changeMonth: true,
		changeYear: true*/
	});
	
	//openAccordion();
	
});

//ouvre tjs le premier élément de l'accordéon par défaut
function openAccordion()
{
	$('.tx-jfmulticontent-pi1 .ui-accordion .ui-accordion-header:first a').trigger('click');	
}

function addConseilTitle(cible)
{
	$(cible).each(function(){
		var elem = $(this);
		elem.prepend('<h3>Le conseil ROC-ECLERC</h3>');
	});
}

function centerVertical(cible)
{
	var maxHeight = 150;
	
	$(cible).each(function(){
		var elem = $(this);
		var elemHeight = elem.height();
		
		var paddingTop = (parseInt(maxHeight) - parseInt(elemHeight)) / 2;
		var newHeight = parseInt(maxHeight) - parseInt(paddingTop);
		
		elem.css('padding-top', paddingTop);
		elem.css('height', newHeight);
	});
}

function verticalAlignBloc(cible)
{
	var maxHeight = 0;
	$(cible).each(function(){
		var elem = $(this);
		var elemHeight = elem.height();
		
		if(elemHeight >= maxHeight)
			maxHeight = elemHeight;
	});
	
	$(cible+' .bodytext a.bloc-link-more').each(function(){
		var elem = $(this);
		var container = elem.parent().parent();
		
		//vérifie s'il y a un élément image
		var containerImg = container.children(':first').children(':first').length;
		
		var containerHeight = container.height();
		
		var marginTop = maxHeight - containerHeight + 3;
		
		if(marginTop > 0 && containerImg > 0)
			elem.css('margin-top', marginTop);
		
	});
}

function verticalAlignLink(cible)
{
	$(cible).each(function(){
		var elem = $(this);
		var newHeight = 30;
		
		var elemHeight = elem.height();
		
		if (elemHeight == 0)
			return;
		var container = elem.parent();
		var paddingTop = (Math.floor((container.height() - elemHeight) / 2) + 6);
		
		if (paddingTop > 0 && elemHeight < 21)
		{
			elem.css('padding-top', paddingTop);
			elem.css('height', 30 - paddingTop + 5);
		}
	});
}

function verticalAlignCenter(cible)
{
	$(cible).each(function(){
		var elem = $(this);
		
		var elemHeight = elem.height();
		if (elemHeight == 0)
			return;
		var container = elem.parent();
		var marginTop = (Math.floor((container.height() - elemHeight) / 2) - 7);
		if (marginTop > 0)
			elem.css('margin-top', marginTop);
	});
}


function initFormDevis()
{
	var tab_source_radio = {
		0 : '#def-cremation',
		1 : '#cpo-inhumation',
		2 : '#cpo-cremation',
		3 : '#cpo-choix-vol',
		4 : '#cpo-gravure',
		5 : '#cpo-periodique',
		6 : '#gravure-oui'}
	
	var tab_source_select = {
		0 : '#cpo-cav',
		1 : '#materiau',
		2 : '#cpo-gravure'}
	
	$.each(tab_source_radio, function(key, id){ 
		if($(id).attr('checked'))
			$(id+'-detail').show();
		else
			$(id+'-detail').hide();
	});
	
	$.each(tab_source_select, function(key, id){
		if($(id).val() == 1)
			$(id+'-detail').show();
		else
			$(id+'-detail').hide();
	});
	
	$('input[type=radio]').bind('click', function(){
		$.each(tab_source_radio, function(key, id){ 
			if($(id).attr('checked'))
				$(id+'-detail').show();
			else
				$(id+'-detail').hide();
		});
	});
	
	$('select').bind('change', function(){
		$.each(tab_source_select, function(key, id){ 
			if($(id).val() == 1)
				$(id+'-detail').show();
			else
				$(id+'-detail').hide();
		});
	});
}

function initGMaps(tab_gps, zoom, detail)
{
	var detail = detail || false;
	var infoWindow = null;
	
	var latlng = new google.maps.LatLng(tab_gps[0].lat, tab_gps[0].lon);
	
	if(zoom < 0)
		var bounds = new google.maps.LatLngBounds();
	
	var options = {
			center: latlng,
			zoom: zoom,
			//streetViewControl: false,
			disableDefaultUI: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var carte = new google.maps.Map(document.getElementById('carte'), options);
	
	$.each(tab_gps, function(key, value){
		var latlng = new google.maps.LatLng(value.lat, value.lon);
		//var icon = new google.maps.MarkerImage('/assets/images/logo-re-footer.png');
		var marqueur = new google.maps.Marker({
				position: latlng,
				map: carte,
				//icon : icon,
				title: value.nom
		});
		
		if(zoom < 0)
		{
			bounds.extend(latlng);
			carte.fitBounds(bounds);
		}
		
		if(detail)
		{
			var contentMarker = value.nom+'<br/>'+value.adr;			
			google.maps.event.addListener(marqueur, 'click', function() {
				if(!infoWindow)
				{
					infoWindow = new google.maps.InfoWindow({
						content  : contentMarker,
						position : latlng
					});
					
					infoWindow.open(carte, marqueur);
				}
				else
				{
					infoWindow.setContent(contentMarker);
		            infoWindow.setPosition(latlng);
					infoWindow.close;
					infoWindow.open(carte, marqueur);
				}
			});
		}
	});
	
	
}

function initOnglets(source, cible)
{
	onglet = 1;
	$(cible).each(function(){
        onglet = (parseInt($(this).attr('id')) > onglet) ? parseInt($(this).attr('id')) : onglet;
    });
	
	$(cible).hide().removeClass('active');
	//$(cible+':first').show().addClass('active');
	$(cible+'-'+onglet).show().addClass('active');
	$(source).removeClass('active');
	$(source+'#item'+onglet).addClass('active');	
	$(source+' a').bind('click',function(){
		$(source).removeClass('active');
		$(source+'#item'+$(this).attr('value')).addClass('active');	
		$(cible).hide().removeClass('active');
		$(cible+'-'+$(this).attr('value')).show().addClass('active');
	});	
}

function resizeHoraires(cible)
{
	$(cible+' tr td').each(function() {
		if(!$(this).hasClass('day'))
		{
			//css_width = parseInt($(this).attr('colspan')) * 5;
			//$(this).attr('width',parseInt($(this).attr('colspan')) * 20 + '');	
			//$(this).css('width', css_width);
		}	
	});	
}

function centerHElementsByParent(cible, children, majoration, mode)
{
	if(typeof mode == 'undefined')
		mode = 'margin-top';
	$(cible).each( function(){
		$(this).children(children).css(mode,(parseInt(($(this).height() - $(this).children(children).height())/2) + parseInt(majoration))+'px');
	});	
}

function centerElementsByParent(cible)
{
	$(cible).each(function(){
		$(this).css("margin-left",parseInt(($(this).parent().width() - $(this).width())/2 )+"px");	
	});	
}

function generateQuotedtxt(cible)
{
	$(cible).each( function(){
		$(this).wrap('<div class="temoin" />');
		$(this).parent().prepend('<span class="quote-start-blue">&nbsp;</span>');
		$(this).parent().append('<span class="quote-end-blue">&nbsp;</span>');
	});	
}

//les images ciblées comportent un picto en bas à droite comme sur la home
function generatePictedImg(cible)
{
	$(cible).each( function(){
		$(this).wrap('<span class="picto-image" />');
		$(this).parent().append("<span>&nbsp;</span>");	
	});	
}

//harmonise la hauteur des images en fonction du texte
function equalizeBlocksImg(cible)
{
	var newHeight = 0;
	$(cible).each( function(){
		var elem = $(this);
		var container = elem.parent().parent();
		
		newHeight = container.height();
		
		elem.css('height', newHeight);
	});
}

//harmonise la hauteur de plusieurs blocs
function equalizeBlocks(cible)
{
	maxH = 0;
	$(cible).each( function() {
		if(parseInt($(this).height()) > maxH)
			maxH = parseInt($(this).height());
	}).css('height',maxH);
}

//sauvegarde la valeur par défaut et vide lors du clic
function listenSearchInput(cible)
{	
	lockV = $(cible).val();
	lockColor = $(cible).css('color');
	$(cible).bind('click', function() {
		if($(this).val() == lockV)
		{
			$(this).css('color','#D0504F');	
			$(this).val('');
		}	
	});	
	
	$(cible).bind('blur', function() {
		if($(this).val() == '')
		{
			$(this).val(lockV);
			$(this).css('color',lockColor);
		}
		
	});
}

