var picScroll;
var gallery = false;
var sound;
var loader;
var loaderWidth = 80;
var loaderHeight = 12;

function loadPic(pic){
	if(gallery){
		loader = new Image();
		loader.src = 'images/loader.gif';
		loader.id = 'preloader';
		loader.style.position = 'absolute';
		loader.style.left = (window.innerWidth/2) - loaderWidth;
		loader.style.top = (window.innerHeight/2) - loaderHeight;

		$('#gallery').append(loader);
		
		$('#black').css('opacity', 1);
		
		$('#gallery').animate({
			opacity: '0'
		}, 100, function(){
			$(this).css('background-image', 'url(images/' + pic + '.jpg)').animate({opacity:'1'}, 100, function(){$('#preloader').remove()});
		});
	} else {
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		gallery = false;
	}
}
function loadSoundPlayer(){
	
	$("#riddle").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/cosmicRiddles.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/cosmicRiddles.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
	$("#enemy").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/enemy.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/enemy.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga",
		cssSelectorAncestor: "#jp_interface_2"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
	$("#feel").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/feelThis.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/feelThis.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga",
		cssSelectorAncestor: "#jp_interface_3"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
	$("#rubber").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/rubberRoom.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/rubberRoom.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga",
		cssSelectorAncestor: "#jp_interface_4"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
	$("#gone").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/longGone.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/longGone.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga",
		cssSelectorAncestor: "#jp_interface_5"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
	$("#echo").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/echoDing.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/echoDing.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga",
		cssSelectorAncestor: "#jp_interface_6"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
	$("#over").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4a: "http://s3.amazonaws.com/soundfeast/yellow/overdrawn.mp3",
				oga: "http://s3.amazonaws.com/soundfeast/yellow/overdrawn.ogg"
			});
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "./scripts",
		supplied: "mp3, oga",
		cssSelectorAncestor: "#jp_interface_7"
	})
	.bind($.jPlayer.event.play, function() { // Using a jPlayer event to avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
	});
	
}
function init()
{	
	
	initG('y5');
	
	$('a').click(function (event){ 
		linkClick(event.target.id);
	  });
	
	$('#tourLink').click(function(){
		
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '0'
		}, 1000);
	});
	$('#picturesLink').click(function(){
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '-1004'
		}, 1000);
	});
	$('#storeLink').click(function(){
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '-2008'
		}, 1000);
	});
	$('#musicLink').click(function(){
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '-3012'
		}, 1000);
	});
	$('#newsLink').click(function(){
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '-4016'
		}, 1000);
	});
	$('#contactLink').click(function(){
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '-5020'
		}, 1000);
	});
	$('#pressLink').click(function(){
		gallery = false;
		$('#black').css('opacity', 0);
		$('#gallery').css('opacity', 0);
		$('#slider').animate({
			left: '-6024'
		}, 1000);
	});
	
	picScroll = new iScroll('pictures', {bounce:true});
	
	$('#pic1').click(function(){gallery = true;loadPic('IMG_4245');});
	$('#pic2').click(function(){gallery = true;loadPic('IMG_4251');});
	$('#pic3').click(function(){gallery = true;loadPic('IMG_4256');});
	$('#pic4').click(function(){gallery = true;loadPic('IMG_4257');});
	$('#pic5').click(function(){gallery = true;loadPic('IMG_4269');});
	$('#pic6').click(function(){gallery = true;loadPic('IMG_4273');});
	$('#pic7').click(function(){gallery = true;loadPic('IMG_4278');});
	$('#pic8').click(function(){gallery = true;loadPic('IMG_4315');});
	$('#pic9').click(function(){gallery = true;loadPic('IMG_4330');});
	$('#pic10').click(function(){gallery = true;loadPic('IMG_4359');});
	$('#pic11').click(function(){gallery = true;loadPic('IMG_4360');});
	$('#pic12').click(function(){gallery = true;loadPic('IMG_4370');});
	$('#pic13').click(function(){gallery = true;loadPic('IMG_4431');});
	$('#pic14').click(function(){gallery = true;loadPic('IMG_4456');});
	$('#pic15').click(function(){gallery = true;loadPic('IMG_4537');});
	$('#pic16').click(function(){gallery = true;loadPic('IMG_4578');});
	$('#pic17').click(function(){gallery = true;loadPic('IMG_4592');});
	$('#pic18').click(function(){gallery = true;loadPic('IMG_4602');});
	$('#pic19').click(function(){gallery = true;loadPic('IMG_4633');});
	$('#pic20').click(function(){gallery = true;loadPic('IMG_4639');});
	$('#pic21').click(function(){gallery = true;loadPic('IMG_4643');});
	$('#pic22').click(function(){gallery = true;loadPic('IMG_4662');});
	$('#pic23').click(function(){gallery = true;loadPic('IMG_4674');});
	$('#pic24').click(function(){gallery = true;loadPic('IMG_4686');});
	$('#pic25').click(function(){gallery = true;loadPic('IMG_4708');});
	$('#pic26').click(function(){gallery = true;loadPic('IMG_4728');});
	
	loadSoundPlayer();
	
}

/***********************
 *	On Document Ready  *
 ***********************/
$(window).load(function(){
	init();
});
