if (jQuery){ (function($){ 

	var _components = {};

	// Define modules using this: $.modular( { module: function } );
	$.modular = function(what) { 
      $.extend(_components, what);
	    return this;
	};

	// Apply modules using this: $("selector").can_has().module();
	$.fn.can_has = function() {
   	$.extend(this, _components);
      return this;
	};

   /* MODULE DEFINITIONS */

   $.modular({
      subscription: function($element, event, callback){
         var $this = $(this);
         $element.bind(event, function(e){ callback.apply($this, [e]); });
         return $this;
      },
      /* BEGIN TOOLBAR SHARE LINKS */
      sharelink: function(shareMedium) {
         var $this = $(this),
             pageUrl = location.href,
             relativeUrl = pageUrl.replace(/http:\/\/[\w\.]+/, ""),
             linkUrl = $this.find("a").attr("href"),
             pageTitle = encodeURIComponent($.trim(document.title.replace("Hartz®: ", ""))),
             //pageTitle = $.trim(document.title.replace("Hartz®: ", "")),
             speciesTemp, speciesString, speciesNum,
             typeString, typeNum,
             popupurl;
         
         switch(shareMedium) {
            case "facebook" : popupurl = "http://www.facebook.com/sharer.php?u=$1&title=$2";
               break;
            case "digg" : popupurl = "http://digg.com/submit?phase=2&url=$1&title=$2&topic=";
               break;
            case "delicious" : popupurl = "http://del.icio.us/post?v=4;url=$1;title=$2";
               break;
         }
         
         // slash, animal name, optional s, slash or period or underscore
         speciesTemp = window.location.pathname.match(/\/(Dog|Cat|Small_Animal|Bird|Reptile|Fish)[s]?[\/|\.|\_]/);
         if (speciesTemp != null) {
            speciesString = speciesTemp[1];
            switch (speciesString) {
               case "Dog" : speciesNum = 1;
                  break;
               case "Cat" : speciesNum = 2;
                  break;
               case "Small_Animal" : speciesNum = 3;
                  break;
               case "Bird" : speciesNum = 4;
                  break;
               case "Reptile" : speciesNum = 5;
                  break;
               case "Fish" : speciesNum = 6;
                  break;
            }
         }
         
         // here we check for the recommend button and parse the params value for item type, else use zero
         typeNum = (function(){
            var $aParams = $("a[params]:eq(0)"),
                paramString;
            
            if ($aParams.length) paramString = $aParams.attr("params");
            else return 0;
            
            return eval("(" + paramString + ")").itemTypeEnumValue;
         
         })();
         
         $this.click(function(e) {
            
            window.open(
               popupurl.replace(/\$1/, /*/pageUrl/**/ "http://www.hartz.com/" + linkUrl)
                       .replace(/\$2/, pageTitle),
               "share"
            );
            
            jsonData = '{"id":"1", "method":"updateSharePage", "params":{"pageUrl":"$1", "pageTitle":"$2", "shareMedium":"$3", "species":"$4", "type":"$5"}}'
              .replace("$1", /*/relativeUrl/**/ linkUrl)
              .replace("$2", pageTitle)
              .replace("$3", shareMedium)
              .replace("$4", speciesNum)
              .replace("$5", typeNum); // yeah I went with quotes here *shrug*
            
            $.ajax({ url: "/Custom/Json/PageToolbar.ashx", data: jsonData, type: "POST", dataType: "json", contentType: "application/json; charset=utf-8" });
            
            e.preventDefault();
            e.stopPropagation();
         });
      },
      /* END TOOLBAR SHARE LINKS */
      
      /* BEGIN RECOMMEND LINKS */
      recommendlink: function(){
         var $this = $(this),
             $recommendBar = $this.parent().parent(),
             $recNum = $recommendBar.find("p.recNum"),
             params = $this.attr("params"),
             jsonData = '{"id":"1", "method":"updatePageRecommendation", "params": $1}'.replace("$1", params);
         
         $this.bind("click.recommend", function(e){
            if (!$recommendBar.hasClass("disabled")) {
               $.ajax({ url: "/Custom/Json/PageRecommendation.ashx", type: "POST", dataType: "json", data: jsonData, contentType: "application/json; charset=utf-8",
                  complete: function(){
                     var recText = $.trim($recNum.text()),
                         prevAmount = $recNum.text().match(/\d+/),
                         incrementedAmount;
                         
                     if (prevAmount && prevAmount.length) {
                        incrementedAmount = Number(prevAmount[0]) + 1;
                        if (incrementedAmount == 2) $recNum.text(recText = "2 recommendations");
                        else $recNum.text(recText.replace(/\d+/, incrementedAmount));
                     }
                     else {
                        $recNum.text("1 recommendation");
                        $this.find("img").removeClass("norecs");
                     }
                     $recNum.css("font-weight", "bold");
                  }
               });   // end $.ajax()
            }
            $recommendBar.addClass("disabled");
            e.preventDefault();
            e.stopPropagation();
         });   // end $this.bind("click.recommend")
      },
      /* END RECOMMEND LINKS */
      
      /* BEGIN DROPDOWN / SELECT MENUS */
      dropdown: function(params){
         var $this = $(this),
             $h4 = $this.children("h4"),
             $ul = $this.children("ul"),
             $li = $ul.children("li")
         
         $li.hover( 
            function(){ $(this).addClass("hover") },
            function(){ $(this).removeClass("hover") }
         ).click( function(){ 
            $h4.text($(this).text()); 
            $(this).children("a:eq(0)").click(); 
         });
         
         $().click(function(e){ 
            if (e.target != $h4[0]) $ul.hide();
            else $ul.toggle(); 
         } )
      },
      /* END DROPDOWN / SELECT MENUS */
      
      /* BEGIN SELECT MODULE */
      selectmodule: function(){
         var $this = $(this),
             $selectHead = $this.find(".selectHead"),
             $header = $selectHead.children("h4"),
             $headerAnchor = $header.children("a"),
             $selectBody = $this.find(".selectBody"),
             $li = $selectBody.find("ul > li");
         
         $li.hover( 
            function(){ $(this).addClass("hover") },
            function(){ $(this).removeClass("hover") }
         )
            .bind("click.selectmodule", function(){ $headerAnchor.text($(this).text()) });
            
         $().bind("click.selectmodule", function(e){
            if ((e.target == $selectHead[0]) || (e.target == $header[0]) || (e.target == $headerAnchor[0])) {
               $selectBody.toggle();
               e.preventDefault();
               e.stopPropagation();
            }
            else $selectBody.hide();
         })
      },
      /* END SELECT MODULE */
      
      /* BEGIN WATERMARKED FORM FIELDS */
      watermark: function(params){
         var	$this = $(this),
	            _getSetText = function(value){
                  var textfunc = ($this.is("input")) ? "val" : "text";
                  if (value != undefined) {
                     $this[textfunc](value);
                     return $this;
                  }
                  else return $this[textfunc]();
               },
               _defaultText = _getSetText();

         $this.focus(function(){ if (_getSetText() == _defaultText) _getSetText("") })
              .blur(function(){ if (_getSetText() == "") _getSetText(_defaultText) })
              
         return $this;
      },
      /* END WATERMARKED FORM FIELDS */

	   /* BEGIN SlideOut Toolbar */
	   slideout: function(params){
	      var $this = $(this),
             _width = $this.width(),
             _settings = $.extend({ onopen: function(){}, onclose: function(){}, speed: 200 }, params);
            
         $this.width(0);
         
         $this.close = function(){ 
            $this.stop().animate({width: 0}, _settings.speed, "easeInQuad", 
            function(){ _settings.onclose.apply($this); $this.trigger("slideout.closed"); }) 
	      };
         $this.open = function(){ 
            $this.stop().animate({width: _width}, _settings.speed, "easeOutQuad", 
            function(){ _settings.onopen.apply($this); $this.trigger("slideout.opened"); }) 
         };
           
         return $this;
	   },
     /* END SlideOut Toolbar */
     
     /* BEGIN Max Char count constraint */
     maxchars: function(count){
         var $this = $(this);
         return $this.bind("keypress", 
            function(){  if ($this.val().length >= count) { $this.val($this.val().substr(0,count)) } }
         );
     },
     /* END Max Char count constraint */
     
     /* BEGIN countdown */
     countdown: function(count, $element){
         var $this = $(this);
         $this.can_has().subscription($element, "keypress", 
            function(e){ $this.text(count - $element.val().length); }
         );
         return $this;
     }
     /* END countdown */
     
	})

	/* END MODULE DEFINITIONS */
  
})(jQuery) }


/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 * 
 * Copyright (c) 2007 George Smith
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
jQuery.extend( jQuery.easing,{
	easeInQuad: function (x, t, b, c, d) { return c*(t/=d)*t + b; },
	easeOutQuad: function (x, t, b, c, d) { return -c *(t/=d)*(t-2) + b; }
});
