//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();	}
			else
				{	Element.removeClassName(onglet.onglet,"actif");
					if(onglet.panelToShow != null) onglet.panelToShow.hide();	}
		});
	}
});

TabbedPanel = Class.create();
Object.extend(TabbedPanel.prototype,{
	onglet:null,
	panelToShow:null,
	initialize: function(ongletId,layoutId) {
		this.onglet = $(ongletId);
		this.panelToShow = $(layoutId);
	}
});

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()
			}


TarifPanelManager = Class.create();
Object.extend(TarifPanelManager.prototype,{
	priceTable:null,
	priceTableWidth:null,
	priceRowWidth:null,
	priceTableContenerWidth:null,
	dateTable:null,
	radioList:null,
	allPriceLine:null,
	allDateLine:null,
	marginToGo:null,
	step:5,
	initialize: function(ongletsList) {
		this.priceTable = $('priceTable');
		this.dateTable = $('dateTable');
		this.priceTableWidth = this.priceTable.getWidth();
		this.priceTableContenerWidth = $('priceTableContener').getWidth();
		this.priceRowWidth = this.priceTable.getElementsBySelector("TD")[0].getWidth();
		this.allPriceLine = this.priceTable.getElementsBySelector("TR");
		this.allDateLine = this.dateTable.getElementsBySelector("TR");
		this.radioList = this.priceTable.getElementsBySelector("input[type='radio']");
		this.radioList.each(
			function(radioButton){
				Event.observe(radioButton,"click",this.firePriceChange.bindAsEventListener(this));
				}.bind(this));
		Event.observe($('boutonPriceToLeft'),"click",this.firePriceToLeft.bindAsEventListener(this));
		Event.observe($('boutonPriceToRight'),"click",this.firePriceToRight.bindAsEventListener(this));
		this.checkNbRow();
		this.initFirstLineHeight();
		this.initMiniPrice();
	},

	destroy:function(){
		this.priceTable = null;
		},

	/*Fonction fixant la hauteur de la premiere ligne de la table de date a la meme valeur que celle des prix*/
	initFirstLineHeight : function(){
		_firstHeight = 	this.priceTable.getElementsBySelector("tr.tarifFirstLine")[0].getHeight();
		this.dateTable.getElementsBySelector("tr.tarifFirstLine")[0].getElementsBySelector("TD")[0].setStyle({ height : _firstHeight+"px"});
	},

	/*Fonction ajoutant la classe "prixMini" au label du plus petit prix de la table et centrant le prix mini*/
	initMiniPrice : function()
	{
		_selectedArticleDiv = this.priceTable.getElementsBySelector("div.selectedArticle");
		_allLabel = this.priceTable.getElementsBySelector("span.labelPrix");
		if(_allLabel.length == 0)
			return false;
		_minPrice = parseInt(_allLabel[0].innerHTML.unescapeHTML().replace(/\s/g,'').replace(/&nbsp;/g,''));
		_minPriceIndex = 0;
		//Si pas d'article selectionne, on prend le mini
		if(_selectedArticleDiv == null || $A(_selectedArticleDiv).length == 0)
		{
		for(i=1;i<_allLabel.length;i++)
			{
			currentMinPrice = parseInt(_allLabel[i].innerHTML.unescapeHTML().replace(/\s/g,'').replace(/&nbsp;/g,''));
			if(currentMinPrice < _minPrice)
				{
				_minPrice = currentMinPrice;
				_minPriceIndex = i;
				}
			}
		}
		//Sinon, un article est selectionne, nous allons chercher son index
		else
		{
		_selectedArticleLabel = _selectedArticleDiv[0].getElementsBySelector("span.labelPrix")[0];
		for(i=1;i<_allLabel.length;i++)
			{
			currentLabel = _allLabel[i];
			if(currentLabel == _selectedArticleLabel)
				{
				_minPriceIndex = i;
				}
			}
		}
		
		//on a le prix mini
		_labelPrixMini = _allLabel[_minPriceIndex];
		//maintenant on va chercher sa position dans la ligne
		$(_labelPrixMini.parentNode).addClassName("prixMini");
		_TDPrixMini = $(_labelPrixMini.parentNode);
		while(_TDPrixMini != null && _TDPrixMini.nodeName != "TD")
			_TDPrixMini = $(_TDPrixMini.parentNode);
		_TRPrixMini = $(_TDPrixMini.parentNode);
		while(_TRPrixMini!=null && _TRPrixMini.nodeName != "TR")
			_TRPrixMini = $(_TRPrixMini.parentNode);
		_TDPrixMiniPosition = 0;
		if(_TRPrixMini.nodeName == "TR")
			{
				_AllTD = _TRPrixMini.getElementsBySelector("TD");
				_AllTD.each(function(_td, index){
					if(_td == _TDPrixMini)
					_TDPrixMiniPosition = index;
				});
				offsetMarge = parseInt((this.priceTableContenerWidth / 2 ) / this.priceRowWidth);
				_marge = _TDPrixMiniPosition - offsetMarge;
				if(_marge > 0 && (this.priceRowWidth * _marge) <= (this.priceTableWidth - this.priceTableContenerWidth ))
					this.priceTable.setStyle({marginLeft : (this.priceRowWidth * -_marge) + "px"});
				if(_marge > 0 && (this.priceRowWidth * _marge) > (this.priceTableWidth - this.priceTableContenerWidth ))
					this.priceTable.setStyle({marginLeft : (this.priceTableContenerWidth - this.priceTableWidth) + "px"});
			}
		if(_TDPrixMini.getElementsBySelector("INPUT[type='radio']")[0])
			_TDPrixMini.getElementsBySelector("INPUT[type='radio']")[0].click();
	},
	/*Fonction verifiant le nombre de colonne de la table de prix pour eventuellement adapter leur taille*/
	checkNbRow : function(){
		if(this.priceTable.getElementsBySelector("TR")[0] && this.priceTable.getElementsBySelector("TR")[0].getElementsBySelector("TH")[0])
			{
				nbRow = this.priceTable.getElementsBySelector("TR")[0].getElementsBySelector("TH").length;
				if(nbRow < 5)
					{
						this.changeRowWidth((this.priceTableContenerWidth/nbRow) - 1);
						$('boutonPriceToLeft').hide();
						$('boutonPriceToRight').hide();
						$('boutonPriceInfo').hide();						
					}
			}
	},
	/*Fonction changeant la taille des colonnes*/
	changeRowWidth : function(targetWidth){
		this.priceTable.getElementsBySelector("DIV").each(function(currDiv){
			currDiv.setStyle({width : targetWidth + "px"});
		});
		this.priceTableWidth = this.priceTable.getWidth();
	},
	/*Fonction appelée lors d'un clic sur un prix (bouton radio ou label)*/
	firePriceChange: function(event){
		this.allPriceLine.each(function(currentLine){currentLine.removeClassName("selectedLine")});
		this.priceTable.getElementsBySelector("TD").each(function(currentLine){currentLine.removeClassName("selectedRow")});
		if(Event.element(event) == document && event.currentTarget)
			{
			currentElement = event.currentTarget;
			line = currentElement.up("TR");
			line.addClassName('selectedLine');
			line = currentElement.up("TD");
			line.addClassName('selectedRow');
			}
		else
		{
			line = Event.findElement(event,"TR");
			line.addClassName('selectedLine');
			line = Event.findElement(event,"TD");
			line.addClassName('selectedRow');
		}
		//this.allDateLine.each(function(currentLine){currentLine.removeClassName("selectedLine")});
		for(i=0;i<this.allPriceLine.length;i++)
			{
				if(this.allPriceLine[i].hasClassName("selectedLine"))
					this.allDateLine[i].addClassName('selectedLine');
				else
					this.allDateLine[i].removeClassName('selectedLine');
			}
		selectArticle();
	},
	/*Fonction appelée lors du clic sur la fleche de défilement gauche de la table des prix*/
	firePriceToLeft:function(event){
		this.marginGoTo = this.getNextLeftRow();
		//this.goTo()
		currentMargin = (parseInt(this.priceTable.getStyle("marginLeft") || 0));
		if(currentMargin < 0)
			this.priceTable.setStyle({marginLeft : this.marginGoTo + "px"});
	},
	/*Fonction appelée lors du clic sur la fleche de défilement droite de la table des prix*/
	firePriceToRight:function(event){
		this.marginGoTo = this.getNextRightRow();
		//this.goTo()
		currentMargin = (parseInt(this.priceTable.getStyle("marginLeft") || 0));
		if(currentMargin >= -(this.priceTableWidth - this.priceTableContenerWidth))
			this.priceTable.setStyle({marginLeft : this.marginGoTo + "px"});
	},
	getNextLeftRow:function(){
		return (parseInt(this.priceTable.getStyle("marginLeft") || 0))+this.priceRowWidth;
	},
	getNextRightRow:function(){
		return (parseInt(this.priceTable.getStyle("marginLeft") || 0))-this.priceRowWidth;
	},
	/*Fonction utilisée pour amener la table des prix à une certaine position (clic sur les fleches ou centrage initial sur le prix mini)*/
	goTo:function(){
		currentMargin = (parseInt(this.priceTable.getStyle("marginLeft") || 0));
		if(this.marginGoTo > currentMargin && currentMargin < 0)
			{
				nextMargin = (this.marginGoTo-currentMargin>this.step) ? (currentMargin+this.step) : this.marginGoTo-currentMargin;
				nextMargin += "px";
				this.priceTable.setStyle({marginLeft : nextMargin});
				setTimeout(this.goTo.bindAsEventListener(this),50);
			}
		else if(this.marginGoTo < currentMargin && currentMargin > -(this.priceTableWidth - this.priceTableContenerWidth))
			{
				nextMargin = (currentMargin-this.marginGoTo>this.step) ? (currentMargin-this.step) : currentMargin-this.marginGoTo;
				nextMargin += "px";
				this.priceTable.setStyle({marginLeft : nextMargin});
				setTimeout(this.goTo.bindAsEventListener(this),50);
			}
	}
});