$(document).ready(function(){

  $(".futureshop_ajax").click(function(e){
    $( "#futureshop_ajax_select_career_path" ).dialog( "open" );
    e.preventDefault();
  });

  $(".futureshop_login_dialog").click(function(e){
    $( "#futureshop_login_dialog" ).dialog( "open" );
    e.preventDefault();
  });

  $(".futureshop_ajax_add_category").click(function(e){
    $.ajax({
      url: this.href,
      success: function(data) {
        $("#futureshop_link").effect("pulsate", {}, "fast");
      }
    });
    e.preventDefault();
  });

  $(".delete a").click(function(e){
    var url = this.href;
    var elementToHide = $(this).parent().parent(); // tr < td < a
    $( "#futureshop_confirm_delete_dialog" ).dialog( {ajax_remote_url: url, hideElement: elementToHide, title: 'Er du sikker på at du vil slette?'} );
    $( "#futureshop_confirm_delete_dialog" ).dialog( "open" );
    e.preventDefault();
  });

  $( "#futureshop_confirm_delete_dialog" ).dialog({
    autoOpen: false,
    resizable: false,
    height:180,
    modal: true,
    buttons: {
      "Ja": function() {
        $.ajax({
          url: $(this).dialog( "option", "ajax_remote_url" ),
          success: function(data) {
            var hideElement = $("#futureshop_confirm_delete_dialog").dialog( "option", "hideElement" );
            hideElement.fadeOut();
          }  
        });
        $( this ).dialog( "close" );
      },
      "Nej": function() {
        $( this ).dialog( "close" );
      }
    }
  });

  $( "#futureshop_ajax_select_career_path" ).dialog({
            autoOpen: false,
            resizable: false,
			height: 240,
            modal: true,
            buttons: {
              "Gem": function() {
                $.ajax({
                  url: '/futureshop/ajax/add/',
                  data: $("#futureshop_ajax_select_career_path form").serialize(),
                  success: function(data) {
                    $("#futureshop_link").effect("pulsate", {}, "fast");
                  }
                });
                $( this ).dialog( "close" );
              },
              "Annuller": function() {
                $( this ).dialog( "close" );
              }
            }
		});

  $(".rename a").click(function(e){
      var url  = this.href;
      var elementToRename = $(this).parent().parent().find("td.name a");
      var name = elementToRename.attr('title');
      $("#new_name").val(name);
      $("#futureshop_rename_dialog").dialog( {ajax_remote_url: url, renameElement: elementToRename, title: 'Omdøb karrierevejs raport'} );
      $("#futureshop_rename_dialog").dialog('open');
      e.preventDefault();
  });

  $("#futureshop_login_dialog").dialog({
            autoOpen: false,
            resizable: false,
			height: 180,
            modal: true,
            buttons: {
              "Ok": function() {
                $( this ).dialog( "close" );
              }
            }
  });

  $("#futureshop_rename_dialog").dialog({
            autoOpen: false,
            resizable: false,
			height: 180,
            modal: true,
            buttons: {
              "Gem": function() {
                var url = $(this).dialog( "option", "ajax_remote_url" );
                var elementToRename = $(this).dialog( "option", "renameElement" );
                $.ajax({
                  url: url,
                  data: $("#futureshop_rename_dialog form").serialize(),
                  success: function(data) {
                    elementToRename.html(data);
                  }
                });
                $( this ).dialog( "close" );
              },
              "Annuller": function() {
                $( this ).dialog( "close" );
              }
            }
		});


  $("#futureshop_ajax_select_career_path").change(function(){
    if ( $("#futureshop_ajax_select_career_path select").length )
    {
      if ( $("#futureshop_ajax_select_career_path option.new_name_selector:selected").length )
      {
        $("#new_name").removeClass('hidden');
      }
      else
      {
        $("#new_name").addClass('hidden');
      }
    }
  });
});

