/* (C)S.Kaizu | Ver.slimlicense */
/*@cc_on _d=document;eval('var document=_d')@*/
/*for Dreamweaver: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />*/

function Kaizu() {
	var userAgent = navigator.userAgent.toLowerCase();
	this.isMac    = userAgent.match(/Mac|mac/);
	this.isWin    = userAgent.match(/Win|win/);
	this.isIE     = userAgent.match(/MSIE|msie/);
	this.isOldIE  = (userAgent.match(/(MSIE|msie) (5|6)\./) != null);
	this.scrollInterval   = null;
	this.scrollTargetID   = '';	  
	this.waitDuration     = 500;
	this.updateDuration   = 10;
	this.easingSpeed      = 30;
}

Kaizu.prototype = {

	addEventListener : function (o, e, f, c) {
		if (f == 'DOMMouseScroll' && o.attachEvent) f = 'mousewheel';
		(o.addEventListener) ? o.addEventListener(e, f, c) : o.attachEvent('on' + e, f);
	},

	removeEventListener : function (o, e, f, c) {
		if (f == 'DOMMouseScroll' && o.attachEvent) f = 'mousewheel';
		(o.removeEventListener) ? o.removeEventListener(e, f, c) : o.detachEvent('on' + e, f);
	},

	getElementsByClassName : function(tagName, className) {
		var results  = new Array();
		var elements = document.getElementsByTagName(tagName);
		for (var i = 0, count = elements.length; i < count; i++) {
			if (!elements[i].className) continue;
			var classes = elements[i].className.split(' ');
			for (var j = 0, n = classes.length; j < n; j++) {
				if (classes[j] == className) {
					results[results.length] = elements[i];
					break;
				}
			}
		}
		return results;
	},

	/*--------------------------------------------------*/

	getOffsetTop : function(o) {
		return o.offsetTop + ((o.offsetParent) ? K.getOffsetTop(o.offsetParent) : 0);
	},

	getOffsetLeft : function(o) {
		return o.offsetLeft + ((o.offsetParent) ? K.getOffsetLeft(o.offsetParent) : 0);
	},

	getScrollTop : function() {
		return document.body.scrollTop || document.documentElement.scrollTop;
	},

	/*--------------------------------------------------*/

	getStyle : function (elememt, property, pseudo) {
		var camelize = function(str) {
			return str.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
		};
		var deCamelize = function(str) {
			return str.replace(/[A-Z]/g, function($0) { return '-' + $0.toLowerCase(); });
		};
		if (elememt.currentStyle) {
			if (property.indexOf('-') != -1) property = camelize(property);
			return elememt.currentStyle[property];
		}
		else if (getComputedStyle) {
			if (property.indexOf('-') == -1) property = deCamelize(property);
			return document.defaultView.getComputedStyle(elememt, pseudo).getPropertyValue(property);
		}
		return '';
	},

	getFloatStyle : function(element) {
		var userAgent = navigator.userAgent.toLowerCase();
		var float = K.getStyle(element, 'float');
		if (!float) {
			var styleFloat = (userAgent.match(/msie/) && !userAgent.match(/opera/)) ? 'styleFloat' : 'cssFloat';
			float = K.getStyle(element, styleFloat);
		}
		return float;
	},

	/*--------------------------------------------------*/

	getFlashPlayerMajorVersion : function() {
		var tmp;
		if (navigator.plugins && navigator.mimeTypes.length) {
			tmp = navigator.plugins['Shockwave Flash'].description.match(/([0-9]+)/);
		}
		else {
			tmp = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').match(/([0-9]+)/);
		}
		return parseInt(tmp[0]);
	},

	createSWFObject : function(src, param, attr) {
		document.write('\n<object');
		if (K.isIE) {
			document.write(' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"');
		}
		else {
			document.write(' type="application/x-shockwave-flash" data="' + src + '"');
		}
		if (attr) {
			for (var key in attr) document.write(' ' + key + '="' + attr[key] + '"');
		}
		document.write('>\n');
		document.write('<param name="movie" value="' + src + '" />\n');
		if (param) {
			for (var key in param) {
				if (key != 'wmode') {
					document.write('<param name="' + key + '" value="' + param[key] + '" />\n');
				}
				else if (param[key] == 'transparent' || K.getFlashPlayerMajorVersion() < 10) {
					document.write('<param name="wmode" value="' + param[key] + '" />\n');
				}
			}
		}
		else 	if (K.getFlashPlayerMajorVersion() < 10) {
			document.write('<param name="wmode" value="opaque" />\n');
		}
		document.write('</object>');
	},
	
	/*--------------------------------------------------*/

	smoothScroll : function() {
		clearInterval(K.scrollInterval);
		var targetTop = K.getOffsetTop(document.getElementById(K.scrollTargetID));
		if (K.scrollTargetID != 'header') {
			targetTop -= ((K.scrollTargetID.indexOf('-') == -1) ? 80 : 120) - (K.isOldIE ? 65 : 0);
		}
		var pageHeight = (document.body.scrollHeight) ? document.body.scrollHeight : document.height;
		var windowHeight = (window.innerHeight) ? window.innerHeight : document.documentElement.offsetHeight;
		var scrollMax = pageHeight - windowHeight;
		if (targetTop > scrollMax) targetTop = scrollMax;
		var scrollTop = K.getScrollTop();
		var distance = targetTop - scrollTop;
		distance += (distance > 0) ? K.easingSpeed : -K.easingSpeed;
		if (Math.abs(distance) > K.easingSpeed) {
			var moving = distance / K.easingSpeed;
			if (Math.abs(moving) < 1) moving = (moving > 0) ? 1 : -1;
			window.scrollTo(0, scrollTop + moving);
			K.scrollInterval = setInterval(K.smoothScroll, K.updateDuration);
		}
		else {
			window.scrollTo(0, targetTop);
		}
	},

	setupAnchor : function() {
		var anchors = document.getElementsByTagName('a');
		for (var i = 0, length = anchors.length; i < length; i++) {
			var anchor = anchors[i].getAttribute('href', 2).replace(/#/g, '');
			if (!anchor || !document.getElementById(anchor)) continue;
			var clickHandler = function(event) {
				(event.preventDefault) ? event.preventDefault() : event.returnValue = false;
				var anchor = (event.srcElement) ? event.srcElement : this;
				while (anchor.tagName.toLowerCase() != 'a') anchor = anchor.parentNode; 
				K.scrollTargetID = anchor.getAttribute('href', 2).replace(/#/g, '');
				K.scrollInterval = setInterval(K.smoothScroll, K.updateDuration);
			};
			K.addEventListener(anchors[i], 'click', clickHandler, false);
		}
	},

	/*--------------------------------------------------*/
	
	stopScroll : function() {
		if (K.scrollInterval != null) {
			clearInterval(K.scrollInterval);
			K.scrollInterval = null;
		}
	},
	
	addStopScrollEvent : function() {
			K.addEventListener(document, 'mousedown', K.stopScroll, false);
			K.addEventListener(document, 'mousewheel', K.stopScroll, false);
	}
	
}

function setupPage() {
	K.setupAnchor();
	K.addStopScrollEvent();
}

var K = new Kaizu();
K.addEventListener(window, 'load', setupPage, false);

