

function resize(imageSrc) {
	var img = new Image();
	img.src = imageSrc;
	parent.window.resizeTo(img.width + 30, img.height + 70);
}

function eliminarSuscripcionCesta(contextPath, idCol) {

	new Ajax.Request(
		contextPath + '/remove-suscripcion-cesta.do', {
		parameters: 'idCol=' + idCol,
		onComplete: function(request) {
			// Tambien actualizamos la cesta del menu izquierdo:
			aj_anadir_art.execute($('anadir_art'));
      	}
	});
}

function eliminarArticuloCesta(contextPath, idArt) {

	new Ajax.Request(
		contextPath + '/remove-articulo-cesta.do', {
		parameters: 'idArt=' + idArt,
		onComplete: function(request) {
			// Tambien actualizamos la cesta del menu izquierdo:
			aj_anadir_art.execute($('anadir_art'));
      	}
	});
}

function anadirArticuloCesta(contextPath, idArticulo, tapaArticulo) {

	// Si es un drag-and-drop, paramos el link antes de que se ejecute:
	if ($('articleLink_' +  idArticulo)) {
		Event.observe('articleLink_' +  idArticulo, 'click',
			function(event) { Event.stop(event); });
	}

	// Con esta request se anade el articulo a la cesta
	new Ajax.Request(
		contextPath + '/add-articulo-cesta.do', {
		parameters: 'idArt=' + idArticulo + '&quantitat=1',
		onComplete: function(request) {
			// La variable 'aj_anadir_art' se crea por el tag <ajax:htmlcontent>.
			// Aqui provocamos que se ejecute para refrescar la lista de articulos:
			aj_anadir_art.execute($('anadir_art'));
      	}
	});

	mostrarPortada(tapaArticulo);
}

function mostrarPortada(tapaArticulo) {
	$('shopping_cart').innerHTML = '<div id="art_anadido" align="center">' +
		'<img src="' + tapaArticulo + '" class="portada_ico" /></div>';
}

function actualizarArticuloCesta(contextPath, idArticulo, cantidad) {

	new Ajax.Request(
		contextPath + '/client/actualizar-articulo-cesta.do', {
		parameters: 'idArt=' + idArticulo + '&quantitat=' + cantidad,
		onComplete: function(request) {
			// Las variables aj_* se crean por el tag <ajax:htmlcontent>.
			// Aqui provocamos que se ejecute para actualizar la cantidad de un articulo:
			eval('aj_actualizar_art_' + idArticulo).execute($('actualizar_art_' + idArticulo));
			
			// Tambien actualizamos la cesta del menu izquierdo:
			aj_anadir_art.execute($('anadir_art'));
      	}
	});
}

function suscribirseRevista(contextPath, idColeccion, numeroInicial, logoRevista) {

	// Con esta request se hace la suscripcion:
	new Ajax.Request(
		contextPath + '/add-suscripcion.do', {
		parameters: 'idCol=' + idColeccion + '&numeroInicial=' + numeroInicial,
		onComplete: function(request) {
			// La variable 'aj_anadir_art' se crea por el tag <ajax:htmlcontent>.
			// Aqui provocamos que se ejecute para refrescar la lista de articulos:
			aj_anadir_art.execute($('anadir_art'));
      	}
	});
	
	mostrarLogo(logoRevista);
}

function mostrarLogo(logoRevista) {
	$('shopping_cart').innerHTML = '<div id="art_anadido" align="center"' +
	'<br/><img src="' + logoRevista + '" class="logo_ico" /></div>';
}

function registrarElementoCesta(idArticle) {
	new Draggable('article_' + idArticle, {revert:true});
}

function resetForm(formName) {

	var form = $(formName);

	for (i = 0; i < form.elements.length; i++) {

		var elem = form.elements[i];

		if (elem.type != 'button' &&
			elem.type != 'submit') {
			elem.value = '';
		}

		if (elem.type == 'select-one' ||
			elem.type == 'select-multiple') {
			elem.selectedIndex = 0;
		}

		if (elem.type == 'checkbox') {
			elem.checked = false;
		}
	}

}

function setCurrentDate(elem) {
	var dataArt = new Date();
	var dia = dataArt.getDate() < 10 ? '0' + dataArt.getDate() : dataArt.getDate();
	var mes = dataArt.getMonth() + 1 < 10 ? '0' + (dataArt.getMonth() + 1) : dataArt.getMonth() + 1;
	var any = dataArt.getFullYear();

	elem.value = dia + '/' + mes + '/' + any;
}

function changeVisibility(elementId) {

	var visibility = $(elementId).style.visibility;

	if(visibility == 'hidden') {
		$(elementId).style.visibility = 'visible';
	}
	else {
		$(elementId).style.visibility = 'hidden';
	}
}

