$(document).ready(function(){

    /* Portal Menu */
    $("ul.topnav li>a").hover(function() {
    		$(this).parent().find("ul.subnav").stop(true, true).slideDown('normal').show();
    		$(this).parent().hover(function() {}, function() {
    			$(this).parent().find("ul.subnav").stop(true, true).slideUp('fast');
    		});
    });
  
    $(".actions").css({'visibility' : 'hidden'});
    
    $(".records td").live("mouseover", function() {
      $(this).parent().children().css({'background-color' : '#fdffc3'});
      $(this).parent().children().children(".actions").css({'visibility':'visible'});
    });
        
    $(".records td").live("mouseout",function() {
      $(this).parent().children().css({'background-color' : '#f8f8f8'});
      $(this).parent().children().children(".actions").css({'visibility':'hidden'});
    });
    
    $('<div id="dialog">')
       .appendTo(document.body)
       .hide();
       
    $(".tooltip-box").each(function()
          {
             $(this).qtip(
             {
                content: {
                   // Set the text to an image HTML string with the correct src URL to the loading image you want to use
                   text: '<img class="throbber" src="/images/icons/etc/throbber.gif" alt="Loading..." />',
                   url: $(this).attr('href'), // Use the rel attribute of each element for the url to load
                   title: {
                      text: $(this).attr('name'), // Give the tooltip a title using each elements text
                      button: 'Close' // Show a close link in the title
                   }
                },
                position: {
                        target: $(document.body), // Position it via the document body...
                        corner: 'center' // ...at the center of the viewport
                     },
                show: {
                        when: 'click', // Show it on click
                        solo: true // And hide all other tooltips
                    },
                hide: false,
                style: {
                   tip: false, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
                   border: {
                      width: 0,
                      radius: 4
                   },
                   name: 'light', // Use the default light style
                   width: 460 // Set the tooltip width
                },
                      api: {
                         beforeShow: function()
                         {
                            // Fade in the modal "blanket" using the defined show speed
                            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
                         },
                         beforeHide: function()
                         {
                            // Fade out the modal "blanket" using the defined hide speed
                            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
                         }
                      }
                   });

                   // Create the modal backdrop on document load so all modal tooltips can use it
                   
                   $('<div id="qtip-blanket">')
                      .css({
                         position: 'absolute',
                         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
                         left: 0,
                         top: 0,
                         height: $(document).height(), // Span the full document height...
                         width: '100%', // ...and full width

                         opacity: 0.5, // Make it slightly transparent
                         backgroundColor: 'white',
                         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
                      })
                      .appendTo(document.body) // Append to the document body
                      .hide(); // Hide it initially
                });

    function popError(msg, a) {
        $('#dialog').html(msg);
        $('#dialog').dialog({
               title: 'Error' + " " + a.attr('name') + " " + a.attr('id'),
               buttons: {
                 "OK": function() {
                   $(this).dialog('destroy');
                 }
            }
        });
        return false;
    }
    
    function deleteRecord(obj) {
        $.getJSON(obj.attr('href'), function(json) {
        if (json.status == 'ok') {
          obj.parents("tr").children().css({'background-color' : 'pink'});
          obj.parents("tr").fadeOut();
        }
        else {
          popError("Failed to delete " + obj.attr('name') + " " + obj.attr('id') +
                "<br /><br/>" + "Server says : " + json.msg, obj);
        }
      });
    }
    
    function jumpPage(obj) {
      $.get(obj.attr('href'), function(data){
        $("#records_list").html(data);
      });
    }
  
    function startEndRecord(obj) {
      id = obj.attr("id");
      $.get(obj.attr('href'), function(data) {
        obj.parents("tr").html(data); 
        ("#maintenance-" + id).children().effect("highlight", {'color':'pink'}, 3000); 
      });
    }
    
    $(".paginate").live("click", function() {
      jumpPage($(this));
    });
	
    $(".start-end").live("click", function() {
      startEndRecord($(this));
    });

    $(".error-dialog").live("click", function() {
        $(this).css({'background-color' : 'pink'});
        $(this).fadeOut();
    });
    
    $('a.delete').live("click", function() { 
      var a = $(this); 
      $('#dialog').html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>');
      $('#dialog').dialog({
            title: 'Delete' + " " + a.attr('name') + " " + a.attr('id'),
            modal: true,
            buttons: {
              "Yes": function() {
                $(this).dialog('destroy');    
                deleteRecord(a, $(this));
              },
              "No": function() {
                  $(this).dialog('destroy');                      
              }
            }
      });
      return false;
    });

});

/* Widget & Form Helpers */

function widget_tooltip(string){
    return '<span class="tooltip">' + string + '</span>';
}

function widget_error_message(string){
    return '<p class="error_message">' + string  + '</p>';
}

