$(document).ready (function () {

	if ($('#slideshow').length > 0 || $('#galflash').length > 0) {	// galeria home o fitxa

		var ss;
		var height;
		var bg;

		if ($('#slideshow').length > 0) {
			ss = $('#slideshow');
			height = '336px';
			bg = 'url(/img/homeGalBg4.gif) no-repeat center center'
		} else {
			ss = $('#galflash');
			height = '180px';
			bg = 'url(/img/fichaGalBg4.gif) no-repeat center center'
		}

		ss.css({
			'height'		: height,
			'background'	: bg
		});

		var listaItems = new Array ();
		var casaActual = 0;
		var fotoActual = -1;

		var masInfo;

		var tempo;

		var lang = ss.attr('title').split('][')[0];
		var url = ss.attr('title').split('][')[1];

		var dir;

		ss.removeAttr ('title');

		$.get(url,function (data) {	// xml carregat
			ss.css ('background','none');

			if (ss.attr('id') == 'slideshow') {

				masInfo = $(data).find('boton').find(lang).text ();

				$('casa',data).each (function () {	// creem array amb les cases i catxegem
				    listaItems.push ($(this));
					$('body').append ('<img style="display:none" src="'+$(this).find('fotos:eq(0) foto').attr('src')+'"></img>');
				})

				setCasa ();

			} else {

				$('foto',data).each (function () {	// creem array amb les fotos
					listaItems.push ($(this));
				})

				setFotos ();

			}

		}, "xml");

	}

	function setCasa () {	// apliquem la casa actual

		clearTimeout (tempo);

		if (ss.find('img').length > 0) {
			ss.empty ();
			fotoActual = -1;
		}

		$('fotos',listaItems[casaActual]).each (function () {
			ss.append ('<img src="'+ $(this).find('foto').attr('src') +'" alt="'+listaItems[casaActual].find('nombre ' + lang).text()+'" />');
			ss.find('img:last-child').hide ().css ('position','absolute');
		})

		ss.append ('<div><a href="#"></a><span><span><strong>'+listaItems[casaActual].find('nombre ' + lang).text()+'</strong>'+listaItems[casaActual].find('descripcion ' + lang).text()+'</span><a href="'+listaItems[casaActual].find('enlace ' + lang).text()+'">'+masInfo+'</a></span><a href="#"></a></div>');

		ss.find('div:last-child').css ({
			'width'				: '500px',
			'height'			: '116px',
			'left'				: '430px',
			'top'				: '210px',
			'position'			: 'relative',
			'color'				: '#4D4D4D'
		});
		ss.find('div > span').css ({
			'background'		: 'url(/img/homeGalBg2.png) no-repeat center center',
			'background-image'	: 'url(/img/homeGalBg.png)',
			'width'				: '427px',
			'height'			: '100%',
			'margin'			: '0 24px',
			'font-size'			: '1.5em',
			'overflow'			: 'hidden',
			'position'			: 'relative',
			'padding'			: '0 10px 0 15px'
		});
		ss.find('span span').css ({
			'position'			: 'absolute',
			'left'				: '15px',
			'right'				: '10px',
			'top'				: '0',
			'height'			: '80px',
			'overflow'			: 'hidden'
		})
		ss.find('div > a').css ({
			'background'		: 'url(/img/homeGalBg.png) no-repeat 0 0',
			'position'			: 'absolute',
			'left'				: '0',
			'top'				: '0',
			'width'				: '24px',
			'height'			: '100%'
		});
		ss.find('div > a:last-child').css ({
			'background'		: 'url(/img/homeGalBg.png) no-repeat right 0',
			'left'				: 'auto',
			'right'				: '0'
		});
		ss.find('strong').css ({
			'display'			: 'block',
			'padding'			: '.6em 0 .1em',
			'font-weight'		: 'bold',
			'font-size'			: '1.5em'
		});
		ss.find('span a').css ({
			'position'			: 'absolute',
			'left'				: '15px',
			'bottom'			: '10px',
			'font-size'			: '.78em',
			'color'				: '#223E47',
			'background'		: '#C3CDD0 url(/img/homeGalBg3.gif) no-repeat right center',
			'padding'			: '.15em 30px .1em 5px',
			'text-decoration'	: 'none',
			'-moz-border-radius': '4px',
			'border-radius'		: '4px'
		});

		if (dir && dir < 0) ss.find('span span').css ('left','455px')
		else if (dir) ss.find('span span').css ('left','-455px')

		ss.find('span span').animate({left:'15px'},'slow');
		ss.hover (function () {ss.find('div > a').fadeIn ()},function () {ss.find('div > a').fadeOut ()})
		ss.find('div > a').hover (function () {$(this).css('background-image','url(/img/homeGalBg2.png)')},function () {$(this).css('background-image','url(/img/homeGalBg.png)')});
		ss.find('span a').hover (function () {$(this).css('background-color','#fff')},function () {$(this).css('background-color','#C3CDD0')});

		ss.find('div > a:first-child').click (function (e) {
			ss.css('background-image','url('+$(listaItems[casaActual]).find('fotos:eq('+fotoActual+') foto').attr('src')+')');
			dir = 1;

			casaActual --;
			if (casaActual < 0) casaActual = listaItems.length-1;
			setCasa ();

			$(this).blur ();
			e.preventDefault ();
		});

		ss.find('div > a:last-child').click (function (e) {
			ss.css('background-image','url('+$(listaItems[casaActual]).find('fotos:eq('+fotoActual+') foto').attr('src')+')');
			dir = -1;

			casaActual ++;
			if (casaActual == listaItems.length) casaActual = 0;
			setCasa ();

			$(this).blur ();
			e.preventDefault ();
		});

		setFoto ();
	}

	function setFoto () {	// transició de foto

		if (ss.attr('id') == 'slideshow') {
			fotoActual ++;
			if (fotoActual == ss.find('img').length) fotoActual = 0;
		}

		var casa = casaActual;

		ss.find('img').fadeOut ('slow');
		ss.find('img:eq('+fotoActual+')').fadeIn ('slow',function () {

			if (ss.attr ('id') == 'slideshow' && casa == casaActual) {
				ss.css('background','none');
				tempo = setTimeout (setFoto,5000);
			}
		});

		if (ss.attr ('id') == 'galflash') {

			if (dir && dir < 0) ss.find('span span').css ('left','455px')
			else if (dir) ss.find('span span').css ('left','-455px')

			ss.find('span span').html (listaItems[fotoActual].find('titulo ' + lang).text());

			ss.find('span span').animate({left:'22px'},'slow');

		}

	}

	function setFotos () {	// muntem fotos (fitxa)

		$(listaItems).each (function () {
			ss.append ('<img src="'+ $(this).attr('src') +'" alt="'+$(this).find('titulo ' + lang).text()+'" />');
			ss.find('img:last-child').hide ().css ('position','absolute');
		})

		ss.append ('<div><a href="#"></a><span><span></span></span><a href="#"><a href="#"></a></div>');

		ss.find('div:last-child').css ({
			'height'			: '26px',
			'top'				: '154px',
			'position'			: 'relative',
			'color'				: '#4D4D4D'
		});
		ss.find('div > span').css ({
			'background'		: 'url(/img/fichaGalBg2.png) no-repeat 0 0',
			'background-image'	: 'url(/img/fichaGalBg3.png)',
			'height'			: '100%',
			'overflow'			: 'hidden',
			'position'			: 'relative',
			'font-size'			: '1.1em'
		});
		ss.find('span span').css ({
			'position'			: 'absolute',
			'left'				: '22px',
			'right'				: '22px',
			'top'				: '6px',
			'height'			: '26px',
			'overflow'			: 'hidden'
		})
		ss.find('div > a:first-child,div > a:last-child').css ({
			'background'		: 'url(/img/fichaGalBg.png) no-repeat 0 0',
			'position'			: 'absolute',
			'left'				: '0',
			'top'				: '0',
			'width'				: '15px',
			'height'			: '100%',
			'z-index'			: '5'
		});
		ss.find('div > a:last-child').css ({
			'background'		: 'url(/img/fichaGalBg.png) no-repeat right 0',
			'left'				: 'auto',
			'right'				: '0'
		});
		ss.find('div > a:eq(1)').css ({
			'position'			: 'absolute',
			'right'				: '10px',
			'top'				: '-30px',
			'width'				: '21px',
			'height'			: '21px',
			'background'		: 'url(/img/fichaGalBg5.gif) no-repeat'
		});

		ss.hover (function () {ss.find('div > a').fadeIn ()},function () {ss.find('div > a').fadeOut ()})
		ss.find('div > a:first-child,div > a:last-child').hover (function () {$(this).css('background-image','url(/img/fichaGalBg2.png)')},function () {$(this).css('background-image','url(/img/fichaGalBg.png)')});
		ss.find('div > a:eq(1)').hover (function () {$(this).css('background-image','url(/img/fichaGalBg6.gif)')},function () {$(this).css('background-image','url(/img/fichaGalBg5.gif)')});

		ss.find('div > a:first-child').click (function (e) {
			dir = 1;

			fotoActual --;
			if (fotoActual < 0) fotoActual = listaItems.length-1;

			setFoto ();

			$(this).blur ();
			e.preventDefault ();
		});

		ss.find('div > a:last-child').click (function (e) {
			dir = -1;

			fotoActual ++;
			if (fotoActual == listaItems.length) fotoActual = 0;

			setFoto ();

			$(this).blur ();
			e.preventDefault ();
		});

		ss.find('div > a:eq(1)').click (function (e) {
			pic.loadPic (listaItems[fotoActual].attr('link'));

			$(this).blur ();
			e.preventDefault ();
		})

		fotoActual = 0;

		setFoto ();
	}

})


