
(function($){

	"use strict";

	/* dom ready hander. */
	$(document).ready(function(){

		/* set rollover. */
		$('img.over, input.over', '#gnav').rollover({
			suffix: '_over'
		});
		$('img.over, input.over', '#content').rollover();

		/* activate current page image. */
		$('img.on').each(function(){
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
		});

		/* open link as '_blank'. */
		$('a[rel=external], a[rel=pdf]').click(function(){					
			window.open(this.href, '_blank');
			return false;
		});

		setupFontScaler();

	});

	/*
	 * setup font scaler.
	 */
	function setupFontScaler()
	{
		$('#font_size ul > li')
			/* store images. */
			.find('img')
				.each(function(){
					var img = new Image();
					img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
					$(this).data('images', {
						defaultSrc: this.src,
						activeSrc: img.src
					});
				})
			.end()

			/* scaler handler. */
			.find('a')
				.click(function(){
					/* switch css. */
					var cssId = $(this).attr('rel') || 'normal';
					switchFontCss(cssId);

					/* save current css id. */
					setCookie('fontCss', cssId, {
						path: '/',
						expires: 7	/* 1 week. */
					});

					/* fire flat heights. */
					if ($.isFunction($.fn.flatHeights)) {
						$.changeLetterSize.currentSize = -1;
					}

					return false;
				})
			.end()
		;

		/* init font css. */
		var cssId = getCookie('fontCss') || getDefaultFontCss();
		switchFontCss(cssId);
	}

	/*
	 * switch font css.
	 */
	function switchFontCss(cssId)
	{
		/* switch active css. */
		$('link', 'head').each(function(i){
			var rel = $(this).attr('rel');
			var title = $(this).attr('title');
			if (rel.indexOf('style') !== -1 && title) {
				this.disabled = true;
				if (title === cssId) {
					this.disabled = false;
				}
			}
		});

		/* switch active button. */
		$('#font_size a').each(function(){
			/* get button src from jQuery#data. */
			var rel = $(this).attr('rel');
			var src;
			if (rel === cssId) {
				src = $('img', this).data('images').activeSrc;
			} else {
				src = $('img', this).data('images').defaultSrc;
			}
			$('img', this).attr('src', src);
		});
	}

	/*
	 * returns default font css ID.
	 */
	function getDefaultFontCss()
	{
		var cssId;
		$('link', 'head').each(function(){
			var rel = $(this).attr('rel');
			var title = $(this).attr('title');
			if (rel.indexOf('style') !== -1 && rel.indexOf('alt') === -1 && title) {
				cssId = title;
				return false;
			}
		});
		return cssId;
	}

})(jQuery);



/*
 * jQuery tiny rollover plugin.
 *
 * @param {Object} Several options.
 * @return {Object} jQuery object.
 */
(function(a){a.fn.rollover=function(c){var d="$1"+a.extend({suffix:"_on"},c).suffix+"$2";return a(this).each(function(){var b=new Image;b.src=this.src.replace(/^(.+)(\.[a-z]+)$/,d);a(this).data("rolloverImage",{defaultImage:this.src,hoverImage:b.src}).hover(function(){this.src=a(this).data("rolloverImage").hoverImage},function(){this.src=a(this).data("rolloverImage").defaultImage})})}})(jQuery);



/*
 * get cookie.
 */
function getCookie(name)
{
	var value;
	if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		var cookie;
		for (var i = 0, len = cookies.length; i < len; i++) {
			cookie = cookies[i].replace(/^\s+|\s+$/g, '');
			if (cookie.substring(0, name.length + 1) == (name +'=')) {
				value = decodeURIComponent(cookie.substring(name.length + 1));
				break;
			}
		}
	}
	return value;
}

/*
 * set cookie.
 */
function setCookie(name, value, option)
{
	option = option || {};
	if (!value) {
		value = '';
		option.expires = -1;
	}
	var expires = '';
	if (option.expires && (typeof option.expires == 'number' || option.expires.toUTCString)) {
		var date;
		if (typeof option.expires == 'number') {
			date = new Date();
			date.setTime(date.getTime() + (option.expires * 24 * 60 * 60 * 1000));
		} else {
			date = option.expires;
		}
		expires = '; expires='+ date.toUTCString();
	}
	var path = option.path ? '; path='+ (option.path) : '';
	var domain = option.domain ? '; domain='+ (option.domain) : '';
	var secure = option.secure ? '; secure' : '';
	document.cookie = [ name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}

/*
 * thx PHP.js. (http://phpjs.org/functions/array_chunk:306)
 */
function array_chunk (input, size) {
	// Split array into chunks  
	// 
	// version: 1006.1915
	// discuss at: http://phpjs.org/functions/array_chunk    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
	// *     example 1: array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
	// *     returns 1: {0 : {0: 'Kevin', 1: 'van'} , 1 : {0: 'Zonneveld'}}
 
	for (var x, i = 0, c = -1, l = input.length, n = []; i < l; i++){        (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
	}
 
	return n;
}



