/*
	基本のJS
	作成日：080211
	更新日：080211
*/

$(function(){
	// 見出しの前にロケータがある場合
	$("div#mainContents div.locator h2:first-child").addClass("siblingA");//margin-top: 10px;
	$("div#mainContents div.locator h3:first-child").addClass("firstChild");//margin-top: 10px;
	$("div#mainContents div.locator h4:first-child").addClass("firstChild");//margin-top: 10px;
	$("div#mainContents div.locator + h2").addClass("siblingA");//margin-top: 10px;
	$("div#mainContents div.locator + h3").addClass("siblingA");//margin-top: 10px;
	$("div#mainContents div.locator + h4").addClass("siblingA");//margin-top: 10px;

	// 見出し要素の後ろにカラムがある場合
	$("div#mainContents h2 + div.locator h3:first-child").addClass("siblingB");//margin-top: 0;

	// 見出しが連続する場合
	$("div#mainContents h2 + h3").addClass("siblingB");//margin-top: 0;
	$("div#mainContents h3 + h4").addClass("siblingB");//margin-top: 0;

	// メインコンテンツ直後にh2が出現する場合
	$("div#mainContents h2:first-child").addClass("firstChild");//margin-top: 0;

	// lastChild
	$("div#platform li:last-child").addClass("lastChild");
	$("div#mainContents table td p:last-child").addClass("lastChild");
	$("div#mainContents table td ul:last-child").addClass("lastChild");
	$("div#mainContents table td ol:last-child").addClass("lastChild");
	$("div#mainContents table td dl:last-child").addClass("lastChild");
	$("div#mainContents table td ul li:last-child").addClass("lastChild");

	// directLastChild
	$("div#mainContents div.boxedItem > :last-child").addClass("directLastChild");

	// firstChild
	$("div#platform h3:first-child").addClass("firstChild");
	$("table.formTable tbody th:first-child").addClass("firstChild");
	$("div#mainContents div.locator div.columnPmA:first-child").addClass("firstChild");

	//インプット
	$("div#mainContents input[type='radio']").addClass("radioBtn");
	$("div#mainContents input[type='checkbox']").addClass("checkbox");

	/*
	 * ToggleFormText
	 *
	 * Author:   Grzegorz Frydrychowicz
	 * E-mail:   grzegorz.frydrychowicz@gmail.com
	 * Date:     16-11-2007
	*/
	$("input:text, textarea, input:password").each(function(){
			$(this).addClass('blur');
		if(this.value == ''){
			this.value = this.title;
				}else if(this.value != this.title){
					$(this).removeClass('blur');
				}
	});
	$("input:text, textarea, input:password").focus(function(){
       if(this.value == this.title){
						$(this).removeClass('blur');
            this.value = '';
				}
	});
	$("input:text, textarea, input:password").blur(function(){
		 if(this.value == ''){
						$(this).addClass('blur');
            this.value = this.title;
				}
	});
	$("input:image, input:button, input:submit").click(function(){
		clearSampleText();
	});

	// text select
	$("input.textSelect").click(function(){
		$(this).select();
	});

	//チェックボックス全てにチェック
	$('p#allcheck input').click(function(){
		if($(this).attr('checked')){
			$('div.article input').attr('checked','true');
	} else {
			$('div.article input').removeAttr('checked');
		}
	});

	//店舗詳細 プルダウンメニュー
	$('div#mainContents div.shopNav ul.nav li').not('li.current').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});


	//グルメ手帳 フォルダ プルダウンメニュー
	$('div#mainContents dl.pulldownLinks dt span span.btn').click(function(){
		if($(this).parent().parent().parent().css('z-index')=='1'){
			$(this).parent().parent().parent().css('z-index','0');
			$(this).parent().parent().parent().removeClass('actv');
		}else{
			$('div#mainContents dl.pulldownLinks').css('z-index','0');
			$('div#mainContents dl.pulldownLinks').removeClass('actv');
			$(this).parent().parent().parent().css('z-index','1');
			$(this).parent().parent().parent().addClass('actv');
		}
	});

	//ieのラベル処理
	if(navigator.userAgent.indexOf("MSIE") >= 0){
		$('div#mainContents label img').click(function(){
			var id = $(this).parent().attr("for");
			$('div#mainContents input#'+id).click();
		});

	}

	//特集ロールオーバー
	$('div#private p.image a img,div#party p.image a img').hover(function(){
		$(this).css('opacity', '0.7');
	},function(){
		$(this).css('opacity', '1');
	});
});

