/* UTF-8 編碼
 * --------------------------------------------------------------------
 * init.sitepage.js
 *
 * by Guan-Ting Chen, fi@livemail.tw
 *
 * Copyright (c) 2009 Guan-Ting Chen
 * Licensed under MIT (http://www.opensource.org/licenses/MIT-license.php)
*/
$(document).ready(function () {
	// font size initialize
	$('a[href*=#font:normal]').bind('click', function (event) {
		event.preventDefault();
		document.cookie = 'fontsize=normal';
		$('#css_site').attr('href', 'css/site.css');
		$('#css_site_box').attr('href', 'css/site.box.css');
	});
	
	$('a[href*=#font:large]').bind('click', function (event) {
		event.preventDefault();
		document.cookie = 'fontsize=large';
		$('#css_site').attr('href', 'css/site-large.css');
		$('#css_site_box').attr('href', 'css/site.box-large.css');
	});
	
	$('a[href*=#font:xlarge]').bind('click', function (event) {
		event.preventDefault();
		document.cookie = 'fontsize=xlarge';
		$('#css_site').attr('href', 'css/site-xlarge.css');
		$('#css_site_box').attr('href', 'css/site.box-xlarge.css');
	});

	var font_size = /fontsize=(\w+)/.exec(document.cookie) || '';
	if ('' != font_size) {
		$('a[href*=#font:'+font_size[1]+']').trigger('click');
	}

	// auxiliary design
	if (0 < $('.table-keyin').length) {
		var input_auxiliary = $('input[class*=auxiliary],textarea[class*=auxiliary]');

		if (0 < input_auxiliary.length) {
			input_auxiliary.each(function () {
				var self = $(this);

				if (undefined != self.attr('title')) {
					self.addClass('input-help');

					if ('' == self.val()) {
						self.addClass('input-help').val(self.attr('title'));
					}

					self.bind('focus', function () {
						$(this).addClass('input-focus');

						if (self.attr('title') == self.val()) {
							self.removeClass('input-help').val('');
						}
					});
		
					self.bind('blur', function () {
						$(this).removeClass('input-focus');

						if ('' == self.val()) {
							self.addClass('input-help').val(self.attr('title'));
						}
					});
				}
			});
		}
	}

	$('a[class*=history-back]').bind('click', function (event) {
		event.preventDefault();
		history.back();		
	});
	/*
	$('a[href*=#page:favorite]').bind('click', function (event) {
		event.preventDefault();
		var data = $(this).attr('rel').split('#'); // title, url
		addFavorite(data[1], data[0]);
	})*/;
	var form = $('form');
	form.find('button').hide();
	form.find('.button').show();

	$('a[href*=#accesskey]').each(function () {
		$(this)
		.bind('focus', function () { $(this).addClass('accesskey-focus'); })
		.bind('blur', function () { $(this).removeClass('accesskey-focus'); });
	});
	
	$('a[href*=#form:submit]').bind('click', function (event) {
		event.preventDefault();
		var self = $(this);
		if ('' == self.attr('rel')) {
			$(this).parents('form:eq(0)').submit();
		} else {
			if (true == confirm(self.attr('rel'))) {
				$(this).parents('form:eq(0)').submit();
			}
		}
	});
});
