$(function(){	
	if (jQuery.fn.lightBox) $('.lightbox').lightBox();
	if (jQuery.fn.validate) $('form').each(function(){$(this).validate()});
	
	$('img.user-avatar').error(function(){this.src='/images/user.png'});
	
	$('button[id=login]').click(function(event){ if (!$('table[id=login]').is(':visible')) { event.preventDefault();
		$('table[id=login]').slideDown();
	} });
	
	$('a[rel=external]').click(function(event){ event.preventDefault(); window.open(this.href); });
	$('a.disabled').click(function(event){ event.preventDefault() });
	
	$('.news-ticker').mouseenter(function(){ clearTimeout($(this).data('ticker')); })
		.mouseleave(function(){ $(this).data('ticker', setTimeout(ticker, 50)); })
		.data('ticker', setTimeout(ticker, 50));
	
	
	$('#accordion').each(function(){ $(this).accordion({ autoHeight: false, collapsible: true }); });
	
	setTimeout(background, 10000);
});

function popup(content) {
	var overlay = $('<div></div>').addClass('ui-widget-overlay').appendTo('body').hide().fadeTo(1000, 0.5);
	var popup = $('<div></div>').addClass('popup').appendTo('body').hide().fadeIn();
	
	$('<p></p>').html(content).appendTo(popup);
	$('<p></p>').html('Close').appendTo(popup);
	
	$('.popup, .ui-widget-overlay').click(function(){ $('.popup, .ui-widget-overlay').fadeOut(function(){ $(this).remove() }) });
}

function background() {
	var images = $('.background img');
	var index = $('.background img:visible').index('.background img');
	
	if ($(images).length > 1) {
		$(images).fadeOut(1000);
		if (index < images.length - 1) $('.background img:eq(' + ++index + ')').stop(true,true).fadeIn(2000);
		else $('.background img:first').stop(true,true).fadeIn(2000);	
		setTimeout(background, 10000);
	}
}

function ticker() { if ($('.news-ticker').length) {
	$('.news-ticker').scrollTop($('.news-ticker').scrollTop()+1).data('ticker', setTimeout(ticker, 50));
	if ($('.news-ticker')[0].scrollHeight - $('.news-ticker').scrollTop() == $('.news-ticker').outerHeight()) $('.news-ticker').scrollTop(0);
} }
