﻿/*--------------------------------------
 Switch Font Size
--------------------------------------*/
var styleswitcher = new Object();
styleswitcher.default_id     = 'Header-Fstyle';
styleswitcher.default_key    = 'fstyleM';
styleswitcher.cookie_key     = 'angfa-fstyle';
styleswitcher.cookie_expires = 30;



/**
 * 
 * 
 */
styleswitcher.init = function(options) {

	var key = $.cookie(styleswitcher.cookie_key);
	if (!key) {
		if (options && options.default_key) {
			key = options.default_key;
		} else {
			/*
			var list = document.getElementsByTagName('link');
			var title, rel;
			for (var i=0,max=list.length; i<max; i++) {
				title = list[i].getAttribute('title');
				rel   = list[i].getAttribute('rel');
				if ((rel.indexOf('style') >= 0) && (rel.indexOf('alternate') < 0) && title) {
					key = title;
				}
			}*/
			if (!key) {
				key = styleswitcher.default_key;
			}
		}
	}

	styleswitcher.default_id = (options && options.default_id)?options.default_id:styleswitcher.default_id;

	$(function() {
		$('#' + styleswitcher.default_id + ' a').each(function() {
			$(this).click(function() {
				var target = $(this).attr('href') + '';

				target = target.replace('#', '');
				styleswitcher.setActive(target);
				styleswitcher.setActiveLink(target);
				
				//コールバックの設定
				if(typeof styleswitcher.callback == 'function') {
					styleswitcher.callback();
				}
				return false;
			});
		});

		styleswitcher.setActiveLink(key);
	});

	styleswitcher.setActive(key);

}

/**
 * クッキーへのセットとcssのアクティブ/非アクティブ化
 * 
 */
styleswitcher.setActive = function(key) {
	$('link').each(function(){
		var title = $(this).attr('title');
		if($(this).attr('rel').indexOf('style') >= 0 && title) {
			$(this).attr('disabled','disabled');
			if(title == key) {
				$(this).removeAttr('disabled');
				$.cookie(styleswitcher.cookie_key, key, {expires: styleswitcher.cookie_expires, path: '/'});
			}
		}
	});
}


/**
 * 
 * 
 */
styleswitcher.setActiveLink = function(key) {
	$('#' + styleswitcher.default_id + ' a').each(function() {
		var href = $(this).attr('href') + '';
		if (href == ('#' + key)) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	});
}
