var prop = $.browser.msie && $.browser.version < 7 ? "height" : "min-height";

$(function() {
	//smartRollover

	var imgCount = 0;
	var images_pre = new Array();
	$('img[src*="_off."],input[src*="_off."]').each (function(){
		images_pre[imgCount] = new Image();
		images_pre[imgCount].src = $(this).attr("src").replace("_off.", "_on.");
		$(this).hover(
			function () {
				$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
			},
			function () {
				$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
			}
		);
		imgCount ++;
	});



	// opacity Rollover

	$('img[src*="_op."],input[src*="_op."],#sideArea .article .ph a,#sideArea ul.btn li,#sideOtherRoom p a, #latestCheckArea .ph a').hover(
		function () {
			$(this).css('opacity', 0.7);
		},
		function () {
			$(this).css('opacity', 1);
		}
	);



	// pull down

	$("#globalNavi div > ul > li").each(function(){
		if($("ul", this).size() > 0) {
			$("> img", this).unbind('mouseout');
			$(this).hover(
				function(e){
					if ($("ul", this).queue().length<=1) {
						$("ul", this).animate({height:'show', opacity:'show'}, 300).css("z-index", 30);
						$("> img", this).attr("src", $("img:first-child", this).attr("src").replace("_off.", "_on."));
					}
				},
				function(e){
					$("ul", this).animate({height:'hide', opacity:'hide'}, 300).css("z-index", 10);
					$("> img", this).attr("src", $("img:first-child", this).attr("src").replace("_on.", "_off."));
				}
			);
		}
	});




	// search box

	if($("#sideSearch input.text").val() != "物件名、区、駅名など") {
		$("#sideSearch input.text").addClass("focus");
	}
	$("#sideSearch input.text").focus( function() {
		$(this).addClass("focus")
		if($(this).val() == "物件名、区、駅名など") {
			$(this).val("");
		}
	});
	$("#sideSearch input.text").blur( function() {
		if($(this).val() == "物件名、区、駅名など" || $(this).val() == "") {
			$(this).val("物件名、区、駅名など").removeClass("focus");
		}
	}).trigger('blur');



	// external

	var notBlank = new Array("");

	var n = "";
	for (var i = 0; i < notBlank.length; i ++) if(notBlank[i]) n += ":not([href*='" + notBlank[i] + "'])";
	if(document.domain) n += ":not([href*='" + document.domain + "'])";

	$("a[rel='external'], a[href$='.pdf']").attr("target", "_blank");
	$("a[href^=http]"+n).attr("target", "_blank");



	// stripe

	$('.stripe > dl:odd').addClass("odd");
	$('.stripe > dl:even').addClass("even");


	// last

	$('.has_last > dl:last').addClass("last");
	$('#sideNarrowing dd:last').addClass("last");


	// smoothScroll

	$('a[href^=#]').each (function(){
		var hash = this.hash;
		if(hash.length > 1 && !this['rel']){
			$(this).click(function() {
				smoothScroll(hash);
				return false;
			})
		}
	});



	//最近チェックした物件

	if($("#latestCheckArea").size() > 0){
		setScrollArrow("#latestList")
		if($("#latestCheckArea #latestList li").size() < 5){
			$('.prevPage, .nextPage', '#latestList').addClass("disabled");
		}
		areaScroll("#latestList", "#latestInner", 4, 34);
	}
});

function smoothScroll(hash) {
	var target = $(hash).offset().top;

	$(($.browser.safari) ? 'body' : 'html')
		.animate({scrollTop: target >= 15 ? target - 15 : target}, 600, 'swing', function(){$(this).unbind("mousewheel DOMMouseScroll");})
		.bind("mousewheel DOMMouseScroll",function(){
			$(this).queue([]).stop();
			$(this).unbind("mousewheel DOMMouseScroll");
		})
}



// スクロールエリア
function areaScroll(wrap, ele, size, g) {
	$(ele).scrollable({
		size: size,
		speed: 500,
		clickable: false,
		onSeek: function() {
			scrollArrowChange(wrap);
		}
	});

	var h = 0;
	$('.items > li', ele).each (function(){
		h = Math.max(h, $(this).height());
	});
	$('.items > li', ele).css(prop, h);

	$('.prevPage span, .nextPage span', wrap).height($(wrap).height() + g);

	scrollArrowChange(wrap);
}
function setScrollArrow(wrap) {
	var prev = $('<p><span></span></p>');
	var next = $('<p><span></span></p>');
	prev.addClass('prevPage');
	next.addClass('nextPage');
	$(wrap).append(prev);
	$(wrap).append(next);
}


function scrollArrowChange(wrap) {
	$('.prevPage, .nextPage', wrap).each( function(){
		if($(this).hasClass("disabled")) {
			$('span', this).unbind().css('cursor', 'default').addClass('disabled').removeClass('hover');
		}else {
			$('span', this).removeClass('disabled');
			$('span', this).hover(
				function () {
					$(this).addClass('hover');
				},
				function () {
					$(this).removeClass('hover');
				}
			).css('cursor', 'pointer');
		}
	});
}

