$(document).ready(function() {
	
	/*
	if ( $("#contact-container").length ) {
		var scrollOffset = 20;
		var scrollMax = $('#touring-container').position().top + $("#current-tour").position().top + parseInt($("#current-tour").css("margin-top"));
		var controlStart = $("#contact-container").offset().top - scrollOffset;
		var focusScrolling = false; // this is used to prevent window-scrolling handler from firing when input focus forces window to scroll
		
		$(window).scroll( function() {
			if (!focusScrolling) { // if box isn't already scrolling for input focus
				if ( $(window).scrollTop() > controlStart ) {
					var scrollTo = $(window).scrollTop() - controlStart;
					$("#contact-container").stop().animate( { top: scrollTo < scrollMax ?  scrollTo : scrollMax + "px" }, 500, "easeOutCubic" );
				} else { $("#contact-container").stop().animate( { top: "20px" }, 400, "easeOutCubic" ); }
			}
		}) //window scroll
	} //if
	*/
	
	if ( $("#contact-container").length ) {
		var scrollOffset = 20;
		var scrollMax = $('#touring-container').position().top + $("#current-tour").position().top + parseInt($("#current-tour").css("margin-top"));
		var controlStart = $("#contact-container").offset().top - scrollOffset;
		var focusScrolling = false; // this is used to prevent window-scrolling handler from firing when input focus forces window to scroll
		
		var topBio = 20;
		var topMedia = $('#music-container').position().top;
		var topDisc = $('#discography-container').position().top;
		var topTour = $('#touring-container').position().top;
		var scrollBuffer = 400;
		var marginTop = -30;
		
		//alert(topBio +','+ topMedia +','+ topDisc +','+ topTour);
		
		$(window).scroll( function() {
			if (!focusScrolling) { // if box isn't already scrolling for input focus
			
				if ( $(window).scrollTop() < topMedia - scrollBuffer - 200 ) {
					var scrollTo = topBio;
				}
				if ( $(window).scrollTop() >= topMedia - scrollBuffer ) {
					var scrollTo = topMedia + marginTop;
				}
				if ( $(window).scrollTop() >= topDisc - scrollBuffer ) {
					var scrollTo = topDisc + marginTop;
				}
				if ( $(window).scrollTop() >= topTour - scrollBuffer ) {
					var scrollTo = topTour + marginTop;
				}
				$("#contact-container").stop().animate( { top: scrollTo + "px" }, 500, "easeOutCubic" );
			
				
			}
		}) //window scroll
	} //if
	
	$("a.thumb").attr('rel', 'photos').fancybox();
	
	$('a.album-link:not(.no-link)').fancybox({ 'type' : 'iframe', 'width' : '85%', 'height' : '75%' })
	
	$(".youtube").click(function() {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 640,
				'height'		: 385,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
				}
			});

			return false;
		});
		
	/* DISCOGRAPHY */
	
	$('.disc-group:first-child').addClass('showing');
	
	$('.disc-next').click( function() {
			$('.disc-prev:not(:visible)').fadeIn(); 
			var amount = $('.disc-group').outerWidth(true); 
			$('.disc-flow').animate({left: '-=' + amount});
			$('.disc-group.showing').removeClass('showing').next().addClass('showing');
			if ( ! $('.disc-group.showing').next().length ) { $('.disc-next').fadeOut(); }
	});

	$('.disc-prev').click( function() {
			$('.disc-next:not(:visible)').fadeIn(); 
			var amount = $('.disc-group').outerWidth(true); 
			$('.disc-flow').animate({left: '+=' + amount});
			$('.disc-group.showing').removeClass('showing').prev().addClass('showing');		
			if ( ! $('.disc-group.showing').prev().length ) { $('.disc-prev').fadeOut(); }
	});
	
	/* PLAYLIST */
	
	var Playlist = function(instance, playlist, options) {
			var self = this;

			this.instance = instance; // String: To associate specific HTML with this playlist
			this.playlist = playlist; // Array of Objects: The playlist
			this.options = options; // Object: The jPlayer constructor options for this playlist

			this.current = 0;

			this.cssId = {
				jPlayer: "jquery_jplayer_",
				interface: "jp_interface_",
				playlist: "jp_playlist_"
			};
			this.cssSelector = {};

			$.each(this.cssId, function(entity, id) {
				self.cssSelector[entity] = "#" + id + self.instance;
			});

			if(!this.options.cssSelectorAncestor) {
				this.options.cssSelectorAncestor = this.cssSelector.interface;
			}

			$(this.cssSelector.jPlayer).jPlayer(this.options);

			$(this.cssSelector.interface + " .jp-previous").click(function() {
				self.playlistPrev();
				$(this).blur();
				return false;
			});

			$(this.cssSelector.interface + " .jp-next").click(function() {
				self.playlistNext();
				$(this).blur();
				return false;
			});
		};

		Playlist.prototype = {
			displayPlaylist: function() {
				var self = this;
				$(this.cssSelector.playlist + " ul").empty();
				for (i=0; i < this.playlist.length; i++) {
					var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
					listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>";

					// Create links to free media
					if(this.playlist[i].free) {
						var first = true;
						listItem += "<div class='jp-free-media'>(";
						$.each(this.playlist[i], function(property,value) {
							if($.jPlayer.prototype.format[property]) { // Check property is a media format.
								if(first) {
									first = false;
								} else {
									listItem += " | ";
								}
								listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
							}
						});
						listItem += ")</span>";
					}

					listItem += "</li>";

					// Associate playlist items with their media
					$(this.cssSelector.playlist + " ul").append(listItem);
					$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
						var index = $(this).data("index");
						if(self.current !== index) {
							self.playlistChange(index);
						} else {
							$(self.cssSelector.jPlayer).jPlayer("play");
						}
						$(this).blur();
						return false;
					});

					// Disable free media links to force access via right click
					if(this.playlist[i].free) {
						$.each(this.playlist[i], function(property,value) {
							if($.jPlayer.prototype.format[property]) { // Check property is a media format.
								$(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
									var index = $(this).data("index");
									$(self.cssSelector.playlist + "_item_" + index).click();
									$(this).blur();
									return false;
								});
							}
						});
					}
				}
			},
			playlistInit: function(autoplay) {
				if(autoplay) {
					this.playlistChange(this.current);
				} else {
					this.playlistConfig(this.current);
				}
			},
			playlistConfig: function(index) {
				$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
				$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
				this.current = index;
				$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
			},
			playlistChange: function(index) {
				this.playlistConfig(index);
				$(this.cssSelector.jPlayer).jPlayer("play");
			},
			playlistNext: function() {
				var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
				this.playlistChange(index);
			},
			playlistPrev: function() {
				var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
				this.playlistChange(index);
			}
		};


		var audioPlaylist = new Playlist("2", [
			{
				name:"3AE - Time",
				mp3:"/music/3AE_Time.mp3"
			},
			{
				name:"Bre Morgan - If I Could",
				mp3:"/music/BreMorgan_IfICould.mp3"
			},
			/*{
				name:"Bre Morgan - Breakaway",
				mp3:"/music/BreMorgan_Breakaway.mp3"
			},*/
			{
				name:"Brian Howe - I'm Back",
				mp3:"/music/BrianHowe_ImBack.mp3"
			},
			/*{
				name:"Brian Howe - It Could Have Been You",
				mp3:"/music/BrianHowe_ItCouldHaveBeenYou.mp3"
			},*/
			{
				name:"Clayton Senne - Cry",
				mp3:"/music/ClaytonSenne_Cry.mp3"
			},
			{
				name:"Evil Engine #9 - Thorn",
				mp3:"/music/EvilEngine9_Thorn.mp3"
			},
			{
				name:"Jennifer Ceccarelli - Redeemed",
				mp3:"/music/JenniferCeccarelli_Redeemed.mp3"
			},
			{
				name:"Jeremy Mix - Alright",
				mp3:"/music/JeremyMix_Alright.mp3"
			},
			{
				name:"Luke Davids - Big Pill To Swallow",
				mp3:"/music/LukeDavids_BigPillToSwallow.mp3"
			},
			{
				name:"Megaphone - Stain",
				mp3:"/music/Megaphone_Stain.mp3"
			},
			{
				name:"Social Ghost - How It Ends",
				mp3:"/music/SocialGhost_HowItEnds.mp3"
			},
			/*
			{
				name:"Social Ghost - Something More",
				mp3:"/music/SocialGhost_SomethingMore.mp3"
			}*/

		], {
			ready: function() {
				audioPlaylist.displayPlaylist();
				audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
			},
			ended: function() {
				audioPlaylist.playlistNext();
			},
			play: function() {
				$(this).jPlayer("pauseOthers");
			},
			swfPath: "../js",
			supplied: "mp3"
		});
	});
