var boxWidth = 300;

var activeDialog;

var MyLarp = {
	showDialog: function(id) {
		activeDialog = id;
		
		Event.observe(window, 'resize', sizes);
		Event.observe(window, 'scroll', sizes);
		
		// create overlay
		createOverlay('overlay');
		
		if(isIE6()) {
			hideSelectBoxes();
		}
		
		// create dialo
		var dialog = $(id); 
		dialog.addClassName('dialog')
		dialog.setStyle({
		  width: boxWidth+'px'
		});
		
		sizes();
		
		// show overlay and dialog
		Element.show($('overlay'));
		new Effect.Appear(dialog, { delay: 0.1, duration: 0.5});
				
		return false;
	},
	
	closeDialog: function(id) {
		activeDialog = null;
		
		Event.stopObserving(window, 'resize', sizes);
		Event.stopObserving(window, 'scroll', sizes);
		
		if(isIE6()) {
			displaySelectBoxes();
		}
	
		Element.hide(id);
		new Effect.Fade('overlay', { duration: 0.2});
		$('overlay').remove();
		
		return false;
	},
	
	addMandatory: function(id) {
		var _label = $(id)
		if(!_label.innerHTML.endsWith(' *')) {
			_label.innerHTML = _label.innerHTML + ' *';
		}
	},

	enableField: function(id) {
		$(id).disabled=false;
		$(id).removeClassName('readonly');
	},
		
	disableField: function(id) {
		$(id).disabled=true;
		$(id).addClassName('readonly');
	},
	
	toggleField: function(id, value) {
		if (value) {
			MyLarp.enableField(id);
		} else {
			MyLarp.disableField(id);
		}
	},

	removeMandatory: function(id) {
		var _label = $(id)
		if(_label.innerHTML.endsWith(' *')) {
			_label.innerHTML = _label.innerHTML.substr(0, _label.innerHTML.length-2);
		}
	},
	
	activateTab: function(tabSize, id) {
		for (var i = 0; i < tabSize; i++) {
			if(i != id && $('tabContent_'+i)) {
				Element.hide($('tabContent_'+i));
			}
			
			if(i != id && $('tab_'+i)) {
				$('tab_'+i).removeClassName('activeTab');
			}
		}
		
		if($('tabContent_'+id)) {
			Element.show($('tabContent_'+id));
		}
		if($('tab_'+id)) {
			$('tab_'+id).addClassName('activeTab');
		}
		
		if($('tab') != null) {
			$('tab').setAttribute('value', id);
		}
	}
}

function createOverlay(id) {
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id',id);
	$('body').insert({top: objOverlay});
	
	$(id).setStyle({
		opacity: 0.7,
	  	display: 'none'
	});
}

function sizes() {
	// overlay
	var fullHeight = document.viewport.getHeight();
	var fullWidth = document.viewport.getWidth();
	
	if (fullHeight > $('body').scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = $('body').scrollHeight;
	}
	
	if (fullWidth > $('body').scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = $('body').scrollWidth;
	}

	$('overlay').setStyle({
	  	height: popHeight+'px',
	  	width: popWidth+'px'
	});

	// dialog
	var off = $('body').cumulativeScrollOffset();
	var dialog = $(activeDialog);

	dialog.setStyle({
	  top: (off.top+(document.viewport.getHeight()-dialog.getHeight())/2)+'px',
	  left: (off.left+(document.viewport.getWidth()-dialog.getWidth())/2)+'px'
	});
}

/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
}

/**
* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*/
function displaySelectBoxes() {
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
			document.forms[i].elements[e].style.visibility="visible";
			}
		}
	}
}

function isIE6() {
	var version = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (version <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		return true;
	} else {
		return false;
	}
}