
(function($){

	$(document).ready(function(){

		// set header bg effect.
		if ($.browser.safari)
			setTimeout(initializeHeaderBg, 500);
		else
			initializeHeaderBg();

		if ($.browser.msie && (parseInt($.browser.version) < 7)) {
			// for background-flickr.
			try {
				document.execCommand('BackgroundImageCache',false,true);
			} catch(e) {}

			// 24bit PNG fix for ie6.
			$('#header01, #header01 img, #gnav01, #gnav01 img').fixpng();
			$('#header01 a > span, #gnav01 a > span').css({cursor: 'pointer'});

			// reset position & wrap relative positioned <div />.
			$('#header01')
				.css('position', 'static')
				.wrap('<div />').parent().css({position: 'relative'});
			$('#gnav01')
				.css('position', 'static')
				.wrap('<div />').parent().css({position: 'relative'});
		}

		// set rollover on gnav.
		$('#gnav01 img[class=over]').each(function(){
			// preload image.
			var img = new Image();
			img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2');

			if ($.support.opacity) {
				// create elements for rollover effects.
				var container = $(this).wrap('<div />').parent();
				$(container)
					// set a hover image to the background.
					.css({
						position: 'relative',
						overflow: 'hidden',
						zIndex: 1,
						width: $(this).width(),
						height: $(this).height(),
						background: 'url('+ img.src +') no-repeat'
					})
					.attr('title', this.alt || '')
					// hover handler.
					.hover(
						function(){
							$(this).find('.default').stop().animate({
								top: -8,
								left: 8,
								opacity: 0
							});
						},
						function(){
							$(this).find('.default').stop().animate({
								top: 0,
								left: 0,
								opacity: 1
							});
						}
					)
					// copy the wrapper & set as a default image.
					.clone().insertAfter(this)
					.css({
						position: 'absolute',
						backgroundImage: 'url('+ this.src +')'
					})
					.addClass('default');

				// remove original.
				$(this).remove();
			} else {
				// i can't resolve the-transparent-png-problem.
				$(this)
					// store the image sources.
					.data('image', {
						defaultSrc: this.src,
						hoverSrc: img.src
					})
					.hover(
						function(){
							this.src = $(this).data('image').hoverSrc;
						},
						function(){
							this.src = $(this).data('image').defaultSrc;
						}
					);
			}

		});

		// set rollover on sidebar & main contents.
		$('#sidebar01 img[class=over], #content01 img[class=over]').each(function(){
			// preload image.
			var img = new Image();
			img.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2');
			$(this)
				// store the image sources.
				.data('image', {
					defaultSrc: this.src,
					hoverSrc: img.src
				})
				.hover(
					function(){
						this.src = $(this).data('image').hoverSrc;
					},
					function(){
						this.src = $(this).data('image').defaultSrc;
					}
				);
		});

		// reset z-index for transparented gnav.
		$('#gnav01').css({zIndex: 16});

		// set active status.
		$('img[class=active]').each(function(){
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_active$2');
		});

		// smooth scroll.
		$('a[href=#top]').click(function(){
			$.scrollTo(this.hash, 512);
			return false;
		});

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

		// set common group box behavior.
		$('#content01 div.group div.item').each(function(){
			var bg = $(this).css('backgroundImage');

			// preload image.
			var img = new Image();
			img.src = $(this).find('a img').attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_over$2');

			$(this)
				// store images.
				.data('background', {
					defaultSrc: bg,
					hoverSrc: bg.replace(/^url\((.*)(\.[a-z\"]+)\)$/, 'url($1_over$2)')
				})
				.data('image', {
					defaultSrc: $(this).find('a img').attr('src'),
					hoverSrc: img.src
				})
				// mouse hover.
				.hover(
					function(){
						$(this)
							.css({backgroundImage: $(this).data('background').hoverSrc})
							.find('a img').attr('src', $(this).data('image').hoverSrc);
					},
					function(){
						$(this)
							.css({backgroundImage: $(this).data('background').defaultSrc})
							.find('a img').attr('src', $(this).data('image').defaultSrc);
					}
				)
				// expand clickable area.
				.css({cursor: 'pointer'})
				.click(function(){
					var a = $(this).find('a');
					if (a.length > 0)
						location.href = $(a).attr('href');
				});
		});
	});

	/*
	 * replace the original background with scrollable background.
	 */
	function initializeHeaderBg() {
		var header = $('#header01');
		if (header.length != 1)
			return;

		var bg = $(header).css('backgroundImage').match(/^url\((.*)\)$/) || [];
		if (bg.length < 1)
			return;

		// create 1st background.
		var bg1st = $('<div />');
		$(bg1st).css({
			position: 'absolute',
			top: '-32px',
			left: 0,
			opacity: 0,
			width: $(header).width(),
			height: $(header).height(),
			background: 'url('+ bg[1].replace(/^(.+)(\.[a-z\"]+)$/, '$101$2') +') no-repeat'
		});

		// create 2nd gackground.
		var bg2nd = $('<div />');
		$(bg2nd).css({
			position: 'absolute',
			top: 0,
			left: 0,
			width: $(header).width(),
			height: $(header).height(),
			background: 'url('+ bg[1].replace(/^(.+)(\.[a-z\"]+)$/, '$102$2') +') no-repeat'
		});

		// for ie6.
		if ($.browser.msie && (parseInt($.browser.version) < 7))
			$(bg2nd).fixpng();

		// set header background.
		$(header)
			.css({backgroundImage: 'none'})
			.prepend(bg2nd)
			.prepend(bg1st)
			.find('div:first').animate({top: 0, opacity: 1}, 1000);

		// scroll behavior.
		$(window).scroll(function(){
			var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			scrollTop = Math.floor(scrollTop / $('#header01').height() * 32);
			$('#header01 > div:eq(1)').css({top: scrollTop});
		});
	}

	/*
	 * light weight pngfix.
	 * notes: this function doesn't filter a agent.
	 */
	$.fn.fixpng = function() {
		var filterName = 'DXImageTransform.Microsoft.AlphaImageLoader';
		return (
			this.each(function(){
				if ((this.tagName.toLowerCase() == 'img') && (/\.png/i).test(this.src)) {
					// wrap filtered <span />.
					$(this)
						.wrap('<span />').parent()
						.css({
							display: 'inline-block',
							width: $(this).width(),
							height: $(this).height(),
							filter: 'progid:'+ filterName +'(sizingMethod="scale",src="'+ this.src +'")'
						})
						.attr('title', this.alt);

					// set rollover.
					if ($(this).hasClass('over')) {
						var span = $(this).parent()[0];
						$(span)
							// store the image sources.
							.data('image', {
								defaultSrc: span.filters[filterName].src,
								hoverSrc: span.filters[filterName].src.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2')
							})
							// hover handler.
							.hover(
								function(){
									this.filters[filterName].src = $(this).data('image').hoverSrc;
								},
								function(){
									this.filters[filterName].src = $(this).data('image').defaultSrc;
								}
							);
					}
					// switch active state.
					if ($(this).hasClass('active')) {
						var span = $(this).parent()[0];
						span.filters[filterName].src = span.filters[filterName].src.replace(/^(.+)(\.[a-z]+)$/, '$1_active$2');
					}

					// remove original image.
					$(this).remove();

				} else {
					// clear background-image & set filter.
					var bg = $(this).css('backgroundImage').match(/^url\((.*)\)$/);
					if (bg && bg.length) {
						$(this).css({
							backgroundImage: 'none',
							filter: 'progid:'+ filterName +'(sizingMethod="scale",src='+ bg[1] +')'
						});
					}
				}
			})
		);
	}

})(jQuery);

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);
