// JavaScript Document

$(document).ready(function() {
	// Fix height on the floated blocks on the homepage.
	var blockHeight1 = $('#left-block').height();
	var blockHeight2 = $('#right-block').height();
	var maxHeight = Math.max(blockHeight1,blockHeight2);
	$('#left-block').height(maxHeight);
	$('#right-block').height(maxHeight);
							   
	// Fix date padding for all browsers on the homepage.
	var datePadding = $('.date').height();
	datePadding = 58 - datePadding;
	datePadding = Math.round(datePadding/2);
	$('.date').css('paddingTop',datePadding+'px');
	$('.date').css('paddingBottom','10px');
	
	// Create birds on main header.
	create_birds();
});

function link(url) {
	window.open('http://'+url);
}

function inlink(url) {
	window.open(url);
}

$(function () {
	jQuery('#parallax').jparallax();
});

function create_birds() {
	var b1 = '.birds1';
	$(b1).css('marginLeft','500px');
	$(b1).css('marginTop','30px');
	var b2 = '.birds2';
	$(b2).css('marginLeft','500px');
	$(b2).css('marginTop','30px');
	
	$('#b1-2').hide();
	$('#b1-3').hide();
	$('#b2-2').hide();
	$('#b2-3').hide();
	
	flutter1('#b1');
	flutter2('#b2');
	call_all();
}

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

var flip1 = 0;
var dir1 = 0;
function flutter1(b1) {
	if(dir1 == 0) {
		if(flip1 == 0) {
			$(b1+'-1').hide();
			$(b1+'-2').show();
			flip1 = 1;
		} else if(flip1 == 1) {
			$(b1+'-2').hide();
			$(b1+'-3').show();
			flip1 = 0;
			dir1 = 1;
		}
	} else {
		if(flip1 == 0) {
			$(b1+'-3').hide();
			$(b1+'-2').show();
			flip1 = 1;
		} else if(flip1 == 1) {
			$(b1+'-2').hide();
			$(b1+'-1').show();
			flip1 = 0;
			dir1 = 0;
		}
	}
	setTimeout(function() { flutter1('#b1'); },60);
}
var flip2 = 0;
var dir2 = 0;
function flutter2(b2) {
	if(dir2 == 0) {
		if(flip2 == 0) {
			$(b2+'-1').hide();
			$(b2+'-2').show();
			flip2 = 1;
		} else if(flip2 == 1) {
			$(b2+'-2').hide();
			$(b2+'-3').show();
			flip2 = 0;
			dir2 = 1;
		}
	} else {
		if(flip2 == 0) {
			$(b2+'-3').hide();
			$(b2+'-2').show();
			flip2 = 1;
		} else if(flip2 == 1) {
			$(b2+'-2').hide();
			$(b2+'-1').show();
			flip2 = 0;
			dir2 = 0;
		}
	}
	setTimeout(function() { 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'};
	}
};

function movement1(b1) {
	$(b1).animate({
		path : new flightArc1
	},2000).animate({
		path : new flightArc2
	},2000);
}
function movement2(b2) {
	$(b2).animate({
		path : new flightArc3
	},1950).animate({
		path : new flightArc4
	},2300);
}