// Settings
menuAnimationTimeout = 5; // In seconds

fadingSpeed = 50; // In miliseconds
fadingStep = 0.20; // In miliseconds

if ( jQuery.browser.mozilla ) {
	fadingSpeed = 70; // In miliseconds
	fadingStep = 0.5; // In miliseconds
}

// Initialise variables
curMenuN = "0";
animationId = 0;
animationHoldId = 0;
fadingId = 0;
curFadeOpacity = 0;

menuImagesSrc = {
	hovered : [
		"http://s.feed.informer.com/images/bn_m11.gif",
		"http://s.feed.informer.com/images/bn_m21.gif",
		"http://s.feed.informer.com/images/bn_m31.gif",
		"http://s.feed.informer.com/images/bn_m41.gif"
	], houted : [
		"http://s.feed.informer.com/images/bn_m10.gif",
		"http://s.feed.informer.com/images/bn_m20.gif",
		"http://s.feed.informer.com/images/bn_m30.gif",
		"http://s.feed.informer.com/images/bn_m40.gif"
	]
};


function preload_images(img_arr) {
	jQuery.each(img_arr, function() {
		var img = new Image();
		img.src = this;
	});
}

function switchMenuItems (newN, curN) {
	menuImages[curN].attr("src", menuImagesSrc.houted[curN]);
	menuImages[newN].attr("src", menuImagesSrc.hovered[newN]);
}

function animateMenu () {
	var n = parseInt(curMenuN);
	n = ( n == 3 ) ? 0 : n+1;
	menuImages[n].triggerHandler("mouseover", [true]);
}


function initMenuAnimation () {
	if ( animationId ) {
		stopMenuAnimation();
	}
	animationId = window.setInterval("animateMenu()", menuAnimationTimeout*1000 );
}

function stopMenuAnimation () {
	if ( !animationId )
		return
	window.clearInterval(animationId);
	animationId = 0;
}

function fadeInMenuBloc (newN, curN) {
	curFadeOpacity += fadingStep;
	menuBlocs[newN].css('opacity', curFadeOpacity);
	if ( curFadeOpacity >= 1 ) {
		window.clearInterval(fadingId);
		menuBlocs[curN].css('opacity', 0).css('zIndex', 0);
		fadingId = 0;
		curFadeOpacity = 0;
	}
}
	
function switchMenu (e, isTriggeredByTimeout) {
	var n = e.data.n;
	if ( !isTriggeredByTimeout )
		stopMenuAnimation ();
	if ( n == curMenuN )
		return;
	if ( fadingId ) {
		window.clearInterval(fadingId);
		menuBlocs[curMenuN].css('opacity', 1);
		curFadeOpacity = 0;
	}
	jQuery.each(menuBlocs, function(i, val) {
		if ( i != curMenuN ) {
			menuBlocs[i].css('opacity', 0).css("zIndex", "0");
		}
	});
	menuBlocs[curMenuN ].css("zIndex", "1");
	menuBlocs[n].show().css("zIndex", "2");
	switchMenuItems (n, curMenuN);
	fadingId = window.setInterval("fadeInMenuBloc(" + n + ", " + curMenuN +  ")", fadingSpeed);
	curMenuN = n;
}

function opera_switchMenu (e, isTriggeredByTimeout) {
	var n = e.data.n;
	if ( !isTriggeredByTimeout )
		stopMenuAnimation ();
	if ( n == curMenuN )
		return;
	switchMenuItems (n, curMenuN);
	menuBlocs[n].show();
	menuBlocs[curMenuN].hide();
	curMenuN = n;
}

jQuery(function() {

	preload_images([
		"http://s.feed.informer.com/images/bn_m21.gif",
		"http://s.feed.informer.com/images/bn_m31.gif",
		"http://s.feed.informer.com/images/bn_m41.gif"
	]);

	initMenuAnimation();
	
	menuImages = [
		jQuery("img", "#menu_item_0"),
		jQuery("img", "#menu_item_1"),
		jQuery("img", "#menu_item_2"),
		jQuery("img", "#menu_item_3")
	];

	menuBlocs = [
		jQuery("#bn0"),
		jQuery("#bn1"),
		jQuery("#bn2"),
		jQuery("#bn3")
	];

	if ( jQuery.browser.opera ) {
		jQuery.each(menuBlocs, function(i, val) {
			if (i) val.hide();
		});
		jQuery.each(menuImages , function(i, val) {
			val.bind("mouseover", {n: i}, opera_switchMenu);
		});
	} else {
		jQuery.each(menuBlocs, function(i, val) {
			if (i) val.css("opacity", 0);
		});
		jQuery.each(menuImages , function(i, val) {
			val.bind("mouseover", {n: i}, switchMenu);
		});
	}

	jQuery("#menu_items").mouseout(initMenuAnimation);
});