/**************************************************************************************************************************************************************************************************/
/* CONSTANTE */
/**************************************************************************************************************************************************************************************************/
/*	window.CONST_BGCOLORFILTER = '#000';*/
	window.CONST_PATH_IMG = '/images/';
/**************************************************************************************************************************************************************************************************/

/**************************************************************************************************************************************************************************************************/
/* VARIABLES GLOBALES */
/**************************************************************************************************************************************************************************************************/
/**************************************************************************************************************************************************************************************************/


/**************************************************************************************************************************************************************************************************/
/* Evenements */
/**************************************************************************************************************************************************************************************************/
//window.addEvent('domready', initAll);

function initAll(){
	/*calendrier_jardin();
	initRounded();
	initAddCart();
	initTg();
	initFAQ();
	initCarroussel();
	initCarrousselZoom();
	initOnglets();
	initAccordion();
	initFicheConseil();
	initFamille();
	initSFamille();
	
	initLexique();
	initPng();
	setMsgError();
	setMsgConf();*/
	if(document.documentElement && document.documentElement.clientWidth!=undefined)
		{window.TsDocRef = document.documentElement;}
	else
		{window.TsDocRef = document.body;}
		
	if( (window.ie || window.ie6) && !window.ie7)
		{window.IElt7=true;}
	else
		{window.IElt7=false;}
	
	initPopup();
	initPng();
    open_ext_link();
}

function initLoad(){
	//initAjaxRecherche();  PLUS UTILE
}
	
/**************************************************************************************************************************************************************************************************/

/**************************************************************************************************************************************************************************************************/
/* Scintillement des backgrounds sous IE */
/**************************************************************************************************************************************************************************************************/
try{document.execCommand('BackgroundImageCache', false, true);}catch(e){};
/**************************************************************************************************************************************************************************************************/

/**************************************************************************************************************************************************************************************************/
/* Fonctions */
/**************************************************************************************************************************************************************************************************/

function locationHref(TsUrl, TsEvent){
	var event = new Event(TsEvent);

	window.location.href = TsUrl;

	event.stopPropagation();
	event.stop();
}
function initPng(){
	if ( !Browser.Engine.trident4 ){return;}
	var tsElementsIMG = $$('img.png');
	for(var i=0, x=tsElementsIMG.length;i<x;i++){
		var tsSrcImg = tsElementsIMG[i].getProperty('src');
		var tsWidthImg = tsElementsIMG[i].getProperty('width');
		var tsHeightImg = tsElementsIMG[i].getProperty('height');
		
		tsElementsIMG[i].setStyles({
			'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+tsSrcImg+'",sizingMethod="scale")',
			'background': 'none'
		});
		
		tsElementsIMG[i].setProperty('src', window.CONST_PATH_IMG + 'bg-blank.gif');
		if(parseInt(tsWidthImg)>0){ tsElementsIMG[i].setProperty('width', parseInt(tsWidthImg)); }
		if(parseInt(tsHeightImg)>0){ tsElementsIMG[i].setProperty('height', parseInt(tsHeightImg)); }
		tsElementsIMG[i].setStyle('visibility', 'visible');
	}
}

function nothing(TsEvent){
	var event = new Event(TsEvent);
	event.stopPropagation();
	event.stop();
}


function open_ext_link()
{
	var liens = document.getElementsByTagName('a');
	// On récupère tous les liens (<a>) du document dans une variable (un array), ici liens.
	// Une boucle qui parcourt le tableau (array) liens du début à la fin.
	for (var i = 0 ; i < liens.length ; ++i)  {
		// Si les liens ont un nom de class égal à lien_ext, alors on agit.
		if (liens[i].className == 'lien_ext')  {
			liens[i].title = 'S\'ouvre dans une nouvelle fenêtre';
			// Au clique de la souris.
			liens[i].onclick = function()  {
				window.open(this.href);
				return false; // On ouvre une nouvelle page ayant pour URL le href du lien cliqué et on inhibe le lien réel.
			};
		}
	}
}
/**************************************************************************************************************************************************************************************************/

/**************************************************************************************************************************************************************************************************/
/* Popup Thomas SEGOND */
/**************************************************************************************************************************************************************************************************/

