
activateMenu = function(nav, width, top, left) {
	if (window.opera || document.all) {
		$(nav).observe('mouseover', function() {
		    var me = this.down('div.dropdown');
		    me.style.display = 'block';
		    me.absolutize();
		    me.style.position = 'absolute';
		    if (!window.opera) {
			    me.style.width = width;
			    me.style.top = top;
			    if (left) me.style.left = left;
			    else me.style.left = 'auto';
		    }
		});
		$(nav).observe('mouseout', function() {
			var me = this.down('div.dropdown');
			me.style.display = 'none';
		});
	}
}
Event.observe(window, 'load', function() {
	// Get sizes
	var user = $('user').cumulativeOffset();
	// Dropdowns
	activateMenu('flag', 600, 140);
	//activateMenu('user', 370, 140, user.left + 15);
	var menus = $('navbar').getElementsByClassName('dropdown');
	for (i=0; i < menus.length; i++) {
		li = menus[i].up('li');
		lidim = li.cumulativeOffset();
		activateMenu(li.identify(), 175, lidim.top + 39, lidim.left + 15);
	}
	// Search
	$('searchbox').observe('focus', function() {
	   	 $('searchbox').value = '';
	});
	$('searchbox').observe('blur', function() {
	    if ($('searchbox').value == '') {
		   	 $('searchbox').value = 'Search MileSplit...';
	   	 }
	});
	// Round corners
	Rounded($$('#sitebar div.lining div.bumper'), 5, 5, 0, 0);
	// Create elements at end of document for dailog
	//var overlay = new Element('div', { 'id': 'overlay' });
	//var dialog_outer = new Element('div', { 'id': 'dialog_outer' });	
	//var dialog = new Element('div', { 'id': 'dialog' });
	//var bod = $$('body')[0];
	//bod.appendChild(overlay);
	//overlay.appendChild(dialog_outer);
	//dialog_outer.appendChild(dialog);
	// Dialog enable all links
	var links = $$('a.lbOn');
	for (i=0; i < links.length; i++) {
		var link = links[i];
		link.observe('click', function() {
			if (this.rel == 'confirm' || this.rel == 'dialog') {
				var yesaction = "document.location.href='" + this.href + "'";
				Ask(this.title, yesaction, null);
			} else if (this.rel == 'alert') {
				var html = '<div><p>' + this.title + "</p></div>";
				html += '<p><a href="#" class="lbAction">Okay, I got it</a></p>';
				loadHtmlDialog(html);
			} else {
				loadDialog(this.href);
			}
			return false;
		});
		link.onclick = function(){return false;};
	}
});

function ExecuteSearch() {
	var elterm = $('searchbox');
	var term = elterm.value.replace(' ', '_');
	if (term != '' && term != 'Search_MileSplit...') {
		document.location = '/search/athletes/' + term;	
	}
	return false;
}

function SearchKeyPress(e) {
	var key;
	if (window.event) key = window.event.keyCode; // IE
	else key = e.which; // FF
	if (key == 13) { // Enter key pressed
		ExecuteSearch();
		return false;
	} else {
		return true;	
	}
}

function HideAllFlash() {
	var objects = $$('object');
	var embeds = $$('embed');
	var iframes = $$('iframe');
	for (i=0; i < objects.length; i++) {
		objects[i].style.display = 'none';
		var wmode = objects[i].down('param[name="wmode"]');
		if (wmode) {
			wmode.value = 'opaque';
		} else {
			var opaque = new Element('param', { 'name' : 'wmode', 'value': 'opaque' });
			objects[i].appendChild(opaque);
		}
	}
	for (i=0; i < embeds.length; i++) {
		embeds[i].style.display = 'none';
		embeds[i].wmode = 'opaque';
	}
	for (i=0; i < iframes.length; i++) {
		iframes[i].style.display = 'none';
	}
}

function ShowAllFlash() {
	var objects = $$('object');
	var embeds = $$('embed');
	var iframes = $$('iframe');
	for (i=0; i < objects.length; i++) {
		objects[i].style.display = '';
	}
	for (i=0; i < embeds.length; i++) {
		embeds[i].style.display = '';
	}
	for (i=0; i < iframes.length; i++) {
		iframes[i].style.display = '';
	}
}


// Base functions

function getWindowHeight() {
	return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight: null;	
}

