var doRssTooltips = false;		// buggy anyway - extra mouseOut events
var rssInterval = 0;
var rssDefInterval = 7000;
var rssHoeverState = null;
var animateOpacity = !($.browser.msie && parseFloat($.browser.version) < 8);

function rssAnimateHeight($first, $container) {
	$first
		.detach()
		.removeAttr('style')
		.appendTo($container);
}

function rssAnimate(interval, $container) {
	if (!interval) {
		interval = rssDefInterval;
	}

if (animateOpacity) {
	rssInterval = setInterval(function() {
		var $first = $container.children().first();
		var height = $first.height();
		$first
			.animate({opacity:0}, 1000)
			.animate({height:0}, 1000, function() {
				rssAnimateHeight($first, $container);
			});
		}, interval);
} else {
	rssInterval = setInterval(function() {
		var $first = $container.children().first();
		var height = $first.height();
		$first
			.animate({height:0}, 1000, function() {
				rssAnimateHeight($first, $container);
			});
		}, interval);
}
//console.log('Interval: ', rssInterval);
}

function rssxml(data, status, xhr) {
	var classes = 'ui-widget-content ui-corner-all';
	var believeMouseIn = true;
	var believeMouseOut = false;
	var widget = '';

	widget += "<fieldset id='rss' class='ui-shadow "+classes+"'>\n" +
		  " <legend>Recent Calmare News</legend>\n" +
		  " <ul class='rsslist'>\n";

	$('item', data).each(function() {
		var $this = $(this);
// too long	var title = $('title', $this).text();
		var summary = $('summary', $this).text();
		var link = $('link', $this).text();
		var desc = $('description', $this).text();
		var pub = $('pubDate', $this).text();
//console.log($('description', $this).text());

		pub = pub.replace(/ \d\d:\d\d:\d\d [+-]\d\d\d\d/, '');
		desc = desc.replace(/\. .*/, '.');

		widget += "  <li class='rssitem'>\n" +
			  "   <a class='rsslink' href='"+link+"'>"+summary+"</a>\n";

//Z		if (doRssTooltips) {
			widget += "   <fieldset class='rsstip "+classes+"'>\n" +
				  "    <legend class='ui-corner-all'>"+pub+"</legend>\n" +
				  "    <div class='rssdesc'>"+desc+"</div>\n" +
				  "   </fieldset>\n";
//Z		}

		widget += "  </li>\n";
	});

	widget += " </ul>\n" +
		  "</fieldset>";

	var $widget = $(widget);
	var $rsslist = $widget.children('ul').first();

	if (doRssTooltips) {
		$('.rssitem > a', $rsslist).tooltip({
			delay: 200,
			effect:'fade',
			offset: [0, -16-12],
			opacity:0.9,
			position:'bottom left',
			predelay: 200,
			relative: true
		});
	}

	$widget
		.hide()
		.prependTo('#header');

	if (animateOpacity) {
		$widget.fadeIn(1000);
	} else {
		$widget.show();
	}


	rssAnimate(rssDefInterval, $rsslist);

	$widget.hover(function() {
rssHoeverState = true;
//console.log('mouseIn');
		if (believeMouseIn) {
			clearInterval(rssInterval);
//console.log('Clear: ', rssInterval);
			rssInterval = 0;
			believeMouseIn = false;
			believeMouseOut = true;
//console.log('In: false, Out: true');
		} else {
			console.log('Ignoring mouseIn');
		}
	}, function() {
rssHoeverState = false;
//console.log('mouseOut', this);
		if (believeMouseOut) {
			if (rssInterval != 0) {
				console.log('Already started');
			} else {
//console.log('Start');
				rssAnimate(rssDefInterval, $rsslist);
			}
			believeMouseIn = true;
			believeMouseOut = false;
//console.log('In: true, Out: false');
		} else {
			console.log('Ignoring mouseOut');
		}
	});

	// if clicked on a link to navigate away to a new page, when back
	// button returns you to the page, no way to tell if mouse is still
	// over rss widget.  unconditionally restart rss widget scroll and
	// let mouseIn event when mouse actually over widget stop scroll.
	$(window).bind('pageshow', function() {
		believeMouseIn = true;
//		believeMouseOut = false;
//console.log('hover: '+rssHoeverState+', rssInterval: '+rssInterval);
		if (rssInterval == 0) {
			rssAnimate(rssDefInterval, $rsslist);
			console.log('proactively starting');
		}
	});
}

function checkfont(font) {
	var $a = $('<span>')
		.css({
			'font-size':'16px',
			'position':'absolute',
			'top':'-999px',
			'left':'-999px'
		}).text('random_words_#_!@#$^&*()_+mdvejreu_RANDOM_WORDS');
	var $b = $a.clone();

	$a.css({'font-family':"monospace"}).appendTo('body');
	$b.css({'font-family':"'"+font+"', monospace"}).appendTo('body');

	var $r = $a.width() == $b.width() && $a.height() == $b.height();

	$a.remove();
	$b.remove();

	return !$r;
}

$(document).ready(function() {
	if (!checkfont('Lucida Calligraphy')) {
		$('.cycle').css('font-style','italic');
	}

	$('.cycle').cycle({
		fx:	'fade',
		pause:	1,
		speed:	5000
	});

//X	$.get('/rss/calmare.rss', rssxml, 'xml');
	$.ajax({
		url: '/rss/calmare.rss',
		cache: false,
		dataType: 'xml',
		success: rssxml
	});
});

