﻿/* ==

　初期設定

============================================================================================== */

try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


var area = Array("hokkaido", "tohoku", "kanto", "chubuhokurik", "kinki", "chugoku", "shikoku", "kyushu");
area["txt"] = Array("北海道", "東北", "関東", "中部・北陸", "近畿", "中国", "四国", "九州・沖縄");

area["hokkaido"] = [];
area["hokkaido"]["code"] = Array("01");
area["hokkaido"]["txt"] = Array("北海道");

area["tohoku"] = [];
area["tohoku"]["code"] = Array("02", "03", "04", "05", "06", "07");
area["tohoku"]["txt"] = Array("青森", "岩手", "宮城", "秋田", "山形", "福島");

area["kanto"] = [];
area["kanto"]["code"] = Array("08", "09", "10", "11", "12", "13", "14");
area["kanto"]["txt"] = Array("茨城", "栃木", "群馬", "埼玉", "千葉", "東京", "神奈川");

area["chubuhokurik"] = [];
area["chubuhokurik"]["code"] = Array("15", "16", "17", "18", "19", "20", "21", "22", "23", "24");
area["chubuhokurik"]["txt"] = Array("新潟", "富山", "石川", "福井", "山梨", "長野", "岐阜", "静岡", "愛知", "三重");

area["kinki"] = [];
area["kinki"]["code"] = Array("25", "26", "27", "28", "29", "30");
area["kinki"]["txt"] = Array("滋賀", "京都", "大阪", "兵庫", "奈良", "和歌山");

area["chugoku"] = [];
area["chugoku"]["code"] = Array("31", "32", "33", "34", "35");
area["chugoku"]["txt"] = Array("鳥取", "島根", "岡山", "広島", "山口");

area["shikoku"] = [];
area["shikoku"]["code"] = Array("36", "37", "38", "39");
area["shikoku"]["txt"] = Array("徳島", "香川", "愛媛", "高知");

area["kyushu"] = [];
area["kyushu"]["code"] = Array("40", "41", "42", "43", "44", "45", "46", "47");
area["kyushu"]["txt"] = Array("福岡", "佐賀", "長崎", "熊本", "大分", "宮崎", "鹿児島", "沖縄");


//地図画像のパス
var mapImgPath = "/chintai/common/images/renew2011/map/direct_img_001_";

//各都道府県ページパス
var kyotenPath = "http://www.daiwahouse.co.jp/kyoten/index.html";


/* ==

　処理

============================================================================================== */
$(function(){


	//「ファミリー向けの物件を探す」または「カップル向け物件」の場合、
	if(pra == 'madori=3&madori=4'){
		$('#search_1').bind("click", function(){
			moveArea(this);
			pra = 'shubetu=1&shubetu=2&madori=3&madori=4';
		});
		$('#search_2').bind("click", function(){
			moveArea(this);
			pra = 'shubetu=3&madori=3&madori=4';
		});
	}else if(pra == 'madori=2'){
		$('#search_1').bind("click", function(){
			moveArea(this);
			pra = 'madori=1';
		});
		$('#search_2').bind("click", function(){
			moveArea(this);
			pra = 'madori=2';
		});
		$('#search_3').bind("click", function(){
			moveArea(this);
			pra = 'madori=3';
		});
	};

	//地図・都道府県ボタン画像プリロード
	var preloadImgs = [];
	var mapImgs = preloadImgs;
	for(var i=0; i<area.length; i++){
		mapImgs[i] = new Image();
		mapImgs[i].src = "/chintai/common/images/renew2011/map/direct_img_001_" + area[i] + ".gif";
		
		//都道府県ボタン
		var preloadBtns = [];
		var prefBtns = preloadBtns;
		var thisArea = area[i];
		for(var j=0; j<area[thisArea]["code"].length; j++){
			prefBtns[j] = new Image();
			prefBtns[j].src = "/chintai/common/images/renew2011/map/btn/btn_pref" + area[thisArea]["code"][j] + ".gif";
		}
		
	}
	//「全国を表示」ボタン
	mapImgs["btnAll"] = new Image();
	mapImgs["btnAll"].src = "/chintai/common/images/renew2011/map/btn/btn_all.gif";

	//「前へ戻る」ボタン
	mapImgs["btnBack"] = new Image();
	mapImgs["btnBack"].src = "/chintai/common/images/renew2011/map/btn/btn_back.gif";
	
	
	mapImgChg("all");
	if($('#directSearch_Second').length){
		mapImgChg_Second("all");
	}
});


