
$(document).ready(function(){

	//ロールオーバー画像の設定
	$("img.imgover").unbind('onmouseover');
	$("img.imgover").unbind('onmouseout');
	$("img.imgover").each(function() {
		this.src = this.src.replace(/_on/, '');
		$(this).removeAttr('hsrc');
	});
	initRollovers();
	
	//現在のページ
	var currentPage = param.condDisp.getValueByKey('currentPage');
	
	//物件画像のセット
	$(".pic input:hidden[name='propertyImgUrl']").each(function() {
		var imgUrl = $(this).val();
		var img = $(this).prev("img")[0];
		new Century21.ImageLoader(imgUrl).setImageSize(
			(param.condDisp.getValueByKey("display") == 'img') ? 213 : 98, img);

		$(img).parent("a.hasImage").eq(0).hover(
			function() {
				if ($(this).find("img:first").hasClass("loaded")) {
					var popup = $(this).parents("div#property").children("div.popupImgArea").eq(0);
					if (!popup.hasClass("loaded")) {
						new Century21.ImageLoader(imgUrl).setImageSize(240, popup.find("img.popupImg")[0]);
						popup.addClass("loaded");
					}
					popup.css("display", "inline");
				}
			},
			function() {
				$(this).parents("div#property").children("div.popupImgArea").eq(0).css("display", "none");
			}
		);
	});
	
	//全選択をクリア
	$(".searchTitleBoxRight li:first").click(function(){
		//こだわり条件の画像を非表示に
		$("dl.kodawari li").css("display", "none");
		param.clear2ndConditions();
		param.executeSearch();
		return false;
	});
	
	//この条件を保存する(賃貸)
	$(".saveConditionRent").click(function(){
		$("#saveConditionInput").val(param.getPath(), '');
		document.saveConditionForm.submit();
		return false;
	});
	
	//この条件を保存する（売買）
	$(".saveConditionBuy").click(function(){
		var query = param.getUrl().replace(/^.+\?/, '');
		var hrefPrefix = 'http://www.century21.jp/buy/index.php/M1120032/return/';
		if(param.condMain.containsKey("pref") && param.condMain.getValueByKey("pref").split(":").length == 1 ) {
			//都道府県コードが入力されていて、複数ではない場合
			location.href = hrefPrefix + query.replace(/(\&|\=)/g, '/') + "/section/" + param.condBasic.getByKey("kind").value + ":a" + param.condMain.getByKey("pref").value; 
		} else {
			location.href = hrefPrefix + query.replace(/(\&|\=)/g, '/') + "/section/" + param.condBasic.getByKey("kind").value;
		}
		
		return false;
	});
	
	//このページのURL
	$(".searchTitleBoxRight li input:text").each(function(){
		var url = param.getUrl();
		jQuery.each(dispOrder, function(i,val){
			var regexp = new RegExp("p\\." + val + "=[^&]+(&|$)", "g");
			url = url.replace(regexp, '').replace(/(&|\?)$/g, '');
		});
		if ((/p\.facilityDist=1/.test(url)) && !(/p\.facility\d=\d/.test(url))) {
			 url = url.replace(/(&|\?)p.facilityDist=1/g, '');
			}
		$(this).val(url);
	});
	
	var getCurrentPage = function(){
		var page = parseInt(currentPage);
		if(isNaN(page)) {
			return 1;
		}else {
			return page;
		}
	}
	
	//ページング 前へ　1ページ前へ
	$(".pagingPrevA, .pagingBefore1A").click(function(){
		param.movePage(getCurrentPage()-1);
		
		return false;
	});
	
	//ページング　最初へ
	$(".pagingFirstA").click(function(){
		param.movePage(1);
		return false;
	});
	
	//ページング　2ページ前へ
	$(".pagingBefore2A").click(function(){
		param.movePage(getCurrentPage()-2);
		return false;
	});
	
	//ページング　2ページ後へ
	$(".pagingAfter2A").click(function(){
		param.movePage(getCurrentPage()+2);
		return false;
	});
	
	//ページング　最後へ
	$(".pagingLastA").click(function(){
		param.movePage($("#pagingLastPageNum").val());
		return false;
	});
	
	//ページング　次へ 1ページ後へ
	$(".pagingNextA, .pagingAfter1A").click(function(){
		param.movePage(getCurrentPage()+1);
		return false;
	});
	
	//こだわり条件の削除
	$("#eraseLastPartCond").click(function(){
		param.condPart.pop();
		param.executeSearch();
		return false;
	});
	
	//フリーワードの削除
	$("#eraseWordCond").click(function(){
		param.condWord.clear();
		param.executeSearch();
		return false;
	});
	
	//表示ボタン
	$("#displayButton").click(function(){
		param.condDisp.put("sort", $("#p_sort").val());
		param.condDisp.put("perPage", $("#pager_perPage").val())
		param.executeSearch();
		return false;
	});
	
	//「通常表示」
	$("#viewBtnDefOff").click(function(){
		param.condDisp.put("display", "def");
		param.showSearchResult();
		return false;
	});
	
	//「画像表示」
	$("#viewBtnImgOff").click(function(){
		param.condDisp.put("display", "img");
		param.showSearchResult();
		return false;
	});
	
	//全件チェックON
	$(".searchToolBtn .btn01 a").click(function(){
		$("div.checkBoxDef").each(function() {
			$(this).removeClass("checkBoxDef").addClass("checkBoxOn");
		});
	});
	
	//全件チェックOFF
	$(".searchToolBtn .btn02 a").click(function(){
		$("div.checkBoxOn").each(function() {
			$(this).removeClass("checkBoxOn").addClass("checkBoxDef");
		});
	});
	
	//お気に入り お問い合わせ　詳細を表示
	$("li.btn03 a").click(function(){

		var ids = new Array();
		$("div.checkBoxOn input[type='hidden']").each(function() {
			ids.push(this.value);
		});
		
		if(ids.length == 0){
			alert("物件を選択してください");
		} else {
			var href = this.href + ids.join(",");
			if (this.className=='blank') {
				window.open(href);
			} else {
				location.href = href;
			}
		}
		
		return false;
	});
	
	//一件チェック
	$("div.checkBoxOn, div.checkBoxDef").click(function() {
		if( $(this).hasClass("checkBoxOn")){
			$(this).removeClass("checkBoxOn").addClass("checkBoxDef");
		}else{
			$(this).removeClass("checkBoxDef").addClass("checkBoxOn");
		}
	});
	
	//詳細を見る
	$("div.checkBoxOn p.btnDetail, div.checkBoxDef p.btnDetail").click(function(e) {
		//チェックボックスがONにならないようにイベントの伝播を止める
		e.stopPropagation();
	});
	
//	//ポップアップ画像の読み込み
//	$("div.popupImgArea").each(function(){
//		new Century21.ImageLoader($(this).find("input:first")[0].value)
//					.setImageSize(240, $(this).find("img.popupLayout")[0]);
//		new Century21.ImageLoader($(this).find("input:last")[0].value)
//					.setImageSize(240, $(this).find("img.popupOutside")[0]);
//	});
	
//	//画像のポップアップ表示
//	$(".searchResultDef .pic a[class='hasImage']").hover(
//		function(){
//			var popup = $(this).parents("div#property").children("div.popupImgArea").eq(0);
//			if (!popup.attr("loaded")) {
//				new Century21.ImageLoader(popup.find("input:first").val()).setImageSize(240, popup.find("img.popupLayout")[0]);
//				new Century21.ImageLoader(popup.find("input:last").val()).setImageSize(240, popup.find("img.popupOutside")[0]);
//				popup.attr("loaded", "on");
//			}
//			popup.css("display", "inline");
//		},
//		function() {
//			$(this).parents("div#property").children("div.popupImgArea").eq(0).css("display", "none");
//		}
//	);
	
	//リンク先からの「戻る」で直近の検索条件・検索結果が表示されるように
	(function(){
		var setCurrentUrlAnchor = function(){
			if (location.href != param.getUrl()) {
				location.hash = param.getUrl();
			}
		};
		var filterStr = "#headArea, #gloNaviArea, .topicsPath, .searchTitleBox01, .searchToolTop, .searchResultDef, .searchResultImgBox, .searchToolBottom, #footerBox, #footerBottom";
		//他システムへのリンクを戻るに対応させる
		$("div").filter(filterStr).find("a").not(".exAnchor, [target]").unbind('click', setCurrentUrlAnchor);
		$("div").filter(filterStr).find("a").not(".exAnchor, [target]").click(setCurrentUrlAnchor);
	})();
	
});

