jQuery(document).ready(function($) {

    var scroll_duration = 1200;
	
	// automatically scroll when user click on link that contain hash
	if(window.location.hash){
		var location = window.location.hash;
		$("body .smooth").css('color','#999');
        $.scrollTo($(location),{ 
            duration:scroll_duration, 
            offset:{left: 0, top:-214},
			onAfter:function(){
				// for horizontal
                $('.smooth[href="'+location+'"]').css({'color':'#000'});
				// for vertical
                var rel = $('.smooth[href="'+location+'"]').parents('[title$="sectioncontent"]');
				if(rel.length!=0) $('.smooth[href$='+rel.attr('title')+']').css({'color':'#000'});
            }
        });
	}
	
    // for vertical scroll
    $('body .smooth[href^="#"]').each(function(){
        var clicked = $(this), 
            pos = clicked.attr('href'),
			posID = pos.toString().split('#');

        clicked.click(function(e){
			
			// prevent page jumping on the page while hash being set
			$('#'+posID[1]).attr('id','a'+posID[1]);
			window.location.hash = '#'+posID[1];
            e.preventDefault();
			$('#a'+posID[1]).attr('id',posID[1]);
			//-----------------------------------------------------//
			
            $("body .smooth").css('color','#999');           
            $.scrollTo($('#'+posID[1]),{ 
                duration:scroll_duration, 
                offset:{left: 0, top:-214},
                onAfter:function(){
                    clicked.css('color','#000');
                }
            });
			

        });

    });

    
    // for horizontal scroll

    $('.sectionnav').each(function(){

        var navigation = $(this), 
            container = $(this).attr('title');

        $('.smooth[href^="#"]',navigation).click(function(e){                  
            var clicked = $(this), 
				pos = clicked.attr('href'),
				posID = pos.toString().split('#');
			
			// prevent page jumping on the page while hash being set
			$('#'+posID[1]).attr('id','a'+posID[1]);
			window.location.hash = '#'+posID[1];
            e.preventDefault();
			$('#a'+posID[1]).attr('id',posID[1]);
			//-----------------------------------------------------//
			
            $('.smooth',navigation).css('color','#999');
            $(container).scrollTo($('#'+posID[1]),{ 
                duration:scroll_duration, 
                offset:{left: 0, top: 0},
                onAfter:function(){
					// for horizontal
					clicked.css('color','#000');
					// for vertical
					var rel = clicked.parents('[title$="sectioncontent"]');
					if(rel.length!=0) $('.smooth[href$='+rel.attr('title')+']').css({'color':'#000'});
                }
            });
            
        }); 

    });
	
	// ie 6 fix
	if($.browser.msie && parseFloat($.browser.version) < 7){
		$('[role="navigation"]').css({ margin: 'auto', width: 960 });
		$('[role="navigation"] ul').css({ marginLeft: 40, listStyle: 'none', textAlign: 'center', clear: 'both', margin: 'auto', width: 960 });
	}
});

