/**********************************************
/ MasterCard - Priceless Days Out 2011
/ big:interactive
/ Created: 25/01/11 - Anys Mechkar
/**********************************************

/* ============ $(document).ready =========== */

$(document).ready(function() {
	
	/*Select attractions*/
	var passes = 12;
  	$(".number div").text(passes);
	$(".attractions input").attr('value','');
	$(".attractions td img").click(function(){
		$e = $(this).parent().prev().find('a');
		$e.trigger('click');
	});
	$("#reset").click(function(){
		passes = 12;
		$(".number div").text(passes);
		$(".number a").html("");
		$(".attractions input").attr('value','');
	});
	$(".number a").click(function(){
		//var divId = $(this).attr("id");
		//var inputName = "input[name=" + $(this).attr("id") + "]";
		if (passes != 0){
			if($(this).html() == ""){
				$(this).html(1);
				$(this).next().attr('value','1');
			} else {
			var number = parseInt($(this).html());
			number += 1;
			$(this).html(number);
			//$('input#' + divId).attr('value',number);
			$(this).next().attr('value',number);
			}
			passes -= 1;
			$(".number div").text(passes);
		}
	});	
	
	/*Error message*/
	if($("#errorSummary").length != 0) {
		$("#errorSummary").animate({height: 65}, {duration:500, easing:'easeOutQuad'});
		$("#passport").animate({left:"242px"},{duration:450, easing:'easeInBack'});
		$("#shadow").fadeIn({duration:450, easing:'easeInBack'});
		$("#mastercard").show(1);
		gonext();
	}
	$('#errorSummary .ok').click(function() {
		$("#errorSummary").animate({height: 0}, {duration:700, easing:'easeOutBounce', complete:function(){$("#errorSummary").css({border:'none'});}});
	});
	
	/*Scroller*/
	$('.horizontal_scroller').SetScroller({	
		velocity: 	 200,
		direction: 	 'horizontal',
		startfrom: 	 'right',
		loop:		 'infinite',
		movetype: 	 'linear',
		onmouseover: 'pause',
		onmouseout:  'play',
		onstartup: 	 'play'
	});
	$('#no_mouse_events').ResetScroller({	onmouseover: 'play', onmouseout: 'play'   });
	
	/*Preloader*/
	if($('#wrapperContent').length>0) {
		$(function() {
			var currentTop = 0;
			var step = 0;
			var stepHeight; 
			$.preload([
				"images/passport.png",
				"images/flag.jpg",
				"images/page_right.png",
				"images/page_left.png",
				"images/bg.jpg",
				"images/banner.png",
				"images/img-congrats.jpg",
				"images/img-looser.jpg",
				"images/img-ending.jpg",
				"images/button-offers.jpg",
				"images/button-enter.jpg",
				"images/button-confirm.jpg",
				"images/button-next.jpg",
				"images/chessington.jpg",
				"images/stars1.png",
				"images/thorpe.jpg",
				"images/alton.jpg",
				"images/warwick.jpg",
				"images/dungeons.jpg",
				"images/legoland1.jpg",
				"images/sealife2.jpg",
				"images/sealife1.jpg",
				"images/legoland.jpg",
				"images/tussauds.jpg",
				"images/mrk_siteheader.jpg",
				"images/form1-bg.jpg",
				"images/knight.png",
				"images/form3-bg.jpg",
				"images/form2-bg.jpg",
				"images/form4-bg.jpg",
				"images/form5-bg.jpg",
				"images/stars2.png",
				"images/shadow.png",
				"images/dolphin.png",
				"images/fish2.png",		
				"images/fish1.png",
				"images/congrats.png",
				"images/help.png",
				"images/clouds2.png",
				"images/help2.png",
				"images/help1.png",
				"images/bubble.png"	,
				"images/clouds3.png",
				"images/clouds1.png"
			], {
				init: function(loaded, total) {
					step = 494/total;
				},
				loaded: function(img, loaded, total) {					
					$("#loadingLogo").animate({top:currentTop - step + "px"},100);
					currentTop= currentTop - step;
				},
				loaded_all: function(loaded, total) {
					//$("#indicator").html("Loaded: "+loaded+"/"+total+". Done!");
					if (!$.browser.msie) {
						$("#pageLoading").fadeOut(200,function(){
							$("#wrapperContent").fadeIn(600);
						});
					} else {
						$("#pageLoading").fadeOut(0,function(){
							$("#wrapperContent").fadeIn(0);
						});
					}
					startAnimation();
				}
			});
		});
	}	
});

/* ============ PRELOADER PLUGIN =========== */
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
					.attr("src", imgArr[i])
				);
			}
			
		}
	});
})(jQuery);

/* ============ ANIMATIONS =========== */

/*Bubble Machines*/
var SoapBubbleMachineNumber1 = $('fn').BubbleEngine({
	particleSizeMin: 0,
	particleSizeMax: 10,
	particleSourceX: 170,
	particleSourceY: 168,
	particleDirection: 'left',
	particleAnimationDuration: 10000,
	particleAnimationVariance: 10,
	particleScatteringX: 100,
	particleScatteringY: 0,
	gravity: -290
});

var SoapBubbleMachineNumber2 = $('fn').BubbleEngine({
	particleSizeMin: 0,
	particleSizeMax: 12,
	particleSourceX: 790,
	particleSourceY: 190,
	particleDirection: 'left',
	particleAnimationDuration: 12000,
	particleAnimationVariance: 5000,
	particleScatteringX: 100,
	particleScatteringY: 0,
	gravity: -220,
	RenewBubbles:'off'
});

