(function($){

  $.extend({

    smoothAnchors : function(speed, easing, redirect){

      speed = speed || "fast";
      easing = easing || null;
      redirect = (redirect === true || redirect == null) ? true : false;

      $("a").each(function(i){

        var url = $(this).attr("href");

        if(url){
          if(url.indexOf("#") != -1 && url.indexOf("#") == 0){

            var aParts = url.split("#",2);
            //alert (aParts[1]);
            var anchor = $("a[name='"+aParts[1]+"']");

            if(anchor){

              $(this).click(function(){
                $(window).resize(function() {
                  //update stuff
                });
/*                alert ( $(document).height() + '-' + anchor.offset().top + '>=' + $(window).height() + '\n'
                        + anchor.offset().top + '>' + $(window).height() + '\n'
                        + $(document).width() + '-' + anchor.offset().left + '>=' + $(window).width() + '\n'
                        + anchor.offset().left + '>' + $(window).width()
                      );*/
                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 goto_x = anchor.offset().left;
                  var goto_y = anchor.offset().top;

                  //alert (anchor.attr('name'));
                  if (anchor.attr('name') == 'consultation') goto_y = goto_y - 90;

                  speed = goto_y - $(document).scrollTop();
                  if (speed < 0) { speed = 0 - speed; }

                  if (speed > 1000 && speed <= 5000)  speed = 2000;
                  if (speed > 5000 && speed <= 15000) speed = 6000;
                  if (speed > 15000 )                 speed = 10000;

			if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')  {
				$('#menu').hide();
			}


//                   alert ( 'from: ' + $(document).scrollTop() + '\n' +
//                           'where: '+ goto_y + '\n' +
//                           'speed: ' + speed
//                         );


//                   $('html, body').animate({
//                     scrollTop: goto_y,
//                     scrollLeft: goto_x
//                   }, speed, easing, function(){
//                     if(redirect){
//                       window.location = url;
//                     }
//                   });

                  $('html, body').animate({
                    scrollTop: goto_y
                  }, speed, easing, function(){
                    if(redirect){
                      window.location = url;
                    }

			if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')  {
				$('#menu').css("top",window.pageYOffset +"px");
				$('#menu').fadeIn(200);
			}

                  });


                  // $(this).attr('class', 'menu_links_selected');


                }

                return false;

              });

            }

          }

        }

      });

    }

  });

})(jQuery);
