var SwitcherBar = $.inherit(
{
	__constructor : function(jContainer)
	{
		this.jContainer = jContainer;
		this.jjItem = this.jContainer.find('li');
		this.jSelector = this.jjItem.filter('.selector:first');
		this.jActiveItem = this.getActiveItem();
		this.jInitialItem = this.jActiveItem;
		this.jDecoration = this.jSelector.find('ins');
		this.oListenerList = {};

		this.jSelector.css(this.self.oSelectorInitialCss);
		if (this.jActiveItem.length)
			this.jSelector.css(this.getGeometry());
		
		this.bRestoreMode = false;
			
		this.activateItem();
		this.bindItemHandler();
if(document.getElementById("realty_switcher")) {

	if (window.location !='http://est.ua/new_request/' && window.location !='http://www.est.ua/new_request/') {
	document.getElementById("realty_switcher").style.visibility = "visible";
	document.getElementById("realty_subtype").style.visibility = "visible";
	}
	
	
	//(16.09.2009 Ж)
//	if (window.location=='http://est.ua/new_request/' || window.location=='http://www.est.ua/new_request/') {
	if (window.location.pathname=='/new_request/') {
//	this.jActiveItem.removeClass('active selector selected');
	var jFader = $('#content_fader');
	jFader.removeClass('hidden');
	//document.getElementById("content").innerHTML="<noindex><h1>Завершите ваш выбор</h1></noindex>";
	//document.getElementById("realty_switcher").style.visibility = "hidden";
	//document.getElementById("realty_subtype").style.visibility = "hidden";
//	this.jDecoration.hide();
	}
}

	},
	
	getActiveItem : function()
	{
		return this.jjItem.filter('.selected:first');
	},
	
	getGeometry : function()
	{
		return {
			'left': this.jActiveItem.position().left,
			'width': this.jActiveItem.outerWidth()
		};
	},
	
	activateItem : function()
	{
		this.jSelector.hide();
		this.jActiveItem.addClass('active selector');
		this.jDecoration.appendTo(this.jActiveItem);
	},
	
	deselectActiveItem : function()
	{
		this.jDecoration.hide();
		this.jActiveItem.removeClass('active selector selected');
	},
	
	bindItemHandler : function()
	{
		this.jjItem.click( function(that) { return function(event)
		{
			if ( ! $(this).hasClass('active') )
			{
				that.jSelector.css('display', 'inline-block');
				if (that.jActiveItem.length)
				{
					that.jActiveItem.removeClass('active selector selected');
					that.jDecoration.show().appendTo(that.jSelector);
				}
				that.jActiveItem = $(this);
				that.toggle();
				
				event.preventDefault();
				event.stopPropagation();
			}
		}}(this));
	},
	
	toggle : function()
	{
		if (this.jActiveItem.find('a').length)
		{
			window.location = this.jActiveItem.find('a').attr('href');
		}
		this.triggerEvent(this.self.EVT_BEFORE_TOGGLE);
		this.jSelector.animate(
			this.getGeometry(),
			'fast',
			'linear',
			function(that) { return function()
			{
				that.activateItem();
				that.triggerEvent(that.self.EVT_ON_TOGGLE);
				if (that.bRestoreMode)
				{
					that.bRestoreMode = false;
					that.triggerEvent(that.self.EVT_ON_RESTORE);
				}
			}}(this)
		);
	},
	
	restoreInitial : function()
	{
		this.bRestoreMode = true;
		this.jInitialItem.click();
	},
	
	addEventListener : function(eventId, callback)
	{
		if(! this.oListenerList[eventId])
		{
			this.oListenerList[eventId] = [];
		}
		this.oListenerList[eventId].push(callback);
	},
	
	triggerEvent : function(eventId)
	{
		if (this.oListenerList[eventId])
		{
			jQuery.each(this.oListenerList[eventId], function(){this()});
		}
	}
},{
	EVT_BEFORE_TOGGLE : 1,
	EVT_ON_TOGGLE : 2,
	EVT_ON_RESTORE : 3,
	
	oSelectorInitialCss : {
		'padding': 0,
		'position': 'absolute'
	}
});


var RequestSwitcherBar = $.inherit( SwitcherBar,
{
	getTarget : function()
	{
		return this.jActiveItem.attr('est_target');
	}
});

var RealtySwitcherBar = $.inherit( SwitcherBar,
{
	__constructor : function(jContainer, oRequestSwitcher)
	{
		this.__base(jContainer);
		
		oRequestSwitcher.addEventListener(
			SwitcherBar.EVT_BEFORE_TOGGLE,
			function(that) { return function()
			{
				that.reset();
			}}(this)
		);
	},
	
	getActiveItem : function()
	{
		var jItem = this.jjItem.filter('.selected:first');
		return jItem.length ? jItem : $(this.jjItem.get(0));
	},
	
	reset : function()
	{
		this.deselectActiveItem();
		this.jContainer.fadeTo(
			'normal',
			0.01,
			function(that) { return function()
			{
				that.jContainer.fadeTo('normal', 100)
			}}(this)
		);
	},
	
	getType : function()
	{
		return this.jActiveItem.attr('est_type');
	}
});

var SubtypeSwitcher =  $.inherit(
{
	__constructor : function(jContainer, oRealtySwitcher, oRequestSwitcher)
	{
		this.jContainer = jContainer;
		this.oRealtySwitcher = oRealtySwitcher;
		this.oRequestSwitcher = oRequestSwitcher;
		this.jjItem = this.jContainer.find('li');
		this.jActiveItem = this.jjItem.filter('.selected:first');
		
		this.oRealtySwitcher.addEventListener(
			SwitcherBar.EVT_ON_TOGGLE,
			function(that) { return function()
			{
				that.toggle();
				that.toggleByParent();
			}}(this)
		);
		
		this.oRealtySwitcher.addEventListener(
			SwitcherBar.EVT_ON_RESTORE,
			function(that) { return function()
			{
				that.restore();
			}}(this)
		);
		
		this.oRequestSwitcher.addEventListener(
			SwitcherBar.EVT_BEFORE_TOGGLE,
			function(that) { return function()
			{
				that.toggle();
				that.toggleByRoot();
			}}(this)
		);
		
		this.toggle();
	},
	
	toggle : function()
	{
		this.sType = this.oRealtySwitcher.getType();
		this.sTarget = this.oRequestSwitcher.getTarget();

		this.jjItem.filter('[est_type!=' + this.sType + ']').hide();
		this.jjActiveItem = this.jjItem.filter('li[est_type=' + this.sType +']');

		this.jjActiveItem.show().each(function(that) { return function()
		{
			var jItem = $(this);
			var iCount = parseInt(h_chooser_count[that.sTarget + '_' + jItem.attr('est_subtype')]) || 0;
			var jLink = jItem.find('a');
			if (iCount > 0)
			{
				jLink.removeClass('blank').attr('href', jLink.attr('href-orig') + that.sTarget + '/');
			}
			else
			{
				that.makeItemBlank(jLink, that.sTarget);
			}
			jItem.find('span.count').text(iCount);
		}}(this));
	},
	
	makeItemBlank : function(jItem, sUri)
	{
		jItem.addClass('blank').removeAttr('href');
	},
	
	toggleByParent : function()
	{
		this.jjActiveItem.show();
		this.jActiveItem.removeClass('selected');
document.getElementById("realty_subtype").style.visibility = "visible";
	},

	toggleByRoot : function()
	{
		this.jjActiveItem.hide();
document.getElementById("realty_switcher").style.visibility = "visible";
	},
	
	restore : function()
	{
		this.jActiveItem.addClass('selected');
	}
});

var RegionDropdown = function (jContainer, jLink)
{
	var jLayout = $('#layout');
	var jBody = $(document.body); 
	jBody.append(jContainer);
	
	var that = popup_block({
		container: jContainer,
		link: jLink,
		keep: true,
//		fader: $('#fader'),
		close: jContainer.find('span.btn-close'),

		onBlockShow: function(options){
			jLink.addClass('active');
			jBody.append(jLink);
		},

		onBlockHide: function(options){
			jLink.removeClass('active');
			jLayout.append(jLink);
		}
	});
	return that;
};

var ChooserPanel = function (jContainer, oRealtySwitcherBar, oRequestSwitcherBar)
{
	var that = {};
	//var bOperationMode = false;
	var jFader = $('#content_fader');
	
	/*
	jContainer.find('> span.btn-close:first').click(function(event)
	{
		that.cancel();
		
		event.preventDefault();
		event.stopPropagation();
	});
	*/
	
	jFader.find('p > span').click(function()
	{
		that.cancel();
	});
	
	oRequestSwitcherBar.addEventListener(
		SwitcherBar.EVT_BEFORE_TOGGLE,
		function() {
			//bOperationMode = true;
			//that.onHoverOver();
			that.fadeContent();
		}
	);

	oRealtySwitcherBar.addEventListener(
		SwitcherBar.EVT_BEFORE_TOGGLE,
		function(){
			//bOperationMode = true;
			//that.onHoverOver();
			that.fadeContent();
		}
	);
	
	/*
	jContainer.hover(
		function() {
			that.onHoverOver();
		},
		function() {
			that.onHoverOut();
		}
	)
	.click(function(){
		$(this).addClass('hover');
	});
	*/
	
	/*
	that.onHoverOver = function()
	{
		jContainer.addClass('hover');
	}
	
	that.onHoverOut = function()
	{
		if (! bOperationMode )
		{
			jContainer.removeClass('hover');
		}
	}
	*/
	
	that.fadeContent = function()
	{
		jFader.removeClass('hidden');
	}
	
	that.unfadeContent = function()
	{
		jFader.addClass('hidden');
	}
	
	that.cancel = function()
	{
		oRequestSwitcherBar.restoreInitial();
		oRealtySwitcherBar.restoreInitial();
		jContainer.removeClass('hover');
		that.unfadeContent();
		bOperationMode = false;
	}

	return that;
};

$(document).ready(function()
{
	window.headSwitcherInit();
});

window.headSwitcherInit = function()
{
	var jContext = $('#chooser');
	var jRequestSwitcher = $('#request_switcher', jContext);
	if (jRequestSwitcher.length)
	{
		oRequestSwitcherBar = new RequestSwitcherBar(jRequestSwitcher);
		oRealtySwitcherBar = new RealtySwitcherBar($('#realty_switcher', jContext), oRequestSwitcherBar);
		oSubtypeSwitcher = new SubtypeSwitcher($('#realty_subtype', jContext), oRealtySwitcherBar, oRequestSwitcherBar);
		oChooserPanel = new ChooserPanel($('#commercial_chooser_set'), oRealtySwitcherBar, oRequestSwitcherBar);
	}
	new RegionDropdown($('#region_switcher'), $('#current_region'));
}
