//Scroller
var scroll_int, scroll_dir, scroll_id, scroll_init;
var scroll_interval = 30, scroll_step = 3;

function init_scroll(obj, direction){
	var oid = '#' + $(obj).attr('rel');
	var ch = $(oid).height();
	var ih = $('div.scroll_content', $(oid)).height();
	var op = $('div.scroll_content', $(oid)).position();
	var min_ot = ch - ih, max_ot = 0;
	if(
		direction < 0 && op.top > min_ot
		||
		direction > 0 && op.top < max_ot
	){
		scroll_init = obj;
		scroll_id = oid;
		scroll_dir = direction;
		$(obj).css({opacity : 1});
		scroll_int = setInterval(process_scroll, scroll_interval);
	}
}
function get_init_scroll(dir){
	return function(){ init_scroll(this, dir); }
}
function stop_scroll(){
	if(scroll_int)
		clearInterval(scroll_int);
	$(scroll_init).css('opacity', .3);
};
function process_scroll(){
	var ch = $(scroll_id).height();
	var ce = $('div.scroll_content', $(scroll_id));
	var ih = ce.height();
	var op = ce.position();
	var min_ot = ch - ih, max_ot = 0;

	var next = op.top + scroll_dir * scroll_step;
	next = Math.min(max_ot, Math.max(min_ot, next));
	ce.css('top', next + 'px');

	if(next == min_ot || next == max_ot)
		stop_scroll();
}

var red_circle_to;
$(function(){
	$('#red_circle').click(function(){
		if(red_circle_to)
			clearTimeout(red_circle_to);
		$(this).stop().animate({opacity : 0}, 300, 'linear', function(){
			this.style.display = 'none';
			$('#content_in').css({opacity : 0}).animate({opacity : 1}, 500);
		});
	});
	$('a.more_link').click(function(){
		var w = 300, h = 400;
		if($(this).attr('rel') != ''){
			var wh = $(this).attr('rel').split(',');
			w = wh[0];
			h = wh[1];
		}
		window.open(this.href, '', 'width=' + w + ',height=' + h + ',menubar=no,toolbar=no,location=yes,directories=no,status=yes,resizable=yes,scrollbars=yes')
		return false;
	});
	$('a').each(function(){
		if($('.building', this).length && !$(this).hasClass('active')){
			$(this)
				.bind('mouseover', function(){
					$('.building', this).stop().animate({opacity : 1}, 500);
				})
				.bind('mouseout', function(){
					$('.building', this).stop().animate({opacity : 0}, 500);
				});
		}
	});
	$('#menu a, #logo a')
		.bind('mouseover', function(){
			if(!$(this).hasClass('active'))
				$('a.active .building').stop().animate({opacity : 0}, 500);
		})
		.bind('mouseout', function(){
			if(!$(this).hasClass('active'))
				$('a.active .building').stop().animate({opacity : 1}, 500);
		});
	$('a.map_it_link').bind('click', function(){
		if(!show_map){
			show_map = true;
			$('#prop_left_photo_block').animate({opacity : 0}, 300, 'linear', function(){
				$(this).css('display', 'none');
				$('#prop_left_map_block').css({display : 'block', opacity : 0, position : 'static'}).animate({opacity : 1}, 300);
			});
		}
		return false;
	});

	//Scroller
	$('div.scroller_top')
		.bind('mouseover', get_init_scroll(+1))
		.bind('mouseout', stop_scroll);
	$('div.scroller_bottom')
		.bind('mouseover', get_init_scroll(-1))
		.bind('mouseout', stop_scroll);
});

function property_init(){
	$('.overlay_trigger').overlay({top : 'center'});
	maps_initialize();

	$('div.ol_right a').bind('click', function(){
		var c = $('#' + $(this).attr('rel'));
		var id = $(this).attr('id');
		$('div.ol_right a', c).removeClass('current_photo');
		$(this).addClass('current_photo');
		$('div.ol_left img', c).stop().css({opacity : 0}).attr('src', '').attr('src', $(this).attr('href')).animate({opacity : 1}, 700);

		return false;
	})
}
