/**
 * @requires jQuery 1.4.1+
 * @requires jQuery Easing Plugin
 *
 */
jQuery.noConflict();
jQuery(function($) {

	// for Automation
	var enableAutomation = true;
	var interval	= 6500;

	// settings
	var duration	= 1200;
	var easing	= 'easeOutExpo';

	// elements
	var $wrapArea = $('#mainimage_area');
	var $listArea = $('#mainimage_imgs');
	var $list = $('ul', $listArea);

	// defs
	var firstRunner = $('li.start', $list).index();
	if( firstRunner < 0 ) firstRunner = 0;

	var current = firstRunner;
	var itemNum = $('li', $list).length;
	var showedIndex = itemNum + firstRunner;

	var _start = ( -880 * showedIndex + 45 ) + 'px';
	function resetListStyles() {
		$list.css({
			'left'		: _start,
			'width'		: ( 880 * itemNum * 3 ) + 'px'
		});
		$('li', $list).css({
			'display'	: 'block'
		});
	};

	var _timer;
	function setListInterval() { 
		clearInterval(_timer);
		_timer = setInterval(function() { moveList(+1); }, interval);
	};

	// init
	$listArea.hide(0, resetListStyles);

	// list
	var $even = $('li', $list); 
	$even.clone().appendTo($list);
	$even.clone().prependTo($list);

	// mask
	// $('<p id="mainimage_mask-l" class="mainimage_mask" />').appendTo($listArea);
	// $('<p id="mainimage_mask-r" class="mainimage_mask" />').appendTo($listArea);
	// var $mask = $('.mainimage_mask');

	if( itemNum > 1 ) {
		// btns
		var $_prev = $('<p id="mainimage_prev" class="mainimage_arrow"><a href="#">前へ</a></p>').appendTo($wrapArea);
		var $_next = $('<p id="mainimage_next" class="mainimage_arrow"><a href="#">次へ</a></p>').appendTo($wrapArea);
		var $_dots = $('<ul id="mainimage_dots">'
			+ (function(n) { for( var i = 0, tmp = ''; i < n; i++ ) tmp += '<li><a href="#">' + i + '</a></li>'; return tmp; })(itemNum)
			+ '</ul>').appendTo($wrapArea);

		var $prev = $('a', $_prev);
		var $next = $('a', $_next);
		var $dots = $('a', $_dots);
		$dots.eq(firstRunner).addClass('current');
		$dots.hide();

		// bind
		$.each([$prev, $next, $dots], function(i,v){
			v.click(function(e) { e.preventDefault(); });
			// v.focus(function(e) { this.blur(); }); // hide outline for IE
		});

		$next.click(function() {
			moveList(+1);
		});

		$prev.click(function() {
			moveList(-1);
		});

		$dots.each(function(i) {
			$(this).click(function() {
				moveList( i - current );
			});
		});
	}

	function moveList(n) {
		if ( n === 0 ) return;

		// reset event
		$list.stop(true, true);
		if( enableAutomation ) setListInterval();

		// set current
		current += n;

		if ( current >= itemNum ) {
			current -= itemNum;
		} else if ( current < 0 ) {
			current += itemNum;
		}

		$dots.removeClass('current');
		$dots.eq(current).addClass('current');

		// remove mask
		// $mask.stop(true, true).fadeOut(100);

		// moveList(n)
		var l = ( 880 * n );
		var w = Math.abs(l);

		$list.animate({left: '-=' + l + 'px', width: '+=' + w + 'px'}, duration, easing,
			function() {
				if ( n > 0 ) {
					var $img = $('li:lt(' + n + ')', this);
					$img.clone().appendTo($list);
				} else {
					var $img = $('li:gt(' + ( itemNum + n - 1 ) + ')', this);
					$img.clone().prependTo($list);
				}
				$img.remove();

				// set mask
				// $mask.fadeIn(300);

				resetListStyles();
			}
		);
	};

	// show!
	$listArea.delay('200').fadeIn('200', function() {
		if( itemNum > 1 ) {
			$dots.fadeIn('200');
			if( enableAutomation ) setListInterval();
		} else {
			return;
		}
	});
});

jQuery(function($) {

	// for Automation
	var enableAutomation = false;
	var firstDelay	= 4000;
	var interval	= 4000;

	// settings
	var duration	= 6000;
	var easing	= 'easeOutExpo';

	var $wrapArea = $('#bannerlist');
	var $list = $('#bannerlist_banners');

	var num = $('ul', $list).size();
	var cur = 0;

	$list.css({
		'width'	: ( 930 * num ) + 'px'
	});

	var $_prev = $('<p id="bannerlist_prev" class="bannerlist_arrow"><a href="#">前へ</a></p>').appendTo($wrapArea);
	var $_next = $('<p id="bannerlist_next" class="bannerlist_arrow"><a href="#">次へ</a></p>').appendTo($wrapArea);

	var $prev = $('a', $_prev);
	var $next = $('a', $_next);

	$.each([$prev, $next], function(i,v){
		v.click(function(e) { e.preventDefault(); });
	});
	$prev.click(function() {
		moveList(-1, false);
	});
	$next.click(function() {
		moveList(+1, false);
	});

	var _timer;
	function setListInterval(int) { 
		clearInterval(_timer);
		_timer = setInterval(function() { moveList(+1, true); }, int);
	};

	function moveList(n, rotation) {
		if ( n == 0 || cur + n < 0 ) {
			return;
		} else if ( cur + n >= num ) {
			if ( rotation ) {
				n = ( cur + n - num ) - cur;
				duration+=1500;
			} else {
				return;
			}
		}

		cur += n;
		$list.stop(true, true).animate({left: '-=' + 930 * n + 'px'}, duration, easing);

		if( enableAutomation ) setListInterval( interval + duration );
	};

	if( enableAutomation ) setListInterval(firstDelay);
});

/**
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright c 2008 George McGinley Smith
 * All rights reserved.
 *
 */
jQuery.extend(jQuery.easing, {
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});

