// animations.js
// Adam Cox - 07/01/2011

// Animation objects
var objBird = new Image(107, 80);
objBird.src = '/images/animation/birdy.png';
var objBirdRight = new Image(107, 80);
objBirdRight.src = '/images/animation/birdy-right.png';
var objBranch = new Image(538, 144);
objBranch.src = '/images/animation/branch.png';
var objReservations = new Image(120, 72);
objReservations.src = '/images/animation/livebookings.png';
var objReservationsNotLive = new Image(120, 72);
objReservationsNotLive.src = '/images/animation/nolivebookings.png';
var objSignup = new Image(129, 174);
objSignup.src = '/images/animation/newsletter-new.png';
var objCurtain = new Image(358, 363);
objCurtain.src = '/images/animation/curtain.png';
var objNotes = new Image(100, 109);
objNotes.src = '/images/animation/musicnotes.gif';
var objFB = new Image(46, 66);
objFB.src = '/images/animation/facebook.png';

// Animation frame tracking
var activeSecond = 0;
var initialDelay = 0; // in ms
// Default variables
var intervalCont = '';
var bird = '';
var branch = '';
var reservations = '';
var curtains = '';
var facebook = '';
var paths = new Array();

// Moved this config out of the init function as IE was too slow loading the svg canvas.

var c = config.evalJSON();
// load the animation objects
var container = Raphael($("animationCanvas"), c.areawidth, c.areaheight);
//container.safari();

// load the starting scene for this page
if(c.isie=='true'){
	// ie user
	initialDelay = 0;
	$("animationCanvas").setStyle({ display: 'none'});
	setTimeout("$('animationCanvas').setStyle({ display: 'block'})", 800);
} else {
	if(c.pageid==3){
		setTimeout("dropFoodMenu('csl-cosmo-foodmenuanimation');", 4000);
	}		
}

if(c.animationset=='1'){
	branch = container.image(objBranch.src, 20, 20, 538, 144);
	bird = container.image(objBird.src, 460, 38, 107, 80);
	paths['bird'] = container.path("M460,38 C460,38 405-20 380,38 C380,38 345-15 310,15 C310,15 285-5 260,10 C260,10 235-5 210,14 C210,10 185-5 160,8 C160,8 135-5 110,10 C110,10 95-5 80,11").attr({ stroke: "none"});			

	if(c.bookingtype=='live'){
		reservations = container.image(objReservations.src, 200, -72, 120, 72);
	} else {
		reservations = container.image(objReservationsNotLive.src, 200, -72, 120, 72);
	}
	reservations.attr({ href: c.bookingurl });
	
	if(c.facebook!= null){
		facebook = container.image(objFB.src, 40, 75, 46, 66);
		facebook.attr({ href: c.facebook });
		facebook.rotate(-25);
	}
	
	/*
	if(c.pageid==3){
		newsletter =  container.image(objSignup.src, 340, 80, 129, 174);
	} else {
		newsletter =  container.image(objSignup.src, 340, -20, 129, 174);
	}	
	newsletter.attr({ href: c.signupurl });
	newsletter.rotate(-25);
	*/
} else if(c.animationset=='2'){
	bird = container.image(objBirdRight.src, -80, 45, 107, 80);
	paths['bird'] = container.path("M-80,45 C-80,45 -40-15 -20,45 C-20,45 10-15 30,45 C30,45 60-15 90,45 C90,45 120-15 150,45 C150,45 180-15 210,45 C210,45 240-15 270,42 C270,42 275-5 280,42").attr({ stroke: "none"});
} else if(c.animationset=='3'){
	curtain = container.image(objCurtain.src, 130, 0, 370, 381);
	bird = container.image(objBird.src, 530, 250, 107, 80);
	paths['bird'] = container.path("M530,38 C530,38 460-20 435,38 C435,38 400-15 365,38 C365,38 340-5 315,38 C315,38 290-15 265,38 C265,38 240-5 215,38 C215,38 190-5 165,38 C165,38 150-5 135,38 C135,38 110-5 105,45 C105,38 80-5 75,48 C75,38 60-1 68,68").attr({ stroke: "none"});
	paths['birddrink'] = container.path("M68,68 L67,68").attr({ stroke: "#000"});
} else if(c.animationset=='4'){
	initialDelay = 1500; // added to allow map to load first
	bird = container.image(objBird.src, 220, 20, 107, 80);
	paths['bird'] = container.path("M220,20 C220,20 100-5 100,52").attr({ stroke: "none"});
	paths['bird2'] = container.path("M100,52 C100,50 90-5 60,47 C60,47 40-15 10,23").attr({ stroke: "none"});
} else if(c.animationset=='5'){
	initialDelay = 1500; // added to allow gallery to load first
	bird = container.image(objBird.src, 400, 10, 107, 80);
	paths['bird'] = container.path("M400,25 C400,25 375-5 350,25 C350,25 325-5 300,25 C300,25 275-5 250,25 C250,25 225-5 200,25 C200,25 175-5 150,25 C150,25 125-5 100,25 C100,25 80-5 75,25 C75,25 65-5 50,25").attr({ stroke: "none"});
} else if(c.animationset=='6'){
	//var notes = container.image(objNotes.src, 360, 60, 100, 109);
	//notes.rotate(25);
	bird = container.image(objBirdRight.src, -100, 30, 107, 80);
	paths['bird'] = container.path("M-100,150 C-100,150 -20-5 30,150 C30,150 50-15 50,90 C50,90 75-15 90,45 C90,45 120-15 130,15 C130,15 160-15 190,15").attr({ stroke: "none"});
} else if(c.animationset=='7'){
	bird = container.image(objBirdRight.src, -100, 110, 107, 80);
	paths['bird'] = container.path("M-100,110 C-100,110 -20-5 10,130 C10,130 30-2 30,120 C30,120 70-5 70,110 C70,110 90-5 90,105").attr({ stroke: "none"});
}

