var paper;

// --------------------------------------------------------------------------

soundManager.url = 'animation/js/';
soundManager.useHTML5Audio = true;

// --------------------------------------------------------------------------

function preloadImg(src, fn) {
	
	if (typeof fn == 'undefined') {
		fn = $.noop;
	}
	
	var i = new Image();
	i.src = src;
	i.onload = fn;
	
}

// --------------------------------------------------------------------------

var images = ['secret-behind.png', 'secret-behind-purple.png', 'we-dont-think.png', 
'bg.jpg', 'tabletop.gif', 'andersen.png', 'jones.png', 
'lightning-andersen.png', 'lightning-jones.png'];
var total_res = images.length;
var loaded = 0;
function preloader_check() {

	loaded++;
	if (loaded == total_res) {
		var stage = document.getElementById('stage');
		paper = Raphael(stage, 1024, 645);
		stage.style.backgroundImage = 'none';
		s1();
	}
	
}

if (typeof animUseStatic == 'undefined') {
	for (var i = 0; i < images.length; i++) {
		preloadImg('animation/images/' + images[i], preloader_check);
	}
}

// --------------------------------------------------------------------------

var thunder = [];
soundManager.onready(function() {
	
	thunder[0] = soundManager.createSound({
		id: 'thunder',
		url: 'animation/audio/thunder.mp3',
		autoLoad: true,
		autoPlay: false,
		volume: 100
	});
	
	thunder[1] = soundManager.createSound({
		id: 'thunder2',
		url: 'animation/audio/thunder2.mp3',
		autoLoad: true,
		autoPlay: false,
		volume: 100
	});
	
});

// --------------------------------------------------------------------------

var secret, tabletop;
function s1() {
	
	var bg = paper.image('animation/images/bg.jpg', 0, 0, 1024, 645);
	bg.attr('opacity', 0);
	bg.animate({
		opacity: 1
	}, 150, s2);
	
	tabletop = paper.image('animation/images/tabletop.gif', 0, 645, 1024, 326);
	
	secret = paper.image('animation/images/secret-behind.png', 250, 183, 500, 94);
	secret.attr('opacity', 0);
	
}

function s2() {
	
	secret.animate({
		opacity: 1
	}, 1250, s3);
	
}

function s3() {
	
	secret.animate({
		y: 400
	}, 500, '>', function() {
		secret.hide();
		secret = paper.image('animation/images/secret-behind-purple.png', 250, 400, 500, 94);
		secret.attr('opacity', 0);
		secret.animate({
			opacity: 1
		}, 200);
	});
	
	tabletop.animate({
		y: 326
	}, 500, '>');
	
	var andersen = paper.image('animation/images/andersen.png', 152, 80, 278, 329);
	andersen.attr('opacity', 0);
	
	var jones = paper.image('animation/images/jones.png', 592, 80, 278, 329);
	jones.attr('opacity', 0);
	
	var brains = paper.set();
	brains.push(andersen, jones);
	brains.animate({
		opacity: 1
	}, 3000);
	
	setTimeout(s4, 2000);
	
}



function playThunder() {
	
	var delay_rand = Math.floor(Math.random() * 251);
	
	setTimeout(function() {
		var rand = Math.floor(Math.random() * 2);
		thunder[rand].play();
	}, delay_rand);
	
}

var texts;
function s4() {
	
	var lightning_left = paper.image('animation/images/lightning-andersen.png', 200, 0, 214, 200);
	setTimeout(function() {
		lightning_left.animate({ opacity: 0 }, 1000);
	}, 1000);
	playThunder();
	
	var lightning_right = paper.image('animation/images/lightning-jones.png', 600, 0, 169, 200);
	setTimeout(function() {
		lightning_right.animate({ opacity: 0 }, 1000);
	}, 1500);
	playThunder();
	
	var wedont = paper.image('animation/images/we-dont-think.png', 210, 410, 600, 38);
	wedont.attr('opacity', 0);
	
	secret.animate({
		opacity: 0
	}, 500);
	
	wedont.animate({
		opacity: 1
	}, 1000);
	
	texts = paper.set();
	
	var text1 = paper.text(210, 490, '									\
Creating great medical device or diagnostic brands requires more 		\
than just creative minds. It demands equal\n\
parts market intelligence and marketing savvy. And that’s exactly 		\
how Andersen Jones has used its brand of \nmarketing medicine to 		\
help many of our clients capture the number one or two spots in 		\
their respective markets. \nWe\'ve specialized in this space since	 	\
1985 and have the ability to get up to speed quickly on your 			\
technology to\ncreate effective work that resonates with your 			\
customers and all your other stakeholders.');

	var text2 = paper.text(210, 540, 'If your device, diagnostic or \
	health-related firm is in the market for a new agency, we invite \
	you to pick our brains.');

	texts.push(text1, text2);

	texts.attr({
		font: '12px Tahoma',
		fill: '#4c474a',
		opacity: 0,
		'text-anchor': 'start'
	});
	
	setTimeout(s5, 1500);
	
}

function s5() {
	
	texts.animate({
		opacity: 1
	}, 500);
	setTimeout(s6, 2000);
}

function s6(){
	var $j = jQuery.noConflict();
	$j("#banner").css({'display':'block'});
	$j("#banner").animate({"margin-top": "-=45px","z-index":"2"}, 1500);
	
}