function $SetFilterPopup(){

	if( !$('filter') ){return;}

	if( $('filter').getStyle('display') == 'block' ){
		setTimeout('$SetFilter()', 20);
		setTimeout('$SetPopup()', 20);
	}
}
function $ShowFilter(TsBgColorFilter){
/*	TsBgColorFilter = TsBgColorFilter || window.CONST_BGCOLORFILTER;

	if( !$('filter') ){$CreateFilter(TsBgColorFilter);}

	if( $('filter').getStyle('display') == 'block' ){return;}

	if ( window.IElt7 ){$GestionSelect('hidden');}

	$SetFilter();
	$('filter').setStyle('display', 'block');

	// Evenements pour gestion du filtre et de la popup
	window.addEvent('resize', $SetFilterPopup);*/
}
function $HideFilter(){
	/*if( !$('filter') ){return;}
	
	$('filter').setStyle('display', 'none');
	if( window.IElt7 ){$GestionSelect('visible');}*/
}
function $SetFilter(){
/*	if( !$('filter') ){return;}

	$('filter').setStyles({'width': 0, 'height': 0});

	var TsLargeur = parseInt(window.TsDocRef.clientWidth) + parseInt(window.TsDocRef.scrollLeft);
	var TsHauteur = parseInt(window.TsDocRef.clientHeight) + parseInt(window.TsDocRef.scrollTop);

    if( $('global') ){
        if( $('global').offsetHeight >  TsHauteur){TsHauteur = parseInt($('global').offsetHeight);}
        if( $('global').offsetWidth  >  TsLargeur){TsLargeur = parseInt($('global').offsetWidth);}
    }

	if( $('filter') ){$('filter').setStyles({'width':TsLargeur, 'height':TsHauteur})}*/
}
function $CreateFilter(TsBgColorFilter){
/*	var TsFilter = new Element('div').inject(document.body);
	TsFilter.setStyles({'top':0, 'left':0, 'width':0, 'height':0, 'background':TsBgColorFilter, 'opacity':0.75});
	TsFilter.setProperty('id', 'filter');*/
}
function $GestionSelect(TsOption){
	var TsElementsSelect = document.getElementsBySelector('select', false);
	for(var i=0,l=TsElementsSelect.length;i<l;i++){TsElementsSelect[i].setStyle('visibility', TsOption);}
}
function $ShowPopup(TsUrl, TsEvent){
	if($chk(TsEvent)){
		var event = new Event(TsEvent);
		event.stopPropagation();
		event.stop();
	}

	//$ShowFilter();
	if( !$('popup') ){$CreatePopup();}else{$HidePopup();}
	var TsUrl = $UrlRamdom(TsUrl);

//	var myAjax = new Ajax(TsUrl, {method: 'get', onRequest:$CreateLoader,  onFailure:$HideAll}).request();
	var myAjax = new Ajax(TsUrl, {method: 'get', onRequest:$CreateLoader, onComplete:$SetPopupComplete, onFailure:$HideAll}).request();
	return false;
}
function $HidePopup(){
	if( !$('popup') ){return;}

	$('popup').setHTML('');
	$('popup').setStyle('display', 'none');
}
function $SetPopupComplete(tsContent){

	if( !$('popup') ){return;}
	
	var tsStart = '<!-- Start POPUP -->';
	var tsEnd = '<!-- End POPUP -->';
	if( tsContent.indexOf(tsStart) > -1 && tsContent.indexOf(tsEnd) > -1 ){
		$('popup').innerHTML = tsContent.substring(tsContent.indexOf(tsStart), tsContent.indexOf(tsEnd)+(tsEnd.length));
	}else{
		$('popup').innerHTML = tsContent;
	}

	TsEvalScripts($('popup').innerHTML);

	var tsElementA = $ES('.close', 'popup');
	for(var i=0, x=tsElementA.length;i<x;i++){tsElementA[i].setStyle('visibility', 'visible');}
	setTimeout('$SetPopup()', 20);
}
function TsEvalScripts(evalResponse){
	var script, scripts;
	scripts = [];

	var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;

	while ((script = regexp.exec(evalResponse))){scripts.push(script[1])};

	scripts = scripts.join('\n');

	if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
}
function $SetPopup(){
	if( !$('popup') ){return;}

	$SetPosition('popup')
	$HidePopupLoader();
}
function $SetPosition(TsElement){
	$(TsElement).setStyles({'visibility':'hidden', 'display':'block'});
	glob = $('global');
	var TsScrollTop     = parseInt(glob.getSize().scroll.y);
	var TsScrollLeft    = 30;
	var TsClientHeight  = parseInt(glob.getSize().size.y);
	var TsClientWidth   = parseInt(glob.getSize().size.x);


	// Calcule la résolution de l'utilisateur pour le scroll des caractéristiques
	
	if( $('comparateur_content_0a') && parseInt($(TsElement).offsetHeight) > TsClientHeight ){
		$('comparateur_content_0a').setStyle('height', ( parseInt($('comparateur_content_0a').getStyle('height')) - (parseInt($(TsElement).offsetHeight) - TsClientHeight) ) );
	}

	var TsElementHeight = parseInt($(TsElement).offsetHeight);
	var TsElementWidth  = parseInt($(TsElement).offsetWidth);
	var TsBlockTop      = Math.round( TsScrollTop + (TsClientHeight - TsElementHeight) / 2)+'px';
	var TsBlockLeft     = TsScrollLeft+'px';

	if( parseInt(TsBlockTop) < 0 ){ TsBlockTop = '0';}
	if( parseInt(TsBlockLeft) < 0 ){ TsBlockLeft = '0';}

	if( $(TsElement) ){$(TsElement).setStyles({'top':297, 'left':80, 'visibility':'visible'})}
}
function $CreatePopup(){
	var TsPopup = new Element('div').inject($('global'));
	TsPopup.setStyles({'top':0, 'left':0});
	TsPopup.setProperty('id', 'popup');
}
function $CreatePopupLoader(){
	var TsPopup = new Element('div').inject($('global'));
	TsPopup.setStyles({'position':'absolute', 'top':150, 'left':40, 'z-index':3, 'display':'none', 'overflow':'hidden'});
	TsPopup.setProperty('id', 'popup_loader');
}
function $CreateLoader(){
	if( !$('popup_loader') ){
		$CreatePopupLoader();
	}else{
		$('popup_loader').setStyles({'visibility':'hidden', 'display':'block'});
		$('popup_loader').setStyles({'top':450, 'left':160, 'visibility':'visible'})
		return;
	}

	var TsLoader = new Element('div');
	TsLoader.setStyles({'position':'relative', 'width':250, 'overflow':'hidden', 'text-align':'center', 'background': '#fff', 'padding-top':10, 'padding-bottom':10});
	TsLoader.setProperty('id', 'loader');

	var TsImage = new Element('img').inject(TsLoader);
	TsImage.setProperty('src', window.CONST_PATH_IMG + 'img-loader.gif');
	TsImage.setProperty('alt', 'Chargement en cours ...');

	var TsTexte = new Element('p').inject(TsLoader);
	TsTexte.setStyles({'padding-left':10, 'padding-right':10});
	TsTexte.setHTML('');
	
	TsLoader.inject($('popup_loader'));
	if( $('popup_loader') ){
		$('popup_loader').setStyles({'visibility':'hidden', 'display':'block'});
		$('popup_loader').setStyles({'top':450, 'left':160, 'visibility':'visible'})
	}
//	$SetPosition('popup_loader');
}
function $HidePopupLoader(){
	if( !$('popup_loader') ){return;}
	$('popup_loader').setStyle('display', 'none');
}
function $HideAll(TsEvent){
	if($chk(TsEvent))
		var event = new Event(TsEvent);

	$HidePopupLoader();
	$HidePopup();
//	$HideFilter();

	if($chk(TsEvent)){	
		event.stopPropagation();
		event.stop();
	}
}
function $UrlRamdom(TsUrl){
	if( TsUrl.contains('?') ){
		return TsUrl + '&nocache='+Math.random();
	}else{
		return TsUrl + '?nocache='+Math.random();
	}
}
function reviews(TsEvent){
	if($chk(TsEvent))
		var event = new Event(TsEvent);

	if( $('review_content_0') && $('form_review') ){
		if( $('review_content_0').getStyle('display') == 'none'){
			$('review_content_0').setStyle('display','block');
			$('form_review').setStyle('display','none');
		}else{
			$('review_content_0').setStyle('display','none');
			$('form_review').setStyle('display','block');
		}
	}
	
	if($chk(TsEvent)){
		event.stopPropagation();
		event.stop();
	}
}

