/*@cc_on _d=document;eval('var document=_d')@*/

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


﻿/**
 * Hotpepper functions
 */
(function() {
	hpr = window.hpr = {

		window: {
			// window.openExternal
			openExternal: function(anchor) {
				window.open(anchor.href, "_blank");
				return false;
			},

			// window.open
			open: function(url) {
				window.open(url, "_blank");
				return false;
			},

			// window.popup
			popup: function(url, name, width, height) {
				var top = (screen.width - width) / 2;
				var left = (screen.height - height) / 2;
				var option = 'resizable=yes,height=' + height + ',width=' + width + ',top=' + top + ',left=' + left
							+ ', scrollbars=yes, toolbar=yes, status=yes, location=yes';
				var win = window.open(url, name, option);

				if (jQuery.browser.mozilla) {
					win.focus();
				}
			},

			// window.popupWithPost
			popupWithPost: function(sendForm, url, name, width, height) {
				var win = popupWindow("about:blank", name, width, height);
				sendForm.target = name;
				sendForm.method = "post";
				sendForm.action = url;
				sendForm.submit();
			},

			// window.closeWelcomeArea
			closeWelcomeArea: function() {
				$('div#welcome').slideUp('fast');
				hpr.common.setWelcomeAreaHidden(); // Cookie書き込み処理
  				return false;
			}
		}, // hpr.window end

		freeword: {
			// freeword.setAction
			setAction: function(kind) {
				if (kind < 1 || 4 < kind) {
					return;
				}

				var ids = ['#shopName', '#food', '#station', '#freeword']

				$('#ARGV1').each(function() {
					$(this).attr('name', 'SK');
					$(this).val(kind);
				});
				$(ids.join(',')).attr('class', '');
				$(ids[kind - 1]).addClass('current');
			},

			// freeword.validate
			validate: function(event, form, validateType) {
			    if (!event) {
			    	event = window.event;
				}

			    if (event.keyCode == 13) {
					var result = this.submit(form, validateType);
					if (result == null || result == false) {
						return false;
					}
				}
				return true;
			},

			// freeword.submit
			submit: function(form, validateType, nextPage) {
				var kind = form.ARGV1.value;
				var message = "";
				switch(validateType) {
				case 601:
					messages = ["店名・TELを入力してください。",
						"料理名を入力してください。",
						"駅名かエリアを入力してください。",
						"キーワードを入力してください。"];
					if (1 <= kind && kind <= 4) {
						if (form.FREEWORD.value == "") {
							message = messages[kind - 1];
						}
					}
					break;
				default :
					if (form.FREEWORD.value == "") {
						message = "キーワードを入力してください。";
					}
				}

				if (message != "") {
					alert(message);
					return false;
				}

				if (nextPage != undefined){
			    	location.href = nextPage + ".html";
				} else {
			    	form.submit();
				}
			}

		}, // freeword end

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

			//ようこそエリアを表示しない
			setWelcomeAreaHidden: function() {
  				document.cookie = "HPR_WELCOME_CHECK=1;"
  					+ "path=/;"
  					+ "expires=Fri, 31-Dec-2030 23:59:59; ";
 			}

		}, // common end

		tooltip: {
			enabled: false,

			// tooltip.show
			show: function(sender, imgfile, imgWidth, imgHeight) {
				this.enabled = true;
				this._createOverlay();
				var targetImg = $(sender).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",
					position: "absolute",
					"z-index": 999,
					top: targetPosY - imgPos,
					left: targetPosX}).fadeTo(400,1.0);
			},

			// tooltip.remove
			remove: function(time){
				var timerId;
				var tooltip = this;
				timerId = setTimeout(function(){
					$("#tooltip").fadeTo(400,0,
						function(){
							$("#tooltip").remove();
							tooltip._removeOverlay();
							tooltip.enabled = false;
						}
					);
					clearTimeout(timerId);
				}, time);
			},

			_createOverlay: function() {
				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"
				});
			},

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

		}, // tooltip end


		keeplist: {
			ON_LOAD_ENABLED: true,
			IDS_COOKIE_NAME: "HPR_K_STORE_IDS",
			GET_STORES_URL: "/CSP/common/ajaxJson",
			MAX_STORE: 10,
			STORES_ELM_ID: "#keepShop div.shopList",

			// keeplist.addStore
			addStore: function(sender, id) {
				sender.blur();
				if (hpr.tooltip.enabled) {
					return;
				}
				if (this._getCount() < this.MAX_STORE) {
					this._addStore(id);
					var filename = "/SYS/cmn/images/common/dialog/balloon_keep.gif";
					var width = "99";
					var height = "79";
				} else {
					var filename = "/SYS/cmn/images/common/dialog/balloon_keep_full.gif";
					var width = "141";
					var height = "89";
				}
				hpr.tooltip.show(sender, filename, width, height);
				hpr.tooltip.remove(2000);
			},

			// keeplist.removeStore
			removeStore : function(id) {
				var ids = this._getStoreIds();
				var i = ids.length;
				while (i-- > 0) {
					if (ids[i] == id) {
						ids.splice(i, 1);
					}
				}
				this._setStoreIds(ids);

				this._setKeepButtonEnabled(id, true);
				$('#keepStore' + id).remove();

				var keepStores = $("div[class='article'][id^='keepStore']");
				if (keepStores.size() == 0) {
					this._showNoStore();
				}
			},

			// keeplist.showStores
			showStores: function(){
				var ids = this._getStoreIds();
				this._showStores(ids);
			},

			_getStoreIds: function() {
				var storeIds = "";
				try {
					storeIds = $.cookie(this.IDS_COOKIE_NAME);
				} catch(e) {
					this._removeAllStore();
				}
				var ids = storeIds ? storeIds.split(",") : [];
				return ids;
			},

			_setStoreIds: function(ids) {
				$.cookie(this.IDS_COOKIE_NAME,
					ids.join(","), {expires:10*365, path:'/'});
			},

			_getCount: function() {
				return this._getStoreIds().length;
			},

			_addStore: function(id){
				var ids = this._getStoreIds();
				var i = ids.length;
				while (i-- > 0) {
					if (ids[i] == id) {
						ids.splice(i, 1);
					}
				}
				ids.unshift(id);
				this._setStoreIds(ids);
				this._showStores(ids);
			},

			_setKeepButtonEnabled: function (id, enabled) {
				function getStyle(enabled) {
					return enabled ? "visible" : "hidden"
				}

				var link = $("#linkID" + id);
				if (link.size()) {
					link.css("visibility", getStyle(enabled));
				} else {
					$("#linkID" + id + "_01").css("visibility", getStyle(enabled));
					$("#linkID" + id + "_02").css("visibility", getStyle(enabled));
				}
			},

			_removeAllStore: function() {
				$.cookie(this.IDS_COOKIE_NAME, "", {expires:10*365, path:'/'});
			},

			_showNoStore: function() {
				$(this.STORES_ELM_ID).html(
					'<div class="noItem"><p class="notes">'
					+ '<img src="/SYS/cmn/images/common/btn/btn_ico_add_keeplist.gif" width="100" height="17" alt="とりあえずキープ" />'
					+ 'ボタンをおして気に入ったお店をとりあえずキープしておくことが出来ます。'
					+ '</p></div>'
				);
			},

			_isUnique : function(ids) {
				var len = ids.length;
				for (var i = 0; i < len; i++) {
					for (var j = i + 1; j < len; j++) {
						if (ids[i] == ids[j]) {
							return false;
						}
					}
				}
				return true;
			},

			_showStores : function(ids) {
				if (!this._isUnique(ids)) {
					this._removeAllStore();
					this._showNoStore();
					return;
				}
				if (ids == null || ids.length == 0) {
					this._showNoStore();
					return;
				}

				var keeplist = this;
				jQuery.each(ids, function() {
					keeplist._setKeepButtonEnabled(this, false);
				});
				$.ajax({
					url: this.GET_STORES_URL,
					dataType: "json",
					cache: false,
					success : function(result) {
						var stores = keeplist._intersection(ids, result.items);
						var validIds = [];
						if (stores.length == 0) {
							keeplist._showNoStore();
						} else {
							var html = [];
							var len = stores.length > keeplist.MAX_STORE ? keeplist.MAX_STORE: stores.length;
							for (var i = 0; i < len; i++ ) {
								var store = stores[i];
								keeplist._pushStoreContent(html, store);
								validIds.push(store.storeId);
							}
							if (len > 0) {
								html.push('<ul class="links"><li class="lastChild external">'
									+ '<a href="javascript:void(0)" onclick="javascript:window.open(\'/CSP/dcs010\', \'keep\', \'width=800, menubar=yes, toolbar=yes, location=yes, status=yes, resizable=yes, scrollbars=yes\'); return false;">お店を比較する</a>'
									+ '</li></ul>');
							}
							$(keeplist.STORES_ELM_ID).html(html.join(""));
						}
						keeplist._setStoreIds(validIds);

						// 画面にはあるが、cookieにはないお店をキープ可能にする(削除されたお店をキープできるようにする)
						keeplist._enableKeepButtons(validIds);
					},

					error: function() {
						var storesElm = $(keeplist.STORES_ELM_ID);
						storesElm.children().remove();
						$(storesElm).html('<p>キープリストの追加に失敗しました。</p>');
					}

				});

			},

			_pushStoreContent: function(html, store) {
				html.push('<div class="article" id="keepStore' + store.storeId + '">');
				if (store.storeStateKbn == "2") {
					html.push('<h4 class="shopname">'+store.storeName+'</h4><p>掲載情報がありません</p>');
				} else {
					// 有料店・無料店で分ける
					var tkc_f = store.storeStateKbn == "1" ? "1" : "0";
					html.push('<h4 class="shopname"><a href="'+ store.storeUrl +'" onclick="SCClick_toriKeepClick(\''+store.storeId+'\',\''+ tkc_f +'\');">'+store.storeName+'</a></h4>');
				}
				html.push('<div class="itemsA">');
				html.push('<div class="pic">');

				// 有料店以外(掲載落ち,Mapple,Dokoiku)
				if (store.storeStateKbn != "1") {
					html.push('<p class="photoSS"></p>');
				} else {
					if(store.topPhotoImgId != ""){
						html.push('<p class="photoSS"><a href="'+ store.storeUrl +'" onclick="SCClick_toriKeepClick(\''+store.storeId+'\',\'1\');"><img src="'+ store.topPhotoImgId +'" alt="" width="58" height="58"></a></p>');
					}else{
						html.push('<p class="photoSS"></p>');
					}
				}
				html.push('</div>'); // <-- /pic -->
				html.push('<div class="txt">');
				html.push('<p class="place">' + store.middleAreaName + '</p>');
				html.push('<p class="shopcatch">' + store.genreCatch + '</p>');
				html.push('</div>'); // <-- /txt -->
				html.push('</div>'); // <-- /itemsA -->
				html.push('<p class="close"><a href="javascript:void(0);" onclick="hpr.keeplist.removeStore(\''+ store.storeId +'\'); return false;" class="deleteBox"><img src="/SYS/cmn/images/common/btn/btn_2nd_delete_02.gif" alt="削除" width="35" height="13"></a></p>');
				html.push('</div>'); // <-- /article -->
			},

			_intersection: function(ids, stores){
				var result = [];
				var idsLen = ids.length;
				var storesLen = stores.length;
				for (var i = 0; i < idsLen; i++) {
					for (var j = 0; j < storesLen; j++) {
						if (ids[i] == stores[j].storeId) {
							result.push(stores[j]);
							break;
						}
					}
				}
				return result;
			},

			_enableKeepButtons : function(validIds) {
				var keepStores = $('li.keeplistBtn01 > a');
				var keeplist = this;
				jQuery.each(keepStores, function() {
					var id = $(this).attr('id').substring(6, 16);
					if (jQuery.inArray(id, validIds) == -1) {
						keeplist._setKeepButtonEnabled(id, true);
					}
				});
			}

		}, // keep end

		history: {
			// history.deleteAll
			deleteAll: function() {
	    		$.cookie('HPR_H_STORE_IDS', null , {path:'/'} );
				$("#newlyShop").remove();
			}
		}, // history end

		favorite: {

			store: {
				// favoriteStoreFront.add
				add: function(id, fwNo, SP, CN, RN, OM, tokenKey, tokenValue) {
					if (hpr.tooltip.enabled) return;
					hpr.tooltip.enabled = true;

					var paramStr = 'SP=' + SP;
					if (fwNo == '07') {
						paramStr='SP=' + SP + '&CN=' + CN;
					} else if (fwNo == '12') {
						paramStr='SP=' + SP + '&RN=' + RN + '&OM=' + OM;
					}

					var sender = $('#' + id);
					sender.blur();

					hpr.tooltip.show(sender, '/SYS/cmn/images/common/dialog/balloon_processing.gif', 141, 49);

					var store = this;

					setTimeout(function() {
						$.ajax({
							type: 'GET',
							url: '/CSP/dfs020/ajaxAddFavoriteStore',
							data: 'SP=' + SP + '&' + tokenKey + '=' + tokenValue ,
							success: function(result) {
								if (result == 'success') {
									hpr.tooltip.remove(0);
									setTimeout(function(){
										store._showSuccess(sender, id);
										hpr.tooltip.enabled = false;
									}, 500);
								} else if (result == 'limit') {
									hpr.tooltip.remove(0);
									tb_open('お気に入りのお店追加', '/CSP/dlg020/?height=230&width=300&modal=true&FW='+fwNo+'&' + paramStr, false, 'selectBox');
									hpr.tooltip.enabled = false;
								} else {
									hpr.tooltip.remove(0);
									setTimeout(function(){
										store._showFailure(sender);
										hpr.tooltip.enabled = false;
									}, 500);
								}
							},
							error: function() {
								hpr.tooltip.remove(0);
								setTimeout(function(){
									store._showFailure(sender);
									hpr.tooltip.enabled = false;
								}, 500);
							}
						})
					}, 500);
				},

				_showSuccess: function(sender, id) {
					hpr.tooltip.show(sender, '/SYS/cmn/images/common/dialog/balloon_diary.gif', 99, 79);
					$("a[@id*='" + id + "']").css("visibility","hidden");
					$("a[@id*='add0']").css("visibility","hidden");
					$("a[@id*='add1']").css("visibility","hidden");
					$("a[@id*='add2']").css("visibility","hidden");
					hpr.tooltip.remove(2000);
				},

				_showFailure: function(sender) {
					hpr.tooltip.show(sender, '/SYS/cmn/images/common/dialog/balloon_error.gif', 141, 89);
					hpr.tooltip.remove(2000);
				}

			}

		} // favoriteStore


	}; // hpr end

})(); // function end

/* ---------------------------------------------------------------------- */
/* SC用仮定義 */
/* ---------------------------------------------------------------------- */
function SCClick_toriKeepClick(){}

