$(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
		{name:"Saudade - Cam MacKellar",mp3:"http://www.kevinprosch.com/store/mp3/Cam MacKellar/01 Go and Find Someone.mp3",ogg:"http://www.kevinprosch.com/store/mp3/Cam MacKellar/01 Go and Find Someone.ogg",pic:"img/covers/saudade.jpg"},
		{name:"Tumbling Ground - Kevin Prosch",mp3:"http://www.kevinprosch.com/store/mp3/Tumbling Ground/02 SHE WALKS IN BEAUTY.mp3",ogg:"http://www.kevinprosch.com/store/mp3/Tumbling Ground/02 SHE WALKS IN BEAUTY.ogg",pic:"img/covers/kp-tumbling.jpg"},
		{name:"Daylight and Stars - Jim Jones",mp3:"http://www.kevinprosch.com/store/mp3/Jim Jones/02 Wintersong.mp3",ogg:"http://www.kevinprosch.com/store/mp3/Jim Jones/02 Wintersong.ogg",pic:"img/covers/daylight.jpg"},
		{name:"The Reality - KDub and the pop-a-wheelies",mp3:"http://www.kevinprosch.com/store/mp3/kdub/03.mp3",ogg:"http://www.kevinprosch.com/store/mp3/kdub/03.ogg",pic:"img/covers/kdub.jpg"},
		{name:"Since We've Been Apart - StreetChoir",mp3:"http://www.kevinprosch.com/store/mp3/Since Weve Been Apart/Twisted Around.mp3",ogg:"http://www.kevinprosch.com/store/mp3/Since Weve Been Apart/Twisted Around.ogg",pic:"img/covers/street.jpg"},
		{name:"Compared to You - Luke Dowler",mp3:"http://www.kevinprosch.com/store/mp3/luke dowler/2.mp3",ogg:"http://www.kevinprosch.com/store/mp3/luke dowler/2.ogg",pic:"img/covers/luke.jpg"},
		{name:"Good News - Adam Cates",mp3:"http://www.kevinprosch.com/store/mp3/this wilderness/1.mp3",ogg:"http://www.kevinprosch.com/store/mp3/this wilderness/1.ogg",pic:"img/covers/adam.jpg"},
		{name:"Fractal - Loren Johnson",mp3:"http://www.kevinprosch.com/store/mp3/fractal/2.mp3",ogg:"http://www.kevinprosch.com/store/mp3/fractal/2.ogg",pic:"img/covers/fractal.jpg"},
		{name:"At the Mountain's Edge - Chris Stewart",mp3:"http://www.kevinprosch.com/store/mp3/The Mountains Edge/Broken Sample.mp3",ogg:"http://www.kevinprosch.com/store/mp3/The Mountains Edge/Broken Sample.ogg",pic:"img/covers/chris.jpg"},
		{name:"My Devotion - Waking the Sound",mp3:"http://www.kevinprosch.com/store/mp3/my devotion/08 RUSH THE LIGHT.mp3",ogg:"http://www.kevinprosch.com/store/mp3/my devotion/08 RUSH THE LIGHT.ogg",pic:"img/covers/kris.jpg"},
		{name:"Missing You Much - Josh Paulson",mp3:"http://www.kevinprosch.com/store/mp3/Josh Paulson/01 MISSING YOU.mp3",ogg:"http://www.kevinprosch.com/store/mp3/Josh Paulson/01 MISSING YOU.ogg",pic:"img/covers/josh.jpg"},
		{name:"Crazy Young Men - Amarillo Birds",mp3:"http://www.kevinprosch.com/store/mp3/Amarillo Birds/01-CRAZY-YOUNG-MEN-1.mp3",ogg:"http://www.kevinprosch.com/store/mp3/Amarillo Birds/01-CRAZY-YOUNG-MEN-1.ogg",pic:"img/covers/amarillo-birds.jpg"},
		{name:"Come to the Light - Kevin Prosch",mp3:"http://www.kevinprosch.com/store/mp3/come-into-the-light/07.mp3",ogg:"http://www.kevinprosch.com/store/mp3/come-into-the-light/07.ogg",pic:"img/covers/kp.jpg"}
	];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(true); // Parameter is a boolean for autoplay.
			//demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
		},
		oggSupport: true
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));

		//demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$("#jplayer_previous").click( function() {
		playListPrev();
		return false;
	});

	$("#jplayer_next").click( function() {
		playListNext();
		return false;
	});

	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			$("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
		
		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
		$("li.jplayer_playlist_current").prependTo("#jplayer_playlist ul");
		$("#music_art").css("background-image", "url(" + myPlayList[playItem].pic + ")" );
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
		
	}

	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange( index );
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange( index );
	}
	
	
	
	// Check if player is playing
	function isPlaying() {
		var p = $('#jquery_jplayer').jPlayer('getData', 'diag.isPlaying');
		return p;
		//delete p;
	}
	
	// Load frame
	$("<iframe name='content' style='position: absolute; left: 0; top: 0; right: 0; bottom: 0; z-index:1; width: 100%; height: 100%; overflow:hidden; display:none' scrolling='no' frameborder='0' ></iframe>").prependTo('body');
	
	// bring back
	function backHome() {
		$('article').removeAttr("style");
		$('header, footer, #welcome, #artists, #music_art').show();
		$('#new').removeAttr("class");
		$('iframe[name|=content]').css('display','none');
		$('#backhome').remove();
	}
	
	location.hash = '';

	$(window).bind( 'hashchange', function(){
		// Alerts every time the hash changes!
		var hash = location.hash.replace('#/','');
		if (hash == 'index' || hash== '') {
			backHome();
		} else {
			$('iframe[name|=content]').attr('src', hash + '.php').css('display','block').load(function() {	
				
				// Resize Iframe based on loaded content
				this.style.height = this.contentWindow.document.body.offsetHeight + 'px'; 
				
				// Relink hrefs to hashs
				$(this).contents().find("a").each( function() {
					$(this).click(function(){
						var Href2 = $(this).attr('href');
						$(this).attr('target','_parent');
						if ( Href2.indexOf('http') == -1 && Href2.indexOf('coope-store.php') == -1) {
							
							window.parent.location.hash = '#/' + Href2.replace('.php','');
							return false;
						}
					});
				});
			});	
		}
		
	})
	
	// Set intial links 
	$('a').each( function() {
		var Href = $(this).attr('href');
		if ( Href.indexOf('http') == -1 && Href.indexOf('coope-store.php') == -1) {
			$(this).click( function() {
				// load link
				var p = isPlaying();
				if (p) {
					$('header, footer, #welcome, #artists, #music_art').hide();
					$('#new').attr('class','moved');
					$('article').css({'position':'relative','overflow':'hidden','z-index':'100'});
					$("<a href='#/index' id='backhome' style='position: absolute; left: 40px; top: 20px; width: 400px; height: 130px; overflow:hidden'></a>").prependTo('article');
					document.location.hash = '/' + Href.replace('.php','');
					return false;
				}
				
				
			}); 
		}
	});
	
	
});