/*Global Animations*/
var ani = {
	clouds1_1: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	1,
		delay:	10
	},

	clouds2_1: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	1,
		delay:	20
	},
	clouds3_1: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	1,
		delay:	40
	},
	banner_pos: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	2,
		delay:	20,
		onstart: function(){
			$("#banner").animate({top:80},{duration:1000, easing:'easeOutCirc'});
		}
	},
	stars1: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	2,
		delay:	30,
		onstart: function(){
			$("#stars1").animate({width:"225px",height:"213px",left:"200px",top:"32px"},{duration:3000, easing:'easeOutBounce'});
		}
	},
	stars2: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	2,
		delay:	20,
		onstart: function(){
			$("#stars2").delay(500).animate({width:"132px",height:"79px"},{duration:2000, easing:'easeOutBounce'});
		}
	},
	fish1_1: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	2,
		delay:	10,
		onstart: function(){
			$("#fish1").animate({left:"120px"},{duration:9000, easing:'easeOutQuad',complete:function(){
				if ($("#wrapperAnimations.noAnimations").length == 0){SoapBubbleMachineNumber1.addBubbles(10);}
			}});
		}
	},
	fish2_1: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	2,
		delay:	10,
		onstart: function(){
			$("#fish2").animate({left:"176px"},{duration:4000, easing:'easeOutQuad'});
		}
	},
	knight: {
		type:	'opacity',
		from:	0,
		to:		100,
		step:	2,
		delay:	5,
		onstart: function(){
			$("#knight").animate({height:"124px",width:"88px"},{duration:500, easing:'easeOutQuad'});
		}
	},
	flag: {
		type:	'backgroundx',
		to:		1664,
		step:	128,
	},
	dolphin: {
		type:	'opacity',
		from:	100,
		to:		100,
		step:	2,
		delay:	2,
		onstart: function(){
			$("#dolphin").animate({height:"62px",width:"114px"},{duration:300, easing:'easeOutQuad'});
		}
	}
};

function fish1(){
	$("#fish1").delay(500).animate({left:"-=20px"},500).animate({left:"+=20px"}, 1000).animate({left:"-=4px"}, 150).animate({left:"+=4px"}, 200).animate({left:"-=25px"},2500).animate({left:"+=5px"}, 1000).animate({left:"+=30px"}, 3000).animate({left:"-=10px"},{duration:1500,complete:function(){if ($("#wrapperAnimations.noAnimations").length == 0){SoapBubbleMachineNumber1.addBubbles(10);}}})
	setTimeout("fish1()",9850);
};
function fish2(){
	$("#fish2").delay(500).animate({left:"+=30px"},500).animate({left:"-=30px"}, 1000).animate({left:"+=6px"}, 150).animate({left:"-=6px"}, 200).animate({left:"+=45px"},{duration:2500,complete:function(){if ($("#wrapperAnimations.noAnimations").length == 0){SoapBubbleMachineNumber2.addBubbles(10);}}}).animate({left:"-=15px"}, 1000).animate({left:"+=10px"}, 2500).animate({left:"-=40px"}, 2000)
	setTimeout("fish2()",9850);
};
function clouds1(){
	$("#clouds1").animate({backgroundPosition: '-549px 0px'},100000).animate({backgroundPosition: '0px 0px'},0)
	setTimeout("clouds1()",100000);
};
function clouds2(){
	$("#clouds2").animate({backgroundPosition: '-606px 0px'},150000).animate({backgroundPosition: '0px 0px'},0)
	setTimeout("clouds2()",150000);
};
function clouds3(){
	$("#clouds3").animate({backgroundPosition: '-552px 0px'},100000).animate({backgroundPosition: '0px 0px'},0)
	setTimeout("clouds3()",100000);
};


function startAnimation(){
	$fx('#clouds1').fxAdd(ani.clouds1_1).fxRun();
	clouds1();
	$fx('#clouds2').fxAdd(ani.clouds2_1).fxRun();
	clouds2();
	$fx('#clouds3').fxAdd(ani.clouds3_1).fxRun();
	clouds3();
	$fx('#flag').fxAdd(ani.flag).fxRun(null,-1);
	$fx('#banner').fxAdd(ani.banner_pos).fxRun(function(){
		$fx('#stars1').fxAdd(ani.stars1).fxRun();
		$fx('#knight').fxAdd(ani.knight).fxHold(1000).fxRun();
		$fx('#dolphin').fxAdd(ani.dolphin).fxHold(1500).fxRun(function(){
			if ($("#wrapperAnimations.noAnimations").length == 0){SoapBubbleMachineNumber2.addBubbles(10);}
		});
		$fx('#stars2').fxAdd(ani.stars2).fxHold(1000).fxRun();
	});
	$fx('#fish1').fxAdd(ani.fish1_1).fxHold(3500).fxRun();
	$fx('#fish2').fxAdd(ani.fish2_1).fxHold(8500).fxRun(function(){
		fish1();
		fish2()
	});
	$("#flipPage0").click(function() {
		gonext();
		$("#passport").animate({left:"242px"},{duration:450, easing:'easeInBack'});
		$("#shadow").fadeIn({duration:450, easing:'easeInBack'});
		$("#mastercard").fadeIn({duration:1500});
	});
};
