
// Initialize

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 = '';
	}
}

Event.observe(window, 'load', function() {
	// Create elements at end of document for dailog
	var overlay = new Element('div', { 'id': 'overlay', 'zIndex': 9998 });
	var dialog = new Element('div', { 'id': 'dialog', 'zIndex': 9999 });	
	var bod = $$('body')[0];
	bod.appendChild(overlay);
	bod.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') {
				var yesaction = "document.location.href='" + this.href + "'";
				Ask(this.title, yesaction, "$('dialog').hide();$('overlay').hide(); ShowAllFlash(); return false;");
			} else if (this.rel == 'dialog') {
				var closeaction = "$('dialog').hide();$('overlay').hide(); ShowAllFlash(); return false;";
				var html = '<div style="padding: 20px; text-align: center; font: bold 12px Arial">';
				html += this.title;
				html += "<p>";
				html += '<button onclick="' + this.href + '; ' + closeaction + '" value="OK">OK</button>';
				html += '<button onclick="' + closeaction + '" value="Cancel">Cancel</button>';
				html += "</p></div>";
				loadHtmlDialog(html);
			} else if (this.rel == 'alert') {
				var closeaction = "$('dialog').hide();$('overlay').hide(); ShowAllFlash(); return false;";
				var html = '<div style="padding: 20px; text-align: center; font: bold 12px Arial">';
				html += this.title;
				html += "<p>";
				html += '<button onclick="' + closeaction + '" value="OK">OK</button>';
				html += "</p></div>";
				loadHtmlDialog(html);
			} else {
				loadDialog(this.href);
			}
			return false;
		});
		link.onclick = function(){return false;};
	}
});


// 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 loadDialog(url) {
	// Show dialog and adjsut properties
	var x = Math.floor((getWindowWidth() - $('dialog').getWidth()) / 2);
	var y = Math.floor(((getWindowHeight() - $('dialog').getHeight()) / 2) + getScrollY());
	$('dialog').update('Loading...');
	$('dialog').style.position = 'absolute';
	$('dialog').style.display = 'block';
	$('dialog').style.top = y + 'px';
	$('dialog').style.left = x + 'px';
	$('overlay').style.position = 'absolute';
	$('overlay').style.display = 'block';
	$('overlay').style.top = (y+10) + 'px';
	$('overlay').style.left = (x+10) + 'px';
	$('overlay').style.zIndex = '9999';
	// 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() {
					$('dialog').hide();
					$('overlay').hide();
					return false;
				});
			}
			return false;
		}	
	});
	return false;
}

function loadHtmlDialog(html, width, height) {
	// Hide flash
	HideAllFlash();
	// Variables
	if (width > 100) w=width;
	else w=500;
	if (height > 100) h=height;
	else h=250;
	// Show dialog and adjsut properties
	$('overlay').style.height = h + 'px';
	$('dialog').style.height = h + 'px';
	$('overlay').style.width = w + 'px';
	$('dialog').style.width = w + 'px';
	var x = Math.floor((getWindowWidth() - $('dialog').getWidth()) / 4);
	var y = Math.floor(((getWindowHeight() - $('dialog').getHeight()) / 2) + getScrollY());
	$('dialog').update('Loading...');
	$('dialog').style.position = 'absolute';
	$('dialog').style.display = 'block';
	$('dialog').style.top = y + 'px';
	$('dialog').style.left = x + 'px';
	$('overlay').style.position = 'absolute';
	$('overlay').style.display = 'block';
	$('overlay').style.top = (y+10) + 'px';
	$('overlay').style.left = (x+10) + 'px';
	$('overlay').style.zIndex = '9999';
	// Write content
	$('dialog').update(html);
	// Enable all links
	var links = $$('a.lbAction');
	for (i=0; i < links.length; i++) {
		var link = links[i];
		link.observe('click', function() {
			$('dialog').hide();
			$('overlay').hide();
			return false;
		});
	}
	return false;
}

function Ask(question, yesaction, noaction) {
	// Show dialog and adjsut properties
	var x = Math.floor((getWindowWidth() - $('dialog').getWidth()) / 2);
	var y = Math.floor(((getWindowHeight() - $('dialog').getHeight()) / 2) + getScrollY());
	$('dialog').update('Loading...');
	$('dialog').style.position = 'absolute';
	$('dialog').style.display = 'block';
	$('dialog').style.top = y + 'px';
	$('dialog').style.left = x + 'px';
	$('overlay').style.position = 'absolute';
	$('overlay').style.display = 'block';
	$('overlay').style.top = (y+10) + 'px';
	$('overlay').style.left = (x+10) + 'px';
	$('overlay').style.zIndex = '9999';
	// Write content
	html = '<div style="padding: 20px; text-align: center">';
	html += '<p style="font: bold 12pt Arial; margin-bottom: 20px;">' + question + "</p>";
	html += "<p>";
	html += '<button onclick="' + yesaction + '" value="Yes">Yes</button>';
	html += '&nbsp; &nbsp; &nbsp; ';
	html += '<button onclick="' + noaction + '" value="No">No</button>';
	html += "</p>";
	html += '</div>';
	$('dialog').update(html);
	return false;
}


// Open ID - this should be moved/replaced

function showOpenID(state) {
	var on = state ? '' : 'none';
	var off = state ? 'none' : '';
	document.getElementById('normal_login').style.display = off;
	document.getElementById('openid_login').style.display = on;
	document.getElementById('login_button').value = state ? 'OpenID Login' : 'Login';
}


// 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 "";
	}
	
	
}
