var homeTop = {
	transitionLeft : 'bounce:out',
	transitionRight : 'bounce:in',
	transitionDurationOut : 1000,
	transitionDurationIn : 1200,
	
	timer : true,
	timerDuration : 7500,
	
	timerInstance : null,
	timerId : null,
	
	nextLinks : [],
	prevLinks : [],
	stopTransition : false,
	
	init : function(){
		
		// GET ALL THE ANCHOR LINKS (prev & next featured post in home page)
		
		var nextLinks = $$('a.top_image_right');
		for (var i = nextLinks.length - 1; i >= 0; i--){
			homeTop.nextLinks.push(nextLinks[i]);
			nextLinks[i].addEvent('click', function(){
				homeTop.next(this.id)
			});
		};
		
		var prevLinks = $$('a.top_image_left');
		for (var i = prevLinks.length - 1; i >= 0; i--){
			homeTop.prevLinks.push(prevLinks[i]);
			prevLinks[i].addEvent('click', function(){
				homeTop.prev(this.id)
			});
		};
		
		if(homeTop.timer)
		{
			if(nextLinks[0])
			{					
				homeTop.timerId = nextLinks[0].id;
				homeTop.timerInstance = setInterval("homeTop.next(homeTop.timerId)", homeTop.timerDuration);
			}
		}
				
	},
	
	next : function(id){
		
		//alert(id);
		
		var index = id.replace("top_image_right_", "").toInt();
		var nextIndex = index + 1;
		
		if(nextIndex > (homeTop.nextLinks.length - 1))
		{
			nextIndex = 0;
		}
		
		if(!homeTop.stopTransition)
		{
			if(homeTop.timer)
			{
				homeTop.timerId = "top_image_right_" + nextIndex;
			}
			homeTop.morph(index, nextIndex);
		}		
	},
	
	prev : function(id){
		
		var index = id.replace("top_image_left_", "").toInt();
		var nextIndex = index - 1;
		
		if(nextIndex < 0)
		{
			nextIndex = homeTop.prevLinks.length - 1;
		}
		
		if(!homeTop.stopTransition)
		{
			homeTop.timerId = "top_image_right_" + nextIndex;
			clearInterval(homeTop.timerInstance);
			homeTop.timerInstance = setInterval("homeTop.next(homeTop.timerId)", homeTop.timerDuration);
			
			homeTop.morph(index, nextIndex);
		}
		
	},
	
	morph : function(index, nextIndex){
	
		homeTop.stopTransition = true;
		
		var currentLeftElement = $('top_home_'+index);
		var currentRightElement = $('top_image_home_'+index);
		
		var nextLeftElement = $('top_home_'+nextIndex);
		var nextRightElement = $('top_image_home_'+nextIndex);
		
		nextLeftElement.set('morph', {
			duration: homeTop.transitionDurationIn, 
			transition: homeTop.transitionLeft,
			onComplete: function(){
				homeTop.stopTransition = false;
			}
		});
		
		nextRightElement.set('morph', {
			duration: homeTop.transitionDurationIn, 
			transition: homeTop.transitionRight,
			onComplete: function(){
				homeTop.stopTransition = false;
			}
		});
		
		currentLeftElement.set('morph', {
			duration: homeTop.transitionDurationOut, 
			transition: homeTop.transitionLeft,
			onComplete: function(){
				currentLeftElement.set('styles', {'display': 'none'});
				nextLeftElement.set('styles', {'display': 'block', 'visibility' : 'hidden'});
				nextLeftElement.morph({
					opacity : [0, 1]
				});
			}
		});
		
		currentRightElement.set('morph', {
			duration: homeTop.transitionDurationOut, 
			transition: homeTop.transitionRight,
			onComplete: function(){
				currentRightElement.set('styles', {'display': 'none'});
				nextRightElement.set('styles', {'display': 'block', 'visibility' : 'hidden'});
				nextRightElement.morph({
					opacity : [0, 1]
				});
			}
		});
		
		currentLeftElement.morph({
			opacity : [1, 0]
		});
		
		currentRightElement.morph({
			opacity : [1, 0]
		});
	
	}
	
}

window.addEvent("domready", function(){
	
	// TOP HOME
	
	if(document.getElementById('top_image_home_1'))
	{
		homeTop.init();
	}
});