candflogin = false;

$(document).ready(function(){

	$('#m1 li a.selectedItem').parent().addClass('hovered');
	$('#m1 li a.selectedItem').parent().prevAll().addClass('hovered');
	
	$('#m1 li').hover( function() {
		$('#m1 li a.selectedItem').css('border', 'none');
		$(this).children().css('border-bottom', '6px solid #193441');
		$(this).siblings().removeClass('hovered');
		$(this).addClass('hovered');
		$(this).prevAll().addClass('hovered');
	}, function() {
		$(this).children().css('border-bottom', 'none');
		$('#m1 li a.selectedItem').css('border-bottom', '6px solid #193441');
		$(this).removeClass('hovered');
		$(this).siblings().removeClass('hovered');
		highlightM1();
	});
	
	if ($('#login_panel').length > 0) candflogin = true;

	applyFancyBox();	
	
	$('.buttonBack').click( function(e) {
		e.preventDefault();
		history.go(-1);
	});
	
});

function highlightM1() {
	$('#m1 li a.selectedItem').parent().addClass('hovered');
	$('#m1 li a.selectedItem').parent().prevAll().addClass('hovered');
}

function fancyClose() {
	if (candflogin == false) {
		$('#page-nav').prepend('<div id="login_panel" style="display: none"></div>');
		$('#login_panel').load('/index.php?component=user&action=login_panel', function() {
			if ($('#login_panel').html() != '') {
				$('#login_panel').slideDown('slow');
				candflogin = true;
				if ($('#um').length > 0) $('#um').load('/index.php?component=user&action=user_menu', function() {
					applyFancyBox();
				});
			}
		});
	}
}

function applyFancyBox() {

	if ($("a.iframe, .iframe a").length > 0) $("a.iframe, .iframe a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 600,
		'frameHeight': 500,
		'callbackOnClose': fancyClose
	});	


}