// JavaScript Document
$(function(){init();});


function init() {
	/*Rollover*/
	var image_cache = new Object();
	$("img.rover,input.rover").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_ro;
		$(this).hover(function() { this.src = imgsrc_ro; },function() { this.src = imgsrc; });
	});
	
  	/*Scroll*/
	$("a").each(function(){
		$(this).click(function(){
			var hash = parseHash($(this).attr("href"));
			if(hash){
				$.scrollTo($(hash), 400 );
				return false;
			}
		});
	});
	/*詳細検索*/
	
	$("#searchDetailInput").each(function(){
		$(this).find("dt:first").each(function(){
			this.visible = false;
			this.bgSrc = $(this).css('background-image');
			$(this).next().hide();
			$(this).find("img").each(function(){
				$(this).attr("src",$(this).attr("src").replace("_on",""));
				var imgsrc = this.src;
				var dot = this.src.lastIndexOf('.');
				var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
				image_cache[this.src] = new Image();
				image_cache[this.src].src = imgsrc_ro;
				$(this).hover(function(){
					this.src = imgsrc_ro; 
				},
				function(){
					this.src = imgsrc;
				});
				$(this).click(function(){
					if(this.visible){
						this.visible = false;
						$(this).parent().next().slideUp('normal');
					}else{
						this.visible = true;
						$(this).parent().next().slideDown();
					}
				});
			});
		});
	});
	
}

/*ハッシュのパース*/
function parseHash(str){
	if(str.indexOf("#")==0 && str.length != 1){
		return str;
	}else{
		return null;
	}
}
/*メニューアクティブ化*/
function Menu_active(id){
	var target_img = "#"+id;	
	$(target_img).each(function(i) {
		dot = this.src.lastIndexOf('.');
    	var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
		this.src = imgsrc_ro;
		$(this).hover(function() { this.src = imgsrc_ro; },function() { this.src = imgsrc_ro; });
	});
}


/*お気に入りへ追加*/
function addBookmark() {
	var title='不動産情報本舗';
	var url = "http://www.realestate-info.jp/";
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}


function addSBM(n)
{
	var base_url = "http://www.realestate-info.jp/";
	/*ソーシャルブックマークに追加*/
	var sbmurl = [
		'http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t='+encodeURIComponent(document.title)+'&amp;u='+encodeURIComponent(base_url)+'&amp;opener=bm&amp;ei=UTF-8',
		'http://b.hatena.ne.jp/append?'+encodeURIComponent(base_url),
		'http://clip.livedoor.com/redirect?link='+encodeURIComponent(base_url)+'&amp;title='+encodeURIComponent(document.title)+'&amp;ie=UTF-8',
		'http://clip.nifty.com/create?url='+encodeURIComponent(base_url)+'&title='+encodeURIComponent(document.title)
	];
	var param = null;
	if(n==0)param='width=550px,height=480px,status=1,location=0,resizable=1,scrollbars=0';
	window.open(sbmurl[n],'bookmarks',param);
}

function allCheck(obj,target)
{
	if($(obj).attr('checked')){
		$(target).find('input:checkbox').attr('checked','checked');
	}else{
		$(target).find('input:checkbox').removeAttr('checked');
	}
}

function changeSort(sort,order)
{
	$('#sorter').val(sort);
	$('#sorder').val(order);
	document.sfm.submit();
}

function addFavarit(id)
{
	$.get("http://www.realestate-info.jp/favarit/ajax/",{i:id},
	function(data){
		if(data)alert("お気に入り物件に追加しました。");
	});
}




