(function($) {
	$.fn.initPlayer = function(options) {
		var defaults = {
			autoPlay: true,
			ready: function() {
				playListInit(options.autoPlay); // Parameter is a boolean for autoplay.
			},
			oggSupport: false,
			playlist: [],
			swfPath: "/javascripts"
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
				var obj = $(this);
				$(this).data("playList.list", options.playlist)
				$(this).jPlayer(options)
				.jPlayerId("play", "play")
				.jPlayerId("pause", "pause")
				.jPlayerId("loadBar", "player-loading-progress")
				.jPlayerId("playBar", "player-play-progress")
				.onSoundComplete( function() {
					$(this).trigger("playList.next")
				})
				.onProgressChange(function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
				  var myPlayedTime = new Date(playedTime);
					var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
					var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
					$("#player-time").text("("+ptMin+":"+ptSec+")");
				})
				.bind("playList.next", function() {
					var playItem = $(this).data("playList.currentItem")
					var index = (playItem+1 < options.playlist.length) ? playItem+1 : 0;
					playListChange( index )
				})
				.bind("playList.previous", function() {
					var playItem = $(this).data("playList.currentItem")
					var index = (playItem-1 >= 0) ? playItem-1 : options.playlist.length-1;
					playListChange( index )
				})
				.bind("playList.change", function(e, index) {
					playListChange(index);
				})				
				$("#prev").click( function() {
					$("#jplayer").trigger("playList.previous");
					return false;
				});
				$("#next").click( function() {
					$("#jplayer").trigger("playList.next");
					return false;
				});
			}
		);		
		function playListInit(autoplay) {
			$("#jplayer").data("playList.currentItem", 0)
			if(autoplay) {
				playListChange( $("#jplayer").data("playList.currentItem") );
			} else {
				playListConfig( $("#jplayer").data("playList.currentItem") );
			}
		};
		
		function playListConfig( index ) {
			var current = $("#jplayer").data("playList.list")[index];
			$("#jplayer").data("playList.currentItem", index);
			$("#player-title").text(current.name);
			$("#jplayer").setFile(current.mp3);
		}
		
		function playListChange( index ) {
			playListConfig( index );
			$("#jplayer").play();
		}
		
		
		
	};
	
	$.fn.makeAjaxy = function(options) {
		var defaults = {
			replacedContent: "#content",
			beforeReplace: function(element) {
				$("#nav .active").removeClass("active");
				$(element).addClass("active");
			},
			afterReplace: function(element) {
				document.title = "Blaine Larsen - " + element.id.capitalize();
			}
		};
		//turn on fragment change watching
		$.fragmentChange(true);
		var options = jQuery.extend(defaults, options);
		//watch the page fragment
		$(document).bind("fragmentChange.page", function() {
			// replace the content of the replacedContent div with the content returned from the ajax request
			$(options.replacedContent).load($.fragment().page + "# " + options.replacedContent)
		})
		
		
		// now lets change the fragment when someone clicks a nav link.  
		$(this).click(function() {
			// fire before callback
			options.beforeReplace(this);
			$.setFragment({page: this.pathname});
			// fire after callback
			options.afterReplace(this);
			return false;
		});
	};
	
	$.fn.simpleTiwtter = function(username, options) {
		if (username) {
			var defaults = {
				count: '1'
			};
			var options = $.extend(defaults, options);
			var url = "http://www.twitter.com/status/user_timeline/" + username + ".json?count=" + options.count + "&callback=?";
			var div = this;
			div.append("<div class='loading'> Loading tweet...</div>")
			$.getJSON(url, function(data) {
				div.html("<div class='status'>" + data[0].text + "&nbsp;&nbsp;&nbsp;<em><a href='http://www.twitter.com/" + username + "' class='red'>Follow</a></em></div>")
			});
		} else {
			console.debug("yo! Give me your twitter name!");
		}
				
		return this;
		
	};
	jQuery.fn.submitWithAjax = function() {
    this.submit(function() {
      $.post(this.action, $(this).serialize(), null, "script");
      $("#subscriber_email").val("")
      return false;
    })
    return this;
  };
})(jQuery)

String.prototype.capitalize = function(){
    return this.replace(/\S+/g, function(a){
        return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
    });
};
