/*  
DM_INFOSLIDER Extension
Author: Kai Tallafus <kai.tallafus@gmx.at>

JS-Code BASED ON:
JAVASCRIPT IMAGE GALLERY W/ mootools
(Devin Ross)
*/

window.addEvent('domready', function() {
		
		var pos = 0;
		var slides = Math.ceil(numberOfItems/itemsPerSlide); // Values are set from outside - PHP generated
		var offset = itemsPerSlide*itemWidth;	// HOW MUCH TO SLIDE WITH EACH CLICK
		var currentslide = 1;	// CURRENT SLIDE IS THE FIRST SLIDE
		
		/* THUMBNAIL IMAGE SCROLL */
		var imgscroll = new Fx.Scroll('thumbwrapper', {
   			offset: {'x': 0, 'y': 0},
   			transition: Fx.Transitions.Cubic.easeOut	// HOW THE SCROLLER SCROLLS
		}).toLeft();
		
		checkButtonVisibility(currentslide, slides);
	
		/* EVENTS - WHEN AN ARROW IS CLICKED THE THUMBNAILS SCROLL */
		if ($('moveleft')) {
  		$('moveleft').addEvent('click', function(event) { event = new Event(event).stop();
  			if(currentslide == 1) return;
  			currentslide--;					// CURRENT SLIDE IS ONE LESS
  			pos += -(offset);				// CHANGE SCROLL POSITION
  			checkButtonVisibility(currentslide, slides);
  			//imgscroll.start(pos);			// SCROLL TO NEW POSITION (MT 1.2)
  			imgscroll.scrollTo(pos, 0); // Mootools 1.11
  		});
  	}
  	if ($('moveright')) {
  		$('moveright').addEvent('click', function(event) { event = new Event(event).stop();
  			if(currentslide >= slides) return;
  			currentslide++;
  			pos += offset;
  			checkButtonVisibility(currentslide, slides);
  			//imgscroll.start(pos); 
  			imgscroll.scrollTo(pos, 0); // Mootools 1.11
  		});
  	}
	
});

// AJAX Loading...
function show(uid, a) {
	
	var loader = $(a).getPrevious('span.loader');
	if ($(loader)) $(loader).setStyle('visibility', 'visible'); // loader-gif einblenden
	
	var thumb = $(loader).getNext('img');
	$(thumb).setStyle('opacity', '0.5'); // Thumbnail faden
	
	var fullimg = $('fullimg');	
	var fullimgfx = fullimg.effects({duration: 350, transition: Fx.Transitions.Sine.easeOut}); // Mootools 1.11
	
	var infodata = $('infodata');	
	var infodatafx = infodata.effects({duration: 350, transition: Fx.Transitions.Sine.easeOut}); // Mootools 1.11
	
	// Fade out
	fullimgfx.start({ 
							'opacity' : 0													
	});
	infodatafx.start({ 
							'opacity' : 0													
	});
	
	new Ajax(ajaxurl, {
		data:'action=getinfo&uid='+uid,
		method: 'post',
		evalScripts: true,
		update: $('infodata'),
		onSuccess: function(responseText){
			
			new Ajax(ajaxurl, {
				data:'action=fullimg&uid='+uid,
				method: 'post',
				update: $('fullimg'),
				onSuccess: function(responseText){
						
						// if both tasks finished, fade in
						infodatafx.start({ 
												'opacity' : 1													
						});
						fullimgfx.start({ 
												'opacity' : 1													
						});
						
						if ($(loader)) $(loader).setStyle('visibility', 'hidden');
						$(thumb).setStyle('opacity', '1');
						
				}
			}).request();
			
		}
	}).request();
	
	
}

function checkButtonVisibility(currentslide, slides) {
	if (slides > 1) {
		if (currentslide == 1) {
			$('moveright').removeClass('hidebuttons');
			if (!$('moveleft').hasClass('hidebuttons')) {
				$('moveleft').addClass('hidebuttons');
			}
		}
		if(currentslide > 1) {
			$('moveleft').removeClass('hidebuttons');
		}
		if (currentslide == slides) {
			if (!$('moveright').hasClass('hidebuttons')) {
				$('moveright').addClass('hidebuttons');
			}
		}
	}
}

// Mootools tooltips
window.addEvent('domready', function(){
	var thetip = new Tips($$('.tips'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			if ($(toolTip).getElement('div').innerHTML)
				this.fx.start(1);
		},
		onHide: function(toolTip) {
			if ($(toolTip).getElement('div').innerHTML)
				this.fx.start(0);
		}
	});
}); 

