var homegallery = null;
var galleryspeed = 3000;
var slider = null;

$(document).ready( function()
{
	
	$('#homegallery').cycle();
	
	//innitHomeGallery();
	innitSlider();
	innitMainBodyOLs();
}
);


function innitHomeGallery()
{
	
	if ($("#homegallery").get(0)!=null)
	{
		
		homegallery = $("#homegallery").get(0);
		
		homegallery.count = $("#homegallery div").length;
		homegallery.thisIndex = 0;
		
		if (homegallery.count>1)
			setTimeout(fadeImg, galleryspeed);
		
	}

}

function fadeImg()
{
	$("#homegallery div:eq("+homegallery.thisIndex+")").fadeOut(function(){
		if (homegallery.thisIndex < homegallery.count-1)
		{
			homegallery.thisIndex = homegallery.thisIndex+1;
		}
		else
		{
			homegallery.thisIndex = 0;
		}
		$("#homegallery div:eq("+homegallery.thisIndex+")").fadeIn(function(){
			setTimeout(fadeImg, galleryspeed);
		});
	});
}

function innitSlider()
{
	if ($("#slidingbar").get(0)!=null)
	{
	
		// set up
		slider = $('#slidingbar').get(0);
		slider.count = $('#slidingbar img').length;
		moveTo(1);
		
		// make the previous button work
		$('.slidingcontrols li.prev a').click(function()
		{
			current = $(slider).css("margin-left").replace('px','')/-681 + 1;
			if (current % 1 ==0) moveTo(current-1);
			return false;
		});
		
		$('.slidingcontrols li.next a').click(moveNext);
		
		$('.slidingcontrols li.number a').click(function() {
			moveTo($(this).parent().prevAll().length);
			return false;
		})
		
		$('#slidingbar img').click(moveNext);
	}
}

function moveNext()
{
	current = $(slider).css("margin-left").replace('px','')/-681 + 1;
	if (current % 1 ==0 && current < slider.count) moveTo(current+1);
	return false;
}

function moveTo(index)
{
	if (index <= slider.count && index > 0)
	{
		newMargin = (index-1) * -681;
		$(slider).animate({marginLeft: newMargin+"px"});
		slider.current = index;
		$('.slidingcontrols li.number a').removeClass('active');
		$('.slidingcontrols li.number:eq('+(index-1)+') a').addClass('active');
	}
}

function innitMainBodyOLs()
{
	$(".mainbody ol li").wrapInner('<span class="numbered"></span>');
	$(".mainbody ol").addClass('numbered');
}