function mapImgChg(thisArea){
	//地図画像
	//var mapImg = "<img src=\"" + mapImgPath + thisArea + ".gif\" alt=\"\" width=\"500\" height=\"149\">";
	//$("#directSearch div#map > p.dsMap").html(mapImg);
	$("#directSearch div#map > p.dsMap img").attr('src',mapImgPath + thisArea + '.gif');
	//ボタン
	if(thisArea == "all"){
		$('div#map ul.btnNavi').hide()
		$(".container").attr("id", "mapAll");
		
		
		var btnArea = "";
		for(var j=0; j<area.length; j++){
			btnArea += "<li class=\"area_" + area[j] + "\">";
			btnArea += "<a href=\"javascript:void(0);\" title=\"" + area["txt"][j] + "\" onClick=\"return false;\"><span>" + area["txt"][j] + "</span></a>";
			btnArea += "</li>";
		}
		$("#directSearch div#map > ul.btn").html(btnArea);
		
		//「全国を表示」削除
		$("div#map .btn_areaAll").remove();

		$("#directSearch div#map ul.btn").children("li").each(function(i){
			$(this).find("a").bind("click", function(){

				var thisArea = area[i];
				$(".container").attr("id", area[i]);
				mapImgChg(thisArea);
				return false;

			});
		});
		$('div#map img').each(function(obj){
			obj.src = obj.src
		});
	}else{
	
		var btnPref = "";
		for(var j=0; j<area[thisArea]["code"].length; j++){
			btnPref += "<li class=\"pref" + area[thisArea]["code"][j] + "\">";
			btnPref += "<a href=\"javascript:void(0);\" title=\"" + area[thisArea]["txt"][j] + "\" onClick=\"return false;\">";
			btnPref += "<span>" + area[thisArea]["txt"][j] + "</span>";
			btnPref += "</a></li>";
		}
		$("#directSearch > div#map > ul.btn").html(btnPref);
		areaAll();
		
		$("#directSearch > div#map > ul.btn li").each(function(i){
			var prefCode = $(this).attr('class').split('pref');
			$(this).find("a").bind("click.eveSearch", function(){
				//選択している都道府県名表示
				if($('#directSearch #choiceText').length == 0){
					$('#directSearch > div#map').append('<p id="choiceText"></p>');
				}
				$('#directSearch #choiceText').text('選択：' + $(this).attr('title'));
				$('#directSearch #choiceText').show(100)
				
				var adiv = $("#directSearch div#map > p.dsMap img").get(0);
				var prefCodeNode = $('#directSearch #choiceText').get(0);
				$.data(adiv, "preSrc", $("#directSearch div#map > p.dsMap img").attr('src'));
				$.data(prefCodeNode, "prefCode", prefCode[1]);
				$("#directSearch div#map > p.dsMap img").attr('src',mapImgPath + 'last.gif');
				$("#directSearch > div#map > ul.btn").hide();
				if(pra != '' && pra != 'shohin'){
					$('#directSearch .naviArea a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=area&' + pra)
					$('#directSearch .naviLine a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=ensen&' + pra)
					$('#directSearch .naviScroll a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=map&' + pra)
					$('#directSearch .naviStore a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=estate&' + pra)
				}else if(pra == 'shohin'){
					//選択されている商品の数が0でなければ
					if($('#storageUl li').length > 0){
						var shohinText = '&shohin=';
						$('#storageUl li').each(function(i, val){
							if(i == 0){
								shohinText += $(this).find('input').val();
							}else{
								shohinText += ',' + $(this).find('input').val();
							}
						});
					}else if($('#storageUl li').length == 0){
						shohinText = '';
					}
					
					$('#directSearch .naviArea a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=area' + shohinText)
					$('#directSearch .naviLine a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=ensen' + shohinText)
					$('#directSearch .naviScroll a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=map' + shohinText)
					$('#directSearch .naviStore a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=estate' + shohinText)
				}else{
					$('#directSearch .naviArea a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=area')
					$('#directSearch .naviLine a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=ensen')
					$('#directSearch .naviScroll a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=map')
					$('#directSearch .naviStore a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=estate')
				}


				$('div#map ul.btnNavi').show(200);
				areaBack();
				return false;
			})
			
		});
		
	}
}

