function getDatos(obj,accion,params){
	objT = (obj!='');

	//esp = $('espera');

	//if(objT && obj!='main')new Effect.Opacity(obj, {duration:0, from:0, to:0, queue:'end'});

	/*(objT)?Position.clone($(obj),esp):Position.clone($('main'),esp);
	esp.show();*/

	var url = 'server/' + accion + '.asp';
	new Ajax.Request(url, {
	method: 'post',
	postBody: params,
	onSuccess: function(transport) {
		if(objT)$(obj).innerHTML = transport.responseText;
		//verificador.innerHTML = '<textarea cols="30" rows="50">' + transport.responseText + '</textarea>';
		//if(objT && obj!='main')new Effect.Opacity(obj, {duration:0.5, from:0, to:1, queue:'end'});
		transport.responseText.evalScripts();
		//esp.hide();
	},
	onFailure: function(tranport){
		//verificador.innerHTML = '<textarea cols="30" rows="50">' + transport.responseText + '</textarea>';
		alert(transport.responseText);
	}
	
	});
	// + '&pag=' + eval(contador)
}

function creaVentana(tam){
	var tmpImg = '';
	tmpImg = rellenaColor('#000000',50);
	tmpImg += ventanaCentrada(tam);
	imgTmp = $('imagenZoomDiv');
	imgTmp.innerHTML = tmpImg;
	imgTmp.show();
}

function windowZoom(accion,params){
	getDatos('windowCenter',accion,params);
}

function clearCapa(){
	imgTmp = $('imagenZoomDiv');
	imgTmp.hide();
}

function ventanaCentrada(tam){
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	posXI = Math.floor((iWidth - tam[0])/2);
	posYI = Math.floor((iHeight - tam[1])/2) + document.viewport.getScrollOffsets().top;

	tmpDiv = '<div id="windowContainer" style="position:absolute;left:' + posXI + 'px;top:' + posYI + 'px;height:' + tam[1] + 'px;width:' + tam[0] + 'px;z-index:500;" class="ventanaContainer">';
	tmpDiv += '<div id="windowCenter" class="ventana">';
	tmpDiv += '<div id="spinner" style="position:absolute; top:50%; left:50%; height:32px; margin-top:-16px; width:32px; margin-left:-16px;"><img src="imagenes/wait.gif" width="32" height="32"></div>';
	tmpDiv += '</div>';
	tmpDiv += '</div>';
	return tmpDiv;
}


function imagenZoom(foto,id){
	var tmpImg = '';
	tmpImg = rellenaColor('#000000',75);
	tmpImg += imagenCentrada(foto,id,eligeTam(),1);
	imgTmp = $('imagenZoomDiv');
	imgTmp.innerHTML = tmpImg;
	//imagenCarga(foto,tamFoto);
	imgTmp.show();
}

function rellenaColor(color,opacidad){
	tamPag = getPageSize();
	tmpDiv = '<div style="background-color:' + color + ';position:absolute;top:0px;left:0px;height:' + tamPag[1] + 'px;width:' + tamPag[0] + 'px;filter:alpha(opacity=' + opacidad + ');-moz-opacity:.' + opacidad + ';opacity:.' + opacidad + ';"></div>';
	return tmpDiv;
}

