/* marzia - 2008/03/10
	funzione che fa lo slide tra contenuti nello stesso box. I contenuti sono presi da un json, la cui url e' passata come parametro.
*/

var SliderBox = Class.create();
SliderBox.prototype = {
	initialize: function(id, json, startFromIndex) {
		this.id = id;
		this.currentIndex = startFromIndex == undefined ? 0 : startFromIndex;
		// immagine per il loading, poi viene tolta col primo change image su onSuccess
		var loading = '<img src="/informativos/res/images/loading.gif" style="margin-top: 140px; margin-left: 150px; padding: 0px;">'
		new Insertion.Top($(this.id), loading);
		this.gallery = new Gallery(this.id, this.currentIndex);
		
		new Ajax.Request(json, {
				onSuccess: this.launchGallery.bind(this),
				onFailure: function(){
					$(this.id).update('<div style="margin-top: 140px; text-align: center;">Error</div>');
				}
		});
	},
	
	launchGallery: function(transport){
		eval(transport.responseText); // defines galleryItems
		this.gallery.start(galleryItems);
	}
};

var Gallery = Class.create();
Gallery.prototype = {
	initialize: function(id, currentIndex) {
		this.id = id;
		this.currentIndex = currentIndex;
		this.preloadedImage = null;		
	},

	start: function(galleryItems){
		this.galleryItems = galleryItems;
		if (this.currentIndex >= this.galleryItems.images.length) {
			// se l'indice che arriva come parametro dalla pagina e' fuori dal limite dell'array, allora parto da 0
			this.currentIndex = 0;
		}
		this.changeImage(this.currentIndex);
		// aggancio gli eventi sugli onClick delle freccie
		Event.observe($(this.id).adjacent('div.footbox')[0].select('p.nav a')[0], 'click', function() {
			this.changeImage(this.currentIndex -1);
		}.bind(this));
		Event.observe($(this.id).adjacent('div.footbox')[0].select('p.nav a')[1], 'click', function() {
			this.changeImage(this.currentIndex +1);
		}.bind(this));
	},
	
	preloadImg: function(index){
		this.preloadedImage = new Image();
		this.preloadedImage.src = this.galleryItems.images[index].src + '?tag='+ Math.round(new Date());
		this.preloadedImage.alt = this.galleryItems.images[index].title;
	},
	
	updateImg: function(index) {
		$('img' + this.id + index).update(this.preloadedImage);
	},
	
	delay: function(millisec) {
		var date = new Date(); 
		var curDate = null;
		do {curDate=new Date();} while (curDate-date < millisec);
	},
	
	changeImage: function(index){
		//console.log("change image");
		// Check dell'index: bordo superiore
		if (index == this.galleryItems.images.length) {
			index = 0; // ricomincia daccapo
		}
		// check dell'index: bordo inferiore
		if (index < 0) {		
			index = this.galleryItems.images.length -1;
		}
		//console.log("before preload");
		this.preloadImg(index); // precarico l' immagine
		
		/* struttura dell'html contenuto del box che mi viene passato:
			<a href=""><img src="resources/image288x265.jpg" alt="image288x265.jpg, 12kB" title="Image288x265" border="0" /></a>
			<h4>Cultura</h4>
			<h3><a href="#">Natalia Bessmértnova, ext primera bailarina del Bolshói</a></h3>
		*/
		
		var htmlString = '<a href=\"' + this.galleryItems.images[index].link + '\" id="img' + this.id + index + '">' 
			//+ '<img src="' + this.galleryItems.images[index].src + '?tag='+Math.random() + '" alt="' + this.galleryItems.images[index].title + '" title="' + this.galleryItems.images[index].title + '">'
			+ '</a>'
			+ '<h4>' + this.galleryItems.images[index].section + '</h4>'
			+ '<h3>' + '<a href="' + this.galleryItems.images[index].link + '">' + this.galleryItems.images[index].title + '</a></h3>';
		
		$(this.id).update(htmlString);	
		
		//for(var i =0; i < 1500000; i++) 	;  // ciclo di delay;
		this.delay(1000);
		
		//console.log("update html: " + htmlString);
		$('img' + this.id + index).update(this.preloadedImage);
				
		this.currentIndex = index;
		
		// precarico le immagini adiacenti 
		//prev img
		var previndex = index - 1;
		// check dell'index: bordo inferiore
		if (previndex < 0) {		
			previndex = this.galleryItems.images.length -1;
		}
		this.preloadImg(previndex);
		//next img
		var nextindex = index + 1;
		if (nextindex >= this.galleryItems.images.length) {
			nextindex = 0; // ricomincia daccapo
		}		
		this.preloadImg(nextindex);
	}
};


/* nuovo slider con DIV nascosti  per ovviare al baco odioso di ie nel preload delle immagini */
var SliderDivBox = Class.create();
SliderDivBox.prototype = {
	initialize: function(base_id, index) {
		this.base_id = base_id;
		this.index = index;
		
		Event.observe($(this.base_id).adjacent('div.footbox')[0].select('p.nav a')[0], 'click', function() {
			this.changeImage(this.index -1);
		}.bind(this));
		Event.observe($(this.base_id).adjacent('div.footbox')[0].select('p.nav a')[1], 'click', function() {
			this.changeImage(this.index +1);
		}.bind(this));
	},
	
	changeImage: function(index){
		//console.log('index: '+index);
		// hide all, then show the current index
		var Slides = $(this.base_id).select('div');
		
		//console.log("tot slides: " +Slides.length);
		
		for (i = 0; i < Slides.length; i++) {
			$(this.base_id + "_" + (i +1)).hide();
		}
		if (index <= 0) { 
			index = Slides.length 		
		}
		if (index > Slides.length) {
			index = 1;
		}
		//console.log("cur id: " + this.base_id + "_" + index);
		$(this.base_id + "_" + index).show();
		this.index = index;
	}
};

