var of = {
	stars_count: 0,
	prevnum: 0,
	currentnum: 0,
	
	star_fade_delay: 1000,
	intervalID: 0,
	click_sign: false,
	$stars: '#omap .stars .star',

	init: function() 
	{
		var _this = this;
		this.stars_count = $('#omap .stars .star').length;
		this.createGlow();

		$('body').click(function() {
			_this.createGlow();
			baloon.hide();			
		});

		$('#omap').click(function() {

		});
		
		$('#baloon').click(function(e) {
			e.stopPropagation();
		});
		
		$('#text_links .pseudo').click(function(e)
		{
			e.stopPropagation();
			_this.destroyGlow();
			_this.showText(this.id.replace('link', ''));
			return false;
		});
		
		$(this.$stars).click(function(e)
		{
			e.stopPropagation();
			_this.destroyGlow();
			_this.showText(this.id.replace('star', ''));
			return false;
		});
	},

	showText: function(num) 
	{
		$(this.$stars).stop(true, true).hide();
		$('#star'+ num).css('opacity', 1).show();
		var text =  $('#star'+ num +'_text').html();
		$('#baloon').removeClass('baloon1 baloon2 baloon3 baloon4 baloon5');
		$('#baloon').addClass('baloon'+num);

		$('#text_links .pseudo').removeClass('active');
		$('#link'+num).addClass('active');

		baloon.text('<p>'+ text +'</p>');
		baloon.show();
	},

	createGlow: function(mode) {
		var _this = this;
		if (window.tt)
			return;

		window.tt = setInterval(function() { _this.glowNext() }, this.star_fade_delay);
		//console.log('CREATED');
	},

	destroyGlow: function() {
		$(this.$stars).clearQueue();
		$(this.$stars).stop(true, true);

		clearInterval(window.tt);
		window.tt = undefined;
		//console.log('DESTROED');
	},

	glowNext: function()
	{
		var starnum = rand(1, this.stars_count);
		// если предыдущая показанная звездочка та же, что и полученная рандомная
		// пытаемся найти следующую
		if (starnum == this.prevnum)
		{
			this.glowNext();
		}
		else
		{
			this.currentnum = starnum;
			this.glowStar(starnum);
			this.prevnum = starnum;
		}
	},

	glowStar: function(num) {
		var sel = "#omap .stars a[id!='"+ this.id +"']";
		// all, but current
		$(sel).fadeOut(this.star_fade_delay);
		//$('#star'+this.prevnum).fadeOut(this.star_fade_delay);
		
		// current star
		$('#star'+num).fadeIn(this.star_fade_delay);
		
		//console.log({num:num, prevnum:this.prevnum})
	}
}

var baloon = {
	text: function(text) {
		$('#baloon').html(text);
	},

	hide: function() {
		$('#baloon').hide();
		//clearTimeout(baloonT);
	},

	show: function() {
		$('#baloon').show();
		//baloonT = setTimeout(baloon.hide, 10000);
	}
}

function rand(min, max) {
    return Math.floor( Math.random() * (parseInt(max) - parseInt(min) + 1) ) + parseInt(min);
}

function map_init() 
{
	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("map_canvas"));
		var center = new GLatLng(55.719555, 37.626719);

		map.setCenter(center, 15);
		var marker = new GMarker(center);
		var html = '<table class="map_info"><tr>';
		html += '<td>E-mail:</td><td><a href="mailto:orehova@advery.ru">orehova@advery.ru</a></td>';
		html += '</tr><tr>';
		html += '<td>Тел./факс:</td><td>(495) 785-56-68</td>';
		html += '</tr><tr>';
		html += '<td>Адрес:</td><td>Москва, улица Павловская, д. 6.</td>';
		html += '</tr></table>';
		
		marker.bindInfoWindowHtml(html);
		map.addOverlay(marker);
	}
}
