var g_rankid ;

/* ---- 初期表示の時だけ、トップに違う画像を表示する設定(例:年始) ---------------- */
var exclude_image = "/eshop/jqImg/new_year.jpg";
var exclude_alt  = "新年のご挨拶";
var exclude_link = "/";
var exclude_from = 101;		//  [対象月 x 100 + 対象日] 以上  ex) 1月1日＝101
var exclude_end  = 105;		//  [対象月 x 100 + 対象日] 以下  ex) 1月3日＝103
/* ------------------------------------------------------------------------------- */


function createPromArea(rankid) {

	  g_rankid = rankid;

      /* ---- bg of main slide ---- */
	  $('#slide').css('backgroundColor','#333');

	  loadSettings(rankid);

      timer = 0;
      current = 1;
      thumb = 1;

	  /* ---- setting for image change speed ---- */
      intervals = 6400;
	  ani_time = 0;	// 600
      speed_fadein = 200;

      pictures = $('#thumbs div img').length;
      width = $('#p1 img').outerWidth();
	
	/* ----
      $('#thumbs div img').animate({ opacity: 0.6 });
      $('#thumbs #b1 img').animate({ opacity: 1 });
	---- */
      $('#thumbs div img').animate({ opacity: 1 }, 100);
      $('#thumbs #b1 img').animate({ opacity: 0.6 }, 100);


      $('#thumbs #b1 img').css("borderColor", "#D4D4D6");

      $('#p1').animate({ "left": "0px" }, ani_time, "swing");
   /* $('#loading').css("display", "none"); */
	
      timer = setTimeout("autoPlay()", intervals);

	/* --------------
      $("#next").click(function () {
        thumb = current;
        current++
        if (current == (pictures + 1)) { current = 1 }
        animateLeft(current, thumb)
      });

      $("#previous").click(function () {
        thumb = current;
        current--
        if (current == 0) { current = pictures }
        animateRight(current, thumb)
      });
	------------------ */

      $("#thumbs div").click(function () {

	  /* ------------------------------------------------ */
	  /* reset Exclude Image when thumb's image clicked   */
	  /* ------------------------------------------------ */
	  if (flag_exclude) {
		resetFirstImage();
	  }

        thumb = current;
        clickButton = $(this).attr('id');
        current = parseInt(clickButton.slice(1));
        if (current > thumb) { animateLeft(current, thumb) }
        if (current < thumb) { animateRight(current, thumb) }
      });

      $("#main1").mouseover(function () { clearTimeout(timer); });
      $("#main1").mouseout(function () { timer = setTimeout("autoPlay()", intervals); });


	/* -- campaign fade -- */
	$(".b_set img").fadeTo(0,1.0);
	$(".b_set img").hover(function(){
		$(this).fadeTo(300,0.6);
	}, function(){
        $(this).fadeTo(300,1.0);
    });

	/* -- when img is clicked -- */
	$(".b_set img").click(function (){
		$(this).fadeTo(150,1.0);
	});

}


    function animateLeft(current, thumb) {

      $('#p' + current).css("left", width + "px");
	  $('#p' + current).fadeOut("fast");
      $('#p' + current).animate({ "left": "0px" }, ani_time, "swing");
	  $('#p' + current).fadeIn(speed_fadein);

      $('#p' + thumb).animate({ "left": -width + "px" }, ani_time, "swing");

      setbutton();
    }


    function animateRight(current, thumb) {
      $('#p' + current).css("left", -width + "px");
	  $('#p' + current).fadeOut("fast");
      $('#p' + current).animate({ "left": "0px" }, ani_time, "swing");	// 400
	  $('#p' + current).fadeIn(speed_fadein);

      $('#p' + thumb).animate({ "left": width + "px" }, ani_time, "swing");

      setbutton();
    }


    function setbutton() {
	/* ----
      $('#b' + thumb + ' img').animate({ opacity: 0.6 })
						.css("borderColor", "#999");
      $('#b' + current + ' img').animate({ opacity: 1 })
						.css("borderColor", "#999");
	---- */

      $('#b' + thumb + ' img').animate({ opacity: 1 },100)
						.css("borderColor", "#999");

      $('#b' + current + ' img').animate({ opacity: 0.6 },100)
						.css("borderColor", "#999");

    }  

    function autoPlay() {

      thumb = current;
      current++
      if (current == (pictures + 1)) { current = 1 }
	  
	  /* ------------------------------------------------------- */
	  /* reset Exclude Image when current thumb's image is [4]   */
	  /* ------------------------------------------------------- */
	  if (flag_exclude==true) {
		resetFirstImage();
		$("#b1").trigger("click");    // event trigger
	  }else{
		animateLeft(current, thumb);
	  }

	  // 再ロードチェック
	  var chkStr = $('#p' + current + " img").attr("src").toString();
	  if (chkStr.indexOf("eshop/t_bg.gif",0) > -1) {
	  	  loadSettings(g_rankid);
	  }

      timer = setTimeout("autoPlay()", intervals);

    }

	function loadSettings(rankid) {

		var x_path = "not_mb";
		switch(rankid) {
			case "NMB":
				x_path = "not_mb";
				break;
			case "MBR":
				x_path = "mb";
				break;
			case "GMB":
				x_path = "gold_mb";
				break;
			case "GPR":
				x_path = "gp";
				break;
			case "SEC":
				x_path = "se";
				break;
			default:
				x_path = "not_mb";
				break;
			
		}

		$.ajax({ 
		    url: "/eshop/xml/" + x_path + "/data_new.xml" + "?" +(new Date()).getTime(), 
			cache : false,
			async: true,
		    type: "GET",
		    dataType: "xml",
			timeout: 500,
		    success: function(xml) { 
				parseXml(xml); 
			} 
		}); 
	}


	/* ------------- for Exclude! ----------------------- */
	var flag_exclude = false;
	var p1_img_back = "";
	var p1_alt_back = "";
	var p1_lnk_back = "";

	/* ------------- parse of XML file ------------------ */
	var b_id = 0;
	var p_id = 0;

	function parseXml(xml) { 
		$(xml).find("Products").find("item").each(function() {
			b_id++;
			p_id++;

			$('#p' + p_id).attr("href", $(this).attr("url"));
			$('#p' + p_id + " img").attr("src", $(this).attr("image"));
			$('#b' + b_id + " img").attr("src", $(this).attr("thumb"));

			// alt setting
			$('#p' + p_id + " img").attr("alt", $(this).attr("alt"));
			$('#b' + p_id + " img").attr("alt", $(this).attr("alt"));

		});

		// for Exclude settings
		var currDay = new Date();
		var mn = currDay.getMonth();
		var dt = currDay.getDate();
		var md = (1 + mn) * 100 + dt;

		/*  -------- detect date -------- */
		if (md >= exclude_from && md <= exclude_end){

			flag_exclude = true;

			// backup of XML settings!
			p1_img_back = $('#p1 img').attr("src");
			p1_alt_back = $('#p1 img').attr("alt");
			p1_lnk_back = $('#p1').attr("href");

			// set for html object
			$('#p1 img').attr("src", exclude_image);
			$('#p1 img').attr("alt", exclude_alt);
			$('#p1').attr("href", exclude_link);

		}
	};

	/* ----- reset 1stTab Image for XML settings ------ */
	function resetFirstImage() {
		$('#p1 img').attr("src", p1_img_back);
		$('#p1 img').attr("alt", p1_alt_back);
		$('#p1').attr("href", p1_lnk_back);
		flag_exclude = false;

	}