function areaAll(){ //「全国を表示」表示

	if($("#directSearch div#map p.btn_areaAll")){ $("#directSearch div#map p.btn_areaAll").remove(); }

	$("#directSearch div#map").prepend("<p class=\"btn_areaAll\"><a href=\"javascript:void(0);\" title=\"全国を表示\">全国を表示</a></p>");
	$("#directSearch div#map p.btn_areaAll").find("a").bind("click", function(){
		$('#choiceText').hide();
		$("#directSearch > div#map > ul.btn").show();
		mapImgChg("all");
		$("#directSearch div#map p.btn_areaBack").remove();
	});

}

function areaBack(){ //「前へ戻る」表示

	if($("#directSearch div#map p.btn_areaBack")){ $("#directSearch div#map p.btn_areaBack").remove(); }

	$("#directSearch div#map").prepend("<p class=\"btn_areaBack\"><a href=\"javascript:void(0);\" title=\"全国を表示\">全国を表示</a></p>");
	$("#directSearch div#map p.btn_areaBack").find("a").bind("click", function(){
		$('#choiceText').hide();
		var adiv = $("#directSearch div#map > p.dsMap img").get(0);
		$("#directSearch div#map > p.dsMap img").attr('src',$.data(adiv, "preSrc"));
		$("#directSearch > div#map > ul.btn").show();
		$('div#map ul.btnNavi').hide();
		$("#directSearch div#map p.btn_areaBack").remove();
	});

}


//同一ページにマップ検索が2つ存在する場合用


function mapImgChg_Second(thisArea){
	//地図画像
	//var mapImg = "<img src=\"" + mapImgPath + thisArea + ".gif\" alt=\"\" width=\"500\" height=\"149\">";
	//$("#directSearch_Second div#map_Second > p.dsMap").html(mapImg);
	$("#directSearch_Second div#map_Second > p.dsMap img").attr('src',mapImgPath + thisArea + '.gif');
	//ボタン
	if(thisArea == "all"){
		$('div#map_Second ul.btnNavi').hide()
		$(".container").attr("id", "mapAll");
		
		
		var btnArea = "";
		for(var j=0; j<area.length; j++){
			btnArea += "<li class=\"area_" + area[j] + "\">";
			btnArea += "<a href=\"javascript:void(0);\" title=\"" + area["txt"][j] + "\" onClick=\"return false;\"><span>" + area["txt"][j] + "</span></a>";
			btnArea += "</li>";
		}
		$("#directSearch_Second div#map_Second > ul.btn").html(btnArea);
		
		//「全国を表示」削除
		$("div#map_Second .btn_areaAll").remove();

		$("#directSearch_Second div#map_Second ul.btn").children("li").each(function(i){
			$(this).find("a").bind("click", function(){

				var thisArea = area[i];
				$(".container").attr("id", area[i]);
				mapImgChg_Second(thisArea);
				return false;

			});
		});
		$('div#map_Second img').each(function(obj){
			obj.src = obj.src
		});
	}else{
	
		var btnPref = "";
		for(var j=0; j<area[thisArea]["code"].length; j++){
			btnPref += "<li class=\"pref" + area[thisArea]["code"][j] + "\">";
			btnPref += "<a href=\"javascript:void(0);\" title=\"" + area[thisArea]["txt"][j] + "\" onClick=\"return false;\">";
			btnPref += "<span>" + area[thisArea]["txt"][j] + "</span>";
			btnPref += "</a></li>";
		}
		$("#directSearch_Second > div#map_Second > ul.btn").html(btnPref);
		areaAll_Second();
		
		$("#directSearch_Second > div#map_Second > ul.btn li").each(function(i){
			var prefCode = $(this).attr('class').split('pref');
			$(this).find("a").bind("click", function(){
				//選択している都道府県名表示
				if($('#directSearch_Second #choiceText').length == 0){
					$('#directSearch_Second > div#map_Second').append('<p id="choiceText"></p>');
				}
				$('#directSearch_Second #choiceText').text('選択：' + $(this).attr('title'));
				$('#directSearch_Second #choiceText').show(200);
				
				var adiv = $("#directSearch_Second div#map_Second > p.dsMap img").get(0);
				$.data(adiv, "preSrc_Second", $("#directSearch_Second div#map_Second > p.dsMap img").attr('src'));
				$("#directSearch_Second div#map_Second > p.dsMap img").attr('src',mapImgPath + 'last.gif');
				$("#directSearch_Second > div#map_Second > ul.btn").hide();
				if(pra != '' && pra != 'shohin'){
					$('#directSearch_Second .naviArea a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=area&' + pra)
					$('#directSearch_Second .naviLine a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=ensen&' + pra)
					$('#directSearch_Second .naviScroll a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=map&' + pra)
					$('#directSearch_Second .naviStore a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=estate&' + pra)
				}else if(pra == 'shohin'){
					//選択されている商品の数が0でなければ
					if($('#storageUl li').length > 0){
						var shohinText = '&shohin=';
						$('#storageUl li').each(function(i, val){
							if(i == 0){
								shohinText += $(this).find('input').val();
							}else{
								shohinText += ',' + $(this).find('input').val();
							}
						});
					}else if($('#storageUl li').length == 0){
						shohinText = '';
					}
					$('#directSearch_Second .naviArea a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=area' + shohinText)
					$('#directSearch_Second .naviLine a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=ensen' + shohinText)
					$('#directSearch_Second .naviScroll a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=map' + shohinText)
					$('#directSearch_Second .naviStore a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=estate' + shohinText)
				}else{
					$('#directSearch_Second .naviArea a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=area')
					$('#directSearch_Second .naviLine a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=ensen')
					$('#directSearch_Second .naviScroll a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=map')
					$('#directSearch_Second .naviStore a').attr('href','/chintai/search.asp?pref=' + prefCode[1] +  '&m=estate')
				}
				$('div#map_Second ul.btnNavi').show(200);
				areaBack_Second();
				return false;
			})
			
		});
		
	}
}

