// Replace the console object if it doesn't work
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

function nl2br(text)
{
	return text.replace(/\n/g, "<br />");
}

function escapeHtml(s)
{
	return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

/*
General button constructor

Parameters:
	message -- what it says
 	silk -- which icon
	href -- (optional) where it links to
	click -- (optional) callback
*/
function button(options)
{
	var button = $('<a href="#" class="button"></a>');
	button.text(options.message);
	button.attr('title', options.message);
	if (options.silk)
	{
		button.addClass('silk-' + options.silk);
		button.css({
			'background-image': 'url(/img/silk/' + options.silk + '.png)'
		});
	}
	if (options.href)
	{
		button.attr('href', options.href);
	}
	button.click(function(e)
	{
		if (!options.href)
		{
			e.preventDefault();
		}
		if (options.click)
		{
			options.click(e);
		}
	});
	return button;
}
		
/*
Call the server API.

Parameters:
	data -- An object to pass to the API as the request.
	success -- An optional callback on success (response "status" is "ok")
	error -- An optional callback on error (response "status" is "error")

Both the success and error callbacks are handed the response object. If the
server gets back to us with a status that isn't "ok" or "error" we throw an
alert. If there is an issue with the connection itself we throw a different
alert.
*/

function api(data, success, error)
{
	$.ajax({
		type: 'POST',
		url: '/api',
		data: data,
		dataType: 'json',
		success: function(res)
		{
			if (res.status == 'ok')
			{
				if (success)
				{
					success(res);
				}
			}
			else if (res.status == 'error')
			{
				if (error)
				{
					error(res);
				}
				else
				{
					// Default error handler.
					alert(res.error)
				}
			}
			else
			{
				// The server got back, but doesn't conform to what we expect.
				alert('We made contact with the server but it isn\'t making sense at the moment...');
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			// Connection issues.
			alert('There was an issue connecting to the server. Please try again in a few seconds. If this problem persists, please contact the administrator.');
		}
	});
}

os = {
	path: {
		dirname: function(path)
		{
			var match = path.match(/^(.+?)\/+[^\/]+\/*$/);
			return match ? match[1] : '';
		},
		basename: function(path)
		{
			var match = path.match(/([^\/]*)$/);
			return match ? match[1] : '';
		},
		ext: function(path)
		{
			match = path.match(/\.(\w+)$/)
			return match ? match[1] : '';
		}
	}
}

function formatSize(bytes)
{
	var order = 0;
	while (bytes > 1024)
	{
		bytes /= 1024;
		order += 1;
	}
	return Math.round(bytes * 100) / 100 + ' ' + 'B KB MB GB TB PB'.split(' ')[order];
}

function formatTime(sec)
{
	return Math.round(sec / 60) + 'min ' + Math.round(sec) % 60 + 's.';
}

// Basic plugin for hover classes.
jQuery.fn.hoverClass = function(opts)
{
    /*
    OPTIONS:
        className -- The class to add.
    */
    switch (typeof(opts))
    {
        case 'undefined':
            opts = {};
            break;
        
        case 'string':
            opts = {className: opts};
            break;
    }
    opts = jQuery.extend({
        className: 'hover'
    }, opts);
    
    var $$ = this;
    $$.hover(function()
    {
        $$.addClass(opts.className);
    }, function()
    {
        $$.removeClass(opts.className);
    });
    
    return this;
}

// 
// jQuery(function($){
// 
//     
//     // Setup the AJAX spinner
//     $('#ajax_spinner').ajaxStart(function()
//     {
//         $(this).show();
//     }).ajaxStop(function()
//     {
//         $(this).fadeOut();
//     });
//     
// 	// Setup banner crossfades
// 
// });



