/*
======== table of content. =================================

Description: change portfolio image
Update: 2010/06/24-
Author: Japan Electronic Industrial Arts Co.Ltd.
        http://jeia.co.jp/

============================================================
*/

new function() {
	
	var selectedClass = 'selected';
	
	if ( typeof jQuery == 'undefined' ) {
		return;
	}
	
	$(document).ready( function() {
		init();
	});
	
	
	/**
	 * 初期化
	 */
	function init() {
		
		// image
		jQuery( '#image_list' ).
		find( '.image' ).
			css({ display: 'block' }).
			hide().
		end().
		find( '.prev a, .next a' ).
			bind( 'click', onClickThumb ).
			bind( 'mouseenter', startFollow ).
			bind( 'mouseleave', endFollow ).
			find( 'span' ).
				css({ display: 'block' }).
				each( function() {
					if ( typeof( DD_belatedPNG ) != 'undefined' ) {
						DD_belatedPNG.fixPng( this );
					}
				}).
				hide().
			end().
		end();
		
		// thumb
		jQuery( '#thumb_list ul li a' ).bind( 'click', onClickThumb );
		
		showImage( '#image-1' );
	}
	
	
	/**
	 * startFollow
	 */
	function startFollow( _e ) {
		
		$(this).
		unbind( 'mousemove', follow ).
		bind( 'mousemove', follow ).
		find( 'span' ).fadeIn();
		
		return false;
	}
	
	
	/**
	 * endFollow
	 */
	function endFollow() {
		
		$(this).
		unbind( 'mousemove', follow ).
		find( 'span' ).fadeOut();
		
		return false;
	}
	
	
	/**
	 * follow
	 */
	function follow( _e ) {
		
		var mouseY = _e.pageY - $(this).offset().top;
		
		$(this).find( 'span' ).css({
			top: mouseY + 'px'
		});
	}
	
	
	/**
	 * click thumb
	 */
	function onClickThumb( _e ) {
		
		try {
			$(this).get(0).blur();
		}
		catch(_e){}
		
		var href = $(this).attr( 'href' );
		var selectId = href.substr( href.indexOf( '#' ) );
		
		showImage( selectId );
		
		return false;
	}
	
	
	/**
	 * show
	 */
	function showImage( _selector ) {
		
		// image
		var $image = $( _selector );
		
		if ( $image.length > 0 ) {
			
			// alt text
			var text = $image.find( 'img' ).attr( 'alt' );
			
			$( '#image_alt' ).html( '<p>' + text + '</p>' ).
				find( 'p' ).hide().fadeIn();
			
			// change image
			$image.hide().appendTo( $image.parent().get(0) ).fadeIn();
		}
		
		// thumb
		var $thumb = $( '#thumb_list ul li a' );
		
		$thumb.removeClass( selectedClass ).
			filter( '[href='+_selector+']' ).addClass( selectedClass );
		
		// next
		var $next = $thumb.filter( '.'+selectedClass ).parent().next().children( 'a' );
		
		if ( ! $next.length ) {
			$next = $thumb.filter( ':first' );
		}
		
		$( '#image_list .next a' ).attr( 'href', $next.attr( 'href' ) );
		
		// prev
		var $prev = $thumb.filter( '.'+selectedClass ).parent().prev().children( 'a' );
		
		if ( ! $prev.length ) {
			$prev = $thumb.filter( ':last' );
		}
		
		$( '#image_list .prev a' ).attr( 'href', $prev.attr( 'href' ) );
	}
}

