Element.extend(function() {
	var slides = [], self, settings = {
		timeout: '2000',
		type: 'sequence'
	};

	function next(current, last) {
		$A(slides).each(function(slide, i) { slides[i].setStyle('display', 'none'); });
		slides[last].setStyles({
			display: 'block',
			zIndex: 0
		});
		slides[current].setOpacity(0).setStyles({
			display: 'block',
			zIndex: 1
		}).effect('opacity').custom(0, 1.0);

		if ( settings.type == 'sequence' ) {
			if ( ( current + 1 ) < slides.length ) {
				current = current + 1;
				last = current - 1;
			}
			else {
				current = 0;
				last = slides.length - 1;
			}
		}
		else if ( settings.type == 'random' ) {
			last = current;
			while (	current == last ) {
				current = Math.floor ( Math.random ( ) * ( slides.length ) );
			}
		}
		else {
			alert('type must either be \'sequence\' or \'random\'');
		}
		setTimeout((function(){next(current, last);}), settings.timeout);
	}
	return {
		slideshow: function() {
			self = this;
			this.setStyles({
				display: 'block',
				position: 'relative'
			});
			var imgs = this.getElements('img'), cid = this.id;
			$A(imgs).each(function(img, i){
				$(img).setStyle('display', 'none');
       			slides[i] = $(document.createElement('div')).setProperty('id', cid+'-slide'+i).setStyles({
					background: '#FFF url('+img.src+') no-repeat center top',
					zIndex: imgs.length-i,
					position: 'absolute',
					top: 0,
					left: 0,
					height: img.height,
					width: img.width
				}).addClass("slide");
				//alert(self.id);
				self.adopt(slides[i]);
   			});

   			if (imgs.length == 1) return;

			if ( settings.type == 'sequence' ) {
				setTimeout(function(){
					next(1, 0);
				}, settings.timeout);
			} else if ( settings.type == 'random' ) {
				setTimeout(function(){
					do { current = Math.floor ( Math.random ( ) * ( slides.length ) ); } while ( current == 0 )
					next(current, 0);
				}, settings.timeout);
			} else {
				alert('type must either be \'sequence\' or \'random\'');
			}
		}
	}
}());

var GOOGLE_MAPS_APIKEY = "ABQIAAAAN-9J1beC873KRHV24wJI2hR1wfQmy4KIobjbjjF-vHIFy5geOBRQZWZc2Y3JLCSL_9svm1Dj3ucaUw";
if (section == 'localizacao')
	document.write('<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;t=h&amp;key='+GOOGLE_MAPS_APIKEY+'"></script>');

Window.onDomReady(function() {
	if (section == 'inicio') initSlideshow();

	var containers = [$('nav-fotos').getElement('ol'), $('nav-produtos').getElement('ol')];
	var togglers = [$('nav-fotos').getElement('a'), $('nav-produtos').getElement('a')];
	togglers.each(function(toggler) {
		toggler.href = "#";
	});
	var menu = new Fx.Accordion(togglers, containers, {
		start: 'hidden'
	});

	if (section == 'fotos') menu.showThisHideOpen(0);
	if (section == 'produtos') menu.showThisHideOpen(1);

	if (section == 'localizacao') {
        var WINDOW_HTML = "<h3>M.P. Marques, Lda</h3><p>Rua Jos&eacute; D. Rolo, 102<br /><strong>Sismaria</strong> 2425-625 Monte Redondo</p>";
        if (GBrowserIsCompatible()) {
            $('mapa').setStyle('height', "300px");
            var map = new GMap2($("mapa"));
            map.setCenter(new GLatLng(39.879576, -8.868155), 8);
            map.addControl(new GSmallZoomControl());
            map.addControl(new GMapTypeControl());
            var marker = new GMarker(new GPoint(-8.868155, 39.879576));
            map.addOverlay(marker);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(WINDOW_HTML);
            });
            marker.openInfoWindowHtml(WINDOW_HTML);
        }
    }

});
