/*
Fancy anchor links.

- jquery.url.js is requrired to highlight "some_anchor" when 
visiting http://host.com/some/page#some_anchor

Todo: Clean up global namespace pollution. Remove the onload. Make it a jquery extension 


Usage example:
=====================
html
-------
<ul>
  <li><a href="#faq1">What is what?</a></li>
<ul>
<div id="faq1">
    <h2></h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>

javascript
-----------
<script type="text/javascript">
$(function() {
    $("a[href^=#]").anchorlinks();
});
</script>


*/

$.fn.anchorlinks = function() {


    var active_anchor;
    var active_link;
        
    function onAnchorClick() {
        var anchorName = $(this).attr('href').substr(1);
        activateAnchor(anchorName);
        return false;        
    }
    
    function activateAnchor(name) {
        var link = $('a[href=#'+name+']'); // 'a[href^=#'+name+']'
        if(link[0]) {
    		if(active_link) {
    		    active_link.removeClass('active');
    		}
    	    active_link = link;
    	    link.addClass('active');
        }

        var target = $('a[name="'+name+'"]')
        if(target[0]) {
    		var target_top = target.offset().top;

    		// Is the target already visible?
    		var from = $(window).scrollTop();
    		var to = from + $(window).height();

            if(target_top > to || target_top < from) {
        		// go to that anchor by setting the body scroll top to anchor top
        		$('html,body').animate({scrollTop:target_top-20}, 500);                
            }


    		if(active_anchor) {
    		    active_anchor.parent().removeClass('active');
    		}

    		active_anchor = target;
    		active_anchor.parent().addClass('active');	    

        }        
    }
    
    this.each(function() {
        var a = $(this);
        a.click(onAnchorClick);
    });
}







// // ========================
// // = Animate anchor links =
// // ========================
// var active_anchor;
// var active_link;
// 
// $(function() {
//     $("a[href^=#]").click(function(event){
//         var anchorName = $(this).attr('href').substr(1);
//         activateAnchor(anchorName);
//         return false;
//     });    
//     
//     if($.url) {
//         var anchor = $.url.attr('anchor');
//         if(anchor) {
//             activateAnchor(anchor)
//         }
//     }
// })
// 
// function activateAnchor(name) {
//     var link = $('a[href^=#'+name+']');
//     if(link[0]) {
//      if(active_link) {
//          active_link.removeClass('active');
//      }
//      active_link = link;
//      link.addClass('active');
//     }
//     
//     var target = $('a[name="'+name+'"]')
//     if(target[0]) {
//      var target_top = target.offset().top;
//      
//      // Is the target already visible?
//      var from = $(window).scrollTop();
//      var to = from + $(window).height();
//         
//         if(target_top > to || target_top < from) {
//          // go to that anchor by setting the body scroll top to anchor top
//          $('html,body').animate({scrollTop:target_top-20}, 500);                
//         }
//         
// 
//      if(active_anchor) {
//          active_anchor.parent().removeClass('active');
//      }
// 
//      active_anchor = target;
//      active_anchor.parent().addClass('active');      
//         
//     }
// }
// 

