// JavaScript Document
var animiationTime = 5000;
var clickAnimationTime = 1000;
var showTime = 4000;
var lastAnimationTime = 0;
var currentEffect = null
var nextEffect = null;

Event.observe(document, 'dom:loaded', Animation__init);
function Animation__init() {
	//alert('Animation__init');
	try {
		setTimeout('Animation__cycle(+1, animiationTime, false);', showTime);
		/**
		* No forward/back controls at the mo'.
		var newsUp = $$('img.newsUp')[0];
		var newsDown = $$('img.newsDown')[0];
		newsUp.observe('click', Animation__newsUp);
		newsDown.observe('click', Animation__newsDown);
		*/
		$('controls').observe("mouseleave", Animation__hideControls);
		$$('img.dot')[0].observe("mouseover", Animation__showControls);
		$('pause').observe("click", Animation__pause);
		$('resume').observe("click", Animation__resume);
		$('fullscreen').observe("click", Animation__fullscreen);
	}
	catch(e) {
		//alert(e);
		//console.log(e);
	}
}

function Animation__cycle(direction, overideAnimTime, user) {
	//alert('Animation__cycle');
	//console.log('Animation__cycle');
	if(new Date().getTime() - lastAnimationTime < animiationTime && !user) {
		//Too early
		return;	
	}

	lastAnimationTime = new Date().getTime();
	
	try {
		var localAnimTime = overideAnimTime;
		
		if(images.length < 2) return;
		var currentItemIndex = null;
		if(currentEffect == null) {
			for(var i = 0; i < images.length && currentItemIndex == null; i++) {
				if(images[i].isVisible()) currentItemIndex = i;
			}
		}
		else {
			for(var i = 0; i < images.length && currentItemIndex == null; i++) {
				if(images[i].element != null && images[i].element == currentEffect.element) currentItemIndex = i;
			}
			currentEffect.cancel();
		}
		
		if(currentItemIndex == null) {
			//Nothing's on show???
			//Just default to the first.
			currentItemIndex = 0;
		}
		
		var nextItemIndex = currentItemIndex+direction;
		if(nextItemIndex >= images.length) nextItemIndex = 0;
		else if(nextItemIndex < 0) nextItemIndex = images.length-1;
		
		
		//Pre-load the next-next image so it's downloaded
		//ready for the fade-in.
		var nextNextItemIndex = nextItemIndex+direction;
		if(nextNextItemIndex >= images.length) nextNextItemIndex = 0;
		else if(nextNextItemIndex < 0) nextNextItemIndex = images.length-1;
		images[nextNextItemIndex].loadImage();
		
		if(nextEffect != null) {
			nextEffect.cancel();
		}
		
		images[currentItemIndex].loadImage(); //Just incase something wierd has happened.
		if(nextEffect != null && nextEffect.element != images[nextItemIndex].element) {
			currentEffect = new Effect.Morph(images[currentItemIndex].element, {
				style:{opacity:"1"},
				duration:localAnimTime/1000,
				afterFinish:Animation__oldFinished
			});
			
			//Setup the animation to move the 
			//"old" next item to wherever we need it
			var oldNextItem =  nextEffect.element;
			nextEffect = new Effect.Morph(oldNextItem, {
				style:{opacity:"0"},
				duration:localAnimTime/1000
			});
		}
		else {
			currentEffect = new Effect.Morph(images[currentItemIndex].element, {
				style:{opacity:"0"},
				duration:localAnimTime/1000,
				afterFinish:Animation__oldFinished
			});
			
			images[nextItemIndex].loadImage(); //Just incase something wierd has happened.
			nextEffect = new Effect.Morph(images[nextItemIndex].element, {
				style:{opacity:"1"},
				duration:localAnimTime/1000
			});
		}
	}
	catch(e) {
		//alert(e);
		//console.log(e);
	}
}

