$(document).ready(function()
{
	$('a.lightbox').fancybox({
		type: 'image',
		cyclic: true,
		overlayOpacity: .5,
		overlayColor: '#000',
		onStart: function()
		{
			$('.slideshow').cycle('pause');
		},
		onClosed: function()
		{
			$('.slideshow').cycle('resume');
		}
	});

	$('a.map_content').fancybox(
	{
		type: 'iframe',
		width: 620,
		height: 360,
		margin: 0,
		padding: 0,
		scrolling: 'no',
		overlayOpacity: .5,
		overlayColor: '#000',
		callbackOnClose: function()
		{
			$('#fancy_content').empty();
		}
	});

	$('.tips a[title]').qtip({
		hide: {
			fixed: true,
			delay: 100
		},
		position: {
			corner: {
				target: 'topMiddle',
				tooltip: 'bottomMiddle'
			}
		},
		style: {
			padding: 4,
			background: '#4a5054',
			color: '#ffffff',
			textAlign: 'center',
			border: {
				width: 1,
				radius: 1,
				color: '#d47840'
			},
			//tip: 'bottomMiddle',
			width: {min: 125, max: 200}
		}
	});
	
	/*
	 * Nav Dropdowns
	 */
	$('#nav li.top_level')
	.mouseenter(function()
	{
		$('ul', this).show();

		$('#nav li.top_level ul').not($('ul', this)).hide();
	});

	$('#nav')
	.mouseleave(function()
	{
		$('ul ul', this).hide();

		$('#nav a:not(.current)').removeClass('active');
	});

	$('#nav ul ul')
	.mouseenter(function()
	{
		$(this).show();

		$('a', $(this).parent()).addClass('active');
	});

	$('#icon_nav a').hover(
		function()
		{
			var pos = $(this).css('backgroundPosition').split(' ');

			$(this).css('backgroundPosition', pos[0] + ' 0');
		},
		function()
		{
			if (! $(this).is('.current')) {
				var pos = $(this).css('backgroundPosition').split(' ');

				$(this).css('backgroundPosition', pos[0] + ' -23px');
			}
		}
	);
});

$(window).load(function()
{
	/*
	 * Nav Dropdowns
	 */
	/*$('#nav ul ul').each(function()
	{
		$(this).width($(this).parent().width());
	});*/
});

function feedRefresh(id)
{
	var interval;

	$.ajax({
		url: '/feed/' + id + '/refresh',
		cache: false,
		success: function(html)
		{
			$('#feed .css-panes > div').html(html);
		},
		beforeSend: function()
		{
			$('.refresh').css('background-image', 'url(/images/refresh_ani.gif)');
		},
		complete: function()
		{
			$('.refresh').css('background-image', 'url(/images/refresh.gif)');
		}
	});
}

function feedUpdate(id, page)
{
	$.ajax({
		url: '/feed/' + id + '/' + page,
		cache: false,
		success: function(html)
		{
			var h = $(html);

			$('#feed .more').remove();
			$('#feed .css-panes > div').append(html);
			$('#feed .refresh:not(:first)').remove();

			$.scrollTo($('#page' + page));
		},
		beforeSend: function()
		{
			$('.more').css('background-image', 'url(/images/refresh_ani.gif)');
		},
		complete: function()
		{
			$('.more').css('background-image', 'url(/images/refresh.gif)');
		}
	});
}

function initSlideshow()
{
	var loading, loading_timer, loading_frame = 1;

	var animate_loading = function()
	{
		if (! $('#loading').is(':visible')) {
			clearInterval(loading_timer);
			return;
		}

		$('#loading div').css('top', (loading_frame * -40) + 'px');

		loading_frame = (loading_frame + 1) % 12;
	}
	
	var is_image_loaded = function(img)
	{
		// IE
		if (! img.complete) {
			return false;
		}

		// Others
		if ((typeof(img.naturalWidth) != 'undefined') && (img.naturalWidth == 0)) {
			return false;
		}

		return true;
	};
	
	var cycle_opts =
	{
		fx: 'fade',
		timeout: 5000,
		pager: '#section_tabs',
		pagerAnchorBuilder: function(idx, el)
		{
			var label = $(el).children('img').attr('alt');

			var item =
				'<li>' +
				'	<a href="#">' +
				'		<span class="tableft"></span>' +
				'		<span class="tabmiddle">' + (label ? label : (idx + 1)) + '</span>' +
				'		<span class="tabright"></span>' +
				'	</a>' +
				'</li>'
			;

			return item;
		},
		updateActivePagerLink: function(pager, idx)
		{
			$(pager).find('a')
				.removeClass('current')
				.filter('a:eq(' + idx + ')')
				.addClass('current')
			;
		},
		before: function(curr, next, opts, fwd)
		{
			if (curr != next) {
				$('#slideshow').cycle('pause');
				
				var img = $('img:first', next);

				if (is_image_loaded(img.get(0))) {
					$('#slideshow').cycle('resume');
				} else {
					img.load(function(e)
					{
						$('#loading').hide();
						clearInterval(loading_timer);

						$('#slideshow').show();
						$('#slideshow').cycle('resume');
					});

					$('#slideshow').hide();

					$('#loading').show();
					loading_timer = setInterval(animate_loading, 66);
				}
			}
		}
	};

	var first_img = $('#slideshow img:first');

	if (is_image_loaded(first_img.get(0))) {
		$('#slideshow').cycle(cycle_opts);
	} else {
		first_img.load(function(e)
		{
			$('#loading').hide();
			clearInterval(loading_timer);

			$('#slideshow').show();
			$('#slideshow').cycle(cycle_opts);
		});

		$('#slideshow').hide();

		$('#loading').show();
		loading_timer = setInterval(animate_loading, 66);
	}
}
