/*
 * MIP Travel slideshow 1.1 - Javascript Image Switcher with mootools 1.2
 * By Mario Zambon (http://www.urbangap.com)
 * Copyright (c) 2008 cody urbangap
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

function mooswitch() {

	var dida 		= true 
	var pplay		= true
	var tImg 		= 500
	var tTxt 		= 1500
	var tAll 		= 6000+(tImg+tTxt)
	var luxBtMax	= 1
	var luxBtMin	= 0.8
	
	var loaderImage	= 'i/loading.gif'
	var imgBig		= $('imgBig')
	var th 			= $('imgTh')
	var box			= $('galleryHome')
	var a	 		= $$('#imgTh .th a')
	var aTxt	 	= $$('.none a')
	var li	 		= $$('#imgTh li')
	var dida		= $$('#imgTh .none')
	var img, txt, counter, i, slide, href, tthis, iAll, title

	th.addClass('dimensioneTh').set('opacity', 0)
	imgBig.addClass('dimensione')
	imgBig.set('styles', {'background': 'url('+loaderImage+') center no-repeat'})

	a.addEvent('click', $lambda(false))
	a.addEvent('click', function(event){
				
		var cl	= this.hasClass('imgSelect')
		tthis	= this
		href	= this.getProperty('href')
		n	= this.getElement('img').getProperty('class')
		slide	= n
		title 	= dida[n].get('html')
		
		a.removeClass('imgSelect')
		this.addClass('imgSelect')

		if(!cl){
			img.destroy()				
			imgBig.set('styles', {'background': 'url('+loaderImage+') center no-repeat'})
			a.set('opacity', luxBtMin)
			this.set('opacity', 1)
			rollover()
			preloadImage()
		}
	})

	a.each(function(item, index){iAll = index})
		
	function slider() {
		a.each(function(item, index){
			i = index
			a[i].removeClass('imgSelect')
			a[i].set('opacity', luxBtMin)
						
			if (i==slide) {
				tthis 	= a[i]
				href 	= a[i].getProperty('href')
				title 	= dida[i].get('html')
				
				a[i].addClass('imgSelect')
				a[i].set('opacity', 1)
	
				rollover()
				preloadImage()			
			}
		})
	}

	function preloadImage() {
		new Asset.image(href, {onload: loadImg})
	}

	function loadImg() {
		th.addClass('dimensioneTh').tween('opacity', 1)
		imgBig.set('styles', {'background-image': 'none', 'cursor': 'pointer'})
		imgBig.addEvents({
			'click': function(){
				var tagA = this.getElement('a')
				var lnk = tagA.getProperty('href')
				window.location=lnk
			}
		})
		
		/*if(img) img.destroy() 
		img = new Element('img', {'id':'image', 'alt': '', 'src': href}).set('opacity', 0).inject(imgBig)
		fx(img, tImg)
		img.tween('opacity', 1)
		
		if(txt) txt.destroy()
		txt	= new Element('div', {'id': 'txTitle', 'class': 'bgTxt'}).set('opacity', 0).inject(imgBig)
		fx(txt, tTxt)
		txt.set('html', '<table border="0" cellspacing="0" cellpadding="0"><tr><td valign="bottom"><div class="bgTxtII">'+title+'</div></td></tr></table>')
		txt.tween('opacity', 0.75)*/

		if(img) img.destroy() 
		img = new Element('div', {'id':'image', 'styles': {'width':'705px', 'height':'323px','background': 'url('+href+') 0 0 no-repeat', 'cursor': 'pointer'}}).set('opacity', 0).inject(imgBig)
		fx(img, tImg)
		img.tween('opacity', 1)
		
		if(txt) txt.destroy()
		txt	= new Element('div', {'id': 'txTitle', 'class': 'bgTxt'}).set('opacity', 0).inject(img)
		fx(txt, tTxt)
		txt.set('html', '<table border="0" cellspacing="0" cellpadding="0"><tr><td valign="bottom"><div class="bgTxtII">'+title+'</div></td></tr></table>')
		txt.tween('opacity', 0.75)



	}		
	
	function fx(el, tmp) {
		el.set('tween', {duration: tmp, transition: Fx.Transitions.Expo.easeInOut})
	}
	
	function rollover() {
		a.addEvents({
			'mouseover': function(){if(tthis!=this) this.tween('opacity',  luxBtMax)},
			'mouseleave': function(){if(tthis!=this) this.tween('opacity', luxBtMin)}
		})
	}

	function slideStop() {
		counter = $clear(counter)
		slideLoop()
	}

	function slideLoop() {
		if(pplay) {
			if(slide<iAll) {slide++} else {slide=0}
			slider()
		}
		counter = slideLoop.delay(tAll)
	}

	box.addEvents({
		'mouseover': function(){pplay = false},
		'mouseleave': function(){pplay = true}
	})
	
	
	if (!iAll) {
		slide = 0
		slider()
		//th.destroy()
	} else {
		slideLoop()
	}
}

window.addEvent('domready', mooswitch)
