var front_link;
var front_img;
var front_desc;
var selected_front_news;
var current_front_img;
var to_front_img;
var effectIsRunning = false;

var from_click = false;
var auto_fade_interval = 0;

var max_fade_ind = 5;
var next_fade_ind = 2;

function front_news_init(e){
	front_link = document.getElementById("front_news_link");
	front_desc = document.getElementById("front_news_desc");
	front_desc.style.height = "50px";

	start_auto_fade();
}

function auto_fade_img(){
	if (next_fade_ind > max_fade_ind){
		next_fade_ind = 1;
	}
	var a;
	if (a = document.getElementById("front_news_"+next_fade_ind)){
		start_fade_to_front_img.apply(a);
	}
}

function start_auto_fade(){
	if (auto_fade_interval <= 0){
		auto_fade_interval = setInterval(auto_fade_img,4000);
	}
}

function stop_auto_fade(){
	if (auto_fade_interval>0){
		clearInterval(auto_fade_interval);
	}
	auto_fade_interval = 0;
}


function start_fade_to_front_img(e){
	if (e && e.type == 'click'){
		from_click = true;
		stop_auto_fade();
	}
	next_fade_ind = (parseInt(this.id.replace("front_news_","")) || 0)+1;
	if (!whi.has_class(this.parentNode,'selected_news') && !effectIsRunning){
		effectIsRunning = true;

		whi.remove_class(selected_front_news.parentNode,'selected_news');

		selected_front_news = this;

		whi.add_class(this.parentNode,'selected_news');
		front_link.href = selected_front_news.parentNode.getElementsByTagName("a")[1].href;
		front_desc.href = front_link.href;

                var selected_id = this.id.replace("front_news_","");
		to_front_img = document.getElementById("front_news_img_"+selected_id);
		to_front_img.style.zIndex = 99;

		var hideTextEffect = new Fx.Morph(front_desc, {duration: 'short', transition: Fx.Transitions.Sine.easeOut, complete:front_switch_img, link:'chain'});
		hideTextEffect.addEvent('complete', front_switch_img);
		hideTextEffect.start({'height': 0, 'padding-top':0, 'padding-bottom':0});
	}
}

function remove_front_selected_class(){
	whi.remove_class(this,'selected_img');
	//current_front_img.style.opacity = 1;
}
function put_front_selected_class(){
	whi.add_class(this,'selected_img');

	current_front_img = this;

	var showTextEffect = new Fx.Morph(front_desc, {duration: 'short', transition: Fx.Transitions.Sine.easeOut, complete:front_switch_img, link:'chain'});
	showTextEffect.addEvent('complete', finishRunningEffect);
	showTextEffect.start({'height': 50, 'padding-top':10, 'padding-bottom':10});
	
	from_click = false;

	start_auto_fade();
}
function finishRunningEffect(){
	effectIsRunning = false;
}

function front_switch_img(){
	front_desc.innerHTML = selected_front_news.parentNode.getElementsByTagName("div")[1].innerHTML;

	var hideImageEffect = new Fx.Morph(current_front_img, {duration: 'normal', transition: Fx.Transitions.Sine.easeOut});
	hideImageEffect.addEvent('complete', remove_front_selected_class.bind(current_front_img));
	hideImageEffect.start({'opacity': 0});

	var showImageEffect = new Fx.Morph(to_front_img, {duration: 'normal', transition: Fx.Transitions.Sine.easeOut});
	showImageEffect.addEvent('complete', put_front_selected_class.bind(to_front_img));
	showImageEffect.start({'opacity': 1});
}


jsRules.addRule("#front_news a.js_front_news",function(){
	if (whi.has_class(this.parentNode,'selected_news')){
		selected_front_news = this;
		var selected_id = this.id.replace("front_news_","");
		current_front_img = document.getElementById("front_news_img_"+selected_id);
	}
	whi.events.addEvent(this,'click',start_fade_to_front_img);
});
whi.events.addEvent(window,'DOMContentLoaded',front_news_init);


