
(function($) {

  $.fn.textExcerpt = function(options) {
    var settings = $.extend({}, $.fn.textExcerpt.defaults, options);

    return this.each(function() {

      var $container = $(this);
      var fullText = $container.text();
      var fullTextLength = fullText.length;
      var excerpt = fullText.substring(0, settings.excerptLength);
      var showMoreText = fullText.substring(settings.excerptLength);

      if (fullTextLength > (settings.excerptLength + settings.fudge)) {

        $container.html(excerpt + ('<span class="tl_show_more_text">' + showMoreText + '</span>'));

        $(' <a class="tl_show_more">' + settings.revealLabel + '</a>')
          .appendTo($container)
          .toggle(function() {
            $container.find('span.tl_show_more_text').slideDown();
            $(this).text(settings.hideLabel);
          }, function() {
            $container.find('span.tl_show_more_text').slideUp();
            $(this).text(settings.revealLabel);
          });

        $container.find('span.tl_show_more_text').hide();

      }

    });

  };

  $.fn.textExcerpt.defaults = {
    excerptLength: 200,
    fudge: 10,
    revealLabel: "Show More",
    hideLabel: "Show Less"
  };

})( jQuery );

// This file contains javascript that is global to the entire Garden application
jQuery(document).ready(function($) {
   
   $(".hideText").textExcerpt({
        revealLabel: '[...]',
        hideLabel: '[-]'
   });
   
   $("a[rel^='prettyPhoto']").fancybox({
        'autoScale': false
   });
   
    $("a[rel^='fbox']").fancybox({
			'autoScale'		: false,
            'showNavArrows' : false
   });
   
   // Set the ClientHour if there is an input looking for it.
   $('input:hidden[name$=ClientHour]').livequery(function() {
      var d = new Date();
      $(this).val(d.getHours());
   });

   // Ajax/Save the ClientHour if it is different from the value in the db.
   $('input:hidden[id$=SetClientHour]').livequery(function() {
      var d = new Date();
      if (d.getHours() != $(this).val()) {
         $.post(
            gdn.url('utility/setclienthour/' + d.getHours() + '/' + gdn.definition('TransientKey')),
            'DeliveryType=BOOL'
         );
      }
   });

   // Main Menu dropdowns
	
   if ($.fn.menu)
      $('#Menu').menu({
         showDelay: 0,
         hideDelay: 0
      });
	
	
   // Go to notifications if clicking on a user's notification count
   $('li.UserNotifications a span').click(function() {
      document.location = gdn.url('profile/notifications');
      return false;
   });
   
   // This turns any anchor with the "Popup" class into an in-page pop-up (the
   // view of the requested in-garden link will be displayed in a popup on the
   // current screen).
   if ($.fn.popup)
      $('a.Popup').popup();

   $(".PopupWindow").live('click', function() {
      var $this = $(this);

      var width = $this.attr('popupWidth');
      var height = $this.attr('popupHeight');
      var left = (screen.width - width) / 2;
      var right = (screen.height - height) / 2;

      var id = $this.attr('id');
      var href = $this.attr('href');
      var args = $this.attr('popupArgs');
      if (args)
         href += '&' + args;

      var win = window.open(href, 'Window_' + id, "left="+left+",height="+height+",width="+width+",height="+height+",status=0,scrollbars=0");

      return false;
   });
   
   // This turns any anchor with the "Popdown" class into an in-page pop-up, but
   // it does not hijack forms in the popup.
   if ($.fn.popup)
      $('a.Popdown').popup({hijackForms: false});
   
   // This turns SignInPopup anchors into in-page popups
   if ($.fn.popup)
      $('a.SignInPopup').popup({containerCssClass:'SignInPopup'});

   // Make sure that message dismissalls are ajax'd
   $('a.Dismiss').live('click', function() {
      var anchor = this;
      var container = $(anchor).parent();
      var transientKey = gdn.definition('TransientKey');
      var data = 'DeliveryType=BOOL&TransientKey=' + transientKey;
      $.post($(anchor).attr('href'), data, function(response) {
         if (response == 'TRUE')
            $(container).slideUp('fast',function() {
               $(this).remove();
            });
      });
      return false;
   });

   // This turns any form into a "post-in-place" form so it is ajaxed to save
   // without a refresh. The form must be within an element with the "AjaxForm"
   // class.
   if ($.fn.handleAjaxForm)
      $('.AjaxForm').handleAjaxForm();
   
   // If a message group is clicked, hide it.
   $('div.Messages').live('click', function() {
      $(this).fadeOut('fast', function() {
         $(this).remove();
      });
   });
   
   // If an information message appears on the screen, hide it after a few moments.
   $('div.Information').livequery(function() {
      setTimeout(function(){
         $('div.Information').fadeOut('fast', function() {
            $(this).remove();
         });
      }, 3000);
   });
	
	// Show hoverhelp on hover
	$('.HoverHelp').hover(
		function() {
			$(this).find('.Help').show();
		},
		function() {
			$(this).find('.Help').hide();
		}
	);


   // Make tables sortable if the tableDnD plugin is present.
   if ($.tableDnD)
      $("table.Sortable").tableDnD({onDrop: function(table, row) {
         var tableId = $($.tableDnD.currentTable).attr('id');
         // Add in the transient key for postback authentication
         var transientKey = gdn.definition('TransientKey');
         var data = $.tableDnD.serialize() + '&DeliveryType=BOOL&TableID=' + tableId + '&TransientKey=' + transientKey;
         var webRoot = gdn.definition('WebRoot', '');
         $.post(gdn.combinePaths(webRoot, 'index.php?p=/dashboard/utility/sort/'), data, function(response) {
            if (response == 'TRUE')
               $('#'+tableId+' tbody tr td').effect("highlight", {}, 1000);

         });
      }});

   // Format email addresses
   $('span.Email.EmailUnformatted').livequery(function() {
      var el = $(this);
      el.removeClass('EmailUnformatted');
      var email = el.html().replace(/<em>dot<\/em>/ig, '.').replace(/<strong>at<\/strong>/ig, '@');
      el.html('<a href="mailto:' + email + '">' + email + '</a>');
   });

   // Make sure that the commentbox & aboutbox do not allow more than 1000 characters
   $.fn.setMaxChars = function(iMaxChars) {
      $(this).live('keyup', function() {
         var txt = $(this).val();
         if (txt.length > iMaxChars)
            $(this).val(txt.substr(0, iMaxChars));
      });
   }

   // Fill the search input with "search" if empty and blurred
   var searchText = 'Search';
   $('#Search input.InputBox').val(searchText);
   $('#Search input.InputBox').blur(function() {
      var searchText = 'Search';
      if (typeof $(this).val() == 'undefined' || $(this).val() == '')
         $(this).val(searchText);
   });
   $('#Search input.InputBox').focus(function() {
       var searchText = 'Search';
       if ($(this).val() == searchText)
         $(this).val('');
   });
   
   // Add a spinner onclick of buttons with this class
   $('input.SpinOnClick').live('click', function() {
      $(this).before('<span class="AfterButtonLoading">&nbsp;</span>').removeClass('SpinOnClick');
   });
   
   // Confirmation for item removals
   $('a.RemoveItem').click(function() {
      if (!confirm('Are you sure you would like to remove this item?')) {
         return false;
      }
   });
   
});
