(function($){
	
	$.extend({
			 
		smoothAnchors : function(speed, easing, redirect, no_smoothanchors){
				
			var scrollElement = 'html, body';
			$('html, body').each(function () {
				var initScrollTop = $(this).attr('scrollTop');
				$(this).attr('scrollTop', initScrollTop + 1);
				if ($(this).attr('scrollTop') == initScrollTop + 1) {
					scrollElement = this.nodeName.toLowerCase();
					$(this).attr('scrollTop', initScrollTop);
					return false;
				}    
			});
				
			speed = speed || "fast";
			easing = easing || null;
			redirect = (redirect === true || redirect == null) ? true : false;
      no_smoothanchors = no_smoothanchors || false;

      if(no_smoothanchors) {

        $("a").not(no_smoothanchors).each(function(i){

          var url = $(this).attr("href");

          if(url){
            if(url.indexOf("#") != -1 && url.indexOf("#") == 0){

              var aParts = url.split("#",2);
              var anchor = $("a[name='"+aParts[1]+"']");

              if(anchor){

                $(this).click(function(){

                  if($(document).height()-anchor.offset().top >= $(window).height()
                   || anchor.offset().top > $(window).height()
                   || $(document).width()-anchor.offset().left >= $(window).width()
                   || anchor.offset().left > $(window).width()){

                   var topheight = 29;
                   
                    $(scrollElement).animate({
                      scrollTop: anchor.offset().top - topheight,
                      scrollLeft: anchor.offset().left
                    }, speed, easing, function(){
                      if(redirect){
                        window.location = url
                      }
                    });

                  }

                  return false;

                });
              }

            }

          }

        });
      } else {
        $("a").each(function(i){

          var url = $(this).attr("href");

          if(url){
            if(url.indexOf("#") != -1 && url.indexOf("#") == 0){

              var aParts = url.split("#",2);
              var anchor = $("a[name='"+aParts[1]+"']");

              if(anchor){

                $(this).click(function(){

                  if($(document).height()-anchor.offset().top >= $(window).height()
                   || anchor.offset().top > $(window).height()
                   || $(document).width()-anchor.offset().left >= $(window).width()
                   || anchor.offset().left > $(window).width()){

                    if ($.browser.webkit) {
                      var topheight = 46
                    } else var topheight = 31

                    $(scrollElement).animate({
                      scrollTop: anchor.offset().top - topheight,
                      scrollLeft: anchor.offset().left
                    }, speed, easing, function(){
                      if(redirect){
                        window.location = url
                      }
                    });

                  }

                  return false;

                });
              }

            }

          }

        });
      }
			
		}
	
	});
	
})(jQuery);

