var pos = 0;
var num = 0;
var ply = false;

$(function() {

  var scr = "Comedian or Routine";
  var user = checkSession();

  $("#fsearch").submit(function() {
    var sv = $("#search").val();
    if(sv == "" || sv == scr)
      alert("Please enter the " + scr + " to search for.");
    else
      document.location.href = '/search/' + sv.replace(/ /g, '+');
  });

  if($("#search").val() == scr)
    $("#search").css("color", "#333333");

  $("#search").focus(function() {
    if(this.value == scr)
    {
      this.value = "";
      this.style.color = "#000000";
    }
  }).blur(function() {
    if(this.value == "")
    {
      this.style.color = "#333333";
      this.value = scr;
    }
  });

  $("#comedian-list").change(function() {
      document.location.href = '/comedian/' + $(this).val();
  });

  $("#album-list").change(function() {
      document.location.href = '/comedian/' + $(this).val();
  });

  $(".vote").click(function() {
    if(!user){
    // prompt for credentials and remember track to favorite upon successful registration
	    showLogin($(this).parent().attr("id"));
    }
    if(!$(this).hasClass("voted"))
    {
      var cls = $(this);
      cls.addClass("voted");
      cls.children('a').text('Remove From Favorites');
      cls.children('a').attr('title','Not doing it for you anymore?');

      $.get("/laughed/" + $(this).parent().attr("id"), function(ret) {
	      if( ret == '-1' ){
		      cls.removeClass("voted"); // make special error class
		      cls.addClass("vote_error");
		      cls.children('a').text('Can\'t add track, too many favorited from this album');
		      cls.children('a').attr('title','');
	      }
      });
    } else {
	$(this).removeClass("voted");
        $(this).children('a').text('Add To Favorites');
        $(this).children('a').attr('title','Vote this track up!');
	$.get("/unlaughed/" + $(this).parent().attr("id"), function(ret){
		// no special processing needed
	});
    }
    });

  });

function playerReady(obj) {
  ply = document.getElementById("fmp3");
  ply.addModelListener("STATE", "statusTrack");
  if($("#fmp3").length > 0)
  {
    if((num = $(".entry").length) > 0)
    {
      loadTrack(0);
      if(typeof(autoplay) != "undefined" && autoplay)
        playTrack();
    }
    // $("#content").sortable();
  }
}

function loadTrack(ct)
{
  pos = ct;
  trackcd = $(".entry:eq(" + pos + ")").attr("id");
  $(".entry").removeClass("entry-active");
  $("#" + trackcd).addClass("entry-active");
  $("#playtitle").html($("#" + trackcd + " h2").text());
  ply.sendEvent("STOP");
  ply.sendEvent("LOAD", {file: "http://thelaughbutton.com/play/" + trackcd, type: "sound"});
  return trackcd;
}

function playTrack()
{
  ply.sendEvent("PLAY", true);
}

function jumpTrack(nm)
{
  var cnt = 0;
  $(".entry").each(function() {
    if(this.id == nm)
    {
      loadTrack(cnt);
      playTrack();
    }
    cnt++;
  });
}

function nextTrack()
{
  trackcd = loadTrack((pos + 1) % num);
  playTrack();
}

function statusTrack(obj)
{
  $("#playstatus").html("PLAYING");
  if(obj["newstate"] == "COMPLETED")
    nextTrack();
  else if(obj["newstate"] == "PAUSED" || obj["newstate"] == "PLAYING")
    $("#playstatus").html(obj["newstate"]);
}

function checkSession()
{
	if( readCookie('session') ){ return true; }
	return false;
}

function showLogin(track)
{
	$("#login_box").show();

	if(track){
		$("#track").val(track);
	}
}

function hideLogin()
{
	$("#login_box").hide();
	$("#login_form :input").val('');
}

function sendLogin()
{
	$("#login_form").hide();

	$("#login_password").val( $.base64Encode( $("#login_password").val() ) );

	var qstring = $("#login_form").serialize();

	// jquery bug workaround
	$('#executor').load('/login',qstring);
}

function sendLogout()
{
	$('#executor').load('/logout');
}

function showRegister(track)
{
	$("#login_box").hide();
	$("#register_box").show();

	if(!track){
		track = 0;
	}
	var qstring = 'laugh=' + track;

	$("#register_box_inner").load('/register', qstring);
}

function hideRegister()
{
	$("#register_box").hide();
	$("#login_box").hide();
	$("#register_form :input").val('');
}

function sendRegister()
{
	if( $("#register_password").val() != $("#register_password2").val() ){
		$("#dialog_error_password_mismatch").show();
		return false;
	}

	$("#register_form").hide();

	$("#register_password").val( $.base64Encode( $("#register_password").val() ) );
	$("#register_password2").val( $.base64Encode( $("#register_password2").val() ) );

	var qstring = $("#register_form").serialize();
	$("#register_box_inner").load('/register',qstring);
}

