var defTextSize;
function setTextSizeListener ( fn ) {
	var e = $('body').append('<div id="textListener">s</div>');
	$('#textListener').css({
	    visibility : 'hidden',
	    position : 'absolute',
	    top : 0
	});
	defTextSize = $('#textListener').height();
	textSizeListener = function(){
		if(defTextSize != $('#textListener').height()){
			fn();
		}
	}
	var n = setInterval(textSizeListener,1000)
}

function getTopicsList(conf){
	var url               = conf.url               || "";
	var targetAreaId      = conf.targetAreaId      || "";
	var targetMainId      = conf.targetMainId      || "";
	var targetListId      = conf.targetListId      || "";
	var max               = conf.max               || 0;

	if (!url || !(targetMainId && targetListId)) {
		return;
	}
	$.ajax({
		"url"      : url,
		"cache"    : false,
		"dataType" : "xml",
		"success"  : function(data){
			$(function(){
				var mainTopicArea = jQuery("#" + targetMainId);
				if (mainTopicArea.length) {
					var topicData = $("balloon data:first", data.documentElement);
					if (topicData.length) {
						var image = topicData.attr("img");
						if (topicData.attr("img")) {
							jQuery('<div class="imageBlock"><img src="' + image + '" alt=""></div>').appendTo(mainTopicArea);
						}

						var subjectData = $("title", topicData);
						if (subjectData.text()) {
							jQuery('<p class="leadText">' + subjectData.text() + '</p>').appendTo(mainTopicArea);
						}

						var bodyData = $("text", topicData);
						if (bodyData.text()) {
							jQuery('<p>' + bodyData.text() + '</p>').appendTo(mainTopicArea);
						}

						var linkData = $("link", topicData);
						var href   = linkData.attr("href") || topicData.attr("link");
						var text   = linkData.text() || href;
						if (href) {
							var target = linkData.attr("target") || topicData.attr("target");
							var link = jQuery('<a href="' + href + '"' + (target ? ' target="' + target + '"' : '') + '>' + text + '</a>').appendTo(mainTopicArea);
						}
					} else {
						mainTopicArea.remove();
					}
				}

				var topicsArea = jQuery("#" + targetListId);
				if (topicsArea.length) {
					var pdl = jQuery('<dl></dl>');
					var datas = $("list data", data.documentElement);
					topicsArea.append(pdl);
					var count = 0;

					datas.each(function(index){
						if (!isNaN(max) && max && count >= max) {
							return false;
						}

						var content = $(this);

						var title       = $("title", content).text();
						var someText    = $("text", content).text();
						var href        = $("title", content).attr('link');

						var dateNode = $('<dt></dt>');
						dateNode.html(someText);
						var titleNode = $('<dd></dd>');

						if (href) {
							titleNode.html('<a href="' + href + '">' + title + '</a>');
						} else {
							titleNode.html(title);
						}
						count++;
						pdl.append(dateNode);
						pdl.append(titleNode);
					});
					if (count == 0) {
						topicsArea.remove();
					}
				}

				if (!jQuery("#" + targetMainId + ",#" + targetListId).length) {
					jQuery("#" + targetAreaId).remove();
				}
			});
		}
	});
}

$(document).ready(function(){
	var setTopLinkSize = function () {
		var maxHeight = 0;
		$('#homeIdeasLinks ul#ideasLinks a span.linkInner').each(function () {
		    maxHeight = ( maxHeight > $(this).height() ) ? maxHeight : $(this).height();
		});
		$('#homeIdeasLinks ul#ideasLinks a').each(function () {
		    if ( maxHeight != $(this).height()+6 ) $(this).height(maxHeight+6);
		});
	};

	setTopLinkSize()
	setTextSizeListener(setTopLinkSize);
});

getTopicsList({
	url : '../flash/swf/topics/topic.xml',
	targetAreaId: 'homeTopics',
	targetMainId: 'homeTopicsMain',
	targetListId: 'homeTopicsList',
	max: 3
});

