var rollNow = -1;
var posContainer;

function init() {
	var cards = document.getElementById( "bck" );
	posContainer = getPosition( cards );
	posContainer.width = cards.offsetWidth;
	posContainer.height = cards.offsetHeight;
	
	preloadImages();

	document.body.onmouseover = resumeRolling;
	startRolling();

	setTimeout( fade, 3000 );
}

function fade() {
	$( "#banner img" )
		.fadeOut( 3000 );
}

function clickTab( hide ) {
	var link = document.getElementById( "cardTypes" ).getElementsByTagName( "a" )[rollNow];
	if( link.onclick() ) {
		window.frames[link.target].location.href = hide ? "about:blank" : link.href;
	}
}

var roller;
function resumeRolling( e ) {
	if( roller ) {
		return;
	}
	if( isMouseInCards( e ) ) {
		return;
	}
	clickTab( true );
	startRolling();
}

function startRolling() {
	document.getElementById( "thumbs" ).style.display = "none";
	var bck = document.getElementById( "bck" );
	bck.style.backgroundPosition = "right center";
	bck.style.cursor = "pointer";
	roller = setInterval( roll, 13000 ); // 0000227: Rotation banner should move slower 
	roll();
}

function stopRolling() {
	if( !roller || rollNow >= 2 ) {
		return;
	}
	clearInterval( roller );
	roller = null;
}
/*
function hideRolling() {
    var bck = document.getElementById( "bck" );
    bck.style.backgroundPosition = "400px center";
    bck.style.backgroundImage = "url(images/loading.gif)";
    bck.style.cursor = "default";
    clickTab();
}
*/
function isMouseInCards( e ) {
	e = e || window.event;
	if( !e ) {
		return false;
	}
	var x = e.clientX + document.body.scrollLeft;
	var y = e.clientY + document.body.scrollTop;
	return x >= posContainer.left && x < posContainer.left + posContainer.width &&
		y >= posContainer.top && y < posContainer.top + posContainer.height;
}

function roll() {
	var links = document.getElementById( "cardTypes" ).getElementsByTagName( "a" );
	rollNow = (rollNow + 1) % links.length;
	var link = links[rollNow];
	selectCardType( link );
}

function scrollLeft() {
}

function scrollRight() {
}

function selectTab( tab ) {
    selectCardType( tab.getElementsByTagName( "a" )[0] );
}

var selectedCardType;
function selectCardType( link ) {
	if( selectedCardType ) {
		selectedCardType.className = "card_unsel";
	}
	selectedCardType = link.parentNode;
	selectedCardType.className = "card_sel";
	link.blur();
    var id = link.href.substr( link.href.lastIndexOf( '=' ) + 1, 1 );
    document.getElementById( "bck" ).style.backgroundImage = "url(images/home/banner_" + id + ".png)";
	return true;
}

function navigateTab() {
	var bck = document.getElementById( "bck" ).style.backgroundImage;
	var pos = bck.lastIndexOf( "_" );
	if( pos < 0 ) {
		return;
	}
	var type = bck.substring( pos + 1, bck.lastIndexOf( "." ) );
	location.href = "~Main.php~type=" + type;
}

var subcat;
function showSubcats( lnkCat ) {
	if( subcat ) {
		subcat.style.visibility = 'hidden';
	}
	subcat = lnkCat.nextSibling;
	subcat.style.visibility = 'visible';
}

function preloadImages() {
	for( var i = 1; i <= 4; i++ ) {
		new Image().src = "images/home/banner_" + i + ".png";
	}
	new Image().src = "images/loading.gif";
}
