$(window).load(function() {
  $(".forumtable td img").each(function() {
    if($(this).width() > 400) $(this).css("width", "400px");
  });
});

$(document).ready(function() {
  $("#links .title div, .inventoryhidden, .tip div, #dailies div, .kinlair img.main").hide();

  $("#searchResults").tablesorter({headers:
    {
      0:{sorter:false},
      1:{sorter:'text'},
      2:{sorter:false},
      3:{sorter:false},
      4:{sorter:'digit'},
      5:{sorter:'digit'},
      6:{sorter:'digit'},
      7:{sorter:'digit'},
      8:{sorter:'digit'},
      9:{sorter:'digit'}
    }
  });
  
  $(".sortable").sortable({items: ".sortableItem"});

  $(".selectAll").change(function() {
    $("input[type='checkbox']").attr('checked', $('.selectAll').is(':checked'));
  });

  $(".tip, #dailies").hover(function() {
    $(this).children().show();
  }, function() {
    $(this).children().hide();
  });

  $(".reportMini").click(function() {
    $(this).children().show();
  });
  
  $(".alertHide").click(function() {
    $(".reportMini div").hide();
    return false;
  });

  $("#deletePosts").click(function() {
    if(confirm("Are you sure you want to do this?")) {
      var allVals = [];
       $("input[name=pid[]]:checked").each(function() {
         allVals.push($(this).val());
       });


      $("#moderatePosts").val(allVals);
    } else return false;
  });
  $(".oneClick").click(function() {
    $(this).attr("disabled", "disabled");
  });

  $(".marketLink").hover(function(event) {
    var idLink = $(this).attr("id");
    var ltext = "";
    if(idLink == "eldara") {
      ltext = "Eldara's Edibles - Delicious fruits a few bloodstones away!";
    } else if(idLink == "ketlan") {
      ltext = "Ketlan's Wares - Special & rare artifacts for collectors";
    } else if(idLink == "fauna") {
      ltext = "Fauna's Pets - Currently all out of stock!";
    } else if(idLink == "ekirir") {
      ltext = "Healer's Hometree - Ekirir's Dwelling";
    }

    $("#marketMini span").html(ltext);
    $("#marketMini").show();
    $("#marketTemp").hide();

  }, function() {
    $("#marketMini").hide();
    $("#marketTemp").show();
  });


  $("#header .link").hover(function() {
    $(this).addClass("hover");
  }, function() {
    $(this).removeClass("hover");
  });

  $(".wtable").each(function() {
    $(this).attr("cellspacing", "1");
    $(this).attr("cellpadding", "4");
  });

  $(".kinlair").hover(function() {
    $(this).children("img.main").show();
  }, function() {
    $(this).children("img.main").hide();
  });


  $("#rpsgame").submit(function() {
    var ante = $("#ante").val();
    var rps = $("#rps").val();
    $("#rpsbutton").attr("disabled", "disabled");
    $.post("actions/games_rps.php", {ante: ante, rps: rps}, function(data) {
      $("#rpsbutton").removeAttr("disabled");
      $("#result").html(data);
    });

    return false;
  });

  $('.colorpick').each(function() {
    hexVal = $(this).val();
    while(hexVal.length < 6) hexVal = "0" + hexVal;
  });
  $('.colorpick').ColorPicker({
  	onSubmit: function(hsb, hex, rgb, el) {
  		$(el).val(hex);
  		$(el).ColorPickerHide();
  	},
  	onBeforeShow: function () {
  		$(this).ColorPickerSetColor(this.value);
  	}
  })
  .bind('keyup', function(){
  	$(this).ColorPickerSetColor(this.value);
  });

  $("#inventory td").click(function() {
    var elemID = $(this).attr("id");
    $.post("actions/inventory_actions.php", { id: elemID }, function(data) {
      $("#"+elemID).children(".inventoryhidden").html(data).toggle("slow", function() {
        $("#"+elemID+"close").click(function() {
          $("#"+elemID).children(".inventoryhidden").hide("slow");
          return false;
        });
      });
    });
  });

  $(".confirm").click(function() {
    if(!confirm("Are you sure you want to proceed?")) return false;
  });


});
