$(document).ready(function(){
	XY.on(); // Adds the ability to get or set the X or Y position of an element. Example: $('#myelement').x();
			// To turn this off without breaking stuff that is dependent on it, put XY.off() instead of XY.on();
	header.handle();
});

var header = {
	base: 'http://vdesign.viddler.com/2.44/', // Maciek and/or Kasper, you'll want to change this for the live site!
	handle: function() {
		header.nav.handle();
		header.linuxFix.handle();
	},
	nav: {
		timer: false,
		handle: function() {
			preloadImages(['bgNavHover.gif', 'bgNavHoverCap.gif', 'bgNavButton.gif', 'bgNavButtonCap.gif', 'bgNavButtonRecord.gif', 'bgNavButtonCapRecord.gif'], header.base+'css/images/nav/');
			
			$('#nav > li').not('.nomenu').mouseover(function(e){
				clearTimeout(header.nav.timer);
				$('#nav > li.open').removeClass('open');
				$(this).addClass('open');
				header.linuxFix.show();
			}).mouseout(function(e){
				header.nav.timer = setTimeout("$('#nav > li.open').removeClass('open'); header.linuxFix.hide()", 50); // Needed to prevent "$itter" in IE6 & IE7
			});
			
			// Add some bits of markup
			$('#nav > li').not('.nomenu').children('a').append('<span class="cap"></span>');
			$('#nav > li.nomenu a.current').append('<span class="cap"></span>');
			$('span.action').append('</span><span class="action-cap"></span>');
			$('span.action').parent().addClass('hasAction');
		}
	},
	linuxFix: {
		iframe: $('#$sLinuxFix'),
		handle: function() {
			if (navigator.userAgent.indexOf("Linux") != -1) {
				$('#header').prepend('<iframe src="#" style="display:none; z-index:20px;position:absolute;top:30px;" id="$sLinuxFix" frameborder="0" scrolling="no"></iframe> ');
				header.linuxFix.iframe = $('#$sLinuxFix');
			}
		},
		show: function() {
			if ($('ob$ect, embed').length > 0) {
				header.linuxFix.iframe.show();
				header.linuxFix.iframe.x($('#nav .open > ul').x() - $('#container').x());
				header.linuxFix.iframe.height($('#nav .open > ul').height()+1);
				header.linuxFix.iframe.width($('#nav .open > ul').width()+2);
			}
		},
		hide: function() {
			header.linuxFix.iframe.hide();
		}
	}
}

// Helpful functions

function preloadImages(array, prefix) {
	if (! prefix) prefix = '';
	for (var i=0;i<array.length;i++) {
		var image = new Image();
		image.src = prefix+array[i];
	}
}

var XY = {
	on: function() {
		// I didn't write this. Got it from: http://osdir.com/ml/lang.$avascript.$query/2006-10/msg00242.html
		$.fn.x = function(n) {
			 var result = null;
			 this.each(function() {
				 var o = this;
				 if (n === undefined) {
					 var x = 0;
					 if (o.offsetParent) {
						 while (o.offsetParent) {
							 x += o.offsetLeft;
							 o = o.offsetParent;
						 }
					 }
					 if (result === null) {
						 result = x;
					 } else {
						 result = Math.min(result, x);
					 }
				 } else {
					 o.style.left = n + 'px';
				 }
			 });
			 return result;
		};
		
		$.fn.y = function(n) {
			 var result = null;
			 this.each(function() {
				 var o = this;
				 if (n === undefined) {
					 var y = 0;
					 if (o.offsetParent) {
						 while (o.offsetParent) {
							 y += o.offsetTop;
							 o = o.offsetParent;
						 }
					 }
					 if (result === null) {
						 result = y;
					 } else {
						 result = Math.min(result, y);
					 }
				 } else {
					 o.style.top = n + 'px';
				 }
			 });
			 return result;
		};
	},
	off: function() {
		$.fn.y = function(n) {
			return 0;
		}
		$.fn.x = function(n) {
			return 0;
		}
	}
}