/**
 * Global JavaScript helper functions.
 */
$(document).ready(function() {

	// Add "first" and "last" classes to LI elements.
	$("ul").find("li:last").addClass("last").end().find("li:first").addClass("first");
	
	// Add "first" and "last" classes to TH elements.
	//$("thead").find("th:last").addClass("last").end().find("th:first").addClass("first");
	$("table").find("th:last").addClass("last").end().find("th:first").addClass("first");
	
	// Add "first" and "last" classes to TR elements.
	$("table tbody").find("tr:last").addClass("last").end().find("tr:first").addClass("first");

	// Add "first" and "last" classes to table.data td elements.
	$("table.data tr").find("td:last").addClass("last").end().find("td:first").addClass("first");

	// add zebra stripes to data tables
	$('table.data tbody').find('tr:not(.tr-collapsed):even').addClass('even');

	// add zebra stripes to day detail tables
	$('#day-detail .left-col tbody tr:even').addClass('even');
	$('#day-detail .fixed-width tbody').find('tr:not(.header, .spacer):even').addClass('even');

	// Add target="_blank" to A elements with rel="external".
	$("a[rel=external]").attr("target", "_blank");

	// Add dividers to UL of class "with-dividers".
	$("ul.with-dividers").find("li:not(:last)").after('<li class="divider"> | </li>');

	// Add tooltips.
	$(".tooltip-trigger").tooltip();
});


/**
 * Debugging.
 */
jQuery.debug = function(data) {
	if (window.console) {
		console.debug(data);
	} else {
		// @todo: JavaScript vardump.
		// alert(data);
	}
};


/**
 * Tooltip.
 */
jQuery.fn.tooltip = function(options) {
	if (typeof options == "undefined") {
		options = {};
	}

	var settings = {
		xOffset: -5,
		yOffset: 10,
		width: '183px',
		fontSize: 'auto',
		textAlign: 'center'
	};
	settings = $.extend(settings, options);

	return this.each(function() {
		var self = $(this);

		self.hover(
			function(e) {
				this.t = $(this).find("div.tooltip-source").html();
				$("body").append('<div id="tooltip"><div class="tooltip-bubble" style="width: ' + settings.width + '; font-size: ' + settings.fontSize + '; text-align: ' + settings.textAlign + ';"><span class="bubble-pointer"></span>' + this.t + '</div></div>');
				$("#tooltip")
					.css("top", (e.pageY - settings.xOffset) + "px")
					.css("left", (e.pageX + settings.yOffset) + "px")
					.fadeIn("fast");
			},
			function() {
				$("#tooltip").remove();
			}
		);

		self.mousemove(function(e) {
			$("#tooltip")
				.css("top", (e.pageY - settings.xOffset) + "px")
				.css("left", (e.pageX + settings.yOffset) + "px");
		});
	});
};


/**
 * STRING TRIM FUNCTION
 */
String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};




/**
* Actual regex to match fractionals
*/
function fractionalRegex() {
    return new RegExp(/^(.*)(\d+) *\/ *(\d+)(\D.*)?$/);
}

/**
* Format fractionals
* Takes a collection of HTML elements, traverses, and formats
*/
function formatFractionals(collection) {
	var regex = fractionalRegex();
	collection.each(function() {
		var text = $(this).text();
		var result = null;
		text = text.trim();
		if (text != null) {
			result = text.match(regex);
		}
		if (result != null) {
			var new_text = formatFractionalResult(result);
			$(this).text("").html(new_text);
		}
	});
}

/**
* Replace fractionals
* Takes a string and returns a formatted string.
*/
function replaceFractionals(text) {
    var regex = fractionalRegex();
    var result = null;
    if (text != null) {
        result = text.match(regex);
    }
    if (result != null) {
        text = formatFractionalResult(result);
    }

    return text;
}


/**
* Takes the regex result and formats a fractional
*/
function formatFractionalResult(result) {
    new_text = "";
    if (typeof result[1] != "undefined") {
        new_text += result[1];
    }
    new_text += '<sup>' + result[2] + '</sup>/<sub>' + result[3] + '</sub>';
    if (typeof result[4] != "undefined") {
        new_text += result[4];
    }
    return new_text;
}



/*
if(ua==undefined){
	var ua=function(){
		var w=window,d=document;
		var ie=w.ActiveXObject&&true;
		return{
			ie:ie,
			ie6:ie&&!window.XMLHttpRequest,
			ie7:ie&&w.XMLHttpRequest&&!d.documentMode,
			ie8:ie&&w.XDomainRequest&&true
		};
	}();
	if(ua.ie8){ua['ie8as'+document.documentMode]=true;}
}
*/

