var globaleval =  function(script){
  if(window.execScript){
    return window.execScript(script);
  } else if(navigator.userAgent.indexOf('KHTML') != -1){ //safari, konqueror..
      var s = document.createElement('script');
      s.type = 'text/javascript';
      s.innerHTML = script;
      document.getElementsByTagName('head')[0].appendChild(s);
  } else {
    return window.eval(script);
  }
}

function log(str){
	if (window.console) {
		console.log(str);
	}
}
var idtype=null;
var idstyle=null;
var idcollection=null;
var idforme=null;
var traverse=null;
var nbElements = 0;
var carousel = null;
var mode_collection=false;
var possibilites_json=null;
var last_recherche=null;

function just_toogle_portail(idchoix){
	$$('#bande_select1 .fleche_bas').invoke('hide');
	$$('#bande_select1 .fleche_normal').invoke('show');
	$$('#col_choix_'+idchoix+' .fleche_bas').invoke('show');
	$$('#col_choix_'+idchoix+' .fleche_normal').invoke('hide');
	$$('td[id*=col_choix2_]').invoke('hide');
	$$('td[id*=col_choix2_'+idchoix+']').invoke('show');
	$('bande_select2').show();
	$('bande_select3').hide();
	idtype=idchoix;
	idstyle=null;
	idcollection=null;
}
function toogle_portail(idchoix){
	just_toogle_portail(idchoix);
	idforme=null;
	traverse=null;
	recherche_ajax();
}
function just_toogle_portail2(idchoix,idtype){
	$$('#bande_select2 .fleche_bas').invoke('hide');
	$$('#bande_select2 .fleche_normal').invoke('show');
	$$('#col_choix2_'+idtype+'_'+idchoix+' .fleche_bas').invoke('show');
	$$('#col_choix2_'+idtype+'_'+idchoix+' .fleche_normal').invoke('hide');
	$$('td[id*=col_choix3_]').invoke('hide');
	$$('td[id*=col_choix3_'+idchoix+']').invoke('show');
	$$('td[id*=col_choix3_] .fleche_bas').invoke('hide');
	$$('td[id*=col_choix3_] .fleche_normal').invoke('show');
	$('bande_select3').show();
	idstyle=idchoix;
	idcollection=null;
	
	//col_choix3_1_7
	for(var i=0;i<50;i++){
		//console.log(i);
		if(possibilites_json[idtype][idchoix][i]){
			//ok
		}else{
			$$('td[id=col_choix3_'+idchoix+'_'+i+']').invoke('hide');
		}
	}
	//console.log(idchoix+ ' '+idtype);
	//idstyle + idtype
//	console.log(possibilites_json[idtype][idchoix]);
//	$A(possibilites_json[idtype][idchoix]).each(function(e,index){
//		console.log(e+ ' '+index)
//	});
	//possibilites_json
}
function toogle_portail2(idchoix,idtype){
	just_toogle_portail2(idchoix,idtype);
	recherche_ajax();
}
function just_toogle_portail3(idchoix,idstyle){
	$$('#bande_select3 .fleche_bas').invoke('hide');
	$$('#bande_select3 .fleche_normal').invoke('show');
	$$('#col_choix3_'+idstyle+'_'+idchoix+' .fleche_bas').invoke('show');
	$$('#col_choix3_'+idstyle+'_'+idchoix+' .fleche_normal').invoke('hide');
	idcollection=idchoix;
}
function toogle_portail3(idchoix,idstyle){
	just_toogle_portail3(idchoix,idstyle);
	recherche_ajax();
}
function recherche_ajax(mode){
	if(mode_collection){
		var url = '/portails/collection/';
		var pars={'idtype':idtype,'idstyle':idstyle,'idcollection':idcollection,'idforme':idforme,'traverse':traverse,'modeAjax':1,'vertical':1,'idproduit':0};
		recherche_forme_traverse(pars,mode);
		new Ajax.Request(url, {
			method: 'get',
			parameters: pars,
			onSuccess: function(transport) {
				$('inner_recherche_vertical').innerHTML=transport.responseText;
				var scripts = transport.responseText.extractScripts();
				var r = scripts.map(function(script) {
					return globaleval(script);
				});
				$('lien_devis').innerHTML=$('lien_devis_hide').innerHTML;
			}
		});
	}else{
		var url = '/portails/collection/';
		var pars={'idtype':idtype,'idstyle':idstyle,'idcollection':idcollection,'idforme':idforme,'traverse':traverse,'modeAjax':1};
		
		//nbElements = 999;
		recherche_forme_traverse(pars,mode);
		//$$('#portail_deroulant ul')[0].innerHTML="";
		$('load_partie_recherche').innerHTML='<div class="inner_portails">		<div id="horizontal_carousel">			<a id="bouton_gauche"   class="previous_button"><span><</span></a>			<div class="container" id="portail_deroulant">				<ul>				</ul>				<div id="spinner"><img src="/images/loading.gif" alt="" />Chargement en cours ...<br/></div>			</div>			<a id="bouton_droite"   class="next_button"><span>></span></a>		</div>		<div class="clear"></div>	</div>	<div class="nettoie"></div>';
		//$$('#portail_deroulant ul')[0].style.left="0px";
		carousel_hor=null;
		runTest();
		/*
		new Ajax.Request(url, {
			method: 'post',
			parameters: pars,
			onSuccess: function(transport) {
				$('load_partie_recherche').innerHTML=transport.responseText;
				var scripts = transport.responseText.extractScripts();
				var r = scripts.map(function(script) {
					return globaleval(script);
				});
			}
		});*/
	}
}
function recherche_forme_traverse(pars,mode){
	if(mode=='forme'){
		return true;
		//refresh juste traverse
		pars.mode='forme';
		var url = '/portails/refresh_forme_traverse/';
		new Ajax.Request(url, {
			method: 'post',
			parameters: pars,
			onSuccess: function(transport) {
				$('inner_traverse').innerHTML=transport.responseText;
				var scripts = transport.responseText.extractScripts();
				var r = scripts.map(function(script) {
					return globaleval(script);
				});
			}
		});
	}else if(mode=='traverse'){
		//refresh juste forme
		pars.mode='traverse';
		var url = '/portails/refresh_forme_traverse/';
		new Ajax.Request(url, {
			method: 'post',
			parameters: pars,
			onSuccess: function(transport) {
				$('inner_forme').innerHTML=transport.responseText;
				var scripts = transport.responseText.extractScripts();
				var r = scripts.map(function(script) {
					return globaleval(script);
				});
			}
		});
	}else{
		if(!!$('vertical_container')){
			//refresh all
			var url = '/portails/refresh_forme_traverse/';
			new Ajax.Request(url, {
				method: 'post',
				parameters: pars,
				onSuccess: function(transport) {
					$('vertical_container').innerHTML=transport.responseText;
					var scripts = transport.responseText.extractScripts();
					var r = scripts.map(function(script) {
						return globaleval(script);
					});
					/*var bottomAccordion = new accordion('vertical_container');
					bottomAccordion.activate($('accordion_toggle_id_1'));*/
				}
			});
		}
	}
	
}
function loadPortail(pidtype,pidstyle,pidcollection,pidproduit,pidforme,ptraverse){
	var url = '/portails/getportail/';
	var pars={'idtype':pidtype,'idstyle':pidstyle,'idcollection':pidcollection,'idproduit':pidproduit,'idforme':pidforme,'traverse':ptraverse};
	$('votre_selection').morph("opacity:0.5", {duration:0.01});
	new Ajax.Request(url, {
		method: 'post',
		parameters: pars,
		onSuccess: function(transport) {
			$('votre_selection').innerHTML=transport.responseText;
			$('votre_selection').morph("opacity:1", {duration:0.5});
			var scripts = transport.responseText.extractScripts();
			var r = scripts.map(function(script) {
				return globaleval(script);
			});
			$('lien_devis').innerHTML=$('lien_devis_hide').innerHTML;
		}
	});
}
function filtre_forme(varidforme){
	idforme=varidforme;
	recherche_ajax('forme');
}
function filtre_traverse(vartraverse){
	traverse=vartraverse;
	recherche_ajax('traverse');
}