//おすすめレポート
function changeBody(yesbutton) {
	$('li#'+yesbutton).parent().after('<p class="message">ただいま処理中です。しばらくお待ちください・・・</p>');
	$('li#'+yesbutton).parent().remove();
}

// 画像ロールオーバー
$(function(){
	RLO.rollover.init();
});
RLO = {};
RLO.rollover ={
	init: function(){
		$("ul.tabNav img, .hover, div.shopNav img").not("img[@src*='_on.'],img[@src*='_cr.']").hover(
			function () { $(this).attr( 'src', RLO.rollover.newimage($(this).attr('src')) ); },
			function () { $(this).attr( 'src', RLO.rollover.oldimage($(this).attr('src')) ); }
		);
		},
	newimage: function( src ) { return src.substring( 0, src.search(/(\.[a-z]+)/) ).replace(/_on/, '') + '_on' + src.match(/(\.[a-z]+)/)[0]; },
	oldimage: function( src ){ return src.replace(/_on/, '');}
};

/* ちらつき回避 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


/*
 *
 * 関数
 *
*/

// 未入力の入力項目の例をクリアする
function clearSampleText() {
	$("input:text, textarea, input:password").each(function() {
		if(this.value == this.title && this.title != ''){
			this.value='';
		}
	});
}

//ブラウザ判定
function checkPlatform(){
	if((navigator.platform).indexOf("Mac") != -1){
		return "mac";
	}else if((navigator.platform).indexOf("Win") != -1){
		return "win";
	}else{
		return "other";
	}
}

function checkIE(){
	return ((navigator.appName).indexOf("Microsoft") != -1);
}

// 印刷ボタン
function hidePrintBtn(storeId, division){
	var srcText = "";
	srcText = "<ul class=\"btn\">";
	srcText += "<li id=\"printBtn\"><img id=\"btn_print_page\" src=\"/SYS/cmn/images/common/btn/btn_print_03.jpg\" alt=\"印刷する\" width=\"142\" height=\"31\" onClick=\"window.print(); return false;\" class=\"hover\" /></li>";
	srcText += "</ul>";
	document.write(srcText);
}

//印刷ボタンJSoff時hidden FAX用
function hideFaxPrintBtn(){
  var srcText = ""
  srcText = "<div class=\"actionBtn\">"
  srcText += "<ul>"
  srcText += "<li id=\"printBtn\"><img src=\"/SYS/cmn/images/common/btn/btn_print.gif\" alt=\"印刷する\" width=\"156\" height=\"29\" onClick=\"window.print();return false;\" class=\"hover\" /></li>"
  srcText += "</ul>"
  srcText += "<!-- /actionBtn --></div>";
  document.write(srcText);
}

function setAreaTop( service_area_cd ){
  tmp = "HPJ_SERVICE_AREA_CK" + "=" + escape( service_area_cd ) + "; ";
  tmp += "path=/;";
  tmp += "expires=Fri, 31-Dec-2030 23:59:59; ";
  document.cookie = tmp;
}

