col_hot_title = null;
currentURL = "#";
showIsMoving = false;
intervalID = null;
news_num = 0;
PERIOD_INTERVAL = 5000;

function init(){
	col_hot_title = $("#all_img li").length;
	this.showIsMoving = false;
	intervalID = null;
	news_num = 0;
	$("<div>").addClass("play").html("&nbsp;").appendTo(".inc_img .pages");
	for(var i = 1; i <= col_hot_title; i++)
	{
		$("<div>").addClass("pg").attr("number", i-1).html(i).appendTo(".inc_img .pages");
	}
	$("#hot_show div.inc_img").bind("click", function(e){
		if(e.target == this)
			document.location.href = currentURL;
	})
	$("#hot_show div.pages div.play").click(playClick);
	$("#hot_show div.pg").click(buttonClick)

	$("#hot_show div.pages div.play").toggleClass("active");
	start_hot_show();
}
function buttonClick(){
		$("#hot_show div.pages div.play").removeClass("active");
		showIsMoving = false;
		clearTimeout(intervalID);
		news_num = parseInt($(this).html()) - 1;
		change_hot_news();
}
function playClick(){
	if($(this).is('.active')){
		showIsMoving = false;
		clearTimeout(intervalID);
		if(news_num == 0)
			news_num = col_hot_title - 1;
		else
			news_num--;
	}
	else{
		showIsMoving = true;
		news_num++;
		if(parseInt(news_num) == parseInt(col_hot_title))
		{
			news_num = 0;
		}
		change_hot_news();
	}
		
	$(this).toggleClass("active");
}
function start_hot_show(){
	change_hot_news(news_num);
	showIsMoving = !showIsMoving;
}
function change_hot_news() {
	$("#hot_show div.pages div.play").unbind("click");
	$("#hot_show div.pg").unbind("click");
	$("#hot_show div.pg").removeClass("active");
	$("#hot_show div.text p").removeClass("active");
	var loaderImage = $("<img>").attr({"src":"img/ajax-loader.gif"});
	var loaderUrl = 'url(' + $(loaderImage).attr("src") + ')';
	$("#hot_show div.inc_img").css({'backgroundImage':loaderUrl, "backgroundPosition":"center center"});
	var img_active = $($("#all_img li")[news_num]).attr("source");
	var tempImage = $("<img>").attr("src", img_active);
	$(tempImage).bind("load", function(evt){
		var str = $($("#all_img li")[news_num]).attr("desc");
		var hotNews = $("#hot_show div.text p a[title|="+ str +"]");
		currentURL = $(hotNews).attr("href");
		$(hotNews).parent("p").addClass("active");
		$("#hot_show div.pages div.pg[number|="+ news_num +"]").addClass("active");

		img_active = 'url(' + img_active + ')';
		$("#hot_show div.inc_img").css('backgroundImage', img_active);
		if(showIsMoving)
		{
			news_num++;
			if(parseInt(news_num) == parseInt(col_hot_title))
			{
				news_num = 0;
			}
			clearTimeout(intervalID);
			intervalID = setTimeout(change_hot_news, PERIOD_INTERVAL);
		}
		else
		{
			clearTimeout(intervalID);
		}
		$(this).unbind("load");
		$("#hot_show div.pages div.play").bind("click", playClick);
		$("#hot_show div.pg").bind("click", buttonClick)
	});
}
$(document).ready(function(){ 
	init();
})
