/**
 * ATHM site - メッセージ出力用
 * @author      ishida
 * @require     jQuery 1.3.2+
 * @version     1.0.0
 */
jQuery.athome = {};

jQuery.athome.message = {
	conf : {
		target :'#popcontents',
		isIE6 :null
	},
	show : function(message) {
		var pop = j$(this.conf.target);
		if (pop.data('visible')) {
			this.hide();
		}
		pop.find('span.close a, p.btn a').click(j$.scope(this, function() {
			this.hide();
			return false;
		})).show().end().find('p.txt').html(message || '');
		if (this.conf.isIE6 == null) {
			this.conf.isIE6 = j$.athome.browser.isIE6();
		}
		pop.setCenterPos().data('visible', true).show();
		if (this.conf.isIE6) {
			//j$('select').css('visibility', 'hidden');
			var iframe = document.createElement('iframe');
			iframe.style.width = pop.outerWidth()+'px';
			iframe.style.height = pop.outerHeight()+'px';
			iframe.style.position = 'absolute';
			iframe.style.left = pop.css('left');
			iframe.style.top = pop.css('top');
			iframe.style.zIndex = '10';
			iframe.style.border = 'none';
			iframe.setAttribute('id', 'messageIframe');
			document.getElementsByTagName('body')[0].appendChild(iframe);
		}
	},
	hide : function() {
		if (!j$.athome.overlay.is() && this.conf.isIE6) {
			//j$('select').not('.dummy').css('visibility', 'visible');
			document.getElementsByTagName('body')[0].removeChild(document.getElementById('messageIframe'));
		}
		j$(this.conf.target).data('visible', false).hide().find(
				'span.close a, p.btn a').unbind('click');
	},
	write : function(html) {
		j$(this.conf.target).find('p.txt').html(html);
	},
	is : function() {
		return (j$(this.conf.target).data('visible'));
	},
	error : {
		maxnum : function() {
			return arguments[0] + 'が' + arguments[1] + '箇所以上選択されています。';
		},
		required : function() {
			return arguments[0] + 'は必須です。';
		},
		noResultList : function() {
			return '申し訳ございません。<br />ご指定の検索条件に該当する物件はありません。';
		}
	}
};

/**
 * http://16c.jp/2008/0528214632.php
 */
jQuery.scope = function(target, func) {
	return function() {
		return func.apply(target, arguments);
	}
};

/**
 * IE6かどうか
 */
jQuery.athome.browser = {
	ie6flg: null,
	isIE6: function(){
		if(this.ie6flg!=null){ return this.ie6flg }
		return this.ie6flg = navigator.userAgent.match(/.*MSIE\s*6\.[0-9].*/)? true: false;
	}
}

/**
 * オーバーレイ
 */
jQuery.athome.overlay = {
	conf: {
		target: '#overlay',
		isIE6: null,
		timer: null
	},
	show: function(){
		var overlay = j$(this.conf.target);
		if(this.conf.isIE6==null){ this.conf.isIE6 = j$.era.browser.isIE6(); }

		if(!overlay.data('visible')){

			if(this.conf.isIE6){
				// for IE
				var doc = j$(document.body);
				j$('select').css('visibility', 'hidden');
				overlay
					.css({
						backgroundColor:'#000', position: 'absolute',
						width: doc.width(), height: doc.height(),
						top:'0px', left: '0px',	opacity: '0.5',	filter: 'alpha(opacity=50)'
					})
					.data('visible', true)
					.show();

				this.conf.timer = setInterval(function(){
					overlay.css({width: doc.width(), height: doc.height()});
				}, 100);
			} else {
				// for others
				overlay
					.css({
						backgroundColor:'#000',	position: 'fixed',
						width: '100%', height: '100%',
						top:'0px', left: '0px',	opacity: '0.5',	filter: 'alpha(opacity=50)'
					})
					.data('visible', true)
					.show();
			}
		}
	},
	hide: function(){
		if(this.conf.isIE6){
			j$('select').not('.dummy').css('visibility', 'visible');
			if(this.conf.timer){ clearInterval(this.conf.timer); }
		}
		j$(this.conf.target)
			.data('visible', false)
			.hide();
	},
	is: function(){
		return (j$(this.conf.target).data('visible'));
	}
};

jQuery.fn.extend({
	/** ポップアップを画面中央に表示する　*/
	setCenterPos: function(){
		var target = j$(this);
		var win = j$(window);
		return target.css({
			left :(win.width() - target.width()) / 2 + win.scrollLeft(),
			top :(win.height() - target.height()) / 2 + win.scrollTop()
		})
	}
});