/**
 * Rank: 3
 */

var birds = {}

birds.flip = [0, 0];
birds.dir = [0, 0];

birds.create = function()
{
	var b1 = '.birds1';
	$(b1).css('marginLeft', '500px');
	$(b1).css('marginTop', '30px');
	var b2 = '.birds2';
	$(b2).css('marginLeft', '500px');
	$(b2).css('marginTop', '30px');

  $('#b1-2, #b1-3, #b2-2, #b2-3').hide();

	birds.flutter1('#b1');
	birds.flutter2('#b2');
	birds.go();
}

birds.go = function()
{
	var b1 = '.birds1';
	var b2 = '.birds2';
	setTimeout(function() { birds.movement1(b1); }, 2000);
	setTimeout(function() { birds.movement2(b2); }, 2200);
	setTimeout(function() { birds.go(); }, 10000);
}

birds.flutter1 = function(b1)
{
	if (birds.dir[0] == 0)
  {
		if (birds.flip[0] == 0)
    {
			$(b1+'-1').hide();
			$(b1+'-2').show();
			birds.flip[0] = 1;
		}
    else if (birds.flip[0] == 1)
    {
			$(b1+'-2').hide();
			$(b1+'-3').show();
			birds.flip[0] = 0;
			birds.dir[0] = 1;
		}
	}
  else
  {
		if (birds.flip[0] == 0)
    {
			$(b1+'-3').hide();
			$(b1+'-2').show();
			birds.flip[0] = 1;
		}
    else if (birds.flip[0] == 1)
    {
			$(b1+'-2').hide();
			$(b1+'-1').show();
			birds.flip[0] = 0;
			birds.dir[0] = 0;
		}
	}
	setTimeout(function() { birds.flutter1(b1); }, 60);
}

birds.flutter2 = function(b2)
{
	if (birds.dir[1] == 0)
  {
		if (birds.flip[1] == 0)
    {
			$(b2+'-1').hide();
			$(b2+'-2').show();
			birds.flip[1] = 1;
		}
    else if (birds.flip[1] == 1)
    {
			$(b2+'-2').hide();
			$(b2+'-3').show();
			birds.flip[1] = 0;
			birds.dir[1] = 1;
		}
	}
  else
  {
		if (birds.flip[1] == 0)
    {
			$(b2+'-3').hide();
			$(b2+'-2').show();
			birds.flip[1] = 1;
		}
    else if (birds.flip[1] == 1)
    {
			$(b2+'-2').hide();
			$(b2+'-1').show();
			birds.flip[1] = 0;
			birds.dir[1] = 0;
		}
	}
	setTimeout(function() { birds.flutter2(b2); }, 70);
}

var flightArc1 = function()
{
	this.css = function(p)
  {
		var s = Math.sin(p * 30);
		var x = 655 - (p * 150);
		var y = s * 1.8 + 25;
		return { marginTop: y+'px', marginLeft: x+'px' };
	}
};

var flightArc2 = function()
{
	this.css = function(p)
  {
		var s = Math.sin(p * 20);
		var x = 505 - (p * -150);
		var y = s * 1.8 + 25;
		return { marginTop: y+'px', marginLeft: x+'px' };
	}
};

var flightArc3 = function()
{
	this.css = function(p)
  {
		var s = Math.sin(p * 30);
		var x = 655 - (p * 160);
		var y = s * 2 + 25;
		return { marginTop: y+'px', marginLeft: x+'px' };
	}
};

var flightArc4 = function()
{
	this.css = function(p)
  {
		var s = Math.sin(p * 20);
		var x = 495 - (p * -160);
		var y = s * 2 + 25;
		return { marginTop: y+'px', marginLeft: x+'px' };
	}
};

birds.movement1 = function(b1)
{
	$(b1).animate({
		path : new flightArc1
	}, 2000).animate({
		path : new flightArc2
	}, 2000);
}

birds.movement2 = function(b2)
{
	$(b2).animate({
		path : new flightArc3
	}, 1950).animate({
		path : new flightArc4
	}, 2300);
}

$(function()
{
  birds.create();
});
