var car_startitem;
var car_totalitems;
var car_height;
var car_autoMoveTO;
var car_autoMoveInterval = 10000;
var car_moveSpeed = 500;
var car_lastItem;
var car_autoMoveDir = 1;
var car_isAtEnd;

function setCarousel( offset, noclear ) {
	if ( !noclear && car_autoMoveTO!=null ) clearInterval(car_autoMoveTO);
	if (offset) car_startitem += offset;
	if ( car_startitem < 1 ) car_startitem = 1;
	car_height = $('#carousel').outerHeight();
	var h=0;
	var top;
	car_isAtEnd = false;
	for ( var i=car_startitem; i<=car_totalitems && h < car_height; i++ ) {
		h += $('#carousel_item'+i).height();
	}
	if ( h < car_height) {
		car_lastItem = car_totalitems;
		var h=0;
		for ( var i=car_totalitems; i>0 && h < car_height; i-- ) {
			h += $('#carousel_item'+i).height();
		}
		if ( h < car_height) {
			car_startitem = 1;
			top = -$('#carousel_item'+car_startitem).position().top;
		} else {
			car_startitem = i + 1;
			top = car_height - $('#carousel_inner').height();
			car_isAtEnd = true;
		}
	} else {
		car_lastItem = i-1;
		top = -$('#carousel_item'+car_startitem).position().top;
	}
	$('#carousel_inner').animate({top: top}, car_moveSpeed );
	$('#carousel_first_text').html( car_startitem );
	$('#carousel_last_text').html( car_lastItem );
//	$('#carousel').height(car_height);
}

function initCarouselAutoMove() {
	car_autoMoveTO = setTimeout('moveCarousel();', car_autoMoveInterval);
}

function moveCarousel() {
	setCarousel( car_autoMoveDir, true );
	if ( car_autoMoveDir > 0 ) {
		if ( car_isAtEnd ) car_autoMoveDir = -1;
	} else {
		if ( car_startitem ==1 ) car_autoMoveDir = 1;
	}
	car_autoMoveTO = setTimeout('moveCarousel();', car_autoMoveInterval);
}

function check_carousel_height() {
	var isIE = document.all ? true : false;
	var new_height = 0;
	var tableheight = 0;

	var check_elm = 'home_left';
	var elem = document.getElementById(check_elm);
	if (elem) {
		if(isIE) {
			new_height = elem.offsetHeight;
		} else {
			new_height = parseInt(document.defaultView.getComputedStyle(elem,null).getPropertyValue('height'));
		}
		tableheight = new_height - 80;
		document.getElementById('carousel').style.height = tableheight + 'px';
		document.getElementById('carousel_back').style.height = tableheight + 'px';
		setTimeout("check_carousel_height();", 100);
	}
	document.getElementById('carousel_outer').style.display = 'block';
}

