/*
 * utility functions.
 */
(function($){

	$(window).load(function(){

		// set rollover.
		$('img.over, input.over').each(function(){
			var img = new Image();
			img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2');
			$(this)
				// store images.
				.data('image', {
					defaultSrc: this.src,
					hoverSrc: img.src
				})
				// hover handler.
				.hover(
					function(){
						this.src = $(this).data('image').hoverSrc;
					},
					function(){
						this.src = $(this).data('image').defaultSrc;
					}
				)
			;
		});

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

		// set link to gallery.
		$('#gnav li.gallery a, #site_menu li.gallery a').click(function(){
			window.open(this.href, 'GemsGallery', 'width=950,height=800,menubar=yes,toolbar=no,location=yes,status=yes,resizable=yes,scrollbars=yes');
			return false;
		});

		// set footer popup menu.
		$('#footer div.nav div.loose > ul li:first')
			// set container style.
			.css({ position: 'relative' })
			// setup popup.
			.find('div.popup')
				.css({
					position: 'absolute',
					bottom: 0,
					width: 495
				})
				.find('li')
					.css({
						float: 'left',
						width: 140
					})
				.end()
				.find('p.close')
					// close handler.
					.click(function(){
						$(this).closest('div.popup').slideUp();
						return false;
					})
				.end()
				.hide()
			.end()
			// popup toggle handler.
			.children('a')
				.click(function(){
					$(this).siblings('div.popup')
						.css({ left: $(this).width() + 18 })
						.slideToggle()
					;
					return false;
				})
			.end()
		;

	});

})(jQuery);


/*
 * style switcher
 */
(function($){

	$(window).load(function(){

		// set font switcher.
		$('#header div.font_switch ul')
			// store images.
			.find('img')
				.each(function(){
					var img = new Image();
					img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
					$(this).data('image', {
						defaultSrc: this.src,
						activeSrc: img.src
					});
				})
			.end()
			// click handler.
			.find('a')
				.click(function(){
					switchStyle(this.getAttribute('rel'));
					return false;
				})
			.end()
		;
		// switch font size.
		var style = getCookie('style') || 'middle';
		switchStyle(style);

	});

	function switchStyle(styleName)
	{
		// switch link.
		$('link[rel*=style][title]').each(function(){
			this.disabled = true;
			if (this.getAttribute('title') == styleName) {
				this.disabled = false;
			}
		});
		setCookie('style', styleName, { expires: 365 });

		/* set the current selected style image. */
		$('#header div.font_switch a > img').each(function(){
			if ($(this).parent('a').attr('rel') == styleName) {
				$(this)
					.attr('src', $(this).data('image').activeSrc)
					.css({ position: 'relative' })
				;
			}
			else {
				$(this)
					.attr('src', $(this).data('image').defaultSrc)
					.css({ position: 'static' })
				;
			}
		});
	}

	function getCookie(name)
	{
		var value = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			var cookie = null;
			for (var i = 0; i < cookies.length; 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, options)
	{
		options = options || {};
		if (value == null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires='+ date.toUTCString();
		}
		var path = options.path ? '; path='+ (options.path) : '';
		var domain = options.domain ? '; domain='+ (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [ name, '=', encodeURIComponent(value), path, domain, secure].join('');
	}

})(jQuery);
