Event.observe(window,"load", function(){
	jsPage = new JSPage();
	window.ajustarPHPDiv();
});


Event.observe(window, "resize", function(){
	window.ajustarPHPDiv();
});

var JSPage = Class.create();

JSPage.prototype = {
	scrollControl : null,
	referencia : '',
	lanzador : null,
	idioma : '',
	subProd : false,

	initialize : function() {
		Object.extend(window, this);

		if($("contenidoFlash")){
			var so = new SWFObject("index230309.swf?limpia=0&destino=home", "index", "955", "625", "8", "#FFFFFF");
			//so.addParam("wmode", "transparent");
			so.write("contenidoFlash");
			leeypasavar();
		}
	},

	mostrarProd : function(ref, idioma){
		window.referencia = ref;
		window.idioma = idioma;
		window.subProd = false;

		new Ajax.Updater('contenidoPHP', 'loading.php', {
			onComplete : function(){
				$("contenidoFlash").hide();
				$('contenidoPHP').show();
				window.mostrarFicha();
			}
		});
	},

	ocultarProd : function(){
		$('contenidoPHP').hide();
		$("contenidoFlash").show();
		if($('index')){
			$('index').SetVariable('_root.sonido',1);
		}
	},

	mostrarSubProd : function (ref, idioma){
		window.subReferencia = ref;
		window.idioma = idioma;
		window.subProd = true;
		window.mostrarFicha();
	},

	mostrarFicha : function(){
		ref = (window.subProd) ? window.subReferencia : window.referencia;
		new Ajax.Updater('contenidoPHP', 'producto.php?ref=' + ref + "&idioma=" + window.idioma, {
			onComplete : function (){
				jsPestanas = new JSPestanas();
				window.initPestanas();
				if($('textProducteCont').getHeight() > $('textProducte').getHeight()){
					$('scroll_producte').show();
					scrollControl = new ScrollControl({
						incremento : 300,
						duracion : 3,
						btnUp : $('scrollUp'),
						btnDown : $('scrollDown'),
						contenedor : $('textProducte'),
						contenido : $('textProducteCont')
					});
				}else{
					$('scroll_producte').hide();
				}

				if(!subProd){
					Event.observe($('volver'), "click", window.ocultarProd);
				}else{
					Event.observe($('volver'), "click", function(){
						window.subProd = false;
						window.mostrarFicha();
					});
				}
			}
		});
	},

	mostrarInfo : function(){
		ref = (window.subProd) ? window.subReferencia : window.referencia;
		new Ajax.Updater('contenidoPHP', 'info.php?ref=' + ref + "&idioma=" + window.idioma, {
			onComplete : function (){
				jsPestanas = new JSPestanas();
				window.initPestanas();

				if($('textProducteCont').getHeight() > $('textProducte').getHeight()){
					$('scroll_producte').show();
					scrollControl = new ScrollControl({
						incremento : 300,
						duracion : 3,
						btnUp : $('scrollUp'),
						btnDown : $('scrollDown'),
						contenedor : $('textProducte'),
						contenido : $('textProducteCont')
					});
				}else{
					$('scroll_producte').hide();
				}


				Event.observe($('volver'), "click", window.mostrarFicha);
			}
		});
	},

	mostrarFaqs : function(){
		ref = (window.subProd) ? window.subReferencia : window.referencia;
		new Ajax.Updater('contenidoPHP', 'faqs.php?ref=' + ref + "&idioma=" + window.idioma, {
			onComplete : function (){
				jsPestanas = new JSPestanas();
				window.initPestanas();
				Event.observe($('volver'), "click", window.mostrarFicha);

				this.scrollControl = new ScrollControl({
					incremento : 300,
					duracion : 3,
					btnUp : $('scrollUp'),
					btnDown : $('scrollDown'),
					contenedor : $('textFaqs'),
					contenido : $('textFaqsCont')
				});

				$$("a.pregunta").each(function(item){
					Event.observe(item, "click", window.mostrarResp);
				});

				$$("a.cerrarRespuesta").each(function(item){
					Event.observe(item, "click", window.cerrarResp);
				});

			}
		});
	},

	mostrarExperiences : function(){
		ref = (window.subProd) ? window.subReferencia : window.referencia;
		new Ajax.Updater('contenidoPHP', 'experiences.php?ref=' + ref + "&idioma=" + window.idioma, {
			onComplete : function (){
				jsPestanas = new JSPestanas();
				window.initPestanas();
				Event.observe($('volver'), "click", window.mostrarFicha);

				this.scrollControl = new ScrollControl({
					incremento : 300,
					duracion : 3,
					btnUp : $('scrollUp'),
					btnDown : $('scrollDown'),
					contenedor : $('textFaqs'),
					contenido : $('textFaqsCont')
				});
			}
		});
	},

	initPestanas : function(){
		Event.observe($('info'), "click", window.mostrarInfo);
		Event.observe($('faqs'), "click", window.mostrarFaqs);
		Event.observe($('experiences'), "click", window.mostrarExperiences);
	},

	mostrarResp : function(evt){
		elemento = Event.element(evt);
		respuesta = elemento.getAttribute('actua');
		if($(respuesta).visible()){
			new Effect.SlideUp($(respuesta), {
				beforeFinish : window.ajustar,
				lanzador : window,
				duration: 0.5
			});
		}else{
			Effect.SlideDown($(respuesta), {
				duration:  0.5
			});

		}
	},

	cerrarResp : function(evt){
		elemento = Event.element(evt);
		respuesta = elemento.getAttribute('actua');
		new Effect.SlideUp($(respuesta), {
			beforeFinish : window.ajustar,
			lanzador : window,
			duration: 0.5
		});
	},

	ajustar : function(evt){
		window.scrollControl.ajustar();
	},

	ajustarPHPDiv : function(){
		$('contenidoPHP').style.width = document.body.clientWidth;
		$('contenidoPHP').style.height = document.body.clientHeight;
	}
}