function areaAll_Second(){ //「全国を表示」表示

	if($("#directSearch_Second div#map_Second p.btn_areaAll")){ $("#directSearch_Second div#map_Second p.btn_areaAll").remove(); }

	$("#directSearch_Second div#map_Second").prepend("<p class=\"btn_areaAll\"><a href=\"javascript:void(0);\" title=\"全国を表示\">全国を表示</a></p>");
	$("#directSearch_Second div#map_Second p.btn_areaAll").find("a").bind("click", function(){
		$('#choiceText').hide();
		$("#directSearch_Second > div#map_Second > ul.btn").show();
		mapImgChg_Second("all");
		$("#directSearch_Second div#map_Second p.btn_areaBack").remove();
	});

}

function areaBack_Second(){ //「前へ戻る」表示

	if($("#directSearch_Second div#map_Second p.btn_areaBack")){ $("#directSearch_Second div#map_Second p.btn_areaBack").remove(); }

	$("#directSearch_Second div#map_Second").prepend("<p class=\"btn_areaBack\"><a href=\"javascript:void(0);\" title=\"全国を表示\">全国を表示</a></p>");
	$("#directSearch_Second div#map_Second p.btn_areaBack").find("a").bind("click", function(){
		$('#choiceText').hide();
		var adiv = $("#directSearch_Second div#map_Second > p.dsMap img").get(0);
		$("#directSearch_Second div#map_Second > p.dsMap img").attr('src',$.data(adiv, "preSrc_Second"));
		$("#directSearch_Second > div#map_Second > ul.btn").show();
		$('div#map_Second ul.btnNavi').hide();
		$("#directSearch_Second div#map_Second p.btn_areaBack").remove();
	});

}



function moveArea(eveNode){
	// アンカーの値取得
	var href= $(eveNode).attr("href");
	// 移動先を取得
	var target = $(href == "#" || href == "" ? 'html' : href);
	// 移動先を数値で取得
	var position = $(target).offset().top;

	$($.browser.safari ? 'body' : 'html').animate({scrollTop:position}, 300, '');
}

