$(function() {
	// alert( Cupcake );
cupcake = Cupcake.spriteInit($.find('#cake')[0], function() {
	this.animation("fly", [
		{x: 17, y:  0, l: 100},
		{x: 15, y: -2, l: 100},
		{x: 12, y: -4, l: 100},
		{x:  9, y: -6, l: 100},
		{x:  7, y: -8, l: 100},
		{x:  5, y:-12, l: 100},
		{x:  3, y:-14, l: 100},
		{x:  1, y:-16, l: 100},
		{x:  0, y:-20, l: 100},
		{x: -1, y:-25, l: 100},
		{x: -3, y:-30, l: 100},
		{x: -6, y:-24, l: 100},
		{x: -9, y:-20, l: 100},
		{x:-15, y:-15, l: 100},
		{x:-30, y:-12, l: 100},
		{x:-25, y: -9, l: 100},
		{x:-20, y: -6, l: 100},
		{x:-18, y: -4, l: 100},
		{x:-15, y: -1, l: 100},
		{x:-12, y:  0, l: 100},
		{x: -9, y:  1, l: 100},
		{x: -6, y:  2, l: 100},
		{x: -3, y:  4, l: 100},
		{x:  0, y:  6, l: 100},
		{x:  2, y:  9, l: 100},
		{x:  5, y: 12, l: 100},
		{x:  7, y: 14, l: 100},
		{x: 11, y: 16, l: 100},
		{x: 13, y: 20, l: 100},
		{x: 15, y: 18, l: 100},
		{x: 17, y: 15, l: 100},
		{x: 19, y: 13, l: 100},
		{x: 19, y: 10, l: 100},
		{x: 17, y: 10, l: 100},
		{x: 15, y: 15, l: 100},
		{x: 13, y: 16, l: 100},
		{x: 13, y: 15, l: 100},
		{x: 15, y: 12, l: 100},
		{x:  7, y:  9, l: 100},
		{x:  4, y:  7, l: 100},
		{x:-89, y:  4, l: 700}
	]);
});

Spinner1 = SpinnerOne.init('images/characters/spinner.png', {
	padding: 0
},
function() {
	this.place(992, 290).css('zIndex', 14).animate([{
		r: 50
	}]);
},
'#island').bind('mouseover', function() {
	$(this).data('mouseover', true);
	var $self = $(this);
	var doubleSpin = function() {
		if ($self.data('mouseover')) Spinner1.animate([{
			r: 50,
			l: 60
		}], false, doubleSpin);
	}
	doubleSpin();
}).bind('mouseout', function() {
	$(this).data('mouseover', false);
});
Spinner2 = SpinnerTwo.init('images/characters/spinner.png', {
	padding: 0
},
function() {
	this.place(1100, 325).css('zIndex', 14).animate([{
		r: 50
	}]);
},
'#island').bind('mouseover', function() {
	$(this).data('mouseover', true);
	var $self = $(this);
	var doubleSpin = function() {
		if ($self.data('mouseover')) Spinner2.animate([{
			r: 50,
			l: 60
		}], false, doubleSpin);
	}
	doubleSpin();
}).bind('mouseout', function() {
	$(this).data('mouseover', false);
});
Spinner3 = SpinnerThree.init('images/characters/spinner.png', {
	padding: 0
}, function() {
	this.place(1052, 250).css('zIndex', 14).animate([{
		r: 50
	}]);
},
'#island').bind('mouseover', function() {
	$(this).data('mouseover', true);
	var $self = $(this);
	var doubleSpin = function() {
		if ($self.data('mouseover')) {
			Spinner3.animate([{
				r: 50,
				l: 60
			}], false, doubleSpin);
		}
	}
	doubleSpin();
}).bind('mouseout', function() {
	$(this).data('mouseover', false);
});

$('#house').hover(function() {
	if (!cupcake.animating) {
		cupcake.animate("fly", false);
	}
});

setInterval(function() {
	$('#neon')[$('#neon').hasClass('none') ? 'removeClass': 'addClass']('none');
}, 1000);

setInterval(function() {
	$('#bulbs2')[$('#bulbs2').hasClass('none') ? 'removeClass': 'addClass']('none');
}, 300);


showRobot = function() {
	$('#doorguys').stop().animate({
		left: "-55px"
	});
	$('#doorwomen').stop().animate({
		left: "112px"
	});
	$('#interior').stop().animate({
		height: "150px",
		width: "123px"
	}, function() {
		setTimeout(function() {
			hideRobot();
		}, 1000 + (1000 * Math.random()));
	});
}
hideRobot = function() {
	$('#doorguys').stop().animate({
		left: "1px"
	});
	$('#doorwomen').stop().animate({
		left: "57px"
	});
	$('#interior').stop().animate({
		height: "100px",
		width: "110px"
	}, function() {
		setTimeout(function() {
			showRobot();
		}, 1000 + (3000 * Math.random()));
	});
}
hideRobotFast = function() {
	$('#doorguys').stop().animate({
		left: "1px"
	}, 100);
	$('#doorwomen').stop().animate({
		left: "57px"
	}, 100);
	$('#interior').stop().animate({
		height: "100px",
		width: "110px"
	}, 100, function() {
		setTimeout(function() {
			showRobot();
		}, 1000 + (3000 * Math.random()));
	});
}
setTimeout(function() {
	showRobot(true)
}, 1000 + (3000 * Math.random()));



$('#guysenter, #womensenter').mouseover(function() {
	StopDoorOpenInterval = setInterval(function() {
		hideRobotFast();
	}, 10);
}).mouseout(function() {
	window.clearInterval( StopDoorOpenInterval );
});

//$('#helterskelter').mouseover(function() {
//	$('#doorguys').stop().animate({
//		left: "-55px"
//	});
//	$('#doorwomen').stop().animate({
//		left: "112px"
//	});
//	$('#interior').stop().animate({
//		height: "150px",
//		width: "123px"
//	});
//}).mouseout(function() {
//	$('#doorguys').stop().animate({
//		left: "1px"
//	});
//	$('#doorwomen').stop().animate({
//		left: "57px"
//	});
//	$('#interior').stop().animate({
//		height: "100px",
//		width: "110px"
//	});
//});
	zebra = Zebra.init('images/characters/zebra.png', function() {

	this.zcorn = this.paper.image("images/characters/zcorn.png", 68, 63, 136, 126).hide();
	
	var runLeft = (0 - screen.availWidth) + 1000;
	
	$(this.canvas).css('zIndex', 100);
	this.animation('over_under', [
		{x:   0, y:   0, r:-15, l: 250},
		{x: -70, y:  40, r:  0, l: 350},
		{x: -30, y:  21, r: 10, l: 150},
		{x: -20, y:  10, r: 10, l: 150},
		{x: -20, y: -10, r: 10, l: 150},
		{x: -20, y: -16, r: 10, l: 150},
		{x: -30, y:   3, r: -5, l: 150},
		{x: -30, y:   8, r:-10, l: 100},
		{x: -30, y:  12, r:-10, l: 100},
		{x: -20, y:  30, r: -5, l: 100},
		{x: -40, y:  30, r:  5, l: 100},
		{x: -50, y:  15, r: 10, l: 250},
		{x:-200, y:   0, r:  0, l: 500, f: function() {
			self.image.hide();
			self.zcorn.show();
		}},
		{x: -200, y: -30, r: 20, l: 1000},
		{x: -100, y: -55, r: 10, l:  500},
		{x: -100, y: -55, r:-10, l:  400},
		{x: runLeft, y: -80, r:-30, l: 2400},
		
		{f: function() {
			self.zcorn.rotate(0, true).hide();
			self.image.show().rotate(0, true);
		}, l: 300}
	]);
	var self = this;
	self.place(1260, 300);
	var loop = function() {
		setTimeout(function() {
			self.place(1260, 300, 0).animate("over_under", false, loop);
		}, 10000 + (1000 * Math.random()));
		// }, 100);
	}
	loop();
},
'#island');
	$('#bear').hide();
$('#bridge div:not(#glowing1)').hide();
	setInterval(function(a) {
		if(!$('#bridge').data('second')) {
			$('#bridge').data('second', true);
			$('#bridge').css('backgroundPosition', '0 0px');
		} else {
			$('#bridge').data('second', false);
			$('#bridge').css('backgroundPosition', '0 400px');
		}
	}, 200);
$('#bear').show();
$('#bear').css('cursor', 'pointer').click(function() {
	window.location = 'http://shop.irregularchoice.com';
});
startBearAnimation();

function startBearAnimation() {
	$('#bear').loop(function() {
	
			$('#bridge #glowing1').hide();
			$('#bear').animate({
				top: "-=100px",
				width: "130px"
			},
			{
				queue: false,
				easing: 'easeOutBack',
				complete: function() {
					$('#bridge #glowing4').show();
				}
			});
			$('#bear').animate({
				left: "+=80px"
			},
			{
				queue: false,
				easing: 'swing'
			});
		},function() {
		
			$('#bridge #glowing4').hide();
			$('#bear').animate({
				top: "+=20px",
				width: "160px"
			},
			{
				queue: false,
				easing: 'easeInBack',
				complete: function() {
					$('#bridge #glowing3').show();
				}
			});
			$('#bear').animate({
				left: "-=120px"
			},
			{
				queue: false,
				easing: 'swing'
			});
		}, function() {
		
					$('#bridge #glowing3').hide();
			$('#bear').animate({
				top: "+=20px",
				width: "195px"
			},
			{
				queue: false,
				easing: 'easeInBack',
				complete: function() {
					$('#bridge #glowing2').show();
				}
			});
			$('#bear').animate({
				left: "-=100px"
			},
			{
				queue: false,
				easing: 'swing'
			});
		}, function() {
			$('#bridge #glowing2').hide();
			$('#bear').animate({
				top: "+=60px",
				width: "213px"
			},
			{
				queue: false,
				easing: 'easeInBack',
				complete: function() {
					$('#bridge #glowing1').show();
				}
			});
			$('#bear').animate({
				left: "+=140px"
			},
			{
				queue: false,
				easing: 'swing'
			});
	}, 2000);
}
	function flyLogoUp() {
		$('#baloon').animate({
			top: "-200px"
		}, 8000 + (6000 * Math.random()), function() {
			flyLogoDown();
		});
	}
	function flyLogoDown() {
		var wwidth = ( window.innerWidth != undefined ) ? window.innerWidth : document.documentElement.clientWidth;
		$('#baloon').css({
			left: (wwidth * Math.random()) + "px"
		}).animate({
			top: "200px"
		}, 8000 + (6000 * Math.random()), function() {
			flyLogoUp();
		});
	}
	flyLogoUp();
});