var COUNT_MENU_NODES = 4;
var CHANGE_INTERVAL  = 5000;

var images = new Array( 'img/pic001.jpg', 'img/pic002.jpg', 'img/pic003.jpg', 'img/pic004.jpg', 'img/pic005.jpg', 'img/pic006.jpg', 'img/pic007.jpg' );

function getCookie( name )
{
  return ( !document.cookie.length || ( str = document.cookie.indexOf( name + '=' ) ) == -1 ) ? '' : document.cookie.substring( ( str += ( name + '=' ).length ), ( result = document.cookie.indexOf( ';', str ) ) == -1 ? document.cookie.length : result );
}

function rotateImage()
{
	document.getElementById( 'mainImage' ).src = images[++window.imgNo >= images.length ? ( window.imgNo = 0 ) : window.imgNo];
}

function readMenu()
{
	if( !document.getElementById ) return;
	var nodes = getCookie( 'nodes' ).split( ',' );
	for( var i = 0; i < nodes.length && nodes.toString() != ''; i++ )
		document.getElementById( nodes[i] ).style.display = 'block';
	window.preloaded = new Array;
	for( var i = 0; i < images.length; i++ )
	{
		window.preloaded[window.preloaded.length] = new Image;
		window.preloaded[window.preloaded.length - 1].src = images[i];
	}
	if( document.getElementById( 'mainImage' ) )
	{
		window.imgNo = 0;
		window.rotator = window.setInterval( 'rotateImage()', CHANGE_INTERVAL );
	}
}

function saveMenu()
{
	if( !document.getElementById ) return;
	var nodes = new Array();
	for( var i = 0; i < COUNT_MENU_NODES; i++ )
		if( document.getElementById( 'n' + i ).style.display == 'block' )
			nodes[nodes.length] = 'n' + i;
	document.cookie = 'nodes=' + nodes.toString();
	if( document.getElementById( 'mainImage' ) )
		window.clearInterval( window.rotator );
}

function menu( node )
{
	if( !document.getElementById ) return;
	for( var i = 0, parent = node.parentNode; i < parent.childNodes.length; i++ )
		if( parent.childNodes[i].tagName && parent.childNodes[i].tagName.toLowerCase() == 'ul' )
			( style = parent.childNodes[i].style ).display = style.display == 'none' || style.display == '' ? 'block' : 'none';
}

window.onload = readMenu;
window.onunload = saveMenu;