var winSize = null;
var aragornSize = null;
var xratio = null;

window.addEvent('domready', function() {
	$$('.hobbitName').setStyle('display', 'none');
	$$('.hobbitImg').setStyle('display', 'inline');
	$('aragorn').setStyle('opacity', 0);
	$('pleaseWait').setStyle('display', 'block');

	Shadowbox.setup('a.hobbit', {
		gallery: 'G',
		continuous: true,
		player: 'iframe',
		height: 450,
		viewportPadding: 0
	});
});

window.addEvent('load', function() {
	try {
		var myTips = new Tips('.hobbit', {className: 'tipContainer'});
	} catch (err) {}
	try {
		initAragorn();
		winSize = window.getSize();
		xratio = (aragornSize.x - winSize.x) / winSize.x;
		centerAragorn();
	} catch (err) {}
	
	/* stupid f***ing IE! */
	var myTimer = setTimeout(function() {
		$('pleaseWait').setStyle('display', 'none');
		$('aragorn').fade(1);
	}, 1);
	
	$$("body").addEvent('mousemove', function(e) {
		if (xratio > 0) {
			var setLeft = e.client.x * xratio * -1;
			$("aragorn").setStyle("left", parseInt(setLeft));
			
			var colorIndex = Math.floor(e.client.x/winSize.x * gandalfColors.length);
			var baseCol = new Color([gandalfColors[colorIndex][0], gandalfColors[colorIndex][1], gandalfColors[colorIndex][2]]);
			var setCol = baseCol.rgbToHex();
			var currentCol = $$("body").getStyle("background-color");
			
			if (currentCol != setCol) {
				$$("body").setStyle("background-color", setCol);
				$("logoContainer").setStyle("background-color", setCol);
				$$("#menu li a").setStyle("color", setCol);
				$$("#menu li a.active").setStyle("color", '#000');
				$$(".tipContainer").setStyle("background-color", setCol);
			}
		}
	});
});

window.addEvent('resize', function() {
	winSize = window.getSize();
	xratio = (aragornSize.x - winSize.x) / winSize.x;
	centerAragorn();
});

function initAragorn() {
	var elements = $$("#frodo li");
	if (elements == null) {
		return;
	}
	var totalWidth = 0;
	var maxHeight = 0;
	for (var i = 0; i < elements.length; i++) {
		var elementSize = elements[i].getSize();
		totalWidth += elementSize.x;
		if (elementSize.y > maxHeight) {
			maxHeight = elementSize.y;
		}
	}
	totalWidth += elements.length * 10;
	$("aragorn").setStyle("width", totalWidth);
	aragornSize = $("aragorn").getSize();
}

function centerAragorn() {
	var setLeft = aragornSize.x / -2 + winSize.x / 2;
	$("aragorn").setStyle("left", parseInt(setLeft));
}

