
function initRollovers(){
	var image_cache = new Object();
	$( '.rollover' ).each( function(){
		var imgsrc = this.src;
		var ftype = imgsrc.substring( imgsrc.lastIndexOf('.'), imgsrc.length );
		var imgsrc_ov = imgsrc.replace( ftype, '_ov' + ftype );
		image_cache[ this.src ] = new Image();
		image_cache[ this.src ].src = imgsrc_ov;
		
		$( this ).hover(
			function(){
				this.src = imgsrc_ov;
			},
			function(){
				this.src = imgsrc;
			}	
		);
	} );
}

/*------------------------------------------------
	scrolling */

var smoothScroll = {
	speed: 16, // this value increase, the speed decrease.
	maxStep: 150, // uniform motion step for long distance.
	brakeC: 5, // the coefficient of slowing down.
	pagetop: 'pagetop',
	timer: null,
	hash: null,
	targetNode: null,
	targetYPos: 0,
	init: function(){
		var allLinks = document.getElementsByTagName( 'a' );
		for ( var i = 0; i < allLinks.length; i++ ){
			var linkNode = allLinks[ i ];
			if ( ( linkNode.href && linkNode.href.indexOf( '#' ) != -1 ) &&
			( ( linkNode.pathname == location.pathname ) ||
				( '/' + linkNode.pathname == location.pathname ) ) &&
			( linkNode.search == location.search ) ){
				linkNode.onclick = smoothScroll.initScroll;
			}
		}
	},
	getElementYPos: function( elm ){
		if ( elm.nodeName.toLowerCase() == 'a' ){
			elm = elm.parentNode;
		}
		var yPos = 0;
		if ( smoothScroll.hash == smoothScroll.pagetop ){
			yPos = 0;
		} else {
			yPos = elm.offsetTop;
			while( elm.offsetParent && ( elm.offsetParent.nodeName.toLowerCase != 'body' ) ){
				elm = elm.offsetParent;
				yPos += elm.offsetTop;
			}
			//while( elm.offsetParent ){
			//	yPos += elm.offsetTop
			//	elm = elm.offsetParent;
			//}
		}
		return yPos;
	},
	getCurrentYPos: function(){
		if ( document.body && document.body.scrollTop ){
			return document.body.scrollTop;
		}
		if ( document.documentElement && document.documentElement.scrollTop ){
			return document.documentElement.scrollTop;
		}
		if ( window.pageYOffset ){
			return window.pageYOffset;
		}
		return 0;
	},
	getWindowHeight: function(){
		if ( window.innerHeight ){ 
			return window.innerHeight;
		}
		if ( document.documentElement && document.documentElement.clientHeight ){
			return document.documentElement.clientHeight;
		}
		if ( document.body.clientHeight ){
			return document.body.clientHeight;
		}
		return 0;
	},
	getDocumentHeight: function(){
		var b = document.documentElement.getElementsByTagName('body')[0];
		if ( b.scrollHeight ){
			return b.scrollHeight;
		}
		if ( document.documentElement.offsetHeight ){
			return document.documentElement.offsetHeight;
		}
		//if ( document.height ){ alert ( 'document.height' ); return document.height;}
		//if ( document.body.offsetHeight ){ alert ( 'document.body.offsetHeight' ); return document.body.offsetHeight;}
		return 0;
	},
	initScroll: function( evt ){
		var eventNode = dom.event.target( evt );
		if ( !eventNode ) return;
		if ( eventNode.nodeName.toLowerCase() != 'a' ) eventNode = eventNode.parentNode;
		if ( eventNode.nodeName.toLowerCase() != 'a' ) return;
		smoothScroll.hash = eventNode.href.substr( eventNode.href.indexOf( '#' ) + 1, eventNode.href.length );
		smoothScroll.targetNode = document.getElementById( smoothScroll.hash );
		if ( !smoothScroll.targetNode ) return;
		smoothScroll.targetYPos = smoothScroll.getElementYPos( smoothScroll.targetNode );
		smoothScroll.scroll();
		dom.event.preventDefault( evt );
		dom.event.stopPropagation( evt );
		//return false;
	},
	scroll: function(){
		var currentYPos = smoothScroll.getCurrentYPos();
		if ( smoothScroll.timer ) clearTimeout( smoothScroll.timer );
		if ( smoothScroll.targetYPos > currentYPos ) {
			var documentHeight = smoothScroll.getDocumentHeight();
			var windowHeight = smoothScroll.getWindowHeight();
			var distance = Math.round( ( documentHeight - ( currentYPos + windowHeight ) ) / smoothScroll.brakeC );
			distance = Math.min( Math.round( ( smoothScroll.targetYPos - currentYPos ) / smoothScroll.brakeC ), distance );
			distance = Math.max( 2, Math.min( distance, smoothScroll.maxStep ) );
		} else {
			var distance = Math.abs( Math.round( ( smoothScroll.targetYPos - currentYPos ) / smoothScroll.brakeC ) );
			distance = - Math.min( distance, smoothScroll.maxStep );
			distance = Math.min ( -2, distance );
		}
		window.scrollTo( 0, currentYPos + distance );
		if ( Math.abs( currentYPos - smoothScroll.targetYPos ) <= 1 ||
			smoothScroll.getCurrentYPos() == currentYPos ){
			//window.scrollTo( 0, smoothScroll.targetYPos );
			if ( !( navigator.userAgent.indexOf( 'Safari' ) >= 0 ) ){
				location.hash = smoothScroll.hash;
			}
			smoothScroll.hash = null;
		} else {
			smoothScroll.timer = setTimeout( smoothScroll.scroll, smoothScroll.speed );
		}
	}
};

/*------------------------------------------------
	DOM event control */

var dom = new Object();
dom.event = new Object();
dom.event.addEventListener = function( elm, type, func, useCapture ){
	if ( !elm ){ return false; }
	if ( !useCapture ){ useCapture = false; }
	if ( elm.addEventListener ){
		elm.addEventListener( type, func, useCapture );
	} else if ( elm.attachEvent ){
		elm.attachEvent( 'on' + type, func );
	} else { return false; }
	return true;
};
dom.event.removeEventListener = function( elm, type, func, useCapture ){
	if ( !elm ){ return false; }
	if ( !useCapture ){ useCapture = false; }
	if ( elm.removeEventListener ){
		elm.removeEventListener( type, func, useCapture );
	} else if ( elm.detachEvent ){
		elm.detachEvent( 'on' + type, func );
	} else { return false; }
	return true;
};
dom.event.target = function( evt ){
	if ( evt && evt.target ){
		if ( evt.target.nodeType == 3 ){ return evt.target.parentNode; }
		else { return evt.target; }
	} else if ( window.event && window.event.srcElement ){
		return window.event.srcElement;
	} else {
		return null;
	}
};
dom.event.preventDefault = function( evt ){
	if ( evt && evt.preventDefault ){
		evt.preventDefault();
		return false;
	} else if ( window.event ){
		window.event.returnValue = false;
	}
	return 0;
};
dom.event.stopPropagation = function( evt ){
	if ( evt && evt.stopPropagation ){ evt.stopPropagation(); }
	else if ( window.event ){ window.event.cancelBubble = true; }
};

/*------------------------------------------------
	initialize */

$( document ).ready( function(){
	initRollovers();
	smoothScroll.init();
} );
