
////////////////////////////////////////////////////////////////////////////////
/// 初期設定
////////////////////////////////////////////////////////////////////////////////

//閲覧履歴表示ブロック
var id_output="OUTPUT";

//登録データの最大個数
var data_max=3;

//クッキー保存期限 ／ 単位 ： 時間
var cookie_period=30; //30日間

//クッキーの登録パス
var cookie_path="/";

//他にチェックした店舗をカウント
var listcount=1;

//HTMLソース
var source  = "";
var sourceOther = "";  // 他にチェックした店舗用

//読み込むXMLを制御するカウンター
var counter = 0;

//1店舗でも書き出したかどうかを判断するフラグ（サイドバー用）
var sideBarFlg = false;


////////////////////////////////////////////////////////////////////////////////
/// クッキー操作
////////////////////////////////////////////////////////////////////////////////

var getCookie=function(key){
    return $.cookie(key);
}
var setCookie=function(key,value){
    $.cookie(key, value, { expires: cookie_period, path: cookie_path });
}
var clearCookie=function(key){
    $.cookie(key, null);
}


////////////////////////////////////////////////////////////////////////////////
/// 閲覧履歴制御
////////////////////////////////////////////////////////////////////////////////

/*
閲覧履歴登録
----------------------------------------------------------------------*/
var saveHistory = function(){

	var dataTextNew;
	var dataText = unescape(getCookie("unyStoreHistory"));
	
	var itemText = tenpo_id;
	dataTextNew = itemText;
	var counter = 1;
	
	if(dataText != "null"){
	    
	    dataText = dataText.split("\t");
	    
		for(var i=0;i < dataText.length; i++){
			
			if(dataText[i] != itemText && counter < data_max){
			    dataTextNew += "\t"+dataText[i];
			    counter++;
			}
		}
	}
	setCookie("unyStoreHistory",escape(dataTextNew));
}

/*
閲覧履歴の読み込み
----------------------------------------------------------------------*/
var loadHistory = function(){
	var dataText = unescape(getCookie("unyStoreHistory"));
    	
    	//チェックした店舗がある
	if(dataText != "null"){
		dataText=dataText.split("\t");
		
		$.ajax({
			url: "shop/" + dataText[counter] + "/xml/store.xml",
			type: 'GET',
			dataType: 'xml',
			timeout: 1000,
			cache :false,
			error: function(){
				
				// 読み込みエラーの場合、チェックしていないことにする
				// カウンターインクリメント
				counter++;
				
				// 次の店舗のXMLを読み込む
				if(counter < dataText.length) {
					loadHistory();
				
				// 次の店舗がない場合＝チェックした店舗がない
				}else{
					setNoStore();
				}
			},
			success: function(xml){
			
				// 読み込み成功の場合
				var storeid = $(xml).find('store').attr("id");
				
				source += "<div class='checked'>";
				source += "<p><strong>";
				
				// 店舗アイコン
				if( $(xml).find('brand').text() == 1 ) {
					source += "<img src='images_cst/common/icon_apita.gif' alt='APITA' width='20' height='16' />";
				} else if( $(xml).find('brand').text() == 2 ) {
					source += "<img src='images_cst/common/icon_piago.gif' alt='PIAGO' width='12' height='16' />";
				} else if( $(xml).find('brand').text() == 4 ) {
					source += "<img src='images_cst/common/icon_lafoods.gif' alt='La:Foods Core' width='19' height='15' />";
				} else if( $(xml).find('brand').text() == 8 ) {
					source += "<img src='images_cst/common/icon_uhome.gif' alt='ユーホーム' width='12' height='16' />";
				}
				
				source += $(xml).find('name').text() + "</strong></p>"
				source += "<h3><img src='images_cst/top/top_chk_thisweek.gif' alt='今週のチラシ' width='178' height='20' /></h3>"
				source += "<iframe src='http://www.shufoo.net/s19/chirashiList.jsp?shopId=" + $(xml).find('shufoo').text() + "' name='flyer' width='178' height='160' frameborder='0'>この部分はインラインフレームを使用しています。</iframe>"
				source += "<h3><img src='images_cst/top/top_chk_good_deals.gif' alt='おトク情報' width='178' height='20' /></h3>"
				
				source += "<dl class='list'>"
				
				// おトク情報
			    	$(xml).find('otokuTop otoku').each(function(){
					var otokuid = "gd"+$(this).attr("id");
					source += "<dt><a href='shop/"+ storeid +"/index.html#"+otokuid+"'>" + $(this).find('title').text() + "</a>";
					
					if($(this).attr("newf") == "1"){
						source += "<img src='images_cst/common/i_new.gif' alt='NEW' width='27' height='11' />";
					}
					source += "</dt>";
					source += "<dd>" + $(this).find('text').text() + "</dd>";
			    	});
			    	
			     source += "</dl>"
			     source += "<ul class='more'>"
				source += "<li><a href='shop/" + storeid + "/index.html'><img src='images_cst/top/top_bu_more.gif' alt='この店舗をもっと見る' width='178' height='27' /></a></li>"
				
				// 専門店情報
				if( $(xml).find('spstoref').text() == 1 ){
					source += "<li><a href='shop/" + storeid + "/specialty_shop/index.html'><img src='images_cst/top/top_bu_specialtystore.gif' alt='専門店の情報を見る' width='178' height='27' /></a></li>"
				}
				source += "</ul>"
				
				// カウンターインクリメント
				counter++;

				// 他にチェックした店舗がある場合
				if(counter < dataText.length){
					getCheckList(dataText);
				
				// ない場合、HTMLを書き出して終了
				} else {
					source += "</div>"
					$("#check").html(source);
				}
			}
		});
    	
    	//チェックした店舗がない
	} else {
		setNoStore();
	}
}


