//Needs Prototype (prototype-1.5.1.1.js)
//Gestion d'onglets avec paneaux à afficher
// Utilisation: Creation de différent TabbedPanel => new TabbedPanel(idDeLOnglet,idDuPaneauLié)
// Creation de TabbedPanelManage avec en parametre un tableau des différent TabbedPanel
// utilisation si necessaire de la méthode changePanel(TabbedPanelAAfficher), par exemple pour initialisation
// Exemple:
// var photoPanel = new TabbedPanel("photo_onglet","photo_panel");
// var videoPanel = new TabbedPanel("video_onglet","video_panel");
// searchPanelManagement = new TabbedPanelManage([photoPanel,videoPanel]);
// searchPanelManagement.changePanel(photoPanel);
TabbedPanelManage = Class.create();
Object.extend(TabbedPanelManage.prototype,{
	ongletList:{},
	initialize: function(ongletsList) {
		this.ongletList = $A(ongletsList);
		this.ongletList.each(function(onglet){
			if(onglet.onglet != null)
				Event.observe(onglet.onglet,"click",this.changePanel.bind(this,onglet))
		}.bind(this));
	},
	changePanel:function(activeOnglet){
		this.ongletList.each(function(onglet){
			if(onglet == activeOnglet)
				{	Element.addClassName(onglet.onglet,"actif");
					if(onglet.panelToShow != null) onglet.panelToShow.show();
					if(onglet.onClickFunction != null) onglet.onClickFunction();	}
			else
				{	Element.removeClassName(onglet.onglet,"actif");
					if(onglet.panelToShow != null) onglet.panelToShow.hide();	}
		});
	}
});
 
TabbedPanel = Class.create();
Object.extend(TabbedPanel.prototype,{
	onglet:null,
	panelToShow:null,
	onClickFunction:null,
	initialize: function(ongletId,layoutId,onClickFn) {
		this.onglet = $(ongletId);
		this.panelToShow = $(layoutId);
		if(onClickFn != null)
			this.onClickFunction = onClickFn;
	}
});

RollOverManagement = Class.create();
Object.extend(RollOverManagement.prototype,{
	classToIgnore:null,
	classOnOver:null,
	initialize: function(contenerId,targetMatch,classOnOver,classToIgnore) {
		this.classOnOver = classOnOver;
		this.classToIgnore = typeof classToIgnore == "string" ? classToIgnore : null;
		$(contenerId).getElementsBySelector(targetMatch).each(
			function(currentNode){
				Event.observe(currentNode,"mouseover",this.onOver.bind(this,currentNode));
				Event.observe(currentNode,"mouseout",this.onOut.bind(this,currentNode));
			}.bind(this));
		},
	onOver:function (node)
	{
	if(this.classToIgnore == null || !node.hasClassName(this.classToIgnore))
		node.addClassName(this.classOnOver);
	},
	onOut:function (node){node.removeClassName(this.classOnOver);}
});



function afficherAvis(id){
	if(document.getElementById('texte_entier_'+id).style.display=="none") {
		document.getElementById('texte_court_'+id).style.display="none";
		document.getElementById('texte_entier_'+id).style.display="block";
	}
	else {
		document.getElementById('texte_entier_'+id).style.display="none";
		document.getElementById('texte_court_'+id).style.display="block";
	}
}

function openPopUp(url){
		var mywindow = window.open(url,'mywindow','width=620,height=620,toolbar=no, location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes, resizable=yes');
		mywindow.focus()
	}
