/*
 * alcaen Wortspiel
 * 2008 by alcaen media
 * http://www.alcaen.com/
 */

var curr_id = 0;
var xmas = new Date(2008, 11, 1);
var s_recv; var s_send;
var b_recv = true; var b_send = true;
var c_knub = 0; var d_knub;

soundManager.url = "misc";
soundManager.debugMode = false;

soundManager.onload = function () {
  s_send = soundManager.createSound({ id: "send", url: "misc/send.mp3" });
  s_recv = soundManager.createSound({ id: "recv", url: "misc/recv.mp3" });
};

function addRow(id, prepend) {
  output = "<div id=\"row" + id + "\"></div>";
  
  if(prepend) {
    $("#form").after(output);
  } else {
    $("#content").append(output);
  }
  
  $("#row" + id).hide();
  $("#row" + id).addClass("block");
  $("#row" + id).addClass((id % 2 == 0) ? "even" : "odd");
}

function addRowData(data) {
  output = "";
 
  output += "<div class=\"post\">" + data.oldword + " <b>-&gt;</b> " + data.word + "</div>";
  output += "<div class=\"user_lh\">"
  output += "<div class=\"user\">" + data.user + " <div class=\"rdk\">" + data.knubbel + "</div></div><br />";
  for(i = 1; i <= data.stars; i++) {
    output += "<img src=\"png/s"+i+".png\" alt=\"\" width=\"17\" height=\"17\" />";
  }
  output += "<br />";
  output += "<div class=\"timestamp\">" + data.timestamp + "</div>";
  output += "</div>";
  
  return output;
}

function fetchRows(limit, start_id, prepend) {
  for(i = 0; i < limit; i++) {
    j = start_id - i;
    addRow(j, prepend);
    
    $.getJSON("get_post.php", { id: j, t: new Date().getTime() }, function(data) {
      if(data.id == curr_id) {
        $("#last").html(data.word);
        $("#content").css("background", "none");
        
        $("#form").animate({ opacity: 1 }, "slow");
    
        $("#nword").removeAttr("disabled");
        $("#nsubmit").removeAttr("disabled");
        $("#nword").focus();
        
        b_send = true;
      }
      
      $("#row" + data.id).html(addRowData(data)).fadeIn().animate({ height: "44px" }, "fast");
      
      $("#row" + data.id).children("div.post").bind("mouseover", function() {
        
        $(this).css("height", "auto");
        
        if($(this).innerHeight() > 42) {
          $(this).parent().stop(true);
          $(this).parent().animate({ height: $(this).innerHeight() }, "fast");
        }
        
        if($(this).parent().attr("class") == "block even") {
          $(this).css("background-color", "white");
        } else {
          $(this).css("background-color", "#f9f9f9");
        }

      });
      
      $("#row" + data.id).children("div.post").bind("mouseout", function() {
        $(this).css("height", "42px");
        $(this).parent().stop(true);
        $(this).parent().animate({ height: "44px" }, "fast");
      });
      
    }, "json"); 
  }
}

function refresh() {
  b_send = false;
  $.getJSON("get_last.php", { t: new Date().getTime() }, function(data) {
    if(data.id > curr_id) {
      //limit = data.id - curr_id
      curr_id = data.id;
      
      fetchRows(1, curr_id, true);
      
      if($("#sound").attr("checked") && b_recv && s_recv) s_recv.play();
      b_recv = true;
      
      stats();
    } else {
      b_send = true;
    }
    
    window.setTimeout("refresh()", 60000);
  });
}

function uon_potm() {
  $("#uon").slideUp(function() {
    $(this).load("get_uon.php", { t: new Date().getTime() }, function() {
      $(this).slideDown(function() {
        $("#potm").slideUp(function() {
          $(this).load("get_potm.php", { t: new Date().getTime() }, function() {
            $(this).slideDown();
          });
        });
      });
    });
  });
}

function stats() {
  $("#stats").slideUp(function() {
    $(this).load("stats.php", { t: new Date().getTime() }, function() {
      $(this).slideDown();
    });
  });
}

function rdk() {
  $("#rdk").slideUp(function() {
    $(this).load("get_rdk.php", { t: new Date().getTime() }, function() {
      $(this).slideDown();
    });
  });
}

function topl(url) {
  $("#topl").slideUp(function() {
    $(this).load(url, { t: new Date().getTime() }, function() {
      $(this).slideDown();
    });
  });
}

function countdown() {
  now = new Date();
  
  cdtime = xmas - now;
  cdtime = Math.floor(cdtime / 1000);
  days = Math.floor(cdtime / 86400);
  
  cdtime %= 86400;
  
  hours = Math.floor(cdtime / 3600);
  
  if(hours < 10) hours = "0" + hours;
  
  cdtime %= 3600;
  minutes = Math.floor(cdtime / 60);
  
  if(minutes < 10) minutes = "0" + minutes;
  
  cdtime %= 60;
  seconds = cdtime;
  
  if(seconds < 10) seconds = "0" + seconds;
  
  $("#countdown").html(days + " Tage und " + hours + ":" + minutes + ":" + seconds);
}