function ImageExpander(oThumb, sImgSrc)
{
	// store thumbnail image and overwrite its onclick handler.
	this.oThumb = oThumb;
	this.oThumb.expander = this;
	this.oThumb.onclick = function() { this.expander.expand(); }

	// record original size
	this.smallWidth = oThumb.offsetWidth;
	this.smallHeight = oThumb.offsetHeight;

	this.bExpand = true;
	this.bTicks = false;

	// self organized list
	if ( !window.aImageExpanders )
	{
		window.aImageExpanders = new Array();
	}
	window.aImageExpanders.push(this);

	// create the full sized image.
	this.oImg = new Image();
	this.oImg.expander = this;
	this.oImg.onload = function(){this.expander.onload();}
	this.oImg.src = sImgSrc;
}

ImageExpander.prototype.onload = function()
{
	this.oDiv = document.createElement("div");
	document.body.appendChild(this.oDiv);
	this.oDiv.appendChild(this.oImg);
	this.oDiv.style.position = "absolute";
	this.oDiv.expander = this;
	this.oDiv.onclick = function() {this.expander.toggle();};
	this.oImg.title = "Click to reduce.";
	this.bigWidth = this.oImg.width;
	this.bigHeight = this.oImg.height;

	if ( this.bExpand )
	{
		this.expand();
	}
	else
	{
		this.oDiv.style.visibility = "hidden";
		this.oImg.style.visibility = "hidden";
	}
}
ImageExpander.prototype.toggle = function()
{
	this.bExpand = !this.bExpand;
	if ( this.bExpand )
	{
		for ( var i in window.aImageExpanders )
			if ( window.aImageExpanders[i] !== this )
				window.aImageExpanders[i].reduce();
	}
}
ImageExpander.prototype.expand = function()
{
	// set direction of expansion.
	this.bExpand = true;

	// set all other images to reduce
	for ( var i in window.aImageExpanders )
		if ( window.aImageExpanders[i] !== this )
			window.aImageExpanders[i].reduce();

	// if not loaded, don't continue just yet
	if ( !this.oDiv ) return;

	// hide the thumbnail
	this.oThumb.style.visibility = "hidden";

	// calculate initial dimensions
	this.x = this.oThumb.offsetLeft;
	this.y = this.oThumb.offsetTop;
	this.w = this.oThumb.clientWidth;
	this.h = this.oThumb.clientHeight;

	this.oDiv.style.left = this.x + "px";
	this.oDiv.style.top = this.y + "px";
	this.oImg.style.width = this.w + "px";
	this.oImg.style.height = this.h + "px";
	this.oDiv.style.visibility = "visible";
	this.oImg.style.visibility = "visible";

	// start the animation engine.
	if ( !this.bTicks )
	{
		this.bTicks = true;
		var pThis = this;
		window.setTimeout(function(){pThis.tick();},25);
	}
}
ImageExpander.prototype.reduce = function()
{
	// set direction of expansion.
	this.bExpand = false;
}
ImageExpander.prototype.tick = function()
{
	// calculate screen dimensions
	var cw = document.body.clientWidth;
	var ch = 1200;
	var cx = document.body.scrollLeft + cw / 2;
	var cy = document.body.scrollTop + ch / 2;

	// calculate target
	var tw,th,tx,ty;
	if ( this.bExpand )
	{
		tw = this.bigWidth;
		th = this.bigHeight;
		if ( tw > cw )
		{
			th *= cw / tw;
			tw = cw;
		}
		if ( th > ch )
		{
			tw *= ch / th;
			th = ch;
		}
		tx = cx - tw / 2;
		ty = cy - th / 2;
	}
	else
	{
		tw = this.smallWidth;
		th = this.smallHeight;
		tx = this.oThumb.offsetLeft;
		ty = this.oThumb.offsetTop;
	}
	// move 5% closer to target
	var nHit = 0;
	var fMove = function(n,tn)
	{
		var dn = tn - n;
		if ( Math.abs(dn) < 3 )
		{
			nHit++;
			return tn;
		}
		else
		{
			return n + dn / 10;
		}
	}
	this.x = fMove(this.x, tx);
	this.y = fMove(this.y, ty);
	this.w = fMove(this.w, tw);
	this.h = fMove(this.h, th);

	this.oDiv.style.left = this.x + "px";
	//this.oDiv.style.top = this.y + "px";
	this.oImg.style.width = this.w + "px";
	this.oImg.style.height = this.h + "px";

	// if reducing and size/position is a match, stop the tick
	if ( !this.bExpand && (nHit == 4) )
	{
		this.oImg.style.visibility = "hidden";
		this.oDiv.style.visibility = "hidden";
		this.oThumb.style.visibility = "visible";

		this.bTicks = false;
	}

	if ( this.bTicks )
	{
		var pThis = this;
		window.setTimeout(function(){pThis.tick();},25);
	}
}