
var tgh = {};

tgh.slideshow ={

	param : {

			url 		: 'images/index_mainimg/',
			interval 	: 4000,
			fadeTime	: 1000

	},


	//----------------------------------------------------
	// 	initialize
	//----------------------------------------------------

	init : function(){

		var param = this.param;

		var timer;	
		var $mainimg 	= $("div#slideshow img");
		var $active		= $mainimg.filter(":last");
		var $next;
		var $container 	= $("div#slideshow");
		var $stage		= $("div#mainimg");
		var max 		= $mainimg.length - 1;

		this.callback  = null;

		$mainimg.not(":last").fadeTo(0,0);
		$(window).bind('resize', onresize);



		function loop(){

			if($next)	$active = $next;
			$next 	= $active.prev();

			if($next.length == 0)
			{
				$next  = $mainimg.filter(":last");
				$next.stop().fadeTo( param.fadeTime, 1, function(){ $active.fadeTo(0,0) } );
			}
			else
			{
				$active.stop().fadeTo( param.fadeTime , 0 );
				$next.stop().fadeTo( 0,1 );
			}

			if(tgh.slideshow.callback != null)	tgh.slideshow.callback(max - $mainimg.index($next))
		}


		function onresize(event){

			var ww = $stage.width();
			if(ww > 950)
			{
				$container.css("left", (ww - 1200) / 2 + "px");
			}
		}




		//----------------------------------------------------
		// 	public method
		//----------------------------------------------------

		this.start = function(){

			timer = setInterval( loop, param.interval );
		};


		this.stop = function(){

			clearInterval(timer);
			timer = null;
		}

		this.change = function(index){

			if(timer != null)	this.stop();

			$active.stop().fadeTo( 0,0 );
			if($next) $next.stop().fadeTo( 0,0 );

			$next = null;
			$active  = $mainimg.eq(index).stop().fadeTo( param.fadeTime / 2, 1 );

			this.start();
		}


		onresize();
		this.start();
	}
}
