var details = true;
var block_count = 0;

function init(no_load)
{
	getUserProjectSelection(no_load);
	getUserNotifications();
	
	details = $.cookie("display_details");
	if (details == 'true' || details == null) {
		details = true;
	}
	else {
		details = false;
	}
};

function block()
{
	if (block_count == 0) {
		$.blockUI({ message: '<span class="ui-widget">Please Wait</span>',
					showOverlay: false,
					css: { 
					border: 'none',
					padding: '10px',
					width: '200px',
					left: '45%',
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .5, 
					color: '#fff' 
				} });
				
		
	}
	block_count++;
};

function unblock()
{
	block_count--;
	if (block_count <= 0) {
		$.unblockUI();
		block_count = 0;
	}
};

function ajaxCall(url, type, params, success, error)
{
	var bodyContent = $.ajax({
		url: url,
		data: params,
		global: false,
		type: type,
		dataType: "json",
		success: success,
		error: error
	}).responseText;
	
	return bodyContent;
};

function goToURL(url)
{
	top.location.href= url;
};

function projectChangedCallback()
{
	var value = $('#myprojects_select').val();
	if (value != 0) {
		goToURL('/view/project/' + value);
	}
};

function display_notice(id, message, pause, duration)
{
	var html = '<div class="notification-message" id="notice_' + id + '"><table width="100%" cellspacing=0 cellpadding=0><tr><td align="left"><div class="notice-text">' + message + '</div></td></tr></table></div>';
	
	$('#temporary_message').html(html);
	$('#notice_' + id).delay(pause).fadeOut(duration);
}

function clearNotice(id) {
	$('#notice_' + id).fadeOut(600);
	$.post("/user/ack_notification", {message_id: id},
		function(data)
		{
			if (data.status == "OK")
			{
				var html = "";
				
				if (data.notifications.length > 0) {
					for (var i = 0; i < data.notifications.length; i++)
					{
						var notice = data.notifications[i]
						html += '<div class="notification-message" id="notice_' + notice.id + '"><table width="100%" cellspacing=0 cellpadding=0><tr><td align="left"><div class="notice-text">' + notice.notice + '</div></td><td align="right"><div class="notice-ok-link"><a href="javascript:clearNotice(\'' + notice.id + '\');">Ok, got it!</a></div></td></tr></table></div>';
					}
					
					$('#notification_message').html(html);
					$('#notification_message').show();
				} 
				else {
					$('#notification_message').html(html);
					$('#notification_message').hide();
				}
			}
			else
			{
				$('#notification_message').hide();
				$('#notification_message').html(html);
			}
			unblock();
			
		}, "json");
		
}

function getUserNotifications()
{
	$.get("/user/notifications", {},
		function(data)
		{
			if (data.status == "OK")
			{
				var html = "";
				
				if (data.notifications.length > 0) {
					for (var i = 0; i < data.notifications.length; i++)
					{
						var notice = data.notifications[i]
						html += '<div class="notification-message" id="notice_' + notice.id + '"><table width="100%" cellspacing=0 cellpadding=0><tr><td align="left"><div class="notice-text">' + notice.notice + '</div></td><td align="right"><div class="notice-ok-link"><a href="javascript:clearNotice(\'' + notice.id + '\');">Ok, got it!</a></div></td></tr></table></div>';
					}
					
					$('#notification_message').html(html);
					$('#notification_message').show();
				} else {
					$('#notification_message').html(html);
					$('#notification_message').hide();
				}
			}
			else
			{
				$('#notification_message').hide();
				$('#notification_message').html(html);
			}
			unblock();
			
		}, "json");
}

// Gets the users projects for combo-box selection
function getUserProjectSelection(no_load)
{
	if (no_load == null) {
		block();
	}
	
	$.get("/user/myprojects", {},
		function(data)
		{
			if (data.status == "OK")
			{
				var html = "";
				html += '<option value="' + 0 + '">' + 'Select Project' + '</option>';
				if (data.result_set.length > 0) {
					for (var i = 0; i < data.result_set.length; i++)
					{
						html += '<option value="' + data.result_set[i].key + '">' + data.result_set[i].name + '</option>';
					}
					$('#myprojects_select').removeAttr("disabled");
				}
				else {
					html += '<option value="' + -1 + '">' + 'Create a Project' + '</option>';
					$('#myprojects_select').html(html);
					$('#myprojects_select').attr("disabled", "disabled");
				}

				$('#myprojects_select').html(html);

				var project_key = $('#project_key').val();
				if (project_key != null) {
					$('#myprojects_select').val(project_key);
				}
			}
			else
			{
				$('#myprojects_select').html(html);
			}
			unblock();
			
		}, "json");
};
