$(window).load(function () {
	window.setTimeout('getStatus()', 100);
	
	$('#nav-services a').click(function () {
		if ($('#services.hidden').length) { 
			$('#services.hidden').slideDown('fast').removeClass('hidden').addClass('active');
			$(this).html('Close');
		} else {
			$('#services.active').slideUp().removeClass('active').addClass('hidden');
			$(this).html('Services');
		}
		return false;
	});
	$('body#page-home #nav-services a').unbind("click").click(function () { return false; });
	/*$('#navService ul.thirdlevel').each(function () { // hide third level links unless its parent or one of its links is selected
		var h = true;
		if ($(this).siblings('a.selected').length || $(this).find('a.selected').length) h = false;
		if (h) $(this).addClass('hidden');
	});*/
	//$('#feedbackForm input[@name="page_title"]').val(document.title);
	$('#feedbackForm').submit(function () {
		var target = this.action + 'ajax/';
		$.ajax({
			type: 'POST',
			url: target,
			dataType: 'json',
			data: {'source': $(this).find('input[@name="source"]').val(), 'rating': $(this).find('input[@name="rating"]:checked').val(), 'page_title': document.title.replace(/\n/g,''), 'comments': $(this).find('textarea').val() },
			success: function (data) {
				if (data.success) {
					$("#feedbackForm > div").html('<p class="success">'+data.message+'</p>');
					//$("#feedbackForm > p.success").fadeIn();
					//$("#feedbackForm").html('');
				} else {
					$("#feedbackForm > div").append('<p class="error">'+data.message+'</p>');
				}
			},
			error: function () {
				$("#feedbackForm > div").prepend('<p class="error">An error occurred. Please make sure you\'ve given the page a rating.</p>');
			}
		});
		return false;
	});
	$('#services.hidden').hide();
});

function getStatus() {
	var cs = document.getElementById('chatstatus');
	cs.style.display = 'inline-block';
	var width = cs.offsetWidth;
	cs.style.display = 'none';
	if (width > 10) {
		$('#nav-contact').after('<li id="nav-chat"><a href="#">Live Chat</a></li>');
		$('#nav-chat a').click(function () {
			launchWin('framemain', 'http://openfire.biola.edu:9090/webchat/start.jsp?workgroup=helpdesk@workgroup.openfire&location='+window.location.href, 500, 400);		
			return false;
		});
	} else {
		$('#nav-contact').after('<li id="nav-chat"><span>Chat is offline</span></li>');
	}
}
