// a3W5.js

addLoadEvent(function() {
	map(removeEmptyTextNodes, ['menu','submenu','footer']);
	cfgBotones(); 
	cfgLang();
	cfgMenu(); 
	cfgSubMenu();
	cfgProductos();
	cfgFooter();
	cfgPortales();
	removeElementClass('pagina','invisible');
});

var $$$ = MochiKit.DOM.getElementsByTagAndClassName;

// alineacion vertical de un elemento en otro
var setVAlign = function(container, contenido) {
	var fuera = $(container);
	var dentro = $(contenido);
	var pos = getElementPosition(fuera, dentro);
	pos.y = (getElementDimensions(fuera).h - getElementDimensions(dentro).h) / 2;
	setElementPosition(dentro, pos);	
}


// conectar efecto de over para todos los botones
var cfgBotones = function() {
	var overFcn = function(show) {
		if (hasElementClass(this, 'fixed')) { addElementClass(this, 'over'); }
		else { if (show != hasElementClass(this, 'over')) { toggleElementClass('over', this); } }
	}
	forEach($$$('button','boton'), function(btn) {
		connect(btn, 'onmouseover', bind(overFcn, btn, true));
		connect(btn, 'onmouseout',  bind(overFcn, btn, false));
	});
}

// configuracion de scroll y flechas de navegacion
var cfgScroll = function(contenido, contenedor, velocidad) {
	var sm = new ScrollManager();
	sm.init(contenido, contenedor, velocidad);
	var overFcn = function(show) {
		if (show) { sm.start(this.id); setStyle(this, { opacity : 0.3 }); } 
		else { sm.stop(); setStyle(this, { opacity : 1 }); }
	}
	forEach($$$('div','nav'), function(nav) {
		connect(nav, 'onmouseover', bind(overFcn, nav, true));
		connect(nav, 'onmouseout',  bind(overFcn, nav, false));
	});
}

// acciones de cambio de idioma
var cfgLang = function() {
	var overLangFcn = function(show) { setStyle(this, {'opacity': show ? 0.7 : 0.3}); };
	var langFcn = function() {
		var pag = document.location.pathname + '?idDivision=' + req.division;
		if (req.pantalla != -1) {pag = pag + '&idPantalla=' + req.pantalla; };
		if (req.producto != -1) {pag = pag + '&idProducto=' + req.producto; };
		pag = pag + '&lang=' + this.id;
		document.location = pag;
	};
	forEach($$$('img',null,'logolang'), function(img) {
		setStyle(img, {'opacity': 0.3});
		if (img.id == req.lang) {
			setStyle(img, {'opacity': 0.7, 'cursor': 'default'});
		} else {
			connect(img, 'onmouseover', bind(overLangFcn, img, true));
			connect(img, 'onmouseout', bind(overLangFcn, img, false));
			connect(img, 'onclick', bind(langFcn, img));
		}
	});
}

// acciones de menu principal
var cfgMenu = function() {
	var menuFcn = function(idDiv) {
		pag = document.location.pathname + '?idDivision=' + idDiv + '&lang=' + req.lang;
		document.location = pag;
	}
	forEach($$$('button',null,'menu'), function(btn) {
		var i = btn.id.substring(1);
		if (i == req.division) {
			addElementClass(btn, 'fixed over');
		} else {
			connect(btn, 'onclick', bind(menuFcn, btn, i));
		}
	});
}

// acciones de submenu
var cfgSubMenu = function() {
	var submFcn = function(idSubm) {
		pag = document.location.pathname + '?idDivision=' + req.division +
				 '&idPantalla=' + idSubm + '&lang=' + req.lang;
		document.location = pag;
	}
	forEach($$$('button',null,'submenu'), function(btn) {
		var i = btn.id.substring(1);
		if (i == req.pantalla) {
			addElementClass(btn, 'fixed over');
		} else {
			connect(btn, 'onclick', bind(submFcn, btn, i));
		}
	});
}

// acciones de productos
var cfgProductos = function() {	
	// animacion
	var marcoAnim = function() {
		this.o = Math.abs(this.o - this.oo);
		Opacity(this, {to: this.over ? this.o : 0,   
			afterFinish: function(effect) {
				var e = effect.element;
				if (e.over || effect.options.to > 0) { bind(marcoAnim, e)(); }
				else { e.run = false; }
			}
		});
	};
	// over
	var marcoOver = function(x) {
		var over = this.over || false;
		var run  = this.run || false;
		if (over != x) {
			(x ? addElementClass : removeElementClass)(this.parentNode, 'over');
			this.over = x;
			if (x && !this.run) {
				this.run = true;
				bind(marcoAnim, this)();
			}
		}
	};	
	var prodFcn = function(idProd) {
		pag = document.location.pathname + '?idDivision=' + req.division +
				'&idProducto=' + idProd + '&lang=' + req.lang;
		document.location = pag;
	}
	var i = 0;
	forEach($$$('div','lnkbtn','productos'), function(prod) {
		var texto = scrapeText(prod);
		var jpg = req.base + 'd/imagenes/productos/prod_' + req.division +'_' + i +'.jpg';
		var img = IMG({'class':'lnkimg','src':jpg});
		var txt = DIV({}, texto);
		var marco = DIV({'class':'lnkborde'});		
		setStyle(marco, {'opacity':'0'});
		marco.o = 0; marco.oo = 0.5;
		replaceChildNodes(prod, img, txt, marco);
		//
		connect(prod, 'onmouseenter', bind(marcoOver, marco, true));
		connect(prod, 'onmouseleave', bind(marcoOver, marco, false));
		connect(prod, 'onclick', bind(prodFcn, prod, i));		
		i++;
	});
}

var cfgFooter = function() {
	var footFcn = function(index) {
		var opcs = ['width=425,height=470', 'width=400,height=320'];
		var wnd = this.id;
		url = req.base + wnd +'.do?&lang=' + req.lang;
		opc = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,' +
				'resizable=0,top=10,left=310,';
		opc = opc + opcs[index];
		ref = window.open(url, wnd, opc);
		ref.focus();
	}
	var i = 0;
	forEach($$$('button',null,'footer'), function(btn) {
		connect(btn, 'onclick', bind(footFcn, btn, i++));
	});
}

var cfgPortales = function() {	
	var letras = $('banda_negra');
	if (letras == null) return;
	Morph(letras, {delay: 0, duration: 1.5, style: {top: "-30px"}});
}



