﻿$(document).ready(function() {
	$(":button").button();
	$(":submit").button();

	$(".datepicker").datepicker({
		showOn: 'both',
		buttonImageOnly: true,
		buttonImage: rootUrl + "assets/images/calendar_icon.gif",
		autoSize: true
	});
	$("#masterNavigation ul li ul").each(function() {
		//		alert($(this).parent().find("a").html());
		var label = $(this).parent().find("a:first");
		label.addClass("ui-button-text-icons");
		label.html($("<span style=\"padding-right: 1.3em;\">" + label.html() + "</span><span class=\"ui-button-icon-secondary ui-icon ui-icon-triangle-1-s\"></span>"));
	});
	$("#masterNavigation ul li").hover(
	// ~~~ add arrow here
		function() {
			$(this).find('ul:first').css({ visibility: "visible", display: "none" }).fadeIn(400);
		},
		function() {
			$(this).find('ul:first').css({ visibility: "hidden" });
		}
	);
	applyDataTableFormatting();
});

function hideMenus() {
	$(document).ready(function() {
		$(".menuHide").hide();
	});
}

function applyDataTableFormatting(skipHeader) {
	if (skipHeader) {
		$("table.dataTable th").removeClass("header");
	} else {
		$("table.dataTable th").addClass("header");
	}
	$("table.dataTable tbody tr:odd td").each(function() {
	$(this).removeClass("odd");
	$(this).removeClass("even");
	if (!$(this).parent().hasClass("selected")) {
			$(this).addClass("odd");
		} else {
			$(this).addClass("selected");
		}
	});
	$("table.dataTable tbody tr:even td").each(function() {
		$(this).removeClass("odd");
		$(this).removeClass("even");
		if (!$(this).parent().hasClass("selected")) {
			$(this).addClass("even");
		} else {
			$(this).addClass("selected");
		}
	});
}

function log(s) {
	if (typeof console != "undefined" && typeof console.debug != "undefined") {
		console.log(s);
	} else {
		//alert(s);
	}
}

function getLabelForId(obj, id) {
	return (obj.find("label[for = " + id + "]").html());
}

function getUserProfileEntry(key, callback) {
	if (callback == null) {
		alert("No callback defined!");
	}
	$.getJSON(rootUrl + "ajax/userService.svc/getUserProfileEntry?key=" + key + '&r=' + new Date().getTime(), function(data) {
		if (typeof (data) == "string") {
			alert(data);
		} else if (!data) {
			alert("Returned null");
		} else if (!data.success) {
			alert("Unable to get user: " + data.message);
		} else {
			log("Received profile entry for '" + key + "' = '" + data.message + "'");
			callback(key, data.message);
		}
	}, "json");
}

function setUserProfileEntry(key, value, callback) {
	$.ajax({
		type: "POST",
		url: rootUrl + "ajax/userService.svc/writeUserProfileEntry",
		contentType: "application/json",
		data: JSON.stringify({ Key: key, Value: value }),
		success: function(data) {
			if (typeof (data) == "string") {
				alert(data);
			} else if (!data.success) {
				alert("Unable to save user profile data: " + data.message);
			} else {
				log("Saved profile entry for '" + key + "' = '" + value + "'");
				if (callback != null) {
					callback(key, value);
				} else {
					log("No callback defined!");
				}
			}
		},
		dataType: "json"
	});
}

function roundNumber(num, dec) {
	var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
	return (result);
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = (x.length > 1 ? '.' + x[1] : '');
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return (x1 + x2);
}

