var goToAd = null;

var homeSlide = new Class({
	Version: "1.3",
	Implements: [Options, Events],
	options: {
		holder: ".holder",
		elementsParent: "ul",
		elements: "li",
		nextItem: ".next",
		prevItem: ".prev",
		stop: ".stop",
		start: ".start",
		speed: 600,
		duration: 4000,
		steps: 1,
		current: 0,
		transition: "sine:in:out",
		direction: "horizontal",
		mode: "callback",
		currentClass: "current",
		nextDisableClass: "next-disable",
		prevDisableClass: "prev-disable",
		paging: false,
		pagingEvent: "click",
		pagingHolder: ".paging",
		random: false,
		autoplay: false,
		autoplayOpposite: false,
		stopOnHover: true
		/* 
		onStart: $empty,
		onPlay: $empty,
		*/ 
	},
	initialize: function(gallery, options) {
		if(gallery.length == null) this.gallery = gallery;
		else this.gallery = gallery[0];
		if(!this.gallery) return false;
		this.setOptions(options);
		
		// items
		this.items = this.gallery.getElements(this.options.elements);
		
		// size
		this.size = this.items[0].getSize().y;
		
		// fx
		this.fx = new Fx.Tween(this.gallery, {'duration':this.options.speed,'property':'top'}).set(0);
		
		this.current = this.options.current;
		this.play(this.current);
	},
	play: function(num) {
		var temp = this.current;
		this.current = num;
		//if(this.current == temp) return false;
		var diff = temp-this.current;
		if(diff > 0) diff -= this.items.length;
		//echo('temp'+temp+' num'+this.current+' diff'+diff);
		if(this.current >= this.items.length) this.current -= this.items.length;
		this.fx.start(this.size*diff).chain(function() {
			for(var i=diff; i<0; i++) {
				if(temp >= this.items.length) temp = 0;
				this.items[temp++].inject(this.gallery, "bottom");
			}
			this.fx.set(0);
		}.bind(this));
	}
});

window.addEvent('domready',function(){
	
	var listSlide = [];
	$$(".thumbs li").each(function(item,index) {
		listSlide.include(item.getProperty('id').replace('slide-',''));
	});
	
  var thumbs = $$(".thumbs li");
  if(thumbs) {
  var gallery = new fadeGallery($$(".sliderEco"), {
  	'speed': 500,
  	'current': 0,
	'stopOnHover': false,
	'autoplay': true,
  	'duration': 8000, ////////////////////////////////!\\\ 8000!
	'onStart': function() {
	},
	'onPlay': function() {
		//this.fireEvent("start");
		thumbsGallery.play(this.current+1);
		
		active = this.current;
		$$(".sliderEco li.active").removeClass('active');
		//lis.removeClass('actif');
		$$(".sliderEco li")[active].addClass('active');
		
		/*.each(function(item,index) {
			console.debug('index:'+index+' - current:'+active);
			item.addClass('active');
			if(active != index) {
			//console.debug('pas ok!');
				item.removeClass('active');
			//console.debug('ok!');
			}
		});
		/*
		var i = this.current;
		if(i >= thumbsGallery.items.length) {
			i=0;
		}
		thumbsGallery.current = i;
		thumbsGallery.play(true);
		*/
	}
  });
	
	
	var thumbsGallery = new homeSlide($$(".thumbsContainer ul"), {
		'speed': 500,
		'duration': 8000,
		'mode': "circle",
		'direction': "vertical",
		'stopOnHover': false,
		//'autoplay': true,
		'holder': ".holderThumbs"
	});
	
  /*
	var thumbsGallery = new slideGallery($$(".thumbsContainer"), {
		'speed': 500,
		'duration': 8000,
		'mode': "circle",
		'direction': "vertical",
		'stopOnHover': false,
		//'autoplay': true,
		'holder': ".holderThumbs",
		'onStart': function() {
			//thumbs.removeClass("active");
			//thumbs[this.current].addClass("active");
		},
		'onPlay': function() {
			//this.fireEvent("start");
			var i = gallery.current+1;
			if(i >= gallery.items.length) {
				i=0;
			}
			gallery.current = i;
			gallery.play(true);
		}
	});
	*/
  
	goToAd = function(id) {
		
		id = ''+id;
		i = listSlide.indexOf(id);
		
		if(i != -1) {
			clearTimeout(gallery.timer);
			gallery.current = i;
			//while(gallery.current != i) {
				gallery.play(true);
			//}
			/*
			clearTimeout(thumbsGallery.timer);
			while(thumbsGallery.current != i) {
				thumbsGallery.nextSlide();
			}
			*/
		}
	}
	
	// check url
	var partenaireURI = new URI(window.location.href);
	pi1 = partenaireURI.getData('tx_rggooglemap_pi1');
	if(pi1) {
		id = pi1.poi;
		if(id) {
			goToAd(id);
		}
	}
  }
	
	var hideHoover = function() {
		var hoov = $('hoover');
		if(hoov){
			hoov.dispose();
		}
	};
	setTimeout(hideHoover, 2000);
});
