/**
 * estate.js
 * @see jquery.js
 */

/**
 * [$.scope] on jQuery
 */
jQuery.scope = function(t, f) {
	return function() {
		return f.apply(t, arguments);
	}
};

/**
 * estateClass
 */
function estateClass() {
    this.initialize.apply(this, arguments);
};

estateClass.prototype = {

	/**
	 * initialize
	 */
	initialize : function() {
		this._tmp = '';
		this._params = null;
	},

	/**
	 * init
	 */
	init : function(params) {
		if(!this._params) this._params = params;
		this.nav(this._params.nav);
		this.null2hide(this._params.pulldown);
		this.rocr(this._params.rocr);
		this.viewFnav(this._params.fnav);
		var tm = setTimeout($.scope(this, function() { this.slideView(this._params.slv);}, 1000));
	},

	/**
	 * rocr
	 */
	rocr : function(cn, r) {
		if(!$('.' + cn).length) return;

		$('.' + cn).each(function() {
			if($(this).children('.rocr-inner').length) return;
			var bg = $(this).css('background-image');
			if(!bg) return;
			var padding = {
				top: parseInt($(this).css('padding-top').replace('px', '')),
				right: parseInt($(this).css('padding-right').replace('px', '')),
				bottom: parseInt($(this).css('padding-bottom').replace('px', '')),
				left: parseInt($(this).css('padding-left').replace('px', ''))
			};
			var doc = '<div class="rocr-inner">' + $(this).html() + '</div>';
			$(this).html([
				'<span class="rocr-tl"></span>',
				'<span class="rocr-tr"></span>',
				'<span class="rocr-bl"></span>',
				'<span class="rocr-br"></span>',
				doc
			].join(''));
			if(!r) var r = 10;

			$(this).css({
				position:'relative',
				backgroundRepeat: 'no-repeat',
				padding: '0px'
			});
			$(this).children('.rocr-inner').css({
				display: 'block',
				position: 'relative',
				paddingTop: padding.top + r + 'px',
				paddingRight: padding.right + r + 'px',
				paddingBottom: padding.bottom + r + 'px',
				paddingLeft: padding.left + r + 'px',
				margin: '0'
			});
			$(this).children('.rocr-tl').css({
				display: 'block',
				position: 'absolute',
				width: '100%',
				height: $(this).height() + 'px',
				backgroundImage: bg,
				backgroundRepeat: 'no-repeat',
				backgroundPosition : 'left top',
				top: '0px',
				left: '0px'
			});
			$(this).children('.rocr-tr').css({
				display: 'block',
				position: 'absolute',
				backgroundImage: bg,
				backgroundRepeat: 'no-repeat',
				top: '0px',
				height: $(this).height() + 'px',
				width: r + 'px',
				backgroundPosition : 'right top',
				right: '0px'
			});
			$(this).children('.rocr-bl').css({
				display: 'block',
				position: 'absolute',
				backgroundImage: bg,
				backgroundRepeat: 'no-repeat',
				backgroundPosition : 'left bottom',
				height: r + 'px',
				width: '100%',
				left: '0px',
				bottom: '0px'
			});
			$(this).children('.rocr-br').css({
				display: 'block',
				position: 'absolute',
				backgroundImage: bg,
				backgroundRepeat: 'no-repeat',
				backgroundPosition : 'right bottom',
				height: r + 'px',
				width: r + 'px',
				right: '0px',
				bottom: '0px'
			});
		});
	},

	/**
	 * nav
	 */
	nav : function(navul) {
		$(navul + ' li').hover(
			function() {
				$('ul', this).css({
					display: 'block',
					position: 'absolute',
					zIndex: '100',
					top: $(this).height() + 'px',
					left: '0px',
					width: 'auto',
					minWidth: $(this).width() + 'px'
				});
			},
			function() { $('ul', this).css('display', 'none'); }
		);
	},

	/**
	 * null2hide
	 */
	null2hide : function(elms) {
		$(elms).each(function() {
			if($(this).children('ul').length) return;
			$(this).hide();
		});
	},

	/**
	 * viewFnav
	 */
	viewFnav : function(elval) {
		for(var i = 0; i < elval.length; i++) {
			var elm = elval[i].split(',');
			if($(elm[0] + ':hidden').length) $(elm[1]).hide();
		}
	},

	/**
	 * askForm
	 */
	askForm : function(htdoc) {
		var rx = new RegExp(htdoc, 'i');
		if(!document.location.toString().match(rx)) return;
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		if($('input[name="cat"]').length) {
			var cat = $('input[name="cat"]').val();
		}
		for(var i = 0; i < parms.length; i++) {
			var pos = parms[i].indexOf('=');
			var k = parms[i].substring(0 , pos);
			var v = parms[i].substring(pos + 1);
			if(!$('input[name="' +k+ '"]').val()) $('input[name="' +k+ '"]').val(v);
			if(k == 'cat') var cat = v;
		}
		if($('form.rform').length + $('form.rpform').length + $('form.sform').length < 1) return;

		$(':input').attr('disabled','disabled');
		$('form').hide();

		if(cat == 'rh' || cat == 'rt' || cat == 'mm') {
			$('form.rform').show();
			$('form.rform :input').removeAttr('disabled');
		} else if(cat == 'rp') {
			$('form.rpform').show();
			$('form.rpform :input').removeAttr('disabled');
		} else {
			$('form.sform').show();
			$('form.sform :input').removeAttr('disabled');
		}
	},

	/**
	 * searchForm
	 */
	searchForm : function(s, h) {
		if($('#rentform') != null && $('#saleform') != null) {
			$('#' . h).hide();
			$('#' . s).show();
		}
	},

	/**
	 * slideView
	 */
	slideView : function(e) {
		if(!$(e).length) return;
		var jsv = new slideViewerClass();
		jsv.add();
	},

	/**
	 * searchTab
	 */
	searchTab : function(t, tabName) {
		if(!$('.' + tabName).length) return;
		// first
		if(!t) {
			var t = $('.' +tabName+ ' > ul > li:first').attr('class').split(' ')[1];
		}
		$('.' +tabName+ ' > div.stab').hide();
		$(':input').attr('disabled','disabled');
		$('.' +tabName+ ' > div.' +t).show();
		$(':visible:input').removeAttr('disabled');
		$('#hidden_params > input').removeAttr('disabled');
		$('.' +tabName+ ' > ul > li > a').removeClass('current');
		$('.' +tabName+ ' > ul > li.' +t+ ' > a').addClass('current');
		var f = this.findParentByTagName($('li.' + t).get(0), 'form');
		$(f).attr('action', $(f).attr('action').replace(/^.*$/, '/search/' +t+ '.html'));
	},

	/**
	 * findParentByTagName
	 */
	findParentByTagName : function(elm, name) {
		var reg = new RegExp('^' +name+ '$', 'i');
		if(elm.parentNode.nodeName.match(reg)) return elm.parentNode;
		return this.findParentByTagName(elm.parentNode, name);
	}
};

/**
 * instance
 */
var estate;
$(document).ready(function() {
	estate = new estateClass();
	estate.init({
		nav: '#nav',
		pulldown: 'li.nrent, li.nsale',
		fnav: ['li.nrent,li.frent', 'li.nsale,li.fsale'],
		rocr: 'rocr',
		slv: 'div.j-ibox'
	});
	estate.askForm('ask.html');
	estate.askForm('thanks.html');
	estate.searchTab(null, 'searchtab');
});