function initializeAnimation(){	
	// start the animation sequence for this page
	intervalCont = setInterval(function(){startSequence(c.animationset)}, 1000);
}

// array [ setid ] [ second ] = [ action ]
var animationSequence = new Array();

// Animation set 1 (Home, Food)
animationSequence[1] = new Array();
animationSequence[1][1] = 'bird.attr({ x: 460, y: 38}).animateAlong(paths[\'bird\'], 3500, false); ';
if(c.facebook!="null"){
	//animationSequence[1][0] = 'facebook.attr({ x: 40, y: 75}).animate({rotation: 20, x: 40, y: 70 }, 1500, "elastic"); ';
}

// reservations sign
animationSequence[1][3] = 'reservations.animate({rotation: -5, x: 170, y: -10 }, 500, function(){ reservations.animate({rotation: 5, x: 200, y: 40}, 500, function(){reservations.animate({rotation: -25, x: 190, y: 90}, 500, function(){})})});';

// Animation set 2 (Drink)
animationSequence[2] = new Array();
animationSequence[2][0] = 'bird.attr({ x: -80, y: 45}).animateAlong(paths[\'bird\'], 3000, false, function(){ $("csl-pub-featureddrinks").setStyle({ zIndex: "5000"}) });';

// Animation set 3 (Sundays)
animationSequence[3] = new Array();
//animationSequence[3][0] = 'curtain.animate({y: -285}, 4000); bird.attr({ x: 475, y: 305}).animateAlong(paths[\'bird\'], 4000, false, function(){ bird.animate({rotation: -25}, 100, function(){ bird.animate({rotation: -25}, 1000, function(){bird.animate({rotation: 170, y: 570}, 700, function(){})})}) });'; 
animationSequence[3][0] = 'curtain.animate({y: -285}, 4000); bird.attr({ x: 475, y: 305}).animateAlong(paths[\'bird\'], 4000, false, function(){ bird.animate({rotation: -25}, 100, function(){ bird.animate({rotation: -25}, 1000, function(){bird.animate({rotation: 0}, 200, function(){})})}) });'; 

animationSequence[3][5] = 'try { $("csl-pub-specialmenus").setStyle({ zIndex: "5000" }); } catch (err) { $$(".pdf").each(function(e){ e.setStyle({ zIndex: "5000"})}); } ';

// Animation set 4 (findus)
animationSequence[4] = new Array();
animationSequence[4][0] = 'bird.attr({ x: 220, y: 20}).animateAlong(paths[\'bird\'], 750, false)';
animationSequence[4][3] = 'bird.attr({ x: 100, y: 50}).animateAlong(paths[\'bird2\'], 1200, false)';

// Animation set 5 (gallery)
animationSequence[5] = new Array();
animationSequence[5][0] = 'bird.attr({ x: 400, y: 25}).animateAlong(paths[\'bird\'], 3000, false)';

// Animation set 6 (Events)
animationSequence[6] = new Array();
animationSequence[6][0] = 'bird.attr({ x: -100, y: 150}).animateAlong(paths[\'bird\'], 3000, false)';

// Animation set 7 (Garden)
animationSequence[7] = new Array();
animationSequence[7][0] = 'bird.attr({ x: -100, y: 150}).animateAlong(paths[\'bird\'], 3000, false)';

function startSequence(sequence){
		try {
			window.eval(animationSequence[sequence][activeSecond]);		
		} catch (e) {
			// no entry
		}
		if(activeSecond < 10){
			activeSecond++;
		} else {
			clearInterval(intervalCont);
		}
}

function dropFoodMenu(elementid){
	new Effect.Move(elementid, {
	  x: 137, y: 542, mode: 'absolute'
	});

}


