Cookie = {};
Cookie.read = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}
Cookie.set = function(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}
function getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1
			 && a.getAttribute("rel").indexOf("alt") == -1
			 && a.getAttribute("title")
			 ) return a.getAttribute("title");
	}
	return null;
}

Util = {};
Util.Path = function(path) {
	var path = path;
	var pathArray = path.split('/');
	var fileName = pathArray.pop();
	var filePath = pathArray.join('/') + '/';
	
	this.getPath = function() {
		return path;
	}
	this.getFileName = function() {
		return fileName;
	}
	this.getFilePath = function() {
		return filePath;
	}
}

FontSizeChanger = function(elems, target) {
	var self = this;
	var elems	= $(elems);
	var target = $(target);
	var path = new Util.Path(target.attr('href'));
	
	var cookie = Cookie.read("style");
	var active = cookie ? cookie : 'm';
	
	self.changers = {};
	
	elems.each(function(){
		var rel = $(this).attr('rel');
		self.changers[rel] = new Changer($(this));
		$(this).bind('click', function(){
			elems.removeClass('current');
			self.changers[rel].change();
			self.changers[rel].setCurrent();
		});
	});
	
	self.changers[active].change();
	self.changers[active].setCurrent();
	
	self.getActive = function() {
		return active;
	}
	
	function Changer(elem) {
		var size = elem.attr('rel');
		this.change = function() {
			target.attr('href', path.getFilePath() + size + '.css');
		}
		this.setCurrent = function() {
			active = size;
			elem.addClass('current');
		}
	}
}

// DOM構築後に実行
$(function() {
	var fs = new FontSizeChanger('#fontSizeChanger a', '#changable');
	$(window).unload(function() {
		Cookie.set("style", fs.getActive(), 365);
	});
});

