﻿var uhri = {
	emptyGuid: '00000000-0000-0000-0000-000000000000',
	setPageTitle: function (title) {
		$('#page-title').html(title);
	}
}

uhri.messages = {
	init: function () {
		var mm = $('.modal-message');
		mm.each(function (i, el) {
			var m = $(el);
			m.dialog({
				modal: true,
				autoOpen: eval(m.attr('data-auto')),
				buttons: {
					Ok: function () {
						$(this).dialog('close');
						uhri.messages.performAction(this);
					}
				},
				close: function () {
					uhri.messages.performAction(this);
				}
			});
		});
		var mc = $('.modal-confirm');
		mc.each(function (i, el) {
			var c = $(el);
			c.dialog({
				modal: true,
				autoOpen: eval(c.attr('data-auto')),
				buttons: {
					Ok: function () {
						uhri.messages.performAction(this);
					},
					Cancel: function () {
						$(this).dialog('close');
					}
				}
			});
		});
	},
	performAction: function (dialog, close) {
		var url = $(dialog).attr('data-redirect');
		if (url && url.length > 0) {
			document.location.href = url;
		}
		var action = $(dialog).attr('data-action');
		if (action && action.length > 0) {
			eval(action);
		}
		if (close) {
			$(dialog).dialog("close");
		}
	}
}

uhri.templates = {
	cached: [],
	load: function (name, callback) {
		if (uhri.templates.cached[name]) {
			callback(uhri.templates.cached[name]);
		}
		$.ajax({
			method: 'GET',
			url: '/templates/load/' + name,
			success: function (result) {
				uhri.templates.cached[name] = result;
				return callback(result);
			},
			async: true
		});
	}
}

uhri.navigation = {
	init: function () {
		$('nav ul li ul').parent().append('<span></span>');
		$('nav ul li span').click(function () {
			var that = $(this);
			that.parent().find('ul').show();
			that.addClass('open');
			that.parent().hover(function () { }, function () {
				that.parent().find('ul').hide();
				that.removeClass('open');
			});
		});
	}
}

$(document).ready(function () {
	uhri.messages.init();
});
