/*---------------------------------------------------------------

 jQuery.slideScroll.js
 
 jQuery required (tested on version 1.2.6)
 encoding UTF-8

 Copyright (c) 2008 nori (norimania@gmail.com)
 http://moto-mono.net
 Licensed under the MIT

 $Update: 2008-12-24 20:00
 $Date: 2008-12-22 23:30
 
 ----------------------------------------------------------------*/

$.fn.slideScroll = function(options){
	var c = $.extend({
		interval: 20,
		easing: 0.6,
		comeLink: false
	},options);
	var d = document;
	
	var timer;
	var pos;
	
	function currentPoint(){
		var current = {
			x: d.body.scrollLeft || d.documentElement.scrollLeft,
			y: d.body.scrollTop || d.documentElement.scrollTop
		}
		return current;
	}
	

	function setPoint(){
		

		var h = d.documentElement.clientHeight;
		var w = d.documentElement.clientWidth;
		

		var maxH = d.documentElement.scrollHeight;
		var maxW = d.documentElement.scrollWidth;
		
		pos.top = ((maxH-h)<pos.top && pos.top<maxH) ? maxH-h : pos.top;
		pos.left = ((maxW-w)<pos.left && pos.left<maxW) ? maxW-w : pos.left;
	}
	
	function nextPoint(){
		var x = currentPoint().x;
		var y = currentPoint().y;
		var sx = Math.ceil((x - pos.left)/(5*c.easing));
		var sy = Math.ceil((y - pos.top)/(5*c.easing));
		var next = {
			x: x - sx,
			y: y - sy,
			ax: sx,
			ay: sy
		}
		return next;
	}
	
	function scroll(){
		timer = setInterval(function(){
			nextPoint();
			
			if(Math.abs(nextPoint().ax)<1 && Math.abs(nextPoint().ay)<1){
				clearInterval(timer);
				window.scroll(pos.left,pos.top);
			}
			window.scroll(nextPoint().x,nextPoint().y);
		},c.interval);
	}
	
	function comeLink(){
		if(location.hash){
			if($(location.hash) && $(location.hash).length>0){
				pos = $(location.hash).offset();
				setPoint();
				window.scroll(0,0);
				if($.browser.msie){
					setTimeout(function(){
						scroll();
					},50);
				}else{
					scroll();
				}
			}
		}
	}
	if(c.comeLink) comeLink();
	
	$(this).each(function(){
		var str_thref = (this.href.split("#")[0]).split("?")[0];
		var str_lhref = (location.href.split("#")[0]).split("?")[0];
		if(this.hash && $(this.hash).length>0 
			&& str_thref.match(new RegExp(str_lhref))){
			var hash = this.hash;
			$(this).click(function(){
				pos = $(hash).offset();
				
				clearInterval(timer);
				
				setPoint();
				scroll();
				return false;
			});
		}
	});
}



$(function(){
$("a[href*='#']").slideScroll();
$("area[href*='#']").slideScroll();
});