//common area
function submitForm(){
  var a = submitForm.arguments
  var f = a[0];
  var id ;
  var nextPage = "";
  if(a.length > 1) id = a[1];
  if(a.length > 2) nextPage = a[2];
    var errStr = "";
    switch(id){
    case 404:

// adding
    var isReserveUsageExist = false;
	if(!!f.reserveUsage) {
		isReserveUsageExist = true;
	}

    if(isReserveUsageExist && f.reserveUsage.selectedIndex == 0){
      errStr = "ご利用方法を選んでください。";
    }else if(f.personNum.value == ""){
      errStr = "人数を入力してください。";
    }else if(f.personName.title == f.personName.value || f.personName.value == ""){
      errStr = "代表者名（漢字）を入力してください。";
    }else if(f.personKanaName.title == f.personKanaName.value || f.personKanaName.value == ""){
      errStr = "代表者名（フリガナ）を入力してください。";
    }else if(f.tel.title == f.tel.value || f.tel.value == ""){
      errStr = "電話番号を入力してください。";
    }else if(f.telConfirm.value == ""){
      errStr = "確認用電話番号を入力してください。";
    }else if(f.mail.title == f.mail.value || f.mail.value == ""){
      errStr = "メールアドレスを入力してください。";
    }

// --adding

//    if(f.USAGE.selectedIndex == 0){
//      errStr = "ご利用方法を選んでください。";
//    }else
//    if(f.RSV_PERSON_NUM.value == ""){
//      errStr = "人数を入力してください。";
//    }else if(f.PERSON_NAME.value == ""){
//      errStr = "代表者名（漢字）を入力してください。";
//    }else if(f.KANA_NAME.value == ""){
//      errStr = "代表者名（フリガナ）を入力してください。";
//    }else if(f.TEL.value == ""){
//      errStr = "電話番号を入力してください。";
//    }else if(f.CONFIRM_TEL.value == ""){
//      errStr = "確認用電話番号を入力してください。";
//    }else if(f.MAIL.value == ""){
//      errStr = "メールアドレスを入力してください。";
//    }
  break;
  case 501:
  case 5011:
  case 503:
  case 504:
  if(f.mail.value == "") errStr = "メールアドレスを入力してください。";
    break;

// 追加20060620 // 修正20060828
  case 601:
  if(f.FREEWORD.value == "") errStr = "駅名を入力してください。";
    break;
  case 602:
  if(f.FREEWORD.value == "") errStr = "料理名を入力してください。";
    break;
  case 603:
  if(f.FREEWORD.value == "") errStr = "店名か電話番号を入力してください。";
    break;
  case 604:
  if(f.FREEWORD.value == "") errStr = "キーワードを入力してください。";
    break;
// 修正20060828

  case 605:
  var i = 0;
  // クーポンの種類の選択チェック
  if ((f.CP_CDA[0].selected == true) && (f.CP_CDB[0].selected == true)) i++;
  // 使用条件の選択チェック
  var j = 0;
  var k = 0;
  while (j < f.CPS_CD.length) {
   if (f.CPS_CD[j].checked == true) k++;
   j++;
  }
  if (k == 0) i++;
  // 未入力であればアラート
  if (i >= 2) errStr = "クーポンの種類か使用条件を選んでください。";
    break;
// NSSOL追加20060828
  case 606:
    // コース検索(SATOP)
    break;

  case 607:
  var i = 0;
  // 使用条件の選択チェック
  if ((f.CP_CDA[0].selected == true) && (f.CP_CDB[0].selected == true) && (f.CP_CDC[0].selected == true)) i++;
  if (f.CPS_CD.checked == false) i++;
  // 未入力であればアラート
  if (i >= 2) errStr = "使用条件を選んでください。";
    break;

  case 608:
  var i = 0;
  // 使用条件の選択チェック
  if (f.CP_CD[0].selected == true) i++;
  // 未入力であればアラート
  if (i >= 1) errStr = "使用条件を選んでください。";
    break;

default :
  if(f.keyword.value == "") errStr = "キーワードを入力してください。";
  }
  if(errStr!=""){
    alert(errStr);
    return false;
  }
  if(nextPage !=""){
    location.href=nextPage + ".html";
  }else{
    f.submit();
  }
}

function LoadPVImgStrClk(dsp,str,sp,spd){
  // PV集計 検索結果の店舗からのClick回数集計の為にClick時に画像を読み込む
  // dsp : 画面ID , str : お店ID , sp : 特集区分CD , spd : 特集明細NO
  // sp,spd は特集検索結果でのみ使用
  img_tmp = new Image();
  if(spd == ""){
    img_tmp.src = "/PV/sh_res_click_" + dsp + "_" + str + ".gif";
  } else {
    img_tmp.src = "/PV/sh_res_click_" + dsp + "_" + str + "_" + sp  + "_" + spd + ".gif";
  }
}

function LoadPVImgBnrStrClk(dsp,str){
  // PV集計 サービスエリア、中エリアのローテーションバナーからのClick回数集計の為にClick時に画像を読み込む
  // dsp : 画面ID , str : お店ID
  img_tmp = new Image();
  img_tmp.src = "/PV/st_bnr_click_" + dsp + "_" + str + ".gif";
}

// 20081118 Add
function LoadPVImgStrUrlClk(str,url){
  // PV集計 お店個別URLリンクのClick回数集計の為にClick時に画像を読み込む
  // 画像がブラウザキャッシュされないように画像URLの後ろに分秒を付加する
  // str : お店ID , url : お店個別URL

  d = new Date();
  mm = d.getMinutes();
  ss = d.getSeconds();

  img_tmp = new Image();
  img_tmp.src = "/PV/st_url_click_" + str +".gif" + "?" + mm + ss;

  window.open(url, 'new');
}

function LoadPVImgSearchStrClk(dsp,str,fwd){
  // PV集計 フリーワード検索結果一覧からのClick回数集計の為にClick時に画像を読み込む
  // dsp : 画面ID , str : お店ID , fwd : フリーワード（エンコード）
  img_tmp = new Image();
  img_tmp.src = "/PV/st_search_click_" + dsp + "_" + str + "_" + fwd + ".gif";
}

