$(document).ready(function() {

//$(".nav").draggable();


var totalpics = 0;
var currentpic = 0;
var int = 0;

function init_lis() {   
   $("ul.nav a").click(function () {
		var idis = $(this).attr('id');
		var topis = $(this).attr('rel');;
		var leftis = $(this).attr('alt');;
		
		if(idis == 'music_li') {
			//alert(idis);
	        //count music player
			if ( $("div#music_player").length < 1 ) {
			//	var ii = i;
			//alert($("div#music_player").length);
				drawplayer();
			}
			//alert(ii);
			
			//
		}
		
		//$('div#music_player').animate({
		//		left: leftis,
		//		top: topis
		//		}, 1000);
	});
}

   var player = ' <div id="music_player"><span id="drag_music">DRAG</span><span id="close_music">close</span><br />';
   player += '<div id="inner">&nbsp;</div><!-- inner --> ';
	player += '<embed width="200" height="64" style="-moz-opacity:.50; filter:alpha(opacity=50); opacity:.50;" src="http://cache.reverbnation.com/widgets/swf/13/widgetPlayerMini.swf?emailPlaylist=artist_350446&backgroundcolor=EEEEEE&font_color=000000&posted_by=artist_350446&shuffle=&autoPlay=true" type="application/x-shockwave-flash"/>';
   player += '</div><!-- inner --> </div><!-- music_player -->';
   
   
   var newplayer ='<div id="music_player"><span id="drag_music">DRAG</span><span id="close_music">close</span><div id="inner"><embed type="application/x-shockwave-flash" src="http://cache.reverbnation.com/widgets/swf/19/tuneWidget.swf?twID=artist_350446&posted_by=artist_350446&shuffle=false&autoPlay=true&blogBuzz=buzz" height="415" width="434"/></div><!-- music_player -->';
   
   
   //alert(player);
   function drawplayer() {
   		$('div#wrapper').append(player);
		init_lis();
		init_close_music();
		//$('#drag_music').parent().draggable();
		//alert(player);
   }
   //drawplayer();
   init_lis();
   
   function init_close_music() {
	   $("span#close_music").click(function () {
			$('div#music_player').remove();
		});
   }
   
   function click_or_change(whichpic){
		//alert('hello click_or_change: '+ whichpic);
		var grabli = 'ul.dyno_gallery li:nth('+whichpic+')';
		var thisrel = $(grabli).attr('rel');
		var thispic = $(grabli).find('img').attr('src');
		var thisdate = $(grabli).find('em').html();
		var thishead = $(grabli).find('h3').html();
		var thisbody = $(grabli).find('div').html();
		//alert('thisbody: '+ thisbody);
		
		// fade out photo
		$('div#photo_wrapper').find('img').animate({
				'opacity': 0.0
				}, 600, function() {
					//change the photo
					$('div#photo_wrapper').find('img').attr('src', thispic);
					//animate it back up
					$('div#photo_wrapper').find('img').animate({
							'opacity': 1.0
						}, 600, function() {
					});
				});
				
		// replace text
		var whichnumber = (whichpic+1)+' of '+(totalpics);
		var newstring = '<em>'+thisdate+'</em><h3>'+thishead+' <span>['+whichnumber+']</span> </h3>'+thisbody;
		$('div#photo_description').html(newstring);
		
   }
   
   
   
   $(".more_photos").click(function () { 
	  //var affectthis = "[li@rel=" + int + "]";
		var direction = $(this).attr('rel');
		//alert(direction);
		if(direction == 'next'){
			currentpic++;
			if(currentpic >= totalpics){
				currentpic = 0;
			}
		}
		if(direction == 'previous'){
			currentpic--;
			if(currentpic < 0){
				currentpic = (totalpics-1);
			}
		}
		//alert('currentpic: '+currentpic);
		click_or_change(currentpic);
		return false;
	});
   
   function ready_the_pics(){
		$('ul.dyno_gallery li').each(function(indexDl) {
			//alert(indexDl);    
			totalpics++;
		});
		click_or_change(0);
		//alert('total pics: '+ totalpics);
   }
   
   ready_the_pics();
   
   
   function fade_out_thank_you() {
	$('div#thankyouwrapper').fadeOut(5000);   
	clearInterval(int);
   }
   int = setInterval(fade_out_thank_you, 3000);

/*  var int = 1;
   var bottom = 1;
   var top = 9;
   
   $("#right").click(function () { 
	  //var affectthis = "[li@rel=" + int + "]";
		if(int < top) {
			var affectthis = "#slide" + (int);
			var affectthisbefore = "#slide" + (int) + " > img";
			var affectthisafter = "#slide" + (int+1) + " > img";
			//alert(affectthis);
			//$(affectthis).addClass('slideLeft', 'slow');
			$(affectthis).animate({
				left: -780
				}, 1000);
			$(affectthisbefore).animate({
				opacity: .65
				}, 1500);
			$(affectthisafter).animate({
				opacity: 1.0
				}, 1500);
			int = int + 1;
		 }
	});
   
   $("#left").click(function () { 
	  //var affectthis = "[li@rel=" + int + "]";
		if(int > bottom) {
			var affectthis = "#slide" + (int-1);
			var affectthisbefore = "#slide" + (int-1) + " > img";
			var affectthisafter = "#slide" + (int) + " > img";
			//alert(affectthis);
			//$(affectthis).addClass('slideLeft', 'slow');
			$(affectthis).animate({
				left: 0
				}, 1000);
			$(affectthisbefore).animate({
				opacity: 1.0
				}, 1500);
			$(affectthisafter).animate({
				opacity: .65
				}, 1500);
			int = int - 1;
		 }
	});
*/
 });