function selectArticle(clefArticle)
{
	/*
$('tarifTable').select('TD.selectable').each(function(currentTD){
	currentTD.removeClassName('selectedArticle');
	if(currentTD.getAttribute("id").indexOf("tdArticle_"+clefArticle) != -1)
		{
		currentTD.addClassName('selectedArticle');
		}			
	});*/

	$('clefArticle').value = clefArticle;
	try
	{
		window.xt_med('F','2','Fiche_pdt::jourCalendrier','N');
	}
	catch(e){	
	}
	$('formulaireTarifParams').submit();
	//$('submitDevisBouton').show();

}
function changeArticleParam(element)
{	
	placeWaitingLayer();
	try
	{
		window.xt_med('F','2','Fiche_pdt::paramCalendrier','N');
	}
	catch(e){
	}		
	new Ajax.Updater({success : window.parent.document.getElementById('articles')}, '/spring/zarticle/onChange_calendar/?', {parameters: { produit: $F('codePareoGenerique'), selectionId: $F('selectionId'), villeDepart: $F('villeDepart'), mois: $F('mois'), duree: $F('duree'), optionChangee: element.id, dateDepart: $F('dateDepartChoisie')}, evalScripts: true } );
	//window.parent.xt_med('C','2','Fiche_pdt::paramCalendrier','N');
}
function initTableArticle(){
	Event.observe(window,"load",function(){
	document.body.appendChild($('layerInfoVoyage'));
	});
	$('tarifTable').select('TD.selectable').each(function(currentTD){
		var clefArticle = currentTD.getAttribute("id").split("_")[1];
		var clickFn = function(clefArticle){
			selectArticle(clefArticle);
			/*$('layerInfoContenu').update(window.infoArticlesTable["info_"+clefArticle]);			
			position = $('tdArticle_'+clefArticle).cumulativeOffset();
			positionTop = (position.top-$('layerInfoVoyage').getHeight())+'px';
			positionLeft = (position.left-$('layerInfoVoyage').getWidth()/2+$('tdArticle_'+clefArticle).getWidth()/2)+'px';
			$('layerInfoVoyage').setStyle({top:positionTop,left:positionLeft});
			$('layerInfoVoyage').show();*/
			};
			if(!currentTD.hasClassName('callCenter')) {
				currentTD.observe("click",clickFn.bind(this,clefArticle));
			}
		var onMouseOver = function(currentTD){
			/*if(currentTD.hasClassName('selectedArticle'))
				{*/
				var clefArticle = currentTD.getAttribute("id").split("_")[1];
				$('layerInfoContenu').update(window.infoArticlesTable["info_"+clefArticle]);
				position = $(currentTD).cumulativeOffset();
				positionTop = (position.top-$('layerInfoVoyage').getHeight());
				positionLeft = (position.left-$('layerInfoVoyage').getWidth()/2+$('tdArticle_'+clefArticle).getWidth()/2);
				positionLeft = positionLeft < 0 ? 0 : positionLeft;
				$('layerInfoVoyage').setStyle({top:positionTop+"px",left:positionLeft+"px"});
				$('layerInfoVoyage').show();
				hideSelect();
				/*}*/
			};			
		currentTD.observe("mouseover",onMouseOver.bind(this,currentTD));
		
		var onMouseOut = function(currentTD){
			/*if(currentTD.hasClassName('selectedArticle'))
				{*/
				$('layerInfoVoyage').hide();
				showSelect();
				/*}*/
			};			
		currentTD.observe("mouseout",onMouseOut.bind(this,currentTD));
		
		if(clefArticle == $F('clefArticle'))
			{			
			/*clickFn(clefArticle);*/
			onMouseOut(currentTD);
			}
	});
	//Initialisation des fleche de navigation du calendrier
	if($('mois').selectedIndex > 0)
	{
		$('boutonPriceToLeft').show();
		$('boutonPriceToLeft').observe("click",function(){
			$('mois').selectedIndex --;
			changeArticleParam($('mois'));
		});
	}
	else
		$('boutonPriceToLeft').hide();
		
	if($('mois').selectedIndex < $('mois').length-1)
	{
		$('boutonPriceToRight').show();
		$('boutonPriceToRight').observe("click",function(){
			$('mois').selectedIndex ++;
			changeArticleParam($('mois'));
		});
	}
	else
		$('boutonPriceToRight').hide();
	
	//Affichage du bouton DEVIS si un article est preselectionne
	/*if($F('clefArticle') != "")
		$('submitDevisBouton').show();*/

}
function initDerniereMinuteTable(){
	Event.observe(window,"load",function(){
	document.body.appendChild($('layerInfoVoyageDerniereMinute'));
	});
	$('tarifTable').select('TD.derniereMinute').each(function(currentTD){
		var clefArticle = currentTD.getAttribute("id").split("_")[1];
		var onMouseOver = function(currentTD){
				var clefArticle = currentTD.getAttribute("id").split("_")[1];
				$('layerInfoDerniereMinuteContenu').update(window.infoDerniereMinuteTable["info_"+clefArticle]);
				position = $(currentTD).cumulativeOffset();
				positionTop = (position.top-$('layerInfoVoyageDerniereMinute').getHeight());
				positionLeft = (position.left-$('layerInfoVoyageDerniereMinute').getWidth()/2+$('tdArticle_'+clefArticle).getWidth()/2);
				positionLeft = positionLeft < 0 ? 0 : positionLeft;
				$('layerInfoVoyageDerniereMinute').setStyle({top:positionTop+"px",left:positionLeft+"px"});
				$('layerInfoVoyageDerniereMinute').show();
				hideSelect();
			};			
		currentTD.observe("mouseover",onMouseOver.bind(this,currentTD));
		
		var onMouseOut = function(currentTD){
				$('layerInfoVoyageDerniereMinute').hide();
				showSelect();
			};			
		currentTD.observe("mouseout",onMouseOut.bind(this,currentTD));
		});
}

