/* CSSハック用 */
/* ----------------------------------------------------- */
var css_browser_selector = function() {
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}();

/* ----------------------------------------------------- */
/* jquery ロード*/
/* ----------------------------------------------------- */
$(function(){

	/* aタグクリック時の枠線無効に */
	/* ----------------------------------------------------- */
	$('a').focus(
		function(){
		this.blur();
	});

	/* 関数Call */
	/* ----------------------------------------------------- */
	/* 物件詳細お問い合わせページへの物件名引継ぎ */
	setNum('#contents a.to-inq');
	
	/* topおすすめ物件情報3件ごとに高さ揃える */
	flat('#home-wrap #recom-article dd.wrap','dl',3);

	/* 過去チラシ一覧3件ごとに高さ揃える */
	flat('#flyer-back-num','>dl',3);
	
	/* 画像拡大 */
	zoomPhoto('#contents a.zoom');
	
	/* ロールオーバー */
	/* ----------------------------------------------------- */
	$('img.over').not('[src*="-o."]').hover(function()
	{
		Rollover($(this),$(this).attr('src'));	
	},function() {
		Rollover($(this),$(this).attr('src'));	
	});	

	/* ページ内スクロール */
	/* ----------------------------------------------------- */
  $('a[href^=#]').click(function(event) {
    var hash = this.hash;
		if(hash!==''){
			var tarPosition = $(hash).offset().top;
			if(undefined !== window.opera && "function" === typeof window.opera.version){ // opera
				$('html').animate({scrollTop: tarPosition}, 400, 'easeOutQuad'); 
			}
			else{
				$('html, body').animate({scrollTop: tarPosition}, 400, 'easeOutQuad');
			}
			return false;
		}
  });

	/* addclass */
	/* ----------------------------------------------------- */
	$('#gnav li:last-child').addClass('last-c');
	$('#side #news dd.wrap dd:last').addClass('last-c');
	$('div.intermediate-list dd.wrap div dl:last-child').addClass('last-c');
	$('dl.joyhouse-list dd.wrap div dl:last-child').addClass('last-c');
	$('#sitemap li:first').addClass('first');
	$('#sitemap li:last-child').addClass('close');

	/* 仲介物件一覧　soldoutの画像を縦センターに */
	if($('div.intermediate-list dd.wrap p.price').size()>0){
		$('div.intermediate-list dd.wrap p.price').each(function(){
			var obj = $(this);
			var marT = eval((obj.next('div').height()-$('img',obj).height())/2-6);
			$('img',obj).css('margin-top',marT+'px')
		
		});
	}

	/* 物件詳細　お問い合わせボタンに物件名設定 */
	/* ----------------------------------------------------- */
	if($('#environ-map').size()===1){
		$('#contents a.to-inq').each(function(){
			var aName = $('#contents h2:first').text();
			$(this).attr('title',aName);
		});
	}

	/* フォーム */
	/* ----------------------------------------------------- */
	$("table input,table textarea").focus(function(){
		$(this).addClass('focus');															
  });
	$("table input,table textarea").blur(function(){
		$(this).removeClass('focus');															
  });

});

/* ----------------------------------------------------- */
/*　ページ内スクロール用　*/
/* ----------------------------------------------------- */
jQuery.easing.easeOutQuad = function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
};

/* ----------------------------------------------------- */
/*　ロールオーバー　*/
/* ----------------------------------------------------- */
var Rollover = function(obj,thisSrc) {
	/* 変数宣言 */
	var sTempSrc = thisSrc;
	var ftype = thisSrc.substring(thisSrc.lastIndexOf('.'), thisSrc.length);
	var hsrc = thisSrc.replace(ftype, '-o'+ftype);	
	var aPreLoad = new Image();

	/* プリロード */
	aPreLoad.src = hsrc;

	re = new RegExp('-o[\.]');
	
	if(!sTempSrc.match(re)){
		$(obj).attr('src', hsrc);
	}
	
	if (sTempSrc.match(re)) {
		sTempSrc = $(obj).attr('src').replace('-o'+ftype, ftype);
		$(obj).attr('src', sTempSrc);
	}
}

/* ----------------------------------------------------- */
/* flatHeights */
/* ----------------------------------------------------- */
var flat = function(obj,flatobj,num){
	var sets = [], temp = [];
	/* numごとで高さ揃える */
		$(obj).find(flatobj).each(function(i) {
				temp.push(this);
				if ((i+1) % num == 0) {
						sets.push(temp);
						temp = [];
				}
		});
		if (temp.length) sets.push(temp);

		/* 各組ごとに高さ揃え */
		$.each(sets, function() {
				$(this).flatHeights();
		});
		sets = [], temp = [];
}

