var menu = {
	init: function (){
		var openLIs = document.getElementsByClassName('open','li',function(theLI){
			var ul = theLI.$$('ul')[0];
			ul.style.overflow = 'hidden';
			ul.OPENHEIGHT = ul.offsetHeight;
			ul.style.height = ul.OPENHEIGHT+'px';
			theLI.OPENED = true;
			theLI.BUSY = false;
			menu.convertSpan ($(theLI.$$('span')[0]),theLI);
			menu.closeMenu(theLI);
			var inner_lis = theLI.$$('li');
			if (inner_lis.length > 0){
				for (var i=0,j; j=inner_lis[i]; i++){
					j=$(j);
					j.addEvent ('focus',function(){
						if (!theLI.BUSY && !theLI.OPENED) {
							menu.openMenu(theLI);
						}
					});
				}
			}
		});
		
	},
	openClose: function (li){
		if (!li.BUSY){
			li.OPENED ?
				menu.closeMenu (li) :
				menu.openMenu (li);
			li.OPENED = !li.OPENED;
		}
	},
	openMenu: function (li){
		var ul = li.$$('ul')[0];
		var ani = new What.Animation.CSS (ul,'height',ul.OPENHEIGHT,15);
		ani.ease ('out');
		ani.onAnimationStart = function (){
			li.BUSY = true;
			li.addClass('open');
		};
		ani.onAnimationEnd = function (){
			li.BUSY = false;
			li.OPENED = true;
		};
		ani.init();
	},
	closeMenu: function (li){
		var ul = li.$$('ul')[0];
		var ani = new What.Animation.CSS (ul,'height',0,15);
		ani.ease ('in');
		ani.onAnimationStart = function (){
			li.BUSY = true;
			li.removeClass('open');
		};
		ani.onAnimationEnd = function (){
			li.BUSY = false;
			li.OPENED = false;			
		};
		ani.init();
	},
	convertSpan: function (span,li){
		var textnode = span.firstChild;
		while (textnode.nodeType != 3){
			textnode = textnode.firstChild;
		}
		var a = What.Element.create ('a',{href:'#toggleMenu'},[textnode]);
		a.addEvent ('click',function(e){
			menu.openClose(li);
			What.Event.preventDefault(e);
			return false;
		});
		a.addEvent ('focus',function(e){
			if (!li.BUSY && !li.OPENED){
				menu.openMenu(li);
			}
		});
		span.parentNode.insertBefore (a,span);
		span.remove();
	}
}

var mp3links = {
	init: function (){
		var as = document.$$('a');
		for (var i=0,k; k=as[i]; i++){
			k = $(k);
			var href = k.href;
			if (/\.mp3$/.test(href)) {
				k.addEvent ('click',function(e){
					var theDialog = new What.GUI.CustomDialog('single-player-dialog');
					var div = What.Element.create ('div',{id:'dialog-inner'});
					theDialog.addContent (div);
					theDialog.draggable = false;
					theDialog.useDropSheet = true;
					theDialog.useESCToClose = true;
					theDialog.xPos = 'center';
					theDialog.yPos = 'center';
					var encodedHref = encodeURIComponent(this.href);
					if (encodedHref.indexOf('.') != -1) {
						encodedHref = encodedHref.replace ('.','%2E');
					}
					var swfURL = "../swf/singleplayer.swf?track="+encodedHref;
					theDialog.onReady = function (){
						var so = new SWFObject (swfURL, "splayer", "269", "37", "8", "#000000");
						so.write (div);
						var button = What.Element.create('button',{},['Close']);
						button.addEvent ('click',function(e){
							theDialog.close();
							return false;
						});
						div.appendChild (button);
					};
					theDialog.onClose = function(){
						location.replace (location.href);
					};
					theDialog.init();
					What.Event.preventDefault(e);
					return false;
				});
			} else {
				continue;
			}
		}
	}
};

function rotateWebcam () {
	var theDate = new Date();
	var image = new Image ();
	image.src = 'http://axis.strengholtgroup.com/axis-cgi/jpg/image.cgi?resolution=320x240&dummy='+theDate.getTime().toString(10);
	image.onload = function (){
		$('webcam-image').src = image.src;
	};
	setTimeout (rotateWebcam,1000);
}

if ($('navigation')){
	menu.init();
}

if ($('webcam-image')){
	rotateWebcam ();
}

mp3links.init();