function placeWaitingLayer(){
	_height = $('tarifTable').getHeight();
	_width = $('tarifTable').getWidth();
	waitingDiv = new Element('div').setStyle({position:"absolute",width:_width+"px",height:_height+"px",textAlign:"center",zIndex:100}).addClassName("fondGris");
	waitingDiv.update("<div style='margin-top:"+(_height/2 - 68)+"px'><img src='/images/recherche/throbber.gif' /><br/><h2 style='color:#76003E;border:none;background-color:#FFF'>Actualisation en cours</h2></div>");
	$('tarifTable').insert({before:waitingDiv});
}
var aideTarifsLayerClosed = false;
function initAideTarifLayer(){
	if(document.cookie.indexOf('aideTarifsLayerClosed=yes') != -1)
		{aideTarifsLayerClosed = true;$('aideTarifsLayer').hide();}
	
	$('pictoFermerAideTarifs').observe("click",function(){
		$('aideTarifsLayer').hide();
		aideTarifsLayerClosed = true;
		document.cookie = "aideTarifsLayerClosed=yes;";
	});
	
	$('aideTarifsPicto').observe("mouseover",function(){
		$('aideTarifsLayer').show();
	});
	
	$('aideTarifsPicto').observe("mouseout",function(){
		if(aideTarifsLayerClosed == true)
		$('aideTarifsLayer').hide();
	});
}
/* Marmara */
function changeArticleParamMarmara(element)
{	
	placeWaitingLayer();
	try
	{
		window.xt_med('F','2','Fiche_pdt::paramCalendrier','N');
	}
	catch(e){
	}		
	new Ajax.Updater({success : window.parent.document.getElementById('articles')}, '/spring/calendar', {parameters: { produit: $F('productId'), villeDepart: $F('villeDepart'), mois: $F('mois'), duree: $F('duree')}, evalScripts: true } );
	//window.parent.xt_med('C','2','Fiche_pdt::paramCalendrier','N');
}
function initFlecheMarmara(){
	//Initialisation des fleche de navigation du calendrier
	if($('mois').selectedIndex > 0)
	{
		$('boutonPriceToLeft').show();
		$('boutonPriceToLeft').observe("click",function(){
			$('mois').selectedIndex --;
			changeArticleParamMarmara($('mois'));
		});
	}
	else
		$('boutonPriceToLeft').hide();
		
	if($('mois').selectedIndex < $('mois').length-1)
	{
		$('boutonPriceToRight').show();
		$('boutonPriceToRight').observe("click",function(){
			$('mois').selectedIndex ++;
			changeArticleParamMarmara($('mois'));
		});
	}
	else
		$('boutonPriceToRight').hide();
}

function initCallCenterLayerMarmara(){
	Event.observe(window,"load",function(){
	document.body.appendChild($('layerMarmaraCallCenter'));
	});
	$('tarifTable').select('TD.callCenter').each(function(currentTD){
		var clefArticle = currentTD.getAttribute("id").split("_")[1];
		var onMouseOver = function(currentTD){
				var clefArticle = currentTD.getAttribute("id").split("_")[1];
				$('layerMarmaraCallCenterContenu').update(window.infoDerniereMinuteTable["info_"+clefArticle]);
				position = $(currentTD).cumulativeOffset();
				positionTop = (position.top-$('layerMarmaraCallCenter').getHeight());
				positionLeft = (position.left-$('layerMarmaraCallCenter').getWidth()/2+$('tdArticle_'+clefArticle).getWidth()/2);
				positionLeft = positionLeft < 0 ? 0 : positionLeft;
				$('layerMarmaraCallCenter').setStyle({top:positionTop+"px",left:positionLeft+"px"});
				$('layerMarmaraCallCenter').show();
				hideSelect();
			};			
		currentTD.observe("mouseover",onMouseOver.bind(this,currentTD));
		
		var onMouseOut = function(currentTD){
				$('layerMarmaraCallCenter').hide();
				showSelect();
			};			
		currentTD.observe("mouseout",onMouseOut.bind(this,currentTD));
		});
}

