var playAll = true;
var trackIndex = 0;

$(document).ready(function(){	
	$("div.link_button").mouseover(function(){
		$(this).css('border','1px solid #c0c0c0');
		}).mouseout(function(){
			$(this).css('border','1px solid #fff');
			});
				
	$('#content').focus();

	initScroller();

	$('a.home').click(function(e){
		about();
		document.title = 'Josienne Clarke / About';
		e.PreventDefault;
		return false;
	});


	$('a.gallery').click(function(e){
		gallery();
		document.title = 'Josienne Clarke / Gallery';
		e.PreventDefault;
		return false;
	});

	$('a.gigs').click(function(e){
		gigs();
		document.title = 'Josienne Clarke / Gigs';
		e.PreventDefault;
		return false;
	});

	$('a.music').click(function(e){
		music();
		document.title = 'Josienne Clarke / Music';
		e.PreventDefault;
		return false;
	});

	$('a.album').click(function(e){
		album();
		document.title = 'Josienne Clarke / One Light Is Gone';
		e.PreventDefault;
		return false;
	});

	$('a.contact').click(function(e){
		contact();
		document.title = 'Josienne Clarke / Contact';
		e.PreventDefault;
		return false;
	});

	if($('body').attr('id') == 'gallery')
		gallery();
	if($('body').attr('id') == 'music')
		music();

});

function playTrack(t,n) {
	$("#jq_player").jPlayer("setFile", t).jPlayer("play");
		$("#trackname").fadeOut(function(){
	$("#trackname").text(n);
	$("#trackname").fadeIn();
	});
	return false;
} 

function initScroller(){
	$('#content').jScrollPane({scrollbarWidth:5,reinitialiseOnImageLoad:true,maintainPosition:false});
}

function about(){
	$('#content').fadeOut(function(){
		$.get('/content/about.php',function(data){
			$('#content').html(data);
			$('#content').onImagesLoad({ 
		            selectorCallback: initScroller
		        }); 
			$('#content').fadeIn();
		});
	});
}

function gallery(){
	//$('#content').fadeOut(function(){
		$.get('/content/gallery.php',function(data){
			$('#content').html(data);
			$('#content img.img_thumb').css('display','none');			
			//$('#content').fadeIn(function(){
			$('#content img.img_thumb').each(function(){
			var rnd = Math.floor(Math.random()*11) * 600;
			$(this).load(function(){$(this).delay(rnd).fadeIn();});
		});
//				$('#content').onImagesLoad({ 
//		            selectorCallback: initScroller
//		        }); 

				$("a[rel^='prettyPopin']").prettyPopin();
				$("a[rel^='prettyPhoto']").prettyPhoto();
				initScroller();
			//});
		});
	//});
}

function gigs(){
	$('#content').fadeOut(function(){
		$.get('/content/gigs.php',function(data){
			$('#content').html(data);
			initScroller();
			$('#content').fadeIn();
		});
	});
}

function music(){
	var jpPlayInfo = $("#jplay-info");
	$('#content').fadeOut(function(){
		$.get('/content/music.php',function(data){
			$('#content').html(data);
			initScroller();
			$('#content').fadeIn();

			$("#oneLightIsGone li a").click(function(e){
				trackIndex = $("#oneLightIsGone li").index($(this).parent());
				playTrack($(this).attr('href'),$(this).text());
				e.PreventDefault;
				return false;
			});

			$('#jq_player').jPlayer({
				ready: function () {
				playTrack($('#oneLightIsGone li:eq(' + trackIndex + ') a').attr('href'), $('#oneLightIsGone li:eq(' + trackIndex + ') a').text()); 
				},
			swfPath: 'script',
			volume:80,
			customCssIds: true
			}).jPlayer("cssId", "play", "play")
			.jPlayer("cssId", "pause", "pause")
			.jPlayer("cssId", "stop", "stop")
			.jPlayer("onSoundComplete",function(){
					trackIndex++;
					if(trackIndex >13) trackIndex = 0;
					playTrack($('#oneLightIsGone li:eq(' + trackIndex + ') a').attr('href'), $('#oneLightIsGone li:eq(' + trackIndex + ') a').text()); 
				})
			.jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
 			jpPlayInfo.text("at " + parseInt(ppa)+"% of " + $.jPlayer.convertTime(tt) + ", which is " + $.jPlayer.convertTime(pt));
			});
		});
	});

}

function album(){
	$('#content').fadeOut(function(){
		$.get('/content/album.php',function(data){
			$('#content').html(data);
			initScroller();
			$('#content').fadeIn();
		});
	});
}

function contact(){
	$('#content').fadeOut(function(){
		$.get('/content/contact.php',function(data){
			$('#content').html(data);
			initScroller();
			$('#content').fadeIn(function(){$("a[rel^='prettyPopin']").prettyPopin();
				$("a[rel^='prettyPhoto']").prettyPhoto();});
		});
	});
}