function getPageSize() {
		
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {  
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}   
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){  
		pageWidth = xScroll;        
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function imagenPinta(cual){
	$('spinner').hide();
	$('imgZ').show();
}

function imagenCentrada(imagen,id,tam,sec){
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	posXI = Math.floor((iWidth - tam[0])/2);
	posYI = Math.floor((iHeight - tam[1])/2) + document.viewport.getScrollOffsets().top;

	tmpImg = 'comun/imagen.asp?id=' + id + '&foto=' + imagen + '&tamx=' + tam[0] + '&tamy=' + tam[1] + '&f=0&d=1&sec=' + sec;

	tmpDiv = '<div style="position:absolute;left:' + posXI + 'px;top:' + posYI + 'px;height:' + tam[1] + 'px;width:' + tam[0] + 'px;z-index:500;">';
	tmpDiv += '<div id="spinner" style="position:absolute; top:50%; left:50%; height:32px; margin-top:-16px; width:32px; margin-left:-16px;"><img src="imagenes/wait2.gif" width="32" height="32"></div>';
	tmpDiv += '<table width="' + tam[0] + '" height="' + tam[1] + '" id="imgZ" border="0" cellpadding="0" cellspacing="0" style="display:none;"><tr><td align="center" valign="middle"><img src="' + tmpImg + '" style="border-color:#FFFFFF;border-width:1px;border-style:solid;" onload="imagenPinta()" border="1" alt="Pulse para cerrar" title="Pulse para cerrar"></td></tr></table>';
	tmpDiv += '</div>';
	return tmpDiv;
}

function eligeTam(){
	var tmpTam;
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth = document.body.clientWidth;
		iHeight =document.body.clientHeight;
	}
	tmpW = Math.floor(iWidth/100) * 90;
	tmpH = Math.floor(iHeight/100) * 90;
	//(tmpH<tmpW)?tmpW = tmpH:tmpH = tmpW;
	tmpTam = [tmpW , tmpH];
	return tmpTam;
}

function paintForm(accion,campos){
	var url = accion + ".asp";
	var tmpCampos = "";
	if(campos.length>0){
		for(var c=0;c<campos.length;c++){
			tmpCampos += campos[c][0] + "=" + campos[c][1];
			if(c<(campos.length-1)){
				tmpCampos += "&";
			}
		}
	}
	if(tmpCampos!="")url+= "?" + tmpCampos;
	new Ajax.Request(url, {
	method: 'get',
	onSuccess: function(transport) {
		transport.responseText.evalScripts();
		}
	
	});

}

function sendQuery(accion,campos){
	paintForm(accion,campos);
}


function basketAddItem(codigo,unidades){
	creaVentana([400,300]);
	tmpDatos = new Array();
	tmpDatos[0] = new Array("accion","add");
	tmpDatos[1] = new Array("idlibro",codigo);
	tmpDatos[2] = new Array("unidades",unidades);
	tmpDatos[3] = new Array("alea",Math.random());
	sendQuery("basket/gesbasket",tmpDatos);
}

function basketAddRevista(codigo,unidades,tipoprecio){
	creaVentana([400,300]);
	tmpDatos = new Array();
	tmpDatos[0] = new Array("accion","add");
	tmpDatos[1] = new Array("idlibro",codigo);
	tmpDatos[2] = new Array("unidades",unidades);
	tmpDatos[3] = new Array("tipoprecio",tipoprecio);
	sendQuery("basket/gesbasket",tmpDatos);
}

function basketDelItem(codigo){
	tmpDatos = new Array();
	tmpDatos[0] = new Array("accion","del");
	tmpDatos[1] = new Array("idlibro",codigo);
	tmpDatos[2] = new Array("idpais",0);
	sendQuery("basket/gesbasket",tmpDatos);
}

function basketModItem(codigo,unidades,precio){
	if((unidades!="")&&(unidades>0)){
		tmpDatos = new Array();
		tmpDatos[0] = new Array("accion","mod");
		tmpDatos[1] = new Array("idlibro",codigo);
		tmpDatos[2] = new Array("unidades",unidades);
		tmpDatos[3] = new Array("precio",precio);
		tmpDatos[4] = new Array("idpais",0);
		sendQuery("basket/gesbasket",tmpDatos);
	}else{
		basketDelItem(codigo);
	}
}

function favoritosAddItem(codigo){
	tmpDatos = new Array();
	tmpDatos[0] = new Array("accion","add");
	tmpDatos[1] = new Array("idlibro",codigo);
	sendQuery("basket/gesfavoritos",tmpDatos);
}

function favoritosDelItem(codigo){
	tmpDatos = new Array();
	tmpDatos[0] = new Array("accion","del");
	tmpDatos[1] = new Array("idlibro",codigo);
	sendQuery("basket/gesfavoritos",tmpDatos);
}

function calculaPortes(idpais,peso){
	tmpDatos = new Array();
	tmpDatos[0] = new Array("idpais",idpais);
	tmpDatos[1] = new Array("peso",peso);
	sendQuery("basket/gesportes",tmpDatos);
}

