
var init = function () {
	var period = 5000;
	
	if($('home-tan') || $('home-main')) {
	
		if($('home-tan')) {
			var touts = document.getElementsBySelector('#home-tan .main-tout .tout-body');
		} else {
			var touts = document.getElementsBySelector('#home-main .main-tout .tout-body');
		}
		
		if($('home-tan')) {
			var anchors = document.getElementsBySelector('#home-tan .main-tout .header a');
		} else {
			var anchors = document.getElementsBySelector('#home-main .main-tout .header a');
		}
	
		touts.each(function (e) {
			e.addEvent('mouseover', function (l) {
				stopRotate();
			});
			e.addEvent('mouseout', function (l) {
				rotateTouts = rotate.periodical(period);
				$('img-main-tout-pause').setStyle('background-position','top left');
				if(!$('img-main-tout-pause').hasClass('pause'))
					$('img-main-tout-pause').addClass('pause');
			});
		});
		
		anchors.each(function (e) {
			if(e.href.substring(e.href.length-5, e.href.length) !='pause') {
				e.addEvent('click', function (el) {
					el = new Event(el);
					el.preventDefault();
					stopRotate();
					var num = e.href.substring(e.href.length-1, e.href.length)
					switchTout(num, true);
					anchors.each(function (e) {
						if(e.href.substring(e.href.length-1, e.href.length) != rotateIndex+1)
							e.style.backgroundPosition = "top left";
					});					   
					this.style.backgroundPosition = "top right";
				});
				e.addEvent('mouseover', function (el) {
					if(e.href.substring(e.href.length-1, e.href.length) != rotateIndex+1)
						this.style.backgroundPosition = "top right";
				});
				e.addEvent('mouseout', function (el) {
					if(e.href.substring(e.href.length-1, e.href.length) != rotateIndex+1)
						this.style.backgroundPosition = "top left";
				});
			} else {
				e.addEvent('click', function (el) {
					el = new Event(el);
					el.preventDefault();
					if(e.hasClass('pause')) {
						stopRotate();
						this.style.backgroundPosition = "top right";
						e.removeClass('pause');
					} else {
						rotateTouts = rotate.periodical(period);
						this.style.backgroundPosition = "top left";
						e.addClass('pause');
					}
				});
			}
			
		});
		
		var rotateIndex = 0;
		
		var rotateLastIndex = 2;
		
		function switchTout (x, flip) {
			if (flip == true) {
				var num = x-1;				
				var old_tout = document.getElementById('main-tout-' + rotateIndex);
				var new_tout = document.getElementById('main-tout-' + num);
				touts.each(function(el) {
					var chnge = new Fx.Styles(el, { duration: 0, transition: Fx.Transitions.linear,
						onComplete: function() {
						}
					});
					chnge.start({
						'opacity': [1, 0]
					});
					var chnge2 = new Fx.Styles(new_tout, { duration: 0, transition: Fx.Transitions.linear});
					chnge2.start({
						'opacity': [0, 1]
					});
				});
				rotateIndex=num;
				
	
			} else {
				if(x == 0) 
					rotateLastIndex = 2;
				else
					rotateLastIndex = x-1;
			
				var old_tout = document.getElementById('main-tout-' + rotateLastIndex);
				var new_tout = document.getElementById('main-tout-' + rotateIndex);
				var fadeOut = new Fx.Styles(old_tout, { duration: 0, transition: Fx.Transitions.linear,
					onComplete: function() {
					}
				});
				fadeOut.start({
					'opacity': [1, 0]
				});
				var fadeIn = new Fx.Styles(new_tout, {duration: 0,transition: Fx.Transitions.linear});
				fadeIn.start({
					'opacity': [0, 1]
				});
				var num1 = document.getElementById('img-main-tout-' + (rotateLastIndex+1)); 
				var num2 = document.getElementById('img-main-tout-' + (rotateIndex+1));
				num1.style.backgroundPosition = "top left";
				num2.style.backgroundPosition = "top right";
			}

		}
		
		function rotate () {	
			if (rotateIndex < 2) {
				rotateIndex++;
			} else {
				rotateIndex = 0;
			}
			switchTout(rotateIndex);
		}
		
	
		function stopRotate () { $clear(rotateTouts); }
		
		var rotateTouts = rotate.periodical(period);
		
	}
};


window.addEvent('load', init);

