
var baloons = ({
	sanfrancisco: ({
		px_top: 22, px_left: 63, px_width: 441, px_height: 119,
		background: "baloon_sanfrancisco.png",
		arrow_direction: "s-w",
		//buttons: ["Follow", "Reply"],
		posts: [
			({ usr: "Sean",	img: "sean.gif", comments:"5 Comments", share:"Share",	dt: "a minute ago",	txt: "Here are the photos form the party yesterday: <strong>http://www.flickr.com/phot...321/</strong>"	}),
			({ usr: "Dan",	img: "dan.gif",	comments:"12 Comments", share:"Share", dt: "a minute ago",	txt: "Good job, it's like driving a Porsche compared to their interface!"	}),
			({ usr: "Ryan",	img: "ryan.gif", comments:"1 Comment",	share:"Share", dt: "a minute ago",	txt: "Carbon dioxide in the atmosphere actually is caused by volcanoes eruptions. It seems that global warming is a natural process."	})
		]
	}),
	plovdiv: ({
		px_top: 1, px_left: 490, px_width: 441, px_height: 106,
		background: "baloon_plovdiv.png",
		arrow_direction: "s-w",
		//buttons: ["Проследи", "Отговори"],
		posts: [
			({ usr: "Никола",	img: "nick.gif", comments:"Коментар", share:"Сподели",	dt: "преди минута",	txt: "Нивото на Дунав се покачва след проливни дъждове."	}),
			({ usr: "Петър",	img: "pesho.gif", comments:"7 Коментари", share:"Сподели",	dt: "преди минута",	txt: "Утре е концертът на AC/DC на стадион Васил Левски. Ще се видим там."	})
		]
	}),
	rio: ({
		px_top: 130, px_left: 272, px_width: 441, px_height: 106,
		background: "baloon_rio.png",
		arrow_direction: "s-w",
		//buttons: ["Seguir", "Responder"],
		posts: [
			({ usr: "Miguel",	img: "miguel.gif", comments:"25 Comentários", share:"Compartilhar" ,	dt: "um minuto atrás",	txt: "Empresa vende clones de Mac com o Snow Leopard. <strong>http://macworldbrasil.uol.com.br/noticia...pard</strong>"	})
		]
	}),
	tokyo: ({
		px_top: 45, px_left: 339, px_width: 441, px_height: 106,
		background: "baloon_tokyo.png",
		arrow_direction: "s-e",
		//buttons: ["彼に従う", "返答する"],
		posts: [
			({ usr: "Hitomi",	img: "hitomi.gif", comments:"2 コメント", share:"共有" ,	dt: "30 秒 以前",	txt: "個人面談の時にテストの結果だけ見たら4はあった長男の成績が、提出物を出さないばっかりにことごとく3とか2になっ"	})
		]
	}),
	moscow: ({
		px_top: 78, px_left: 514, px_width: 441, px_height: 106,
		background: "baloon_moscow.png",
		arrow_direction: "n-w",
		//buttons: ["Следовать", "Ответ"],
		posts: [
			({ usr: "Борис",	img: "borris.gif", comments:"1 Kомментарии", share:"Доля" ,	dt: "минуту назад",	txt: "Закон разрешает безнаказанно лгать в суде: <strong>http://newsru.com/russia/../highcourt.html</strong>"	}),
			({ usr: "Дмитри",	img: "dmitry.gif", attachments:"video.jpg", comments:"11 Kомментарии", share:"Доля" ,	dt: "минуту назад",	txt: "Ух ты какие флешки нынче продают ;-))"	})
		]
	}),
	london: ({
		px_top: 80, px_left: 407, px_width: 441, px_height: 106,
		background: "baloon_moscow.png",
		arrow_direction: "n-w",
		//buttons: ["Follow", "Reply"],
		posts: [
			({ usr: "Michael",	img: "michael.png", comments:"9 Comments", share:"Share",	dt: "a minute ago",	txt: "I was the first to come to the office today. Drinking my first coffee, reading my feeds and getting ready to work."	})
		]
	}),
	newyork: ({
		px_top: 12, px_left: 215, px_width: 441, px_height: 106,
		background: "baloon_plovdiv.png",
		arrow_direction: "s-w",
		//buttons: ["Follow", "Reply"],
		posts: [
			({ usr: "Steve",	img: "steve.png", comments:"5 Comments", share:"Share",	dt: "a minute ago",	txt: "I'm stuck in a traffic jam on my way to work. I'll come too late to the office."	}),
			({ usr: "Dave",		img: "dave.gif", 	comments:"3 Comments", share:"Share", dt: "a minute ago",	txt: "Getting ready for the conference. It's gonna be very interesting."	})
		]
	}),
	paris: ({
		px_top: -2, px_left: 417, px_width: 441, px_height: 106,
		background: "baloon_plovdiv.png",
		arrow_direction: "s-w",
		//buttons: ["Suivre", "Répondre"],
		posts: [
			({ usr: "Bernard",	img: "bernard.gif", attachments:"img.jpg", comments:"Commentaire", share:"Répartir",	dt: "il ya une minute",	txt: "Je vais au cinéma ce soir."	})
		]
	}),
	berlin:  ({
		px_top: 84, px_left: 455, px_width: 441, px_height: 106,
		background: "baloon_moscow.png",
		arrow_direction: "n-w",
		//buttons: ["Folgen", "Antworten"],
		posts: [
			({ usr: "Helga",	img: "helga.gif", comments:"5 Bemerkungen", share:"Teilen",	dt: "vor einer minute",	txt: "Möchtet ihr auch in solchem Caravan auf Reisen gehen? <strong>http://michal.ontheroad.to/show/karavan</strong>"	})
		]
	}),
	madrid: ({
		px_top: 11, px_left: -20, px_width: 441, px_height: 106,
		background: "baloon_tokyo.png",
		arrow_direction: "s-e",
		//buttons: ["seguir", "responder"],
		posts: [
			({ usr: "Hernan",	img: "hernan.gif", comments:"8 Comentarios", share:"Compartir",	dt: "hace un minuto",	txt: "El médico me puso un yeso en el brazo."	})
		]
	})
});
var baloons_show_steps	= 7;
var baloons_show_tmout	= 20;
var baloons_hide_steps	= 6;
var baloons_hide_tmout	= 20;
var baloons_stay_time	= 2000;
var baloons_none_time	= 1;
var baloons_imgdir_towns	= "img/site2/baloons/";
var baloons_imgdir_posts	= "img/site2/baloons/people/";
var d	= document;
var w	= window;