$(document).ready(function() {

  if($.browser.msie && parseInt($.browser.version) <= 6) {
    alert("Dieser Internet Explorer ist zu alt! Bitte verwende einen anderen Browser.");
  }

  /* $("#head").bind("click", function() {
    location.reload();
  }); */
  
  $("#toplist").draggable(); //{ handle: "img:first" });
  //$("#toplist").resizable({ handles: "s", minHeight: 320 });
  
  $("#opentop1").bind("click", function() {
    $("#toplist").fadeIn();
    topl("get_topl.php");
    return false;
  });
  
  $("#opentop2").bind("click", function() {
    $("#toplist").fadeIn();
    topl("get_rubl.php");
    return false;
  });
  
  $("#hmap area").bind("click", function() {
    $("#toplist").fadeOut();
    return false;
  });
  
  $("#form form").bind("submit", function() {
    
    if($.trim($("#nword").val()).length == 0 && b_send) {
      
      $("#nword").val("").css("position", "relative").focus();
      $("#nword").animate({ left: -10 },10).animate({ left: 0 },50).animate({ left: 10 },10).animate({ left: 0 }, 50);
      $("#nword").animate({ left: -10 },10).animate({ left: 0 },50).animate({ left: 10 },10).animate({ left: 0 }, 50);
    
    } else {
    
      b_recv = false;
      if($("#sound").attr("checked") && s_send) s_send.play();
    
      $("#form").animate({ opacity: 0.25 }, "slow");
      
      $("#nword").attr("disabled", "disabled");
      $("#nsubmit").attr("disabled", "disabled");
      
      user.word = $.trim($("#nword").val());
      user.last = curr_id;
      
      $.post("put_post.php", user);
      
      $("#nword").val("").blur();
      
    }
    
    return false;
  });
  
  $("#nlimit").bind("change", function() {
  
    expire = new Date();
    expire.setTime(expire.getTime() + 31536000000);
    document.cookie = "limit=" + $(this).val() + "; expires=" + expire.toUTCString();
  
    if($("#content > div").length > $(this).val()) {
    
      $("#content > div:lt(" + $(this).val() + ")").fadeIn();
      $("#content > div:gt(" + $(this).val() + ")").fadeOut();
      
    } else {
      fetchRows($(this).val() - $("#content > div").length + 1, $("#content > div:last").attr("id").replace("row", "") - 1, false);
    }
  });
  
  $("#sound").bind("change", function() {
    expire = new Date();
    expire.setTime(expire.getTime() + 31536000000);
    document.cookie = "sound=" + $(this).attr("checked") + "; expires=" + expire.toUTCString();
  });
  
  $("#knubbel").bind("mouseover", function() {
  
    c_knub++;
    
    if(c_knub == 1) d_knub = new Date();
    if(c_knub > 99) {
    
      d = new Date();
      t = Math.round((d.getTime() - d_knub.getTime())) / 1000;
      
      if(t < 100) {
        t = t.toString();
        user.knubbel = t;
        $.post("put_rdk.php", user);
        alert("Fertig! Du rubbelst in " + t + " Sekunden von 0 auf 100.");
      } else {
        alert("Du bist ja sehr langsam! Nochmal...");
      }
    
      rdk();
      c_knub = "0";
    }
    
    $(this).html(c_knub);
    $(this).css("left", Math.floor(Math.random() * 32) + 58);
    $(this).css("top", Math.floor(Math.random() * 16) - 8);

    $(this).animate({ left: 77, top: 0 }, 64);
    
  });
  
  cookies = document.cookie.split("; ");
  selected = false;
  
  for(i = 0; i < cookies.length; i++) {
    cookie = cookies[i].split("=");
    
    if("limit" == cookie[0]) {
      $("#nlimit option:contains(" + cookie[1] + "):first").attr("selected", "selected");
      selected = true;
    }
    
    if("sound" == cookie[0] && "false" == cookie[1]) $("#sound").removeAttr("checked");
  }
  
  if(!selected) $("#nlimit option:eq(2)").attr("selected", "selected");
  
  $.getJSON("get_last.php", { t: new Date().getTime() }, function(data) {
    curr_id = data.id;
    fetchRows($("#nlimit").val(), curr_id, false);
    window.setTimeout("refresh()", 30000);
  });
  
  //countdown();
  //window.setInterval("countdown()", 1000);
  
  uon_potm();
  window.setInterval("uon_potm()", 300000);
  
  stats();
  rdk();
  
  $("#nword").focus();
  
});