function calculaPortesF(idpais,peso){
	tmpDatos = new Array();
	tmpDatos[0] = new Array("idpais",idpais);
	tmpDatos[1] = new Array("peso",peso);
	sendQuery("basket/gesportesF",tmpDatos);
}

function abrePop(direccion){
	v = window.open(direccion,"pop","height=300,width=400,left=0,top=0,scrollbars=yes");
}

function vcentrada(nombre,name,x,y){
	posx = Math.floor(screen.width / 2)-Math.floor(x/2);
	posy = Math.floor(screen.height / 2)-Math.floor(y/2);
	v = window.open(nombre,name,'scrollbars=no,toolbar=no,directories=no,menubar=no,resizable=no,height=' + y + ',width=' + x + ',left=' + posx + ',top=' + posy);
}

var objFluids,cObjFluids;

document.observe("dom:loaded", function() {
	objFluids = $$('div.fluid');
	cFluids = objFluids.length;
	redimFluid();
	initAcordeon();
});

/*Event.observe(window, "load", function(){
	objFluids = $$('div.fluid');
	cFluids = objFluids.length;
	redimFluid();
 } );*/

Event.observe(window, "resize", function(){ redimFluid(); } );

function redimFluid(){
	for(var j=0;j<cFluids;j++){
		var tamUtil = objFluids[j].getWidth();
		if(typeof(objFluids[j].down('.cajaItem'))!='undefined'){
			var tamItem = objFluids[j].down('.cajaItem').getWidth();
		}else{
			var tamItem = objFluids[j].down('.cajaItemOtros').getWidth();
		}
		var espItem = 20;
		var minItems = 1;
		var maxItems = Math.floor(tamUtil / (tamItem + espItem));
		if(maxItems<minItems){
			maxItems = minItems;
		}
		var porcItem = Math.floor(99 / maxItems);
		hijosFluidos = objFluids[j].childElements();
		cHF = hijosFluidos.length;
		for(var f=0;f<cHF;f++){
			if(f>(maxItems-1)){
				hijosFluidos[f].style.zIndex=10;
				hijosFluidos[f].style.display='none';
			}else{
				hijosFluidos[f].style.zIndex=100;
				hijosFluidos[f].style.width = porcItem + '%';
				hijosFluidos[f].style.display='';
			}
		}
	}
}

function initAcordeon(){
	if(document.getElementById('sm')!=null)slideMenu.build('sm',50,10,10,1);
	if(document.getElementById('sm1')!=null)slideMenu.build('sm1',50,10,10,1);
}

var slideMenu=function(){
	var sp,st,t,m,sa,l,w,sw,ot;
	return{
		build:function(sm,sw,mt,s,sl,h){
			sp=s; st=sw ; t=mt;
			m=document.getElementById(sm);
			sa=m.getElementsByTagName('li');
			l=sa.length; w=99; sw=w/l;
			ot=Math.floor(((w-st)/(l-1))*1000)/1000; var i=0;
			for(i;i<l;i++){s=sa[i]; s.style.width=sw+'%'; this.timer(s)}
			if(sl!=null){m.timer=setInterval(function(){slideMenu.slide(sa[sl-1])},t)}
		},
		timer:function(s){s.onmouseover=function(){clearInterval(m.timer);m.timer=setInterval(function(){slideMenu.slide(s)},t)}},
		slide:function(s){
			var cw=Math.floor(parseFloat(s.style.width,'10')*1000)/1000;
			if(cw<st){
				var owt=0; var i=0;
				for(i;i<l;i++){
					if(sa[i]!=s){
						var o,ow; var oi=0; o=sa[i]; ow=Math.floor(parseFloat(o.style.width,'10')*1000)/1000;
						if(ow>ot){oi=Math.floor((ow-ot)/sp); oi=(oi>0)?oi:1; o.style.width=(ow-oi)+'%'}
						owt=owt+(ow-oi)}}
				s.style.width=(w-owt)+'%';
			}else{clearInterval(m.timer)}
		}
	};
}();