w.onload	= function() {
	baloons_start();
};

var baloons_posts	= [];

function baloons_start()
{
	var i, j, p;
	for(i in baloons) {
		for(j=0; j<baloons[i].posts.length; j++) {
			p	= baloons[i].posts[j];
			p.town	= i;
			baloons_posts[baloons_posts.length]	= p;
		}
	}
	baloons_posts	= baloons_arrshuffle(baloons_posts);
	baloons_preloadpost(0, function(){baloons_showpost(0)});
}

function baloons_preloadpost(indx, callback_after)
{
	if( baloons_posts.length == indx ) {
		indx	= 0;
	}
	var post	= baloons_posts[indx];
	if( post && baloons[post.town] ) {
		var i1	= new Image();
		var i2	= new Image();
		var i3	= new Image();
		var stat;
		var done	= function() {
			if( stat ) { clearInterval(stat); }
			if( callback_after ) { callback_after(); }
		};
		i1.onerror	= done;
		i2.onerror	= done;
		i1.onabort	= done;
		i2.onabort	= done;
		i3.onabort	= done;
		i1.src	= baloons_imgdir_posts+post.img;
		i2.src	= baloons_imgdir_towns+baloons[post.town].background;
		if (post.attachments) {
      i3.src	= baloons_imgdir_posts+post.attachments;
    }
		stat	= setInterval( function() { if( i1.complete && i2.complete ) { done(); } }, 100 );
		return;
	}
	if( callback_after ) {
		callback_after();
	}
}

function baloons_showpost(indx)
{
	if( baloons_posts.length == indx ) {
		indx	= 0;
	}
	var post	= baloons_posts[indx];
	if( ! post ) {
		return;
	}
	var town	= baloons[post.town];
	if( ! town ) {
		return;
	}
	var container	= d.getElementById("indexbubbles");
	if( ! container ) {
		return;
	}
	var dv	= d.createElement("DIV");
	dv.className	= "baloon baloon_"+post.town;
	dv.style.display	= "none";
	dv.style.backgroundImage	= "url("+baloons_imgdir_towns+town.background+")";
	var dv2	= d.createElement("DIV");
	dv2.className	= "baloonpost";
	var dv3	= d.createElement("DIV");
	dv3.className	= "bp_avatar";
	var img	= d.createElement("IMG");
	img.alt	= "";
	img.src	= baloons_imgdir_posts+post.img;
	dv3.appendChild(img);
	dv2.appendChild(dv3);
	var dv4	= d.createElement("DIV");
	dv4.className	= "bp_body";
	var dv5	= d.createElement("DIV");
	dv5.className	= "bp_user";
	dv5.appendChild(d.createTextNode(post.usr));
	dv4.appendChild(dv5);
	var dv6;
	if( town.buttons ) {
		for(i=0; i<town.buttons.length; i++) {
			dv6	= d.createElement("DIV");
			dv6.className	= "bp_btn";
			dv6.appendChild(d.createTextNode( town.buttons[i] ));
			dv4.appendChild(dv6);
		}
	}
	var dv8	= d.createElement("DIV");
	dv8.className	= "bp_text";
	dv8.innerHTML	= post.txt;
  
  if( post.attachments ) {
    var attachment	= d.createElement("IMG");    //"·"
    attachment.alt	= "";
    attachment.src	= baloons_imgdir_posts+post.attachments;
    dv8.appendChild(attachment);
  }
	
	var sml	= d.createElement("SMALL");    //"·"
	//sml.appendChild(d.createTextNode(post.dt));
	
	var smlCnt	= d.createElement("SPAN");
	smlCnt.className	= "bp_comments";
	smlCnt.appendChild(d.createTextNode(post.comments));
	
	//sml.appendChild(d.createTextNode(post.comments));
	sml.appendChild(smlCnt);
	sml.appendChild(d.createTextNode(' · '));
	sml.appendChild(d.createTextNode(post.dt));
	sml.appendChild(d.createTextNode(' · '));
	sml.appendChild(d.createTextNode(post.share));
	
	//sml.innerHTML("12 comments · " + post.dt + " · Share");
	dv8.appendChild(sml);
	dv4.appendChild(dv8);
	dv2.appendChild(dv4);
	dv.appendChild(dv2);
	dv.onmousedown	= function() { return false; };
	container.appendChild(dv);
	dv.style.top	= town.px_top+"px";
	dv.style.left	= town.px_left+"px";
	if( town.arrow_direction == "n-e" || town.arrow_direction == "s-e" ) {
		dv.style.left	= (town.px_left + town.px_width) + "px";
	}
	if( town.arrow_direction == "s-e" || town.arrow_direction == "s-w" ) {
		dv.style.top	= (town.px_top + town.px_height) + "px";
	}
	dv.style.overflow	= "hidden";
	dv.style.width	= "0px";
	dv.style.height	= "0px";
	dv.style.display	= "block";
	var i;
	var func_show	= function() {
		i	++;
		if( i >= baloons_show_steps ) {
			dv.style.width		= town.px_width + "px";
			dv.style.height		= town.px_height + "px";
			dv.style.top	= town.px_top+"px";
			dv.style.left	= town.px_left+"px";
			dv.style.opacity		= 1;
			dv.style.overflow	= "visible";
			setTimeout( function() { i=baloons_hide_steps; dv.style.overflow="hidden"; func_hide() }, baloons_stay_time );
			return true;
		}
		var w	= Math.round( i * town.px_width / baloons_show_steps );
		var h	= Math.round( i * town.px_height / baloons_show_steps );
		if( town.arrow_direction == "n-e" || town.arrow_direction == "s-e" ) {
			dv.style.left	= (town.px_left + town.px_width - w) + "px";
		}
		if( town.arrow_direction == "s-e" || town.arrow_direction == "s-w" ) {
			dv.style.top	= (town.px_top + town.px_height - h) + "px";
		}
		dv.style.width	= w + "px";
		dv.style.height	= h + "px";
		dv.style.opacity	= i/baloons_show_steps;
		setTimeout( func_show, baloons_show_tmout );
	};
	var func_hide	= function() {
		i	--;
		if( i <= 0 ) {
			dv.style.width		= "0px";
			dv.style.height		= "0px";
			dv.style.display	= "none";
			setTimeout( function() { i=0; baloons_showpost(indx+1); }, baloons_none_time );
			dv.parentNode.removeChild(dv);
			return true;
		}
		var w	= Math.round( i * town.px_width / baloons_hide_steps );
		var h	= Math.round( i * town.px_height / baloons_hide_steps );
		if( town.arrow_direction == "n-e" || town.arrow_direction == "s-e" ) {
			dv.style.left	= (town.px_left + town.px_width - w) + "px";
		}
		if( town.arrow_direction == "s-e" || town.arrow_direction == "s-w" ) {
			dv.style.top	= (town.px_top + town.px_height - h) + "px";
		}
		dv.style.width	= w + "px";
		dv.style.height	= h + "px";
		dv.style.opacity	= i/baloons_hide_steps;
		setTimeout( func_hide, baloons_hide_tmout );
	};
	i	= 0;
	dv.style.opacity		= 0;
	func_show();
	baloons_preloadpost(indx+1);
}


function baloons_arrshuffle(o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};