$.fn.main_slider = function(type, settings) {

	// 初期設定
	settings = $.extend({
		
		contWidth     : null,            // コンテンツの横幅
		contHeight    : null,            // コンテンツの高さ
		contClass     : '.slideUnit',    // スライドのクラス
		slideInterval : 5000,            // スライド間隔（ms）
		slideEasing   : "linear",        // エフェクトのeasing
		slideDuration : 500,

		fixWidth     : false,            // コンテンツの横幅固定（falseのときcontWidthもしくは一枚目のスライドを横幅とする）
		fixHeight    : false,            // コンテンツの高さ固定（falseのときcontHeightもしくは一枚目のスライドを縦幅とする）

		panelCount    : 10,              // パネルの分割数
		panelSize     : 100              // パネルの分割幅（panel）
		
	}, settings);



	/*
	 * 初期値
	 */
	var currentNum  = 0;          // 現在のスライド
	var nextNum     = 0;          // 次に表示するスライド
	var contNum     = null;       // スライドの枚数
	var currentFlag = false;      // スライド動作フラグ
	var naviFlag    = false;      // ナビ動作フラグ



	var timeId      = null;
	var depth       = 1;
	var nowTarget   = null;
	var nextTarget  = null;


	return this.each(function(){
		$(this).css('position', 'relative');
		$(this).css('z-index',  depth);

		contNum = $(settings.contClass).length;
		baseObj = $(settings.contClass).get(currentNum);

		$(settings.contClass).each(function(){
			this.defaultWidth = $(this).width();
			if (settings.fixWidth == true) {
				this.defaultWidth = $(baseObj).width();
				$(this).css('overflow', 'hidden');
			}
			if (settings.contWidth != null) {
				this.defaultWidth = settings.contWidth;
				$(this).css('overflow', 'hidden');
			}

			this.defaultHeight = $(this).height();
			if (settings.fixWidth == true) {
				this.defaultHeight = $(baseObj).height();
			}
			if (settings.contHeight != null) {
				this.defaultHeight = settings.contHeight;
			}

			$(this).css('position', 'absolute');
			$(this).css('width',  this.defaultWidth   + 'px');
			$(this).css('height', this.defaultHeight  + 'px');
			$(this).css('overflow', 'hidden');

			$(this).css('opacity', 0);
			depth++;
			$(this).css('z-index', depth);
		});

		if($(settings.contClass).length > 0) {
			if (settings.contWidth == null) {
				settings.contWidth = $(settings.contClass).eq(0).width();
			}

			if (settings.contHeight == null) {
				settings.contHeight = $(settings.contClass).eq(0).height();
			}

			$(settings.contClass).eq(0).parent().css('position', 'relative');
			$(settings.contClass).eq(0).parent().css('width', settings.contWidth + 'px');
			$(settings.contClass).eq(0).parent().css('height', settings.contHeight + 'px');
		}

		var pw = Math.ceil(settings.contWidth / settings.panelCount);
		var ph = Math.ceil(settings.contHeight / settings.panelCount);

		/*
		 * set navigation
		 */
		$(this).append('<ul class="slideNavi"></ul>');
		$(this).children('.slideNavi').attr('id', 'mainBtn');
		$(this).children('.slideNavi').css('position', 'absolute');
		$(this).children('.slideNavi').css('z-index', 50000);
		
		for (i = 0; i < contNum; i++) {
			$(this).children('.slideNavi').append('<li><a>&nbsp;</a></li>');
		}
		$(this).children('.slideNavi').children().css('cursor', 'pointer');

		var par = $(this).children('.slideNavi');
		$(this).children('.slideNavi').children().click(function(){
			if (currentFlag == true && naviFlag == false) {
				nextNum = $(par).children().index(this);
				if (nextNum != currentNum) {
					clearTimeout(timeId);
					naviFlag = true;
					main();
				}
			}
		});

		/*
		 * set_panel
		 */
		$(settings.contClass).each(function(idx){
			if (type == 'noren' || type == 'koushi') {
				src = $(this).html();

				$(this).after('<li id="slidePanelerList' + idx + '"></li>');
				$('#slidePanelerList' + idx).css('position', 'absolute');
				$('#slidePanelerList' + idx).css('overflow', 'hidden');

				$('#slidePanelerList' + idx).css('z-index', 1);
				$('#slidePanelerList' + idx).append('<div></div>');
				$('#slidePanelerList' + idx).children().css('position', 'relative');
				$('#slidePanelerList' + idx).children().css('width', $(this).width() + 'px');
				$('#slidePanelerList' + idx).children().css('height', $(this).height() + 'px');

				for (i=0; i<settings.panelCount; i++) {
					$('#slidePanelerList' + idx + '>div').prepend('<div id="slidePaneler' + idx + '_' + i + '" class="slidePaneler' + idx + ' slidePaneler"></div>');
					$('#slidePaneler' + idx + '_' + i).html(src);
					$('#slidePaneler' + idx + '_' + i).css('position', 'absolute');
					$('#slidePaneler' + idx + '_' + i).css('overflow', 'hidden');
					$('#slidePaneler' + idx + '_' + i).css('margin-left', pw * i + 'px');
					$('#slidePaneler' + idx + '_' + i).children().css('margin-left', -pw * i + 'px');

					if (type == 'koushi') {
						$('#slidePaneler' + idx + '_' + i).css('width', pw + 'px');
						if (i%2 == 0) {
							$('#slidePaneler' + idx + '_' + i).css('margin-top', -settings.contHeight + 'px');
						} else {
							$('#slidePaneler' + idx + '_' + i).css('margin-top', settings.contHeight + 'px');
						}
					}  else if (type == 'noren') {
						$('#slidePaneler' + idx + '_' + i).css('opacity', 0);
						$('#slidePaneler' + idx + '_' + i).css('width', 50 + 'px');
					}
				}
			} else if (type == 'take' || type == 'wave') {
				src = $(this).html();

				$(this).after('<li id="slidePanelerList' + idx + '"></li>');
				$('#slidePanelerList' + idx).css('position', 'absolute');
				$('#slidePanelerList' + idx).css('overflow', 'hidden');

				$('#slidePanelerList' + idx).css('z-index', 1);
				$('#slidePanelerList' + idx).append('<div></div>');
				$('#slidePanelerList' + idx).children().css('position', 'relative');
				$('#slidePanelerList' + idx).children().css('width', $(this).width() + 'px');
				$('#slidePanelerList' + idx).children().css('height', $(this).height() + 'px');

				for (i=0; i<settings.panelCount; i++) {
					$('#slidePanelerList' + idx + '>div').prepend('<div id="slidePaneler' + idx + '_' + i + '" class="slidePaneler' + idx + '"></div>');
					$('#slidePaneler' + idx + '_' + i).html(src);
					$('#slidePaneler' + idx + '_' + i).css('position', 'absolute');
					$('#slidePaneler' + idx + '_' + i).css('overflow', 'hidden');
					$('#slidePaneler' + idx + '_' + i).css('height', ph + 'px');
					$('#slidePaneler' + idx + '_' + i).css('margin-top', ph * i + 'px');
					$('#slidePaneler' + idx + '_' + i).children().css('margin-top', -ph * i + 'px');

					if (type == 'take') {
						if (i%2 == 0) {
							$('#slidePaneler' + idx + '_' + i).css('margin-left', -settings.contWidth + 'px');
						} else {
							$('#slidePaneler' + idx + '_' + i).css('margin-left', settings.contWidth + 'px');
						}
					} else if (type == 'wave') {
						$('#slidePaneler' + idx + '_' + i).css('margin-left', -settings.contWidth + 'px');
					}

				}
			} else if (type == 'panel') {
				src = $(this).html();

				$(this).after('<li id="slidePanelerList' + idx + '"></li>');
				$('#slidePanelerList' + idx).css('position', 'absolute');
				$('#slidePanelerList' + idx).css('overflow', 'hidden');

				$('#slidePanelerList' + idx).css('z-index', 1);
				$('#slidePanelerList' + idx).append('<div></div>');
				$('#slidePanelerList' + idx).children().css('position', 'relative');
				$('#slidePanelerList' + idx).children().css('width', $(this).width() + 'px');
				$('#slidePanelerList' + idx).children().css('height', $(this).height() + 'px');

				maxCountX = Math.ceil(settings.contWidth / settings.panelSize);
				maxCountY = Math.ceil(settings.contHeight / settings.panelSize);

				if (maxCountX > maxCountY) {
					maxCount = maxCountX + maxCountY;
					minCount = maxCountY;
				} else {
					maxCount = maxCountX + maxCountY;
					minCount = maxCountX;
				}

				for (i = 0; i < maxCount; i++) {
					c = i;
					if (c > minCount-1) {
						c = minCount-1;
					}
					for (j=0; j<=c; j++) {
						$('#slidePanelerList' + idx).prepend('<div class="slidePaneler' + idx + '_' + i + ' slidePaneler' + idx + '"></div>');

						$('.slidePaneler' + idx + '_'  + i).eq(0).css('position', 'absolute');
						$('.slidePaneler' + idx + '_'  + i).eq(0).css('opacity', 0);
						$('.slidePaneler' + idx + '_'  + i).eq(0).css('overflow', 'hidden');
						$('.slidePaneler' + idx + '_'  + i).eq(0).css('width', settings.panelSize + 'px');
						$('.slidePaneler' + idx + '_'  + i).eq(0).css('height', settings.panelSize + 'px');
						$('.slidePaneler' + idx + '_'  + i).eq(0).css('margin-left', settings.panelSize * (i - j) + 'px');
						$('.slidePaneler' + idx + '_'  + i).eq(0).css('margin-top', settings.panelSize * j + 'px');

						$('.slidePaneler' + idx + '_'  + i).eq(0).html(src);
						$('.slidePaneler' + idx + '_'  + i).eq(0).children().css('margin-left', -settings.panelSize * (i -j) + 'px');
						$('.slidePaneler' + idx + '_'  + i).eq(0).children().css('margin-top', -settings.panelSize * j + 'px');
					}
				}
			}
		});

		main();

		function main() {
			if (naviFlag == false) { nextNum = currentNum+1; }
			if (nextNum >= contNum) { nextNum = 0; }
			if (currentFlag == false) { nextNum = 0; }

			if (currentFlag == true) {
				nowTarget = $(settings.contClass).get(currentNum);
				$($('.slideNavi').children().get(currentNum)).children('a').removeClass('navi_on');
			}

			nextTarget = $(settings.contClass).get(nextNum);
			$(nextTarget).css('z-index', depth);
			depth++;

			$($('.slideNavi').children().get(nextNum)).children('a').addClass('navi_on');

		//	if (currentFlag == false) {
		//		$(nextTarget).animate({'opacity' : 1 }, 100, settings.slideEasing);
		//		$('.slideNavi').css('opacity', 1);
		//	} else {
				$('.slideNavi').animate({'opacity' : 0.3 }, 500);

				if (type == 'fade') {
					fade();
				} else if (type == 'slide') {
					slide();
				} else if (type == 'panel') {
					panel();
				} else if (type == 'koushi') {
					koushi();
				} else if (type == 'take') {
					take();
				} else if (type == 'wave') {
					wave();
				} else if (type == 'gate') {
					gate();
				} else if (type == 'noren') {
					noren();
				}
		//	}

			currentNum = nextNum;
			if (currentNum >= contNum) { currentNum = 0; }
			if (currentFlag == false){
				currentNum = 0;
				currentFlag = true;
			}
			timeId = setTimeout(main, settings.slideInterval);
		};




		function noren() {
			$('#slidePanelerList' + nextNum).css('z-index', 40000);

			var i = 0;
			var mId = setInterval(function(){
				if (i < settings.panelCount) {
					naviFlag = true;
					$('#slidePaneler' + nextNum + '_' + i).css('opacity', 0.3);
					$('#slidePaneler' + nextNum + '_' + i).animate({ 'width' : pw + 'px', 'opacity' : 1 }, settings.slideDuration, settings.slideEasing);
				} else {
					if ( Math.ceil($('#slidePaneler' + nextNum + '_' + (settings.panelCount-1)).width()) >= pw) {
						if (nowTarget != false) {
							$(nowTarget).css('opacity', 0);
						}
						$(nextTarget).css('opacity', 1);

						$('#slidePanelerList' + nextNum).css('z-index', 1);
						$('.slidePaneler').css('width', 20 + 'px');
						$('.slidePaneler').css('opacity', 0);
						naviFlag = false;
						$('.slideNavi').animate({'opacity' : 1 }, 500);
						clearInterval(mId);
					}
				}
				i++;
			}, 100);
		};


		function gate() {
			var src = $(nextTarget).html();
			$(nextTarget).after('<li id="slidePanelerList"></li>');
			$('#slidePanelerList').css('position', 'absolute');
			$('#slidePanelerList').css('overflow', 'hidden');
			$('#slidePanelerList').css('z-index', 9999);
			$('#slidePanelerList').append('<div></div>');
			$('#slidePanelerList').children().css('position', 'relative');
			$('#slidePanelerList').children().css('width', $(nextTarget).width() + 'px');
			$('#slidePanelerList').children().css('height', $(nextTarget).height() + 'px');

			$(nextTarget).next().children().prepend('<div id="paneler"></div>');
			$('#slidePaneler').html(src);
			$('#slidePaneler').css('position', 'absolute');
			$('#slidePaneler').css('overflow', 'hidden');
			$('#slidePaneler').css('width', pw + 'px');
			$('#slidePaneler').css('margin-left', settings.contWidth/2 - pw/2 + 'px');
			$('#slidePaneler').css('text-indent', -settings.contWidth/2 + pw/2 + 'px');

			$('#slidePaneler').animate({ 'width' : settings.contWidth + 'px', 'marginLeft' : '0px', 'textIndent': '0px' }, settings.slideDuration, function(){
				if (nowTarget != false) {
					$(nowTarget).css('opacity', 0);
				}
				$('#slidePanelerList').remove();
				$(nextTarget).css('opacity', 1);
				$('.slideNavi').animate({'opacity' : 1 }, 500);
				naviFlag = false;
			});
		};


		function wave() {
			$('#slidePanelerList' + nextNum).css('z-index', 40000);

			var i = 0;
			var mId = setInterval(function(){
				if (i<settings.panelCount) {
					naviFlag = true;
					$('#slidePaneler' + nextNum + '_' + i).animate({ 'marginLeft' : '0px' }, settings.slideDuration, settings.slideEasing);
				} else {
					if ( $('#slidePaneler' + nextNum + '_' + (settings.panelCount-1)).css('margin-left') == '0px') {
						if (nowTarget != false) {
							$(nowTarget).css('opacity', 0);
						}
						$(nextTarget).css('opacity', 1);
						$('#slidePanelerList' + nextNum).css('z-index', 1);

						$('.slidePaneler' + nextNum).css('margin-left', -settings.contWidth + 'px');

						naviFlag = false;
						$('.slideNavi').animate({'opacity' : 1 }, 500);
						clearInterval(mId);
					}
				}
				i++;
			}, 100);
		};

		function take() {
			$('#slidePanelerList' + nextNum).css('z-index', 40000);

			var i = 0;
			var mId = setInterval(function(){
				if (i<settings.panelCount) {
					naviFlag = true;
					$('#slidePaneler' + nextNum + '_' + i).animate({ 'marginLeft' : '0px' }, settings.slideDuration, settings.slideEasing);
				} else {
					if ( $('#slidePaneler' + nextNum + '_' + (settings.panelCount-1)).css('margin-left') == '0px') {
						if (nowTarget != false) {
							$(nowTarget).css('opacity', 0);
						}
						$(nextTarget).css('opacity', 1);
						$('#slidePanelerList' + nextNum).css('z-index', 1);
						for (k=0; k<settings.panelCount; k++) {
							if (k%2 == 0) {
								$('#slidePaneler' + nextNum + '_' + k).css('margin-left', -settings.contWidth + 'px');
							} else {
								$('#slidePaneler' + nextNum + '_' + k).css('margin-left', settings.contWidth + 'px');
							}
						}
						naviFlag = false;
						$('.slideNavi').animate({'opacity' : 1 }, 500);
						clearInterval(mId);
					}
				}
				i++;
			}, 200);
		};


		function koushi() {
			$('#slidePanelerList' + nextNum).css('z-index', 40000);

			var i = 0;
			var mId = setInterval(function(){
				if (i < settings.panelCount) {
					naviFlag = true;
					 $('#slidePaneler' + nextNum + '_' + i).animate({ 'marginTop' : '0px' }, settings.slideDuration, settings.slideEasing);
				} else {
					if ($('#slidePaneler' + nextNum + '_' + (settings.panelCount-1)).css('margin-top') == '0px') {
						if (nowTarget != false) {
							$(nowTarget).css('opacity', 0);
						}
						$(nextTarget).css('opacity', 1);

						$('#slidePanelerList' + nextNum).css('z-index', 1);
						for (k=0; k<settings.panelCount; k++) {
							if (k%2 == 0) {
								$('#slidePaneler' + nextNum + '_' + k).css('margin-top', -settings.contHeight + 'px');
							} else {
								$('#slidePaneler' + nextNum + '_' + k).css('margin-top', settings.contHeight + 'px');
							}
						}
						$('.slideNavi').animate({'opacity' : 1 }, 500);
						naviFlag = false;
						clearInterval(mId);
						}
				}
				i++;
			}, 200);
		};

		function panel() {
			$('#slidePanelerList' + nextNum).css('z-index', 40000);

			maxCountX = Math.ceil(settings.contWidth / settings.panelSize);
			maxCountY = Math.ceil(settings.contHeight / settings.panelSize);

			if (maxCountX > maxCountY) {
				maxCount = maxCountX + maxCountY;
				minCount = maxCountY;
			} else {
				maxCount = maxCountX + maxCountY;
				minCount = maxCountX;
			}

			var i = 0;

			var mId = setInterval(function(){
				if (i<maxCount) {
					naviFlag = true;
					 $('.slidePaneler' + nextNum + '_' + i).animate({ opacity : 1 }, settings.slideDuration, settings.slideEasing);
				} else {
					if ($('.slidePaneler' + nextNum + '_' + (maxCount-1)).eq(0).css('opacity') == 1) {
						if (nowTarget != false) {
							$(nowTarget).css('opacity', 0);
						}
						$(nextTarget).css('opacity', 1);
						$('#slidePanelerList' + nextNum).css('z-index', 1);
						$('.slidePaneler' + nextNum).css('opacity', 0);

						naviFlag = false;
						$('.slideNavi').animate({'opacity' : 1 }, 500);
						clearInterval(mId);
					}
				}
				i++;
			}, 200);
		};



		function slide() {

			$(nextTarget).css('opacity', 1);

			$(nextTarget).children().css( 'margin-left' , -nextTarget.defaultWidth + 'px');

			$(nextTarget).children().animate({ marginLeft : 0 }, settings.slideDuration, settings.slideEasing, function(){
				$('.slideNavi').animate({'opacity' : 1 }, 500);
				naviFlag = false;
			});
		};


		function fade() {
			if (currentFlag != false) {
				$(nowTarget).animate({ opacity : 0 }, 1000, settings.slideEasing);
			}
			nextTarget = $(settings.contClass).get(nextNum);
			$(nextTarget).css('z-index', depth);
			depth++;
			$($('.slideNavi').children().get(nextNum)).children('a').addClass('navi_on');
			$(nextTarget).animate({ 'opacity' : 1 }, settings.slideDuration, settings.slideEasing, function(){
				$('.slideNavi').animate({'opacity' : 1 }, 500);
				naviFlag = false;
			});

		};
	});



};

