//-------------------------------------
$(document).ready (function (){

  $("#SectTable").find ("a").click (function () {
    var SectLnk = $(this).attr ('id');
    var Ix = SectLnk.substring (1, 99);
    //alert (Ix);
    var Last = $("#Sections > div:eq(3)").attr ("id");
    if (Last != undefined)
    {
      $("#" + Last).remove ();
    }
    Show_Section (Ix);
  });

  $("#form1").submit (function () {
    var Title = $("#title").val ();
//alert (Title);
    if (Title == "")
    {
      Error ("You must input a link title");
      return false;
    }

    if (Title.length < 2)
    {
      Error ("Title is too long, maximum is " + Max_Text_Length);
      return false;
    }

    var Link = $("#link").val ();
    if (Link == "" || Link == "http://")
    {
      Error ("You must input a valid link (http://...");
      return false;
    }

    var Sect = $("#section").val ();
    //alert (Sect);

    var NewSect = $("#new_sect").val ();
    //alert (NewSect);

    if (NewSect == "")
    {
      if (Sect == 0)
      {
        Error ("You must select a section or create a new one");
        return false;
      }
    }
    else
    {
      if (NewSect == "")
      {
        Error ("You must select a section or create a new one");
        return false;
      }
    }

    Error ("");

    $.post (ScriptUrl,
      {
        act: "add_link_ajax",
        title: Title,
        link: Link,
        section: Sect,
        new_sect: NewSect
      },
      Cmd_Callback); // , "xml");

    return false;
  });
});

//-------------------------------------
function Cmd_Callback (Xml)
{
  var Msg = $("msg", Xml).text ();

  if (Msg == "")
  {
    var Sect = $("sect", Xml).text ();
    alert (Xml);
    if ($("new", Xml).text () == "1")
    {
      document.location.href = ScriptUrl + "?first=" + Sect;
    }
    else
    {
      var Last = $("#Sections > div:eq(3)").attr ("id");
      if (Last != undefined)
      {
        $("#" + Last).remove ();
      }
      Show_Section (Sect);
    }
  }
  else
  {
    Error (Msg);
  }
}

//-------------------------------------
function Error (Text)
{
  $("#Error").html (Text);
}

//-------------------------------------
function Show_Section (SectId)
{
  var Id = "Sect-" + SectId;
  if ($("#" + Id).length == 0)
  {
    $("#Sections").html (
        "<div id='" + Id + "' class='Droppable'></div>\n" + 
        $("#Sections").html ());
  }

  $("#" + Id).load (ScriptUrl + "?act=show_section&id=" + SectId, {},
    function ()
    {
      if ($(".ST").draggable)
      {
        $(".ST").draggable ({ stack: { group: '#set div', min: 1 }, opacity: 0.7, helper: 'clone' });
        $(".Lnk").draggable ({ stack: { group: '#set div', min: 1 }, opacity: 0.7, helper: 'clone' });
        $(".Droppable").droppable ({
          drop: function(event, ui) {
            var To = $(this).attr ('id').substring (5, 99);
            if (ui.draggable.hasClass ("Lnk"))
            {
              var Link = ui.draggable.find ("img").attr ('id');
              var From = ui.draggable.attr ('id');
              //alert (Link + " From: " + Sect + " => " + To); return;
              Move_Link (Link, From, To);
            }
            else
            {
              var From = ui.draggable.attr ('id');
              //alert (From + " => " + To); return;
              Move_Links (From, To);
            }
          }
        });
      }
    });
}

//-------------------------------------
