


var LssPT_ChangeCss = Class.create({
	
	initialize : function(inStyleTagId, inDefault){
		if($(inStyleTagId)){
			this.element = $(inStyleTagId);
		}else{
			return false;
		}
		
		if(arguments.length>2){
			for(i=2;i<=arguments.length;i+=3){
				if(arguments[i] && arguments[i+1] && arguments[i+2]){
					this.add($(arguments[i]), arguments[i+1], arguments[i+2]);
				}
			}
		}
		
		// 初期値の設定(Cookie)
		this.cookieManager = new CookieJar();
		currentSrc = this.cookieManager.get("ls_style");
		if(currentSrc){
			this.replaceSrc(currentSrc);
		}
		// 規定値の設定
		else if(inDefault){
			this.replaceSrc(inDefault);
		}
	},
	
	add : function(inElement, inEvent, inStyleSrc){
		Event.observe($(inElement), inEvent, this.replaceSrc.bind(this, inStyleSrc));
	},
	
	setCookieName : function(inCookieName){
		this.option.cookieName = inCookieName;
	},
	
	setCookiePath : function(inCookiePath){
		this.option.cookiePath = inCookiePath;
	},
	
	replaceSrc : function(inStyleSrc){
		if(this.element){
			this.element.href = inStyleSrc;
			
			if(typeof recalcZoomImagePosition == "function"){
				recalcZoomImagePosition();
			}
		}
		this.cookieManager.put("ls_style", inStyleSrc);
	}
});






var LsPT_SmoothScrolling = Class.create();

LsPT_SmoothScrolling.prototype = {
	
	option:{
		moveUnit:6,
		interval:30
	},
	
	initialize:function(inOption){
		if(inOption && typeof inOption == "object"){
			this.option = Object.extend(this.option, inOption);
		}
		this.anchers = document.getElementsByTagName("a");
		for(i=0; i<this.anchers.length; i++){
			var targetAncher = this.anchers[i];
			if(targetAncher.href && targetAncher.href.indexOf("#") != -1
				&& (targetAncher.pathname==location.pathname || "/" + targetAncher.pathname==location.pathname )){
				var self = this;
				Event.observe(targetAncher, "click", this.scrollTo.bindAsEventListener(targetAncher, this));
			}
		}
	},
	
	scrollTo:function(evt, self){
		// 規定イベントのキャンセル
		if(window.event){
			// IE用
			window.event.returnValue = false;
		}else if(evt.preventDefault){
			// FireFox用
			evt.preventDefault()
		}
		
		var ancher = this.hash.substr(1);
		var target = null;
		for(i=0; i<self.anchers.length; i++){
			if(self.anchers[i].name && self.anchers[i].name == ancher){
				target = self.anchers[i];
			}
		}
		if(target == null){
			target = ($(ancher) || null);
		}
		
		if(target != null && typeof target == "object"){
			self.scrollingFunction(this, target);
			self.interval = setInterval(self.scrolling, self.option.interval);
		}
	},
	
	scrollingFunction:function(from, to){
		this.from = from;
		this.to = to;
		this.debug("移動開始", true);
		
		this.scrolling = (function(){
			this.currPosition = this.scrollTop();
			
			/**
			* 移動量の計算　参考
			* http://slightlyblue.com/blog/2006/08/javascript_smoothscroll2.html
			*/
			if(this.currPosition < this.to.offsetTo){
				var pageMoveTo = this.currPosition + (Position.cumulativeOffset(this.to).top - this.currPosition) / (this.option.interval / this.option.moveUnit) * 2;
			}else{
				var pageMoveTo = this.currPosition - (this.currPosition - Position.cumulativeOffset(this.to).top) / (this.option.interval / this.option.moveUnit) * 2;
			}
			pageMoveTo = parseInt(pageMoveTo);
			
			this.debug("<p>CurrentPosition:" + this.currPosition + " / From:" + this.from.offsetTop + " / To:" + this.to.offsetTop + " / moveTo:" + pageMoveTo + "</p>");
			window.scrollTo(0, pageMoveTo);
			
			// 規定位置への移動、ページ上端、下端で移動完了とみなす
			if(pageMoveTo == this.currPosition || pageMoveTo <= 0 || this.currPosition == this.scrollTop()){
				// 移動完了
				this.debug("移動完了");
				clearInterval(this.interval);
			}
		}).bind(this);
	},
	
	scrollTop: function (){
		body=document.body
	    d=document.documentElement
	    if (body && body.scrollTop) return body.scrollTop
	    if (d && d.scrollTop) return d.scrollTop
	    if (window.pageYOffset) return window.pageYOffset
	    return 0
	},
	
	debug : function(insertString, clearElement){
		if(Object.isElement($("debug"))){
			if(clearElement == true){
				$("debug").innerHTML = "";
			}
			new Insertion.Bottom($("debug"), insertString);
		}
	}
}








Event.observe(window, "load", function(){
	new LsPT_SmoothScrolling();
	new LssPT_ChangeCss(
					"cssFontSize", "/css/fontsize/small.css"
					, "fontSize01", "click", "/css/fontsize/small.css"
					, "fontSize02", "click", "/css/fontsize/middle.css"
					, "fontSize03", "click", "/css/fontsize/large.css"
					);
});

