$(document).ready(function() {
	Template.init();
});

var Template = 
{
	init: function() {
		//this.requestCustomBackground();
		this.randomiseBlocks('#sidebar ul.random-widths li',180, 230);
		
		// if the news page, force the nav highlight
		//if ($('#content h2').html().toLowerCase() == 'news') {
			//$('#nav-left .current_page_item').removeClass('current_page_item');
			//$('#nav-left a[title=News]').parent().addClass('current_page_item');
		//}
	}
	,
	randomiseBlocks: function(selector, min, max) {
		$(selector).each(
			function(index) {
				var w = min + Math.floor(Math.random() * (max-min));
				$(this).css('width', w+'px');
			}
		);
	}
	,
	getScreenWidth: function() {
		if (screen.width != undefined) {
			return screen.width;
		} else {
			return null;
		}
	}
	,
	requestCustomBackground: function() {
		var w = this.getScreenWidth();
		var bg = $('body').css('background-image');
		console.log(bg);
		if (bg && w >= 1024) {
			var regex = new RegExp('.*\/([a-zA-Z0-9\-]+)_?([a-zA-Z0-9]+)?\.jpg.*','i');
			var matches = regex.exec(bg);
			if (matches.length > 0 && matches[1]) {
				var bgID = matches[1];
				var bgPath = TEMPLATE_DIRECTORY + '/bg.php?n=' + bgID + '&w=' + w;
				var img = 'url(' + bgPath + ')';
				//window.open(bgPath, 'bg');
				$('body').css('background-image', img);
				$('body').css('background-position', '0 0');
			}
		}
	}
}

