var player = null;
var activePreview = null;
var previewStatus = null;
var playlist = null;
var position = null;

jQuery(document).ready(function() {
  jQuery('#backBtn').hide();
  jQuery('#nextBtn').hide();
  
  if(navigator.appName.indexOf("Microsoft") != -1) {
	player = window.yamaha_player;
  } else {
	player = window.document.yamaha_player;
  }
	
  jQuery('#stopBtn').click(function() {
	stopSong();
	jQuery("#playerDiv").slideUp();
  });
 
  jQuery('#backBtn').click(function() {
	if (playlist != null && position > 0) {
	  position --;
	  playSong(playlist[position]);
	}	
  });
  
  jQuery('#nextBtn').click(function() {
	if (playlist != null && position < playlist.length - 1) {
	  position ++;
	  playSong(playlist[position]);
	}
  });

  jQuery('#previewActions span').hover(function() {
	jQuery(this).addClass("hover");
  }, function() {
	jQuery(this).removeClass("hover");
  });
  
  jQuery('a.preview').click(function() {
	//check for flash
	try {
	  player.ready();
	} catch(e) {
	  return true;
	}
	
	if (player == null) {
	  return false;
	}
	
	//new preview is requested
	if (activePreview != null && jQuery(this).attr('href') != activePreview.attr('href')) {
	  stopSong()
	}
	
	//set which link/button is active
	activePreview = jQuery(this);
	
	if(previewStatus == 'playing') {
	  stopSong();
	  jQuery("#playerDiv").slideUp();
	} else {
	  jQuery("#playerDiv").slideDown("normal", function() {
		//set the song title in the preview box
		var details = eval('(' + activePreview.attr('rel') + ')');
		var title = '';
		if (details[0]) {
		  playlist = details;
		  position = 0;
		  title = playlist[position].title;
		} else {
		  if (details.item_number == undefined) {
			playlist = new Array();
			jQuery('a.preview').each( function(i) {
			  var song = eval('(' + jQuery(this).attr('rel') + ')');
			  song.preview_position = i;
			  playlist.push(song);
			  if (jQuery(this).attr('href') == activePreview.attr('href')) {
				position = i;
			  }
			});
		  } else {
			playlist = null;
		  }
		  title = details.title;
		}
		
		//play the song
		var song = Object();
		song.title = title;
		song.location = activePreview.attr('href')
		playSong(song);
	  });
	}
	
	//stop the link  
	return false;
  });

  // bundle member previews
  jQuery(".member_preview").click(function () {
	  // this.nth-child(1).slideToggle("slow");
	  // jQuery(this).parentNode.children("div").slideToggle("slow");
	  jQuery(this).parent(".member_preview_holder").children("div").slideToggle("normal");
  });




});

  function playSong(song) {
	var title = song.title;
	var url = null;


	
	
	if (song.location != undefined) {
	  url = song.location
	} else if (song.preview_location != undefined) {
	  url = song.preview_location;
	}
	if (song.preview_position != undefined) {	   
	  activePreview =  jQuery('a.preview:eq(' + song.preview_position + ')');
	}
	
	//check prev/fwd buttons
	if (playlist != null && position < playlist.length - 1) {
	  jQuery('#nextBtn').show();
	  jQuery('#nextBtn').removeClass('disabled');
	} else {
	  jQuery('#nextBtn').show();
	  jQuery('#nextBtn').addClass('disabled');
	}

	if (playlist != null  && position > 0) {
	  jQuery('#backBtn').show();
	  jQuery('#backBtn').removeClass('disabled');
	} else {
	  jQuery('#backBtn').show();
	  jQuery('#backBtn').addClass('disabled');
	}
	
	if(playlist.length == 1) {
	  jQuery('#backBtn').hide();
	  jQuery('#nextBtn').hide();
	  jQuery('#playerDiv #previewActions').removeClass('large');
	  jQuery('#playerDiv #previewActions').addClass('small');
	} else {
	  jQuery('#playerDiv #previewActions').removeClass('small');
	  jQuery('#playerDiv #previewActions').addClass('large');
	}
	
	jQuery('a.preview').show();
	activePreview.hide();
	jQuery('div.playing').remove();
	activePreview.after("<div class='playing'>Playing...</div>");

	if (url == null && activePreview.attr('href')) {
		url = activePreview.attr('href');
	}
	if (!title && activePreview.attr('title')) {
		title = activePreview.attr('title');
	}
	jQuery("#previewSong").html(title);
	
	player.playSong("http://" + location.host + url);
	previewStatus = 'playing';	
  }
  
  function stopSong() {
	//make sure the player is stopped
	player.stopSong();
  
	//set all the buttons to default state
	jQuery('div.playing').remove();
	jQuery('a.preview').show();
	previewStatus = null;
  }
  
  function setStop() {
	return true;
  }
  
  function setPlay() {
	if (playlist != null) {
	  position ++;
	  if (position < playlist.length) {
		playSong(playlist[position]);
	  } else {
		stopSong();
		jQuery("#playerDiv").slideUp();
	  }
	} else {
	  stopSong();
	  jQuery("#playerDiv").slideUp();
	}
  }
