/**
 * @fileOverview Анимация эквалайзера для Musicdepo
 * @author PsychodelEKS [psychodeleks@gmail.com]
 * @requires jquery.js 1.3+
 */

$(function(){ initEqAnimationSwitch(); });

var global_eq_animation_timer = 0;

/* Включает/выключает анимацию эквалайзера при наведении на лого после 3 секунд */
function initEqAnimationSwitch()
{
	var eqAnimationRunning = false;
	var eqAnimationTimer = 0;
	$('#logo').bind('mouseover.eqAnimation',
		function()
		{
			eqAnimationTimer = setTimeout(
				function()
				{
					if(eqAnimationRunning)
						initEqAnimation(false);
					else
						initEqAnimation(true);

					eqAnimationRunning = !eqAnimationRunning;
				}, 3000);
		}).bind('mouseout.eqAnimation', function(){ clearTimeout(eqAnimationTimer); });
}

/* Анимирует/отключает анимацию эквалайзера */
function initEqAnimation(start_animation)
{
	if(start_animation)
	{
		var tmp_bars = $('div[id^="eq_"]').get();
		var bars = new Array();

		for(var i in tmp_bars)
		{
			bars.push(
			{
				bar: $(tmp_bars[i]),
				pos: parseInt($(tmp_bars[i]).css('marginTop'))
			});
		}

		function barAnimation(bar, start, end)
		{
			bar.stop().animate(
				{ marginTop: end+"px" },
				250,
				'linear',
				function()
					{
						bar.animate(
						{
							marginTop: start+"px"
						}, 250, 'linear');
					}
			);
		}

		function animateBars()
		{
			for(var i in bars)
				barAnimation(bars[i].bar, bars[i].pos, bars[i].pos+(-30 + Math.random()*60));
		}

		global_eq_animation_timer = setInterval(function(){ animateBars(); }, 515 );
	}
	else
		clearInterval(global_eq_animation_timer);
}
