
(function($){

	"use strict";

	$(document).ready(function(){

		/* set rollover. */
		$('#gnav img.over').rollover({
			suffix: '_over'
		});
		$('img.over, input.over', '#lnav, #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], area.external').click(function(){					
			window.open(this.href, '_blank');
			return false;
		});

		/* popup. */
		$('#content a.popup').click(function(){					
			window.open(this,'_blank','toolbar=no,status=yes,location=no,directories=no,scrollbars=yes,width=600,height=650');
			return false;
		});
		$('#content a.cm_popup').click(function(){					
			window.open(this,'allergen','toolbar=no,status=yes,location=no,directories=no,scrollbars=yes,width=965,height=650');
			return false;
		});

		/* text size changer. */
		$('#text_size')
			/* 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()

			/* changer handler. */
			.find('ul a')
				.click(function(){
					/* switch text size. */
					var textCss = $(this).attr('rel') || 'normal';
					setTextCss(textCss);

					/* save current css name. */
					setCookie('textCss', textCss, {
						path: '/',
						expires: 365	/* 365 days. */
					});

					return false;
				})
			.end()
		;

		/* init text size. */
		var textCss = getCookie('textCss') || getDefaultTextCss();
		setTextCss(textCss);

		function setTextCss(css)
		{
			/* 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 === css) {
						this.disabled = false;
					}
				}
			});

			/* switch active button. */
			$('#text_size a').each(function(){
				var rel = $(this).attr('rel');

				/* get button src from jQuery#data. */
				var src;
				if (rel === css) {
					src = $('img', this).data('images').activeSrc;
				}
				else {
					src = $('img', this).data('images').defaultSrc;
				}

				$('img', this).attr('src', src);
			});
		}

		function getDefaultTextCss()
		{
			var css;
			$('link', 'head').each(function(){
				var rel = $(this).attr('rel');
				var title = $(this).attr('title');
				if (rel.indexOf('style') !== -1 && rel.indexOf('alt') === -1 && title) {
					css = title;
					return false;
				}
			});
			return css;
		}

	});

})(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);



/*
 * cookie functions.
 */
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;
}
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();
	}

	document.cookie = [
		name, '=', encodeURIComponent(value),
		expires,
		option.path ? '; path='+ (option.path) : '',
		option.domain ? '; domain='+ (option.domain) : '',
		secure = option.secure ? '; secure' : ''
	].join('');
}



(function(){
	/*Use Object Detection to detect IE6*/
	var m = document.uniqueID /*IE*/ && document.compatMode /*>=IE6*/ && !window.XMLHttpRequest /*<=IE6*/ && document.execCommand ;
	try{ if(!!m){ m("BackgroundImageCache", false, true) /* = IE6 only */ } }catch(oh){}; 
})();



