page_size = 0;
active_id = 1;
next_scroll = null;

function getOffset(id){
	if(id == 1) return 0;
	
	return page_size-(page_size-960)/2+70 + (900 * (id - 2));
}

function resize(){
	page_size = $("#top_bg").width();
	
	$("#hm1").css("width" , page_size);
	
	$("#hm_slider").css("margin-left" , getOffset(active_id) * -1);
}

function scheduleNextScroll(){
	next_id = active_id + 1;
	if(next_id > 4) next_id = 1;
	
	next_scroll = setTimeout("scroolTo(" + next_id + ")" , 10000);
}

function scroolTo(id){
	$("#hm_publish>p").css("visibility","hidden");
	
	$("#hm_slider").animate({"margin-left":getOffset(id) * -1}, {
		complete:function(){
			active_id = id;
			
			if(id > 1)
				$($("#hm_publish>p")[id - 2]).css("visibility", "visible");
			
			$("#pagin>a").removeClass("current");
			
			$("#pg" + id).addClass("current");
			
			if(next_scroll != null) scheduleNextScroll();
		}
	});
	
	
}

$(document).ready(function(){
	resize();
	
	$("#pagin>a").click(function(ev){
		ev.preventDefault();
		
		clearTimeout(next_scroll);
		next_scroll = null;
		
		button = $(this);
		
		id = parseInt(/^pg([0-9]+)$/.exec(button.attr("id"))[1]);
		
		scroolTo(id);
		
	});
	
	$(window).resize(function() {
		resize();
	});
	
	scheduleNextScroll();
});