/**************************************************************************************************************************************************************************************************/

/**************************************************************************************************************************************************************************************************/
/* Addon MOOTOOLS */
/**************************************************************************************************************************************************************************************************/

/*if(Element){
	Element.extend({
		getParentTag: function(parent){ // Retourne le premier parent avec le tag demand� et/ou la id et/ou class -> div#monid.maclass
			var TsTagName, TsClassName , TsId;
			var TsParentClass = parent.split('.');
			TsParentClass.length > 1 ? TsClassName = TsParentClass[1] : TsClassName = '';
			var TsParentId = TsParentClass[0].split('#');
			if( TsParentId.length > 1 ){TsId = TsParentId[1];}else{TsId = '';}
			TsTagName = TsParentId[0];
			var TsElement = this;
			while( $(TsElement) ){
				if(TsElement.getTag() == 'body'){break;}

				var TsFlag = true;
				if( TsElement.getTag() != TsTagName ){TsFlag = false;}
				if( TsId != '' && TsElement.getProperty('id') != TsId ){TsFlag = false;}
				if( TsClassName != '' && !TsElement.hasClass(TsClassName) ){TsFlag = false;}
				if( TsFlag ){break;}

				TsElement = TsElement.parentNode;
			}
			return $(TsElement);
		}
	})
}
*/
function initPopup(){
	var listLienPopup = $$('a.popup');
	for(var i=0;i<listLienPopup.length;i++){
		listLienPopup[i].getParentTag('a').addEvent('click',function(event,el){
			event = new Event(event);
			event.stop();
			//alert(this.href);
			$ShowPopup(this.href+"?tmpl=component");
		});
	}
}//'$ShowPopup(this.href+="&format=raw",event)'

if(window.addEvent)
    window.addEvent("load",initAll)
/**************************************************************************************************************************************************************************************************/