/**
* Move the "old" news item, now above the container,
* to the parking area below the container.
*/
function Animation__oldFinished(effect) {
	//No longer necessary.
	//var newsItem = effect.element;
	//newsItem.style.top = (newsItem.parentNode.getHeight()+1)+"px";
	currentEffect = null;
	nextEffect = null;
	setTimeout('Animation__cycle(+1, animiationTime, false);', showTime);
}

function Animation__newsDown() {
	Animation__cycle(-1, clickAnimationTime, true);
}

function Animation__newsUp() {
	Animation__cycle(+1, clickAnimationTime, true);
}

function Animation__pause() {
	//Bit of a hack to stop the next one running.
	lastAnimationTime += showTime;
	
	if(currentEffect != null) {
		currentEffect.cancel();
		//Reverse
		new Effect.Morph(currentEffect.element, {
			style:{opacity:"1"},
			duration:clickAnimationTime/1000
		});
		currentEffect = null;
	}
	
	if(nextEffect != null) {
		nextEffect.cancel();
		//Reverse
		new Effect.Morph(nextEffect.element, {
			style:{opacity:"0"},
			duration:clickAnimationTime/1000
		});
		nextEffect = null;
	}
	$$('img.dot')[0].src = "/images/pause.png";
}

function Animation__resume() {
	$$('img.dot')[0].src = "/images/dot.png";
	if(currentEffect != null || nextEffect != null) return;
	lastAnimationTime -= showTime;
	setTimeout('Animation__cycle(+1, animiationTime, false);', 1);
}

function Animation__fullscreen() {
	try {
		var img = null;
		if(currentEffect != null) {
			var imageID = currentEffect.element.id.match(/^image([0-9]+)$/)[1];
			for(var i = 0; i < images.length && img == null; i++) {
				if(images[i].imageID == imageID) img = images[i];
			}
		}
		else {
			for(var i = 0; i < images.length && img == null; i++) {
				if(images[i].isVisible()) img = images[i];
			}
		}
		window.open('/home/index/'+img.imageID+'.htm', 'ChristianHillsDesignFullscreen', 'channelmode=yes,fullscreen=yes,top=0,left=0,width='+screen.availWidth+',height='+screen.availHeight);
	}
	catch(e) {
		console.log(e);	
	}
}

function Animation__hideControls(evnt) {
	var controls = $('controls');
	controls.style.display = "none";
}

function Animation__showControls() {
	$('controls').style.display = "block";
}

var BannerImage = function(imgID, imgURL, w, h, x, y, a) {
	this.imageID = imgID;
	this.url  = imgURL;
	this.width = w;
	this.height = h;
	this.transX = x;
	this.transY = y;
	this.alt = a;
};
BannerImage.prototype.imageID;
BannerImage.prototype.url;
BannerImage.prototype.alt;
BannerImage.prototype.transX;
BannerImage.prototype.transY;
BannerImage.prototype.width;
BannerImage.prototype.height;
BannerImage.prototype.element = null;
BannerImage.prototype.loadImage = function() {
	if(this.element != null) return;
	if($('image'+this.imageID) != null) {
		//This happens to the first two images that
		//are written into the document ready.
		this.element = $('image'+this.imageID);
		return;	
	}
	var styleStr = "position:absolute; opacity:0.0; filter:alpha(opacity=0); top:"+this.transY+"px; left:"+this.transX+"px;";
	this.element = new Element("img", {id:"image"+this.imageID, src:this.url, alt:this.alt, style:styleStr, width:this.width, height:this.height});
	$$('div.contentContainer')[0].appendChild(this.element);
}

BannerImage.prototype.isVisible = function() {
	if(this.element == null && $('image'+this.imageID) != null) {
		//This happens to the first two images that
		//are written into the document ready.
		this.element = $('image'+this.imageID);	
	}
	
	if(this.element == null) return false;
	/*@cc_on
		@if (@_jscript)
		if(this.element.style.filter == '' || this.element.style.filter == 'alpha(opacity=100)') return true;
		@else @*/
		if(this.element.style.opacity == '' || parseFloat(this.element.style.opacity) == 1) return true;
		/*@end
	@*/	
	return false;
}