
var Styles = ['blue', 'purple', 'pink', 'green', 'orange', 'red'];
var Opened = false;
var inMotion = false;

var animationTime = 1000;
var timeOut = animationTime / 3.5;

// Листалочка
var Cron = { 
	cycleTime : 5000,
	enabled : true,
	init : function()
		{
		  setTimeout('Cron.action()', this.cycleTime);
		},
	action : function()
		{
		  if (this.enabled) 
			{ 
			  this.handler(); 
			  setTimeout('Cron.action()', this.cycleTime);
			}
		},
	handler : function()
		{
		  Style.left();
		}
};


// DONT CHANGE ANYTHING BELOW THIS LINE
var dBottleMargin = -100;
var dSeriesMargin = -3;
var dExpertMargin = 0;

var Style = {
	show : function(id, dir)
		{
		  inMotion = true;
		  Opened = id;
		  var indent = 25;
		  
		  // calculate start point
		  var bottlePos = dBottleMargin + (indent * dir);
		  $('.bottle.'+id).css('margin-left',bottlePos);
		  $('.series.'+id).css('margin-left',dSeriesMargin + 20);
		  $('.expert.'+id).css('margin-top',dExpertMargin + 20);
		  
		  //animate
		  $('.glow.'+id).animate({ opacity : 1}, animationTime/1.2, 'linear', function(){ });
		  $('.bottle.'+id).css('z-index', 1000);
		  $('.bottle.'+id).animate({opacity : 1, marginLeft : dBottleMargin }, animationTime/1.5, 'linear', function(){ inMotion = false; queue.handle(); });
			
		  $('.series.'+id).animate({ opacity : 1, marginLeft : dSeriesMargin }, animationTime / 1.5, 'linear', function(){  });
		  $('.expert.'+id).animate({ opacity : 1, marginTop  : dExpertMargin }, animationTime / 1.5, 'linear', function(){  });
		 
		  // If this is orange series - move allseries button down
		  if (id == 'orange') 
			{ $('.series-box h4').animate({ marginTop : -29}, animationTime/3); }
		  if (id == 'purple' || id == 'green')
			{ $('.experts-box .allexperts').animate({ marginTop : 107 }, animationTime/3); }
		},
	hide : function(id, dir)
		{
		  inMotion = true;
		  Opened = id;
		  var indent = 25;
		  
		  // calculate start point
		  var bottlePos = dBottleMargin - (indent * dir);
		  
		  //animate
		  $('.glow.'+id).animate({ opacity : 0}, animationTime, 'swing', function(){  });
		  $('.bottle.'+id).css('z-index', 10);
		  $('.bottle.'+id).animate({ opacity : 0, marginLeft : bottlePos}, animationTime/ 3, 'swing', function(){  inMotion = true; });
		  $('.series.'+id).animate({ opacity : 0, marginLeft : 20}, animationTime / 3, 'swing', function(){  });
		  $('.expert.'+id).animate({ opacity : 0, marginTop : 20}, animationTime / 3, 'swing', function(){  });
		
		  if (id == 'orange') 
			{ $('.series-box h4').animate({ marginTop : 0 }, animationTime/3); }
		  if (id == 'purple' || id == 'green')
			{ $('.experts-box .allexperts').animate({ marginTop : 87 }, animationTime/3); }
		
		},
	left : function()
		{
		  // If block allready in motion, move request to queue
		  if (inMotion) 
		  { 
			queue.push(this.left);
			return false; 
		  }
		  Style.hide(Opened, 1);
		  
		  var key;
		  // find style key
		  var total = Styles.length;
		  total--;
		  for (i in Styles) { if (Styles[i] == Opened) { key = i; } }
		  
		  if (key == total)
			{
			  setTimeout('Style.show(\''+Styles[0]+'\', 1)', timeOut);
			}
		  else
			{
			  key++;
			  setTimeout('Style.show(\''+Styles[key]+'\', 1)', timeOut);
			}
			
	      Cron.enabled = false;
		},
	right : function()
		{
		  if (inMotion) 
		  { 
			queue.push(Style.right);
			return false; 
		  }
		  Style.hide(Opened, -1);
		  
		  var key;
		  // find style key
		  var total = Styles.length;
		  total--;
		  for (i in Styles) { if (Styles[i] == Opened) { key = i; } }
		  
		  if (key == 0)
			{
			  //Style.show(Styles[total], -1);
			  setTimeout('Style.show(\''+Styles[total]+'\', -1)', timeOut);
			  
			}
		  else
			{
			  key--;
			 // Style.show(Styles[key], -1);
			  setTimeout('Style.show(\''+Styles[key]+'\', -1)', timeOut);
			}
	      Cron.enabled = false;
		},
	init : function()
		{
		  $('.glow').css('opacity','0');
		  $('.glow').css('visibility','visible');
		  $('.bottle').css('opacity','0');
		  $('.bottle').css('visibility','visible');
		  $('.series').css('opacity','0');
		  $('.series').css('visibility','visible');
		  $('.expert').css('opacity','0');
		  $('.expert').css('visibility','visible');
		  Style.show('blue', 1);
		  
		  // set Hanlders
		  $('#bottles-holder .arrow.right').click(this.left);
		  $('#bottles-holder .arrow.left').click(this.right);
		}
};

// Queue
var queue = {
  queue : [],
  push : function(v)
    {
	  this.queue.push(v);
	},
  handle : function()
    {
	  var entry = this.queue.shift();
	  if(entry)
	    {
	      entry();
	    }
	}
};
	
	
	
function initHoverable()
	{
	  $('.hvrbl').each(function()
		  { $(this).mouseenter(function() { $(this).addClass('selected'); });
			$(this).mouseleave(function() { $(this).removeClass('selected'); });
		  });
	}