function setaction (f_name) {
  if (document.getElementById && document.all) {
    var MAC = ( navigator.userAgent.indexOf("Mac") >= 0 );
    if( MAC ){
      setactionMac(f_name);
    } else {
      setactionWin(f_name);
    }
  } else if (document.getElementById) {
    setactionWin(f_name);
  }

}
function setactionWin (f_name) {

  var Food = document.getElementById("food");

  if( Food ){
    document.getElementById("food").className="";
  }
  document.getElementById("station").className="";
  document.getElementById("shopName").className="";
  document.getElementById("freeword").className="";
  document.getElementById("ARGV1").value = "";

  //document.getElementById("tab1").action = "/CSP/psh020/doFreewordSearch";

  if( f_name == 1 ){
    document.getElementById("shopName").className="current";

    document.getElementById("ARGV1").name="SK";
    document.getElementById("ARGV1").value = "1";


  } else if ( f_name == 2 ){
    document.getElementById("food").className="current";

    document.getElementById("ARGV1").name="SK";
    document.getElementById("ARGV1").value = "2";


  } else if ( f_name == 3 ){
    document.getElementById("station").className="current";

    document.getElementById("ARGV1").name="SK";
    document.getElementById("ARGV1").value = "3";


  } else if ( f_name == 4 ){
    document.getElementById("freeword").className="current";

    document.getElementById("ARGV1").name="SK";
    document.getElementById("ARGV1").value = "4";


  }

}
function setactionMac (f_name) {

  var Food = document.all("food");

  if( Food ){
    document.all("food").className="size12";
    document.all("word_food").className="";
  }

  document.all("station").className="size12";
  document.all("name_tel").className="size12";
  document.all("freeword").className="size12";

  document.all("word_station").className="";
  document.all("word_name_tel").className="";
  document.all("word_freeword").className="";
  document.all("ARGV1").value = "";

  //document.all("tab1").action = "/CSP/psh020/doFreewordSearch";

  if( f_name == 1 ){
    document.all("shopName").className="current";

    document.all("ARGV1").name = "SK";
    document.all("ARGV1").value = "1";


  } else if ( f_name == 2 ){
    document.all("food").className="current";

    document.all("ARGV1").name = "SK";
    document.all("ARGV1").value = "2";


  } else if ( f_name == 3 ){
    document.all("station").className="current";

    document.all("ARGV1").name = "SK";
    document.all("ARGV1").value = "3";


  } else if ( f_name == 4 ){
    document.all("freeword").className="current";

    document.all("ARGV1").name = "SK";
    document.all("ARGV1").value = "4";


  }

}

function submitFW(){
  var a = submitFW.arguments;
  var f = a[0];
  var md = f.ARGV1.value;
  var id ;
  var nextPage = "";
  if(a.length > 1) id = a[1];
  if(a.length > 2) nextPage = a[2];
    var errStr = "";
  switch(id){
  case 601:
  if( md == 1 ){
    if(f.FREEWORD.value == "") errStr = "店名を入力してください。";
      break;
  }else if( md == 2 ){
    if(f.FREEWORD.value == "") errStr = "料理名を入力してください。";
       break;
  }else if( md == 3 ){
    if(f.FREEWORD.value == "") errStr = "駅名かエリアを入力してください。";
      break;
  }else if( md == 4 ){
    if(f.FREEWORD.value == "") errStr = "キーワードを入力してください。";
    break;
  }

default :
  if(f.FREEWORD.value == "") errStr = "キーワードを入力してください。";
  }
  if(errStr!=""){
    alert(errStr);
    return false;
  }
  if(nextPage !=""){
    location.href=nextPage + ".html";
  }else{
    f.submit();
  }
}
function setdefaultAction () {
  if (document.getElementById && document.all) {
    var MAC = ( navigator.userAgent.indexOf("Mac") >= 0 );
    if( MAC ){
      document.getElementById("ARGV1").name="SK";
      document.getElementById("ARGV1").value="1";
    } else {
      document.all("ARGV1").name = "SK";
      document.all("ARGV1").value = "1";
    }
  } else if (document.getElementById) {
    document.getElementById("ARGV1").name="SK";
    document.getElementById("ARGV1").value="1";
  }
}
// common area end


//PCフロント用おすすめレポート(ボタンを消さない) add 2009/03/16

function changeBodyFront(yesbutton, cnt) {

	$('li#'+yesbutton).parent().after('<p class="message">ただいま処理中です。しばらくお待ちください・・・</p>');
	$('li#'+yesbutton).parent().remove();
	// ボタンを消した後に下記によりPOSTさせる
	var f = document.forms[cnt];
	f.submitAnchor.click(); // 非表示ボタンをクリック

}

function loadBody(){
	document.getElementById("bd").style.display = 'block';
}

// PCフロント用フリーワード入力エリアのsubmit制御 add 2009/05/28
function checkSubmitFW(){
	var a = checkSubmitFW.arguments;
	var e = a[0];
    if (!e) {
    	e = window.event;
	}

    if(e.keyCode == 13) {
		// Enter key
		var result = submitFW(a[1], a[2]);
		if (result == null || result == false) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

//
// from navjigation.js
//
function showExternalWindow(a) {
	window.open(a.href, "_blank");
	return false;
}

function popupWin(url, myname, wid, hei) {
  var win_width = (screen.width - wid) / 2;
  var win_height = (screen.height - hei) / 2;
  var win_detail = 'resizable=yes,height='+ hei +',width='+ wid +',top='+ win_height +',left='+ win_width +', scrollbars=yes, toolbar=yes, status=yes, location=yes';
  var win = window.open(url, myname, win_detail);
  if (parseInt(navigator.appVersion) >= 4) { win.focus(); }
}

function openWinSendData(sendform, url, myname, wid, hei){
	var win = popupWin("about:blank", myname, wid, hei);
	sendform.target = myname;
	sendform.method = "post";
	sendform.action = url;
	sendform.submit();
}

//
// from tooltips.js
//
function tooltips_init(tooltipsClass){
	$(tooltipsClass).click(function(){
		var url = this.href;
		url = url.split('?')[1];
		var storeIDParam = url.split('=')[1];
		jQuery.loadJson.removeCookie(storeIDParam);
		$("a[@href*='?keeplistID=" + storeIDParam + "&']").css("visibility","visible");
		return false;
	});
}

function addCookieBtn(component, isGourmet) {
		var id = component.id;
		var url = component.href;
		var queryString = url.replace(/^[^\?]+\??/,'');
		var params = parseQuery(queryString);
		var storeIDParam = params['keeplistID'];

		if(jQuery.loadJson.countCookie() < jQuery.loadJson.keepListLength){
			var imgfile = params['imgfile'];
			var imgWidth = params['imgWidth'];
			var imgHeight = params['imgHeight'];
			if (isGourmet) {
				jQuery.loadJson.addCookieForGourmet(storeIDParam);
			} else {
				jQuery.loadJson.addCookie(storeIDParam);
			}
		}else{
			var imgfile = "/SYS/cmn/images/common/dialog/balloon_keep_full.gif";
			var imgWidth = "141";
			var imgHeight = "89";
		}

		showTooltip(id, imgfile, imgWidth, imgHeight);
		removeTooltip(2000);
}

function showTooltip(id, imgfile, imgWidth, imgHeight){

	createOverlay();

	var targetImg = $('#' + id).children('img');
	var ew = targetImg.width();
	var eh = targetImg.height();
	var targetPosX = targetImg.offset().left + ew;
	var targetPosY = targetImg.offset().top + eh*0.5;
	var imgPos = parseInt((imgHeight*0.5),10);

	$("body").append('<p id="tooltip"><img src="'+ imgfile +'" width="'+ imgWidth +'" height="'+ imgHeight +'" /></p>');
	$("#tooltip").css("opacity","0");
	$("#tooltip").css("position","absolute");
	$("#tooltip").css("z-index","999");
	$("#tooltip").css("top",targetPosY - imgPos);
	$("#tooltip").css("left",targetPosX);
	$("#tooltip").fadeTo(400,1.0);
}

function removeTooltip(time){
	var timerID;
	timerID = setTimeout(
		function(){
			$("#tooltip").fadeTo(400,0,
				function(){
					$("#tooltip").remove();
					removeOverlay();
				}
			);
			clearTimeout(timerID);
		},
	time);
}

function parseQuery(query){
	var Params = {};
	if(!query){return Params;}
	var Pairs = query.split(/[;&]/);
	for(var i=0;i<Pairs.length;i++){
	var KeyVal = Pairs[i].split('=');
		if(!KeyVal || KeyVal.length != 2){continue;}
		var key = unescape(KeyVal[0]);
		var val = unescape(KeyVal[1]);
		val = val.replace(/\+/g, ' ');
		Params[key] = val;
	}
	return Params;
}

function createOverlay(){
	var h = $("body")[0].offsetHeight;

	$("body").append("<div id='unclickableLayer'></div>");

	$("#unclickableLayer").css({
		width: "100%",
		height: h,
		position: "absolute",
		top: "0",
		left: "0",
		background: "#fff",
		opacity: "0"
	});
}

function removeOverlay(){
	$('#unclickableLayer').remove();
}

