/*==============================================
 ready
==============================================*/
$(function(){

	var ie = (function(){var undef,v=3,div=document.createElement('div'),all=div.getElementsByTagName('i');while(div.innerHTML='<!--[if gt IE '+(++v)+']><i></i><![endif]-->',all[0]);return v>4 ? v:undef;}());
	if(ie < 7){
		DD_belatedPNG.fix('.alphatrans');
	}	
	
	//custom easing
	$.easing.custom01 = function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b};
	
	//vars
	var $base     = $('#header div.visual_image'),
			$control  = $('ul.control', $base),
			$prev     = $control.children('li.prev'),
			$next     = $control.children('li.next'),
	    $images   = $('ul.main_image>li', $base),
	    imglen    = $images.length,
	    zindex    = parseInt($images.eq(0).css('z-index'), 10),
	    current   = 0,
			protect   = false,
			auto_timer,
			/* config */
			maxlen    = 5,
			move_x    = 30,
			skip_time = 3500,
			skip_1st  = 4500;

	function changeImage(move){
		if(protect || !move) return;
		clearTimeout(auto_timer);
		protect = true;
		var goal, pos;
		if(move > 0){
			goal = (current+move >= imglen) ? 0 : current+move;
			pos  = 1;
		} else {
			goal = (current <= 0) ? imglen-1 : current+move;
			pos  = -1;
		}
		$images
			.eq(current)
				.stop()
				.animate({opacity:0, left:(move_x*-pos/2)}, 700)
			.end()
			.eq(goal)
				.css({opacity:0, zIndex:zindex++, top:0, left:(move_x*pos)})
				.stop()
				.delay(650)
				.animate({opacity:1, left:0}, 1500, 'custom01', function(){
					protect = false;
					timerSet();
					current = goal;
				})
		;
	}

	function timerSet(msec){
		clearTimeout(auto_timer);
		auto_timer = setTimeout(function(){changeImage(1)}, (msec || skip_time));
	}
			
	function init(){
		$control.show();
		$prev.click(function(){
			changeImage(-1);
		});
		$next.click(function(){
			changeImage(1);
		});
		$images.not(':first').css({opacity:0});
		timerSet(skip_1st);
	}
	
	init();
	
});