/* ----------------------------------------------------- */
/* 物件名引継ぎ */
/* ----------------------------------------------------- */
var setNum = function(obj){
	$(obj).click(function(){
		var Insert_No = new Array();
		var Parameter = new Array();	
		Parameter[0] = "No_Name";
		var Str,Target,Value;
		var Set_Url ="";
		Value = "";
		URL =$(this).attr("href");
		Str = $(this).attr("title");
		Value += Str;
		Set_Url += "?" +Parameter[0]  + "=" + escape(Value);
		URL = URL + Set_Url;
		$(this).attr("href",URL)
	});
}

/* ----------------------------------------------------- */
/* 画像拡大 */
/* ----------------------------------------------------- */
var zoomPhoto = function(obj){
		//表示画像の幅・高さ取得　自身にclass付加（cashImg0'+(i+1)）
		$(obj).each(function(i){
			var bigPhotosrc = $(this).attr('href');
			$(this).addClass('cashImg0'+(i+1))
			var cashImg = '<img src="'+ bigPhotosrc + '" class="cashImg" id="cashImg0'+(i+1)+'" />'
			$("body").append(cashImg);
		});
		
		$(obj).click(function(i){
			//IEはselect非表示に
			if(jQuery.browser.msie){
				$('select').css('visibility','hidden');
			}
			
			//自身のクラスから表示する画像が格納されている非表示の要素のID取得
			var tar = '#'+$(this).attr('class').split(' ')[1];
			
			//拡大画像のパス
			var bigPhotosrc = $(this).attr('href');
			var bigPhoto = '<p id="zoomPhoto"><img src="'+ bigPhotosrc + '" /><span id="close">×CLOSE</span></p>'
			
			//ローディングイメージのパス
			var loadImg = 'img/load.gif'  
			var loading = '<p id="zoomPhoto"><img src="'+ loadImg + '" /></p>'
			
			//画像データキャッシュ
			var aPreLoad = new Image();
			aPreLoad.src = bigPhotosrc;
			
			//zoomPhotoの表示座標
			if(undefined !== window.defaultstatus){ //Safari　Google Chrome 
				var top = $('body').scrollTop();
			}
			if ( undefined !== window.Components) { //Firefox
				var top = $('html').scrollTop();
			}
			if ( jQuery.browser.msie && !(jQuery.browser.version < 7)) { // over IE6
				var top = $('html').scrollTop();
			}
			if(undefined !== window.opera && "function" === typeof window.opera.version){ // opera
				var top = $('html').scrollTop();
			}
			if ( jQuery.browser.msie && jQuery.browser.version == 6) { // under IE6
				var top = $('body').scrollTop();
			}
			var left = $(window).width();

			//zoomPhotoが存在しない場合
			if($('#zoomPhoto').size()===0){
				//拡大画像読み込み完了
				if(aPreLoad.complete){
					$("body").append(bigPhoto);
					$('#zoomPhoto').css('left',Math.floor((left-$('#zoomPhoto').width())/ 2)).css('top',Math.floor(top+($(window).height()-$('#zoomPhoto').height())/ 2));
				}
				//拡大画像読み込み未完了
				else{
					$("body").append(loading);
					$('#zoomPhoto').css('left',Math.floor((left-$('#zoomPhoto').width())/ 2)).css('top',Math.floor(top+($(window).height()-$('#zoomPhoto').height())/ 2));
					//画像読み込み完了時に差し替え
					aPreLoad.onload = function(){
						$('#zoomPhoto').remove();
						$("body").append(bigPhoto);
					$('#zoomPhoto').css('left',Math.floor((left-$('#zoomPhoto').width())/ 2)).css('top',Math.floor(top+($(window).height()-$('#zoomPhoto').height())/ 2));
					}
				}
			}
			//zoomPhotoが存在する場合
			else{
				$('#zoomPhoto').remove();
				$("body").append(bigPhoto);
					$('#zoomPhoto').css('left',Math.floor((left-$('#zoomPhoto').width())/ 2)).css('top',Math.floor(top+($(window).height()-$('#zoomPhoto').height())/ 2));
			}
			return false;
		});
		
		//zoomPhotoクリックで閉じる
		$('#close').live("click", function(){
			$('#zoomPhoto').remove();													 
				$('select').css('visibility','visible');
		});
}
