/**
* Set up stuff
**/
$(document).ready(function() {
	$(window).resize(background_load);
	
	background_load();
});

/**
* Load the background - after everything else has happened
**/
function background_load() {
	var width = $(window).width();
	var height = $(window).height();
	
	var imgwidth = 1024;
	var imgheight = 768;
	
	if (width > imgwidth || height > imgheight) { imgwidth = 1280; imgheight = 1024; }
	if (width > imgwidth || height > imgheight) { imgwidth = 1440; imgheight = 900; }
	if (width > imgwidth || height > imgheight) { imgwidth = 1680; imgheight = 1050; }
	if (width > imgwidth || height > imgheight) { imgwidth = 1920; imgheight = 1080; }
	
	$('body').css('background-image', 'url(' + ROOT + 'skin/default/images/event/bg_body_' + timeofday + '_' + imgwidth + '.jpg)')
}



/**
* When somthing goes messy, show a facebox message - these are basically trappable errors
**/
function report_error(message) {
	$.facebox(
		'<h2>There has been a server error</h2>' +
		'<ul class="messages"><li class="error">' + message + '</li></ul>' +
		'<p>Reload the page, and try again.<br>If the problem persists, please <a href="/visit_raw_pearls">contact us</a>.'
	);
}

/**
* These are other AJAX errors, like 404s or 500s
**/
$(document).ajaxError(function() {
	if ($('#ajax-error').length == 0) {
		var $err = $('<div id="ajax-error">There has been a server error while trying to communicate with the back-end data server. Please reload the page.</div>');
		$err.hide();
		$err.insertAfter('#header');
		$err.slideDown();
		scrollTo(0,1);
	}
	
	$('#ajax-error').animate({"opacity":0.5}).animate({"opacity":1.0});
});


function addCommas(str) {
	var x = str.split('.');
	str = x[0];
	
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(str)) {
		str = str.replace(rgx, '$1,$2');
	}
	
	return str + '.' + x[1];
}