function getWindowWidth() {
	return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth: null;	
}

function getScrollY() {
    if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop) return document.body.scrollTop;
    else if (window.pageYOffset) return window.pageYOffset;
    else if (window.scrollY ) return window.scrollY;
    else return 0;
}

    
// Dialog

function showOverlay() {
	$('overlay').style.display = 'block';
	$('overlay').style.position = 'absolute';
	$('overlay').style.top = 0;
	$('overlay').style.left = 0;
	$('overlay').style.width = getWindowWidth();
	$('overlay').style.height = getWindowHeight();
}

function loadDialog(url) {
	// Hide flash
	//HideAllFlash();
	// Show dialog and adjsut properties
	$('container').fade();
	$('dialog').update('Loading...');
	showOverlay();
	// Get content
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			$('dialog').update(transport.responseText);
			// Enable all links
			var links = $$('a.lbAction');
			for (i=0; i < links.length; i++) {
				var link = links[i];
				link.observe('click', function() {
					new Effect.Puff('overlay');
					Effect.SlideDown('container');
					return false;
				});
			}
			return false;
		}	
	});
	return false;
}

function loadHtmlDialog(html, width, height) {
	// Hide flash
	//HideAllFlash();
	// Show dialog and adjsut properties
	var x = Math.floor((getWindowWidth() - $('dialog').getWidth()) / 2);
	var y = Math.floor(((getWindowHeight() - $('dialog').getHeight()) / 2) + getScrollY());
	Effect.SlideUp('container');
	html += '<p><a href="#" class="lbAction">Close</a></p>';
	$('dialog').update(html);
	showOverlay();
	// Enable all links
	var links = $$('a.lbAction');
	for (i=0; i < links.length; i++) {
		var link = links[i];
		link.observe('click', function() {
			Effect.Shrink('overlay');
			Effect.SlideDown('container');
			return false;
		});
	}
	return false;
}

function Ask(question, yesaction, noaction) {
	// Hide flash
	//HideAllFlash();
	// Show dialog and adjsut properties
	var x = Math.floor((getWindowWidth() - $('dialog').getWidth()) / 2);
	var y = Math.floor(((getWindowHeight() - $('dialog').getHeight()) / 2) + getScrollY());
	$('container').fade();
	// Write content
	html = '<div><p>' + question + "</p></div>";
	html += "<p>";
	if (yesaction) html += '<a href="#" onclick="' + yesaction + '">Yes</a>';
	else html += '<a href="#" class="lbAction">Yes</a>';
	html += '&nbsp; &nbsp; &nbsp; or &nbsp; &nbsp; &nbsp; ';
	if (noaction) '<a href="#" onclick="' + noaction + '">No</a>';
	else html += '<a href="#" class="lbAction">No</a>';
	html += "</p>";
	$('dialog').update(html);
	showOverlay();
	// Enable all links
	var links = $$('a.lbAction');
	for (i=0; i < links.length; i++) {
		var link = links[i];
		link.observe('click', function() {
			new Effect.Puff('overlay');
			Effect.SlideDown('container');
			return false;
		});
	}
	return false;
}

// Old stuff


// Shortcut

function el(name) {
	return document.getElementById(name);	
}

// Dropdown

function Dropdown(el) {
	
	this.el = el;
	this.Value = this.el.options[this.el.selectedIndex].value;
	this.Text = this.el.options[this.el.selectedIndex].text;
	
	this.IndexOfText = function(str) {
		for (i=0; i < this.el.options.length; i++) {
			if (this.el.options[i].text == str) return i;
		}
		return -1;
	}
	
	this.AddOption = function(text, value) {
		var option = document.createElement("option");
		option.text = text;
		option.value = value;
		this.el.options.add(option);
	}
	
}

function GetDropdownValue(elname) {
	var el = document.getElementById(elname);
	return el.options[el.selectedIndex].value;
}


// Radio/Checkbox Functions

function RadioSet(arr) {

	this.buttons = array;
	
	this.SelectedIndex = function() {
		for (i=0; i < this.buttons.length; i++) {
			if (this.buttons[i].checked) return i;
		}
		return -1;
	}
	
	this.SelectedValue = function() {
		for (i=0; i < this.buttons.length; i++) {
			if (this.buttons[i].checked) return this.buttons[i].value;
		}
		return "";
	}
	
	
}



