window.addEvent ('domready', function() {
	domreadyGeneral ();

	var amagaDesc = new Fx.Tween('Descripcio', {
		onComplete: function() {
			$('Tancar').set('html', ($('Descripcio').getStyle('top').toInt() < 0) ? '[+] informaci&oacute;n' : '[x] cerrar');
		}
	});
	$('Tancar').addEvent('click', function(e){
		e.stop();
		amagaDesc.start('top', ($('Descripcio').getStyle('top').toInt() < 0) ? '0px' : '-80px');
	});

	if ($chk($('VorePreus'))) {
		$('Preus').fade ('hide');
		$('VorePreus').addEvent('click', function(e){
			e.stop();
			$('Preus').fade ('toggle');
		});
	}

	// amagar els divs d'articuls de mes
	$$('.botoMes').each (function (e) {
		_n = $(e).get ('id');
		$("d" + _n.substr (1)).setStyle ('display', 'none');
	});

	// mirar si he seleccionat un articul
	if ($chk(acordeoSG) && $chk(acordeoSG) && (acordeoSN > 1)) {
		$('dItems'+acordeoSG+'c'+1).setStyle ('display', 'none');
		$('dItems'+acordeoSG+'c'+acordeoSN).setStyle ('display', 'block');
	}

	// events dels botons mes i manco
	$$('.botoMes').addEvent ('click', function (e) {
		e.stop();
		_n = $(e.target).get ('id');
		_d_ant = "d" + _n.substr (1);
		_d = _d_ant.split ('c');
		_d_act = _d[0] + 'c' + (_d[1].toInt() - 1);
		$(_d_ant).setStyle ('display', 'block');
		$(_d_act).setStyle ('display', 'none');
	});
	$$('.botoManco').addEvent ('click', function (e) {
		e.stop();
		_n = $(e.target).get ('id');
		_d_ant = "d" + _n.substr (1);
		_d = _d_ant.split ('c');
		_d_act = _d[0] + 'c' + (_d[1].toInt() + 1);
		$(_d_ant).setStyle ('display', 'block');
		$(_d_act).setStyle ('display', 'none');
	});

	abrePrimeraVolta = true;

	//create our Accordion instance
	var abreGrup = new Accordion($('span.Nivell2'), 'a.grup2', 'ul.Nivell3', {
		display: acordeoGrup,
		height: true,
		opacity: false,
		onActive: function(toggler, element){
			toggler.addClass ('seleccionat');
			$(toggler.parentNode.parentNode).addClass ('seleccionat');
			$(toggler.parentNode.parentNode).setStyle ('height', 'auto');
		},
		onBackground: function(toggler, element){
			toggler.removeClass ('seleccionat');
			$(toggler.parentNode.parentNode).removeClass ('seleccionat');
		},
		onComplete: function() {
			var el = $(this.elements[this.previous]);
			if (el.offsetHeight > 0) el.setStyle('height', 'auto');
		}
	});

	//create our Accordion instance
	var abreCatalec = new Accordion($('span.Nivell1'), 'a.grup1', 'ul.Nivell2', {
		display: acordeoCatalec,
		height: true,
		opacity: false,
		onActive: function(toggler, element){
			toggler.addClass ('seleccionat');
			$(toggler.parentNode.parentNode).addClass ('seleccionat');
			if (!abrePrimeraVolta) abreGrup.display (acordeoPrimer[this.togglers.indexOf(toggler)]);
			abrePrimeraVolta = false;
		},
		onBackground: function(toggler, element){
			toggler.removeClass ('seleccionat');
			$(toggler.parentNode.parentNode).removeClass ('seleccionat');
		},
		onComplete: function() {
			var el = $(this.elements[this.previous]);
			if (el.offsetHeight > 0) el.setStyle('height', 'auto');
		}
	});
});