// Delay response
Ajax.Request.prototype.originalInitialize = Ajax.Request.prototype.initialize;
Ajax.Request.prototype.initialize = function(url, options) {
  options.onSuccess = options.onSuccess.wrap(function(proceed, request, json) {
    proceed.curry(request, json).delay(1);
  });
  this.originalInitialize(url, options);
}
Ajax.Response.prototype._getHeaderJSON = function() {
    var zetxt=this.request.transport.responseText;
   // console.log(this.request.transport.responseText);
    //nbElements=nbElements;
    var from = this.request.parameters.from;
    var to   = Math.min(nbElements, this.request.parameters.to);
    //console.log(' nbElements '+nbElements);
    var html = zetxt;
    //console.log(from+' '+nbElements+' '+to);
    if(this.request.parameters.to<nbElements){
    	return {html: html, from: from, to: to, more: true};
    }else{
    	return {html: html, from: from, to: to, more: false};
    }
    if(nbElements>2){
    	return {html: html, from: from, to: to, more: nbElements>to};
    }else{
    	return {html: html, from: from, to: to, more:false};
    }
}
var carousel_hor=null;
function runTest() {
	carousel_hor = new UI.Ajax.Carousel("horizontal_carousel", {url: "/portails/collection/", elementSize:450})
      .observe("request:started", function() {
            $('spinner').show().morph("opacity:0.8", {duration:0.2});
        })
      .observe("request:ended", function() {
            $('spinner').morph("opacity:0", {duration:0.2, afterFinish: function(obj) { obj.element.hide(); }});
        });
}
function runTestVertical() {
	vCarousel = new UI.Ajax.Carousel("vertical_carousel", {url: "/portails/collection/?vertical=1",direction: "vertical", elementSize:65})
	.observe("request:started", function() {
		$('spinner').show().morph("opacity:0.8", {duration:0.2});
	})
	.observe("request:ended", function() {
		$('spinner').morph("opacity:0", {duration:0.2, afterFinish: function(obj) { obj.element.hide(); }});
	});
}
/*vCarousel = new UI.Carousel("vertical_carousel", {direction: "vertical"});*/

function box_show(xhtml){
	$('light_pillier_xl').innerHTML=unescape(xhtml);
	$('fond_opaque').show();
	$('light_contenu').show();
	$('id_visuel_portail_xl').show();
	$('light_pillier_large_xl').hide();
	//$('light_contenu').style.marginRight="-490px";
}

function box_show_xl(xhtml){
	$('light_pillier_large_xl').innerHTML=unescape(xhtml);
	$('fond_opaque').show();
	$('light_contenu').show();
	$('id_visuel_portail_xl').hide();
	$('light_pillier_large_xl').show();
	$('light_contenu').style.marginRight="-450px";
	$('light_contenu').style.top="350px";
}

function loadtechnique(varthis,name){
	$$('.liste_image .current').each(function(e){
		e.removeClassName('current');
	});
	var e=$(varthis);
	e.addClassName('current');
	$('block_texte_grande_illus').innerHTML=e.readAttribute('title')+'<br /><img src="/images/technique/l/'+name+'" alt="" />';
}

var lastnum=-1;
function launch_diaporama(){
	if(lastnum>=0){
		$$('.diaporama')[lastnum].morph("opacity:0", {duration:1, afterFinish: function(obj) {
                obj.element.hide();
            }});
	}
	lastnum++;
	if(!$$('.diaporama')[lastnum]){
		lastnum=0;
	}
	$$('.diaporama')[lastnum].appear({ duration: 1.0 });
//	$$('.un_portail_diaporama')[lastnum].hide().morph("opacity:0", {from:0,duration:0});
//	$$('.un_portail_diaporama')[lastnum].show().morph("opacity:1", {from:0,duration:1});
	window.setTimeout("launch_diaporama()",5000);
}

var activePageCurrent=0;
function menu_over(pavee){
//	$(pavee).show();
//	$$('#menu_top .current').each(function(e){
//		e.removeClassName('current');
//		if(activePageCurrent==0)
//			activePageCurrent=e;
//	});
//	$('a_'+pavee).addClassName('current');
}
function menu_out(pavee){
//	$(pavee).hide();
//	$$('#menu_top .current').each(function(e){
//		e.removeClassName('current');
//	});
//	activePageCurrent.addClassName('current');
}


