function UserMenu(){
  var menuHeight = $('#usermenu ul').height();
  $('#usermenu ul').css({height:'0px', opacity:0});
  $('#usermenu').hover(function(){
    $('#usermenu ul').stop();
    $('#usermenu ul').animate({height: menuHeight +'px', opacity:1}, 200);
  }, function(){
    $('#usermenu ul').stop();
    $('#usermenu ul').animate({height:'0px', opacity:0}, 600);
  });
}

function ShoutBox(){
  var $shout = $('#shout');
  if ($shout){
    $shout.focus(function(){
      var postshout = function(e){
        var tVal = $shout.val();
        if (e.keyCode == 13){
          $shout.unbind('keypress');
          $.post(HTTPPATH + 'ajax/save_shout.php', {message: tVal}, function(data){
            LoadShouts();
            $shout.val('');
            $shout.bind('keypress', postshout);
          });
          return false;
        }else{
          if (tVal.length > 255 && e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40){
            return false;
          }
        }
      }

      $shout.bind('keypress', postshout);
    });
    $shout.blur(function(){
      $shout.unbind('keypress');
    });

    $('.deleteshout').live('click', function(){
      $.post(HTTPPATH + 'ajax/delete_shout.php', {id: $(this).attr('id')}, function(data){
        LoadShouts();
      });
      return false;
    });

  }
}

function LoadShouts(){
  var $shout = $("#shoutbox ul");
  if ($shout){
    if (typeof shoutbox !== 'undefined'){
      clearTimeout(shoutbox);
    }
    if (json === true){
      $.get(HTTPPATH + 'ajax/load_shouts.php', {json:'true'}, function(data){
        var tHtml='';
        var temp;
        for (i=0; i<data.length; i++){
          data[i].text = $().emoticon(data[i].text);

          temp = '<li>'
               + '<p class="name"><a href="' + HTTPPATH + 'users/' + data[i].username + '">' + data[i].username + '</a></p>'
               + '<p class="time">' + data[i].time + '</p>'
               + '<p class="message">' + data[i].text + '</p>'
               + '</li>';
          tHtml += temp;
        }
        $shout.html(tHtml);
      }, 'json');
    }else{
      $shout.load(HTTPPATH + 'ajax/load_shouts.php');
    }
    var shoutbox = setTimeout('LoadShouts()', 1000 * SHOUTBOX_REFRESH);
  }
}

function NewsTicker(old, id){
  if (typeof news !== 'undefined'){
    clearTimeout(news);
  }
  var detail = $('#newsdetail').children();

  $(detail[id]).css({opacity:0, display:'block'});
  $(detail[id]).addClass('active');
  $(detail[old]).removeClass('active');

  var changeclasses = function(){
    var list = $('#newslist').children();
    $(list[old]).find('a').removeClass('active');
    $(list[id]).find('a').addClass('active');
  };
  setTimeout(changeclasses, 1000);

  $(detail[old]).animate({opacity:0}, 3000);
  $(detail[id]).animate({opacity:1}, 3000, function(){
    var newid = (id != 4) ? id + 1 : 0;
    news = setTimeout('NewsTicker('+id+', '+newid+')', 5000);
  });
}

function IE6(){
  var t;

  $("body").append('<div id="ie6">Why does the site look shit?</div>');
  $("#ie6").css("opacity", 0);
  $("#ie6").click(function(){
    window.open('http://www.google.co.uk/chrome');
  });

  $("#ie6").hover(function(){
    t = $(this).html();
    $(this).html("Because you're using a 10 year old web browser. Click here to stop being retarded.");
  },
  function(){
    $(this).html(t);
  });

  setTimeout('$("#ie6").animate({height:30, opacity:1}, 500)', 1000);
};

function NewChallenge(){
  $(document).ready(function(){

    $("#challenge input, #challenge select").not("#Squad, #Open_Challenge").parent().hide();

    if ($("#Squad").is(":hidden")){
      $("#Challenge_Type").parent().show();
      $("#Challenge_Type").load(HTTPPATH + 'ajax/load_challengetypes.php', $("#challenge").serialize());
    }

    if ($("#Open_Challenge").attr('checked') === true){
      $("#Squad_to_Challenge").parent().hide('fast');
      $("p.challenge_to").hide('fast');
      $("#Open_Challenge").addClass('checked');
    }

    $("#Challenge_Type").change(function(){
      $("#Squad_to_Challenge").load(HTTPPATH + 'ajax/load_squadtochallenge.php', $(this).parents('form').serialize());
      $("#Map").load(HTTPPATH + 'ajax/load_challengemaps.php', $(this).parents('form').serialize());
      $("#Squad_to_Challenge").parent().show();
    });

    $("#Open_Challenge").change(function(){
      if ($(this).hasClass('checked')){
        $("#Squad_to_Challenge").parent().show('fast');
        $("p.challenge_to").show('fast');
        $(this).removeClass('checked');
      }else{
        $("#Squad_to_Challenge").parent().hide('fast');
        $("p.challenge_to").hide('fast');
        $(this).addClass('checked');
      }
      $("#Squad_to_Challenge").load(HTTPPATH + 'ajax/load_squadtochallenge.php', $(this).parents('form').serialize());
      //$("#Map").load(HTTPPATH + 'ajax/load_challengemaps.php', $(this).parents('form').serialize());
    });

    $("#Squad, #Open_Challenge").change(function(){
      $("#Challenge_Type").parent().show();
      $("#Map").load(HTTPPATH + 'ajax/load_challengemaps.php', $(this).parents('form').serialize());
      $("#Challenge_Type").load(HTTPPATH + 'ajax/load_challengetypes.php', $(this).parents('form').serialize());
      $.post(HTTPPATH + 'ajax/load_server.php', $(this).parents('form').serialize(), function(data){
        $("#Server_Address").val(data.server);
        $("#Server_Password").val(data.password);
      }, 'json');
      $.post(HTTPPATH + 'ajax/load_challengenotes.php', $(this).parents('form').serialize(), function(data){
        $("#Notes").val(data.notes);
      }, 'json');
    });

    $("#Squad_to_Challenge").change(function(){
      $("#challenge input, #challenge select").parent().show();
      $.post(HTTPPATH + 'ajax/load_server.php', $(this).parents('form').serialize(), function(data){
        $("#Server_Address").val(data.server);
        $("#Server_Password").val(data.password);
      }, 'json');

      $.post(HTTPPATH + 'ajax/load_challengenotes.php', $(this).parents('form').serialize(), function(data){
        $("#Notes").val(data.notes);
      }, 'json');
    });

  });
};

$(document).ready(function(){
  if ($("#notice")){
    $("#notice").css({'height':0, 'opacity':0}).animate({'height':'60px', 'opacity':1}, 1000);
  }
});
