var RealtyTape = function ()
{
	var that = {};
	
	that.init = function(control)
	{
		that.control = control;
		that.tapes_items = that.control.find('> .tape[est_target][est_type]');
		that.left = that.control.find('.scroller.left:first');
		that.left_control = that.left.find('span');
		that.right = that.control.find('.scroller.right:first');
		that.right_control = that.right.find('span');
		
		that.find_tape();
		
		that.anim_speed = 250;
		
		that.left_control.click(function()
		{
			that.scroll_left();
		});
		that.right_control.click(function()
		{
			that.scroll_right();
		});
		
		that.check_left();
		that.check_right();
		
		return that;
	}
	
	that.toggle = function(target, type)
	{
		var active_items = that.tapes_items.filter('[est_target=' + target + '][est_type=' + type + ']');
		that.tapes_items.filter(':visible').hide();
		active_items.show();

		that.find_tape();
		that.check_left();
		that.check_right();

		if (active_items.attr('est_count') != 0)
		{
			$('.scroller', that.control).show().removeClass('hidden');
		}
		else
		{
			$('.scroller', that.control).hide();
			active_items.css('left', 0);
		}
	}
	
	that.find_tape = function(){
		that.tape = that.control.find('.tape:visible');
//		that.tape.get(0).style.left = that.left.width() + 'px';
	}
	
	that.scroll_left = function()
	{
		if(that.check_left() && !$(this).hasClass('disabled') && !that.tape.is(":animated"))
		{
			to_scroll = that.hidden_tape_width - that.item_width * Math.floor(that.hidden_tape_width / that.item_width);
			if (to_scroll < 0.2 * that.item_width && that.hidden_tape_width >= that.item_width)
			{
				to_scroll += that.item_width;
			}
			
			that.tape.animate({ left: "+=" + to_scroll }, that.anim_speed, null, function(){
				that.check_left();
				that.check_right();
			});

			jRealtyTapePopup.addClass('hidden');
		}
	}
	
	that.scroll_right = function()
	{
		if(that.check_right() && !$(this).hasClass('disabled') && !that.tape.is(":animated"))
		{
			to_scroll = that.tape_width - that.visible_tape_space - that.item_width * Math.floor((that.tape_width - that.visible_tape_space) / that.item_width)
			if (to_scroll < 0.2 * that.item_width && that.tape_width - that.visible_tape_space >= that.item_width)
			{
				to_scroll += that.item_width;
			}
			
			to_scroll += 15;
			
			$(that.tape).animate({ left: "-=" + to_scroll }, that.anim_speed, null, function()
			{
				that.check_left();
				that.check_right();
			});
			
			jRealtyTapePopup.addClass('hidden');
		}
	}
	
	that.check_left = function()
	{
		that.item_width = that.tape.find('.item').outerWidth(true);
		var hidden_tape_width = that.tape.position().left; // - that.left.width(); 
		that.hidden_tape_width = Math.abs(hidden_tape_width);
		
		if(hidden_tape_width < 0)
		{
			that.left_control.removeClass('disabled');
			return true;
		}
		else
		{
			that.left_control.addClass('disabled');
			return false;
		}		
	}
	
	that.check_right = function()
	{
		that.item_width = that.tape.find('.item').outerWidth(true);
		that.visible_tape_space = that.control.outerWidth() - that.left.width() - that.right.width();
		that.tape_width = that.tape.position().left + that.item_width * that.tape.find('.item').length; // - that.left.width();
		
		if (that.visible_tape_space < that.tape_width)
		{
			that.right_control.removeClass('disabled');
			return true;
		}
		else
		{
			that.right_control.addClass('disabled');
			return false;
		}
	}
	
	return that;
};

var SubtypeSwitcherHome = $.inherit( SubtypeSwitcher,
{
	__constructor : function(jContainer, oRealtySwitcher, oRequestSwitcher, oTapeControl)
	{
		oRealtySwitcher.addEventListener(
			SwitcherBar.EVT_ON_TOGGLE,
			function(that) { return function()
			{
				$("#realty_tape_popup").addClass("hidden");
				$(".realty_tape_popup_holder").removeClass('tape-demand');

				oTapeControl.toggle(
					oRequestSwitcher.getTarget(),
					oRealtySwitcher.getType()
				);
			}}(this)
		);
	
		this.__base(jContainer, oRealtySwitcher, oRequestSwitcher);
	}
});


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);

		oTapeControl = RealtyTape().init( $('#realty_tape') );
		
		oTapeControl.toggle(
			oRequestSwitcherBar.getTarget(),
			oRealtySwitcherBar.getType()
		);
		
		$(window).resize(function()
		{
			oTapeControl.check_left();
			oTapeControl.check_right();
		});
	
		oSubtypeSwitcher = new SubtypeSwitcherHome($('#realty_subtype', jContext), oRealtySwitcherBar, oRequestSwitcherBar, oTapeControl);
		oChooserPanel = new ChooserPanel($('#commercial_chooser_set'), oRealtySwitcherBar, oRequestSwitcherBar);
	}
	new RegionDropdown($('#region_switcher'), $('#current_region'));
	
	initRealtyTapePopup();	
}



function initRealtyTapePopup() {
	
	jRealtyTapePopupHolder = $('.realty_tape_popup_holder');
	jRealtyTapePopup = $('#realty_tape_popup');
	jRealtyTapePopupContent = $('.container', jRealtyTapePopup);
	eRealtyTapePopupContent = jRealtyTapePopupContent.get(0);
	
	iPopupWidth = 215;
	
	if (jRealtyTapePopup.width() && iPopupWidth != jRealtyTapePopup.width()) {
		iPopupWidth = jRealtyTapePopup.width();
	}

	$('#realty_tape div.item a').bind('mouseenter',

		$.throttle(
			function(e){
				var jItem = $((e || window.event).target || (e || window.event).srcElement).parents('div.item:first');
				
				//if (targ.nodeType == 3) // defeat Safari bug
				//targ = targ.parentNode;
				
				jRealtyTapePopupContent.html('').append($('.popup-content', jItem).clone(false)).append('<div class="clear"></div>');
				
				iNewLeft = $('a.h-a', jItem).offset().left;
				
//				document.title = jRealtyTapePopup.width();
				
				if ($(document).width() < iNewLeft + iPopupWidth - ($.browser.msie && $.browser.version < 7 ? 0 : 16)) {
					iNewLeft = $(document).width() - iPopupWidth + ($.browser.msie && $.browser.version < 7 ? -5 : 16); // .realty_tape_popup_holder left
				}
				
				if (iNewLeft < 0) {
					iNewLeft = 16;
//					iNewLeft = ($.browser.msie ? 16 : 16); // .realty_tape_popup_holder left
				}
				
				if (jItem.hasClass('item-demand')){
					jRealtyTapePopupHolder.addClass('tape-demand');
				}
				
				
				jRealtyTapePopup
					.css('left', iNewLeft + 'px')
					.removeClass('hidden')
				;
				
				
				if (jRealtyTapePopup.width() && iPopupWidth != jRealtyTapePopup.width()) {
					iPopupWidth = jRealtyTapePopup.width();
				}
			},
			400
		)

	);
	
	$('.close', jRealtyTapePopup).click(function(){
		jRealtyTapePopup.addClass('hidden');
		jRealtyTapePopupHolder.removeClass('tape-demand');
	});
	jRealtyTapePopup.bind('mouseleave', function(){
		jRealtyTapePopup.addClass('hidden');
		jRealtyTapePopupHolder.removeClass('tape-demand');
	});

}