var _pageTracker;
function trackLink(a, removeHost) {
	var url = a.href;

	if (removeHost) {
		var root = window.location.protocol + "//" + window.location.host;
		url = url.replace(root, "");
	}

	_pageTracker = _pageTracker || _gat._getTracker("UA-10984763-1");
	_pageTracker._trackPageview(url);
}

function openDialog(selector, titleArg, widthArg, heightArg){
	var dialog = $(selector).dialog(
		{ autoopen: false, 
			title: titleArg, 
			width: widthArg, 
			height: heightArg, 
			modal: true 
		});
	dialog.dialog('open').show();
}

$(function(){ $('input[type=text]:first').focus(); });


// http://frogsbrain.wordpress.com/2007/04/28/javascript-stringformat-method/
/////////////////////////////////////////////////////////////////////////////
String.format = function(text)
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
};

function assertParam(val, name, index, ctor) {
	if (!val)
		throw String.format("Parameter {0} (index: {1}) is required.", name, index);
	if (ctor && !(val instanceof ctor))
		throw String.format("Parameter {0} (index: {1}) must be of type {2}.", name, index, ctor.name);
}

Object.keys = function Object$keys(obj) {
	var result = [];
	for (var key in obj) result.push(key);
	return result;
}

Object.values = function Object$values(obj) {
	var result = [];
	for (var key in obj) result.push(obj[key]);
	return result;
}

Array.prototype.max = function Array$max(func) {
	if (this.length == 0) return;
	assertParam(func, "func", 0, Function);

	var max = this[0];
	for (var i = 1; i < this.length; i++)
		max = func(max, this[i]);
	return max;
}

Array.prototype.min = function Array$min(func) {
	if (this.length == 0) return;
	assertParam(func, "func", 0, Function);

	var min = this[0];
	for (var i = 1; i < this.length; i++)
		min = func(min, this[i]);
	return min;
}

// http://jtauber.com/blog/2008/04/28/auto-scrolling_is_jquery/
///////////////////////////////////////////////////////////////////////////////
function scrollIntoView(selector) {
	var targetOffset = $(selector).offset().top;
	var targetHeight = parseFloat($(selector).height().toString().replace("px", ""));
	var winHeight = parseFloat($(window).height().toString().replace("px", ""));
	var padding = (winHeight - targetHeight) / 2;
        $('html,body').animate({scrollTop: targetOffset - padding}, 500);
}