/*
チェックした店舗がない場合の処理
----------------------------------------------------------------------*/
function setNoStore(){
	source += "<div class='noCheck'>"
	source += "<p><img src='images_cst/top/top_txt_nothing.gif' alt='まだありません' width='83' height='11' /></p>"
	source += "<p>最近チェックした店舗のおトク情報や今週のチラシがここに表示されます。</p>"
	source += "<p class='btn'><a href='shop/index.html'><img src='images_cst/top/top_bu_store.gif' alt='店舗をチェックする' width='178' height='27' /></a></p>"
	source += "</div>"
		
	$("#check").html(source);
}



/*
他にチェックした店舗の読み込み
----------------------------------------------------------------------*/
function getCheckList(dataText){
	
	$.ajax({
		url: "shop/" + dataText[counter] + "/xml/store.xml",
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		cache :false,
		error: function(){
		
			// 読み込みエラーの場合、チェックしていないことにする
			// カウンターインクリメント
			counter++;
			
			// 次の店舗のXMLを読み込む
			if(counter < dataText.length) {
				getCheckList(dataText);
			
			// 次の店舗がない場合＝終了処理
			}else{
				setOtherChecked();
				source += "</div>"
				
				// HTML書き出し
				$("#check").html(source);
			}
		},
		success: function(xml){

			// 読み込み成功の場合
			sourceOther += "<li><a href='shop/" + $(xml).find('store').attr("id") + "/index.html'>" + $(xml).find('name').text() + "</a></li>"

			// カウンターインクリメント
			counter++;

			// 他にチェックした店舗がまだある場合
			if(counter < dataText.length){
				getCheckList(dataText);
			
			// 他にチェックした店舗がもうない場合＝終了処理
			} else {
				setOtherChecked();
				source += "</div>"
				
				$("#check").html(source);
			}
		}
	});
}


/*
「他にチェックした店舗を見る」部の生成
----------------------------------------------------------------------*/
function setOtherChecked(){
	if(sourceOther != ""){
		source += "<div class='other'>";
		source += "<p><img src='images_cst/top/top_txt_other.gif' alt='他にチェックした店舗を見る' width='138' height='11' /></p>";
		source += "<ul>";
		source += sourceOther;
		source += "</ul>"
		source += "</div>"
	}
}




/*
閲覧履歴の読み込み（サイドバー用）
----------------------------------------------------------------------*/
var loadHistorySideBar = function(){
	var dataText = unescape(getCookie("unyStoreHistory"));

    //チェックした店舗がある
	if(dataText != "null"){
		dataText=dataText.split("\t");
		getCheckListSideBar(dataText);

  	//チェックした店舗がない
	} else {
		setNoStoreSideBar();
	}
}


/*
他にチェックした店舗の読み込み（サイドバー用）
----------------------------------------------------------------------*/
function getCheckListSideBar(dataText){
	
	$.ajax({
		url: rootPath + "shop/" + dataText[counter] + "/xml/store.xml",
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		cache :false,
		error: function(){
		
			// 読み込みエラーの場合、チェックしていないことにする
			// カウンターインクリメント
			counter++;
			
			// 次の店舗のXMLを読み込む
			if(counter < dataText.length) {
				getCheckListSideBar(dataText);
			
			// 次の店舗がない場合＝終了処理
			}else{
				
				if(sideBarFlg){
					// HTML書き出し
					$("#check").html("<ul>" + sourceOther + "</ul>");
				}else{
					setNoStoreSideBar();
				}
			}
		},
		success: function(xml){

			// 読み込み成功の場合
			sideBarFlg = true;
			sourceOther += "<li><a href='"+rootPath +"shop/" + $(xml).find('store').attr("id") + "/index.html'>" + $(xml).find('name').text() + "</a></li>"

			// カウンターインクリメント
			counter++;

			// 他にチェックした店舗がまだある場合
			if(counter < dataText.length){
				getCheckListSideBar(dataText);
			
			// 他にチェックした店舗がもうない場合＝終了処理
			} else {
				$("#check").html("<ul>" + sourceOther + "</ul>");
			}
		}
	});
}


/*
チェックした店舗がない場合の処理（サイドバー用）
----------------------------------------------------------------------*/
function setNoStoreSideBar(){
	source += "<p>まだありません。</p>";
	source += "<p class='btn'><a href='"+rootPath +"shop/index.html'><img src='"+rootPath +"images_cst/common/snavi_chkstore_bu.gif' alt='店舗をチェックする' width='140' height='27' /></a></p>";
	$("#check").html(source);
}

