/*!
 * Bnner Ticker for jQuery
 * Copyright 2011 ITNET Inc. (http://www.itn.co.jp/) 
 * 
 * Date: 2011-04-06
 * Rev:  1.0.01
 * Rev:  1.1.01 
 */

$(function() {

	//自動移動感覚
	var autoSlideMin = 2000;

	//左端の初期画像
	var current_ID = 0;

	//表示する画像数
	var preview_num = 5;

	//画像の総数
	var totalBnners	= $("#paBanners").children().size();

	//スライド距離
	var movingDistance = 217;

	//フレームの全体サイズ
	var movingFlameSize = movingDistance * (totalBnners + 1);

	//メイン画像の接頭詞
	var mainIMGhead = "L";
	//メイン画像のID
	var mainIMGid = "#mainImg li";

	//現在位置
	var nowPointer = 0;

	//その他
	var LI_CLASS_E = "</li>\n";
	var DEFmovingSpan = movingDistance * -1;
	var current_potision = 0;
	var hoverOut = "#paSlider";

	//mian画像固定の値
	var MainMasterID = "imgLink3";

	//元のCSS
	var HiddenWidthSize = movingFlameSize;
	changeWidth("#paBanners",HiddenWidthSize);

	//Main画像、全てhideし、最後尾に先頭を追加→show
	//$("#mainImg li").eq("+PreviewMainNow+")")
	var PreviewMainNow = $("#mainImg li:eq(0)").attr("id");
	firstObj = $("#"+PreviewMainNow).html();
	firstObj = '<li id="mainImgViwe">' + firstObj + '</li>';
	$('#mainImg').append(firstObj);
	$("#mainImg li").hide();
	$("#mainImgViwe").fadeIn("0");
	$("#mainImg li").css({"position":"absolute","top":"0px"});

	//previewの配列初期化
	var preview_ary = new Array();
	for(i = 0; i < totalBnners; i++){
		preview_ary[i] = $('.item_'+i).html();
//		preview_ary[i] = '<li class="item'+ i +'">' + $('.item_'+i).html() + "</li>";
	}

	$("#paLeft").click(function(){ bnnerMove('left'); });
	$("#paRight").click(function(){ bnnerMove('right'); });
	$("#paLeft").mouseover(function(){clearTimeout(timerObj); });
	$("#paRight").mouseover(function(){clearTimeout(timerObj); });
	$("#paLeft").mouseout(function(){timerMove(); });
	$("#paRight").mouseout(function(){timerMove(); });

	$("#paBanners li").mouseover(function(){
		$("#paBanners li").removeClass("current");
		$(this).addClass("current");
		clearTimeout(timerObj);
		linkId = $(this).attr('id');
		cahgeMainIMG(linkId);
	});

	$(hoverOut).hover(
	  function () {
		;
	  },
	  function () {
			$("#paBanners li").removeClass("current");
			$("#paBanners li:first-child").addClass("current");
			cahgeMainIMG(MainMasterID);
	  }
	);
	$("#paBanners li").mouseout(function(){ 
		timerMove();
	});

	$("#kotei li").mouseover(function(){ 
		$("#kotei li").removeClass("current");
		$(this).addClass("current");
		clearTimeout(timerObj);
		linkId = $(this).attr('id');
		cahgeMainIMG(linkId);
	});
	$("#koteiSlider").hover(
	  function () {
		;
	  },
	  function () {
			$("#kotei li").removeClass("current");
			$("#kotei li:first-child").addClass("current");
			cahgeMainIMG(MainMasterID);
	  }
	);
	$("#kotei li").mouseout(function(){ 
		timerMove();
	});


	//タイマーセット
	var timerObj;
	$("#paBanners li:eq("+current_potision+")").addClass("current");
	timerMove();

//----------------------------------------------
//----------------------------------------------

	function bnnerMove(direction) {


		$("#imgLink" + current_ID).removeClass("current");

		//右スライド
		if(direction == 'left'){
			nowPointer = nowPointer - 1;
			current_ID = current_ID - 1;
			nextPointer = nowPointer;
			delPointer = nowPointer + totalBnners;
			if(current_ID < 0){
				current_ID = totalBnners - 1;
			}
			movingPointer = movingDistance * nowPointer;

			add_ID = current_ID;
			//左に画像追加
			LI_CLASS = '<li class="item_'+ nextPointer +'" id="imgLink' + add_ID + '">';
			ADD_PANNEL = LI_CLASS + preview_ary[add_ID] + LI_CLASS_E;
			//右端を一つ消す
			DEL_PANNEL = "#paBanners ."+"item_"+delPointer;
			movingPointer = movingDistance * 1;

			$('#paBanners').prepend(ADD_PANNEL);
			movA = DEFmovingSpan + "px";

//			movingSpan = movingDistance * -1;
//			flameMove(movingSpan);
			param = "0px";
			$("#paBanners").animate(
					{"left": movA},
					{duration: 0}
			).animate(
				{"left": param},
				{duration: "slow"}
			);

			$(DEL_PANNEL).remove();

		}
		//左スライド
		if(direction == 'right'){
			nowPointer = nowPointer + 1;
			current_ID = current_ID + 1;
			nextPointer = nowPointer + totalBnners - 1;
			delPointer = nextPointer - totalBnners;
			if(current_ID >= totalBnners){
				current_ID = 0;
			}
			add_ID = current_ID + totalBnners - 1;
			if(add_ID >= totalBnners){
				add_ID = add_ID - totalBnners;
			}
			//左端を一つ消す
			DEL_PANNEL = "#paBanners ."+"item_"+delPointer;
			//右に画像追加
			LI_CLASS = '<li class="item_'+ nextPointer +'" id="imgLink' + add_ID + '">';
			ADD_PANNEL = LI_CLASS + preview_ary[add_ID] + LI_CLASS_E;
			movingPointer = movingDistance * -1;
			param = movingPointer + "px";
			$("#paBanners").animate(
				{"left": param},
				{duration: "slow"}
			).animate(
					{"left": "0"},
					{duration: 0,complete: function(){$(DEL_PANNEL).remove();}}
			);
			$('#paBanners').append(ADD_PANNEL);

		}

		$("#paBanners li").unbind();
		$("#paBanners li").mouseover(function(){ 
			$("#paBanners li").removeClass("current");
			$(this).addClass("current");
			clearTimeout(timerObj);
			linkId = $(this).attr('id');
			cahgeMainIMG(linkId);
		});

		$(hoverOut).hover(
		  function () {
			;
		  },
		  function () {
			$("#paBanners li").removeClass("current");
			$("#paBanners li:first-child").addClass("current");
			cahgeMainIMG(MainMasterID);
		  }
		);
		$("#paBanners li").mouseout(function(){ 
			timerMove();
		});

		//lightBox 
//		$('a.lbClass').lightBox();

		$("#imgLink" + current_ID).addClass("current");

//スライドでメイン画像を変えない仕様に変更
//		cahgeMainIMG("imgLink"+current_ID);


	}

	function changeWidth(target,movingSpan){
		param = movingSpan + "px";
		$(target).animate(
			{"width": param},
			{duration: 0}
		);
	}

	function flameMove(movingSpan){
		param = movingSpan + "px";
		$("#paBanners").animate(
			{"left": param},
			{duration: 0}
		);
	}
	function cssChange(obj){
		obj.addClass("current");
	}


	function cahgeMainIMG(IMGNO){
		if(PreviewMainNow != IMGNO){
			PreviewMainNow = IMGNO;

			nextObj = $("#"+IMGNO+"m").html();
//			nextObj = '<a href="' + ObjLink + '">' + nextObj + '</a>';
			nextObj = '<li id="mainImgViweNext" style="display:none">' + nextObj + '</li>';

			$('#mainImg').append(nextObj);

			$("#mainImgViwe").fadeOut("0");
			$("#mainImgViwe").css("display:none");

			$("#mainImgViweNext").fadeIn("0",
				function(){
					nextImgName = $("#mainImgViweNext img").attr("src");
					nextImgUrl = $("#mainImgViweNext a").attr("href");
					$("#mainImgViwe img").attr("src",nextImgName);
					$("#mainImgViwe a").attr("href",nextImgUrl);
					$("#mainImgViwe").show();
					$("#mainImgViweNext").remove();
					$("#mainImg li").css({"position":"absolute","top":"0px"});
				}
			);

		}

	}

	function mainImgLink(PARAM){
		//リンク先
		alert('ok');
	}

	function timerMove(){
		timerObj = setTimeout( function() {
			bnnerMove("right");
			timerMove();
		}, autoSlideMin);
	}
});

