/*

	--------------------------------------------
	Vamos
	Image fading library for all pages
	Created: 23rd November 2011
	--------------------------------------------

*/

function imageFadeControl(fadeT, pauseT, pauseAfterViewT) {

	var imagesToFade = null;
	var selectorImages = null;
	var timer = null;
	var imagePtr = 0;
	var activeClass = 'fadeactive';
	var fadeTime = fadeT;
	var pauseTime = pauseT;
	var pauseAfterViewTime = pauseAfterViewT;
	
	// initialise all the images that are to be faded
	imagesToFade = $('img', $('#fadeimages'));

	// if we've found any images then look for thumbnails
	if(imagesToFade.length > 1) {
		// initialise the image's thumbnails by looking for a collection of them within a "fadeselectors" parent. 
		selectorImages = $('img' ,$('#fadeselectors'));
		// kick off the fading routine
		timer = setTimeout(fadeNextImage, pauseTime);
	}
	
	// attach a click event to all the thumbnails to display a specific image
	// obj must be a valid instance of imageFadeControl
	this.attachClickFade = function(obj) {
		// attach a click event to all the thumbnails which will display the large image relating tothe cliecked thumbnail
		$('img' ,$('#fadeselectors')).click(function() { obj.fadeToImage(this); });	
	}
	
	// fades to the next image then sets the timer to call itself after [pauseTime]
	function fadeNextImage() {
		clearTimeout(timer);
		fadeOut(imagePtr);
		imagePtr++;
		if(imagePtr >= imagesToFade.length)
			imagePtr = 0;
		fadeIn(imagePtr);
		timer = setTimeout(fadeNextImage, pauseTime);
	}

	// fade the selected image in and highlight its thumbnail
	function fadeIn(ptr) {
		$(imagesToFade[ptr]).fadeIn(fadeTime);
		setSelector(ptr)		
	}

	// fade the selected image out
	function fadeOut(ptr) {
		$(imagesToFade[ptr]).fadeOut(fadeTime);
	}
	
	// de-activates the previous thumb and activates the current one
	function setSelector(ptr) {
		$('img' ,$('#fadeselectors')).removeClass(activeClass);
		$(selectorImages[ptr]).addClass(activeClass);	
	}
	
	// visitor has clicked on a thumbnail so show that image for [pauseAfterViewTime]
	this.fadeToImage = function(img) {
		if(img != null)
		{
			clearTimeout(timer);
			$(imagesToFade[imagePtr]).hide();
			imagePtr = Number(img.id.replace('splashthumb', '')) - 1;		
			$(imagesToFade[imagePtr]).show();
			setSelector(imagePtr);
			timer = setTimeout(fadeNextImage, pauseAfterViewTime);
		}
	}
}


function homeImageFadeControl(fTime, pTime, pAViewTime) {

	var tleft = 2;
	var tbottom = -2;
	var fadeTime = fTime;
	var pauseTime = pTime;
	var pauseAfterViewTime = pAViewTime;
	var timer = null;
	var imagePtr = 0;	
	var imagesToFade = $('img', $('.home-main-images'));
	var selectorThumbs = $('a', $('.thumblinks'));
	var homePanels = $('div', $('.home-main-panel-contaner'));

	// if we've found any images then look for thumbnails
	if(imagesToFade.length > 1) {
		// kick off the fading routine
		timer = setTimeout(fadeNextImage, pauseTime);
	}			
	
	// fades to the next image then sets the timer to call itself after [pauseTime]
	function fadeNextImage() {
		clearTimeout(timer);
		fadeOut(imagePtr);
		imagePtr++;
		if(imagePtr >= imagesToFade.length)
			imagePtr = 0;
		fadeIn(imagePtr);
		timer = setTimeout(fadeNextImage, pauseTime);
	}			
			
	// fade the selected image in and highlight its thumbnail
	function fadeIn(ptr) {
		$(imagesToFade[ptr]).fadeIn(fadeTime);
		$(homePanels[ptr]).show();
		setSelector(null);	
	}

	// fade the selected image out
	function fadeOut(ptr) {
		$(imagesToFade[ptr]).fadeOut(fadeTime);
		$(homePanels[ptr]).hide();
	}			

	// attach a click event to the active png
	this.attachClickFade = function(obj) {
		$('a' ,$('.thumblinks')).click(function() { return obj.fadeToImage(this); });
	}			

	this.fadeToImage = function(lnk) {
		clearTimeout(timer);
		$(imagesToFade[imagePtr]).hide();	
		$(homePanels[imagePtr]).hide();
		imagePtr = Number(lnk.id.replace('tl', '')) - 1;
		$(imagesToFade[imagePtr]).show();
		$(homePanels[imagePtr]).show();
		setSelector(imagePtr);
		timer = setTimeout(fadeNextImage, pauseAfterViewTime);	
		return false;
	}
	
	function setSelector(ptr) {
		var fTime;
		if(ptr != null) {
			tleft = (ptr * 118) + 2;
			tbottom = (ptr * 2) - 2;
			fTime = 0;
		} else { 
			tleft += 118;
			tbottom += 2;
			fTime = fadeTime / 4;
			if(tleft > 356) {
				tleft = 2;
				tbottom = -2;	
			}
		}
		$('.home-thumbs-active').fadeOut((fTime), function() {
			$('.home-thumbs-active').css('left', tleft + 'px');
			$('.home-thumbs-active').css('bottom', tbottom + 'px');
			$('.home-thumbs-active').fadeIn(fTime); });
	}
}

