if (!window.Avus)
	var Avus = new Object();

Avus.PopupForm = Class.create({
	visible: false,

	initialize: function(opener, target) 
	{
		if (!Object.isElement($(opener)) || !Object.isElement($(target)))
			return;

		this.opener = $(opener);
		this.target = $(target);

		this.opener.insert(this.downarrow = new Element('div', {'class': 'dropdown-arrow'}));
		this.opener.addClassName('button-dropdown-arrow');

		this.wrap = new Element('div', {'class': 'popupform-wrap'}).update(this.target);
		this.wrap.setStyle({
			position: 'absolute'
		});
		document.body.insert(this.wrap);

		this.wrap.hide();

		this.opener.observe('click', function(ev){
			if (this.visible)
				this.hideTarget();
			else
				this.showTarget();
		}.bindAsEventListener(this));
		
		this.target.select('input[type="reset"]').invoke('observe', 'click', this.hideTarget.bindAsEventListener(this));
		
		this.bel = function(e){
			var ancestors = e.element().ancestors();
			var b = false;
			for (var i=0; i<ancestors.length && !b; i++)
				if (ancestors[i] == this.wrap)
					b = true;
			if (b || e.element() == this.opener || e.element() == this.downarrow)
				return;
			this.hideTarget();
		}.bindAsEventListener(this);
	},
	
	showTarget: function(){
		this.opener.toggleClassName('inverted');
		this.setWrapPosition();
		this.wrap.show();
		document.observe('click', this.bel);
		this.visible = !this.visible;
	},

	hideTarget: function(){
		this.opener.toggleClassName('inverted');
		this.wrap.hide();
		document.stopObserving('click', this.bel);
		this.visible = !this.visible;
	},
	
	setWrapPosition: function()
	{
		var oX = this.opener.cumulativeOffset()[0];
		var oY = this.opener.cumulativeOffset()[1];

		var oW = this.opener.getWidth();
		var oH = this.opener.getHeight();

		var sW = document.viewport.getWidth();
		var sH = document.viewport.getHeight();

		var wW = this.wrap.getWidth();
		var wH = this.wrap.getHeight();

		if (oX + wW + 10 > sW)
			var wX = oX + oW - wW;
		else
			var wX = oX;
		var xY = oY + oH;
		
		this.wrap.setStyle({
			left: wX + 'px',
			top: xY + 'px'
		});
	}

});


Avus.ConditionalCombobox = Class.create({

	options: {
		beforeAction: Prototype.emptyFunction,
		targetDefaultOption: '',
		targetDisabledMessage: ''
	},

	initialize: function(sourceId, targetId, actionURL, options) 
	{
		if (Object.isElement($(sourceId)))
			this.source = $(sourceId);
		else
			return;

		if (Object.isElement($(targetId)))
			this.target = $(targetId);
		else
			return;
		
		Object.extend(this.options, options || {});
		this.actionURL = actionURL;
		
		this.addObservers();
	},
	
	addObservers: function(){
		this.source.observe('change', function(){
			this.options.beforeAction.bind(this)();
			if (this.source.value && this.source.value != 0)
			{
				this.target.disabled = true;
				ajax_submitPostURL(this.actionURL, 'sourceValue=' + this.source.value, function(response){
					this.target.updateOptions(response.list);
					if (this.options.targetDefaultOption)
						this.target.insert({top: new Element('option', {value: 0}).update(this.options.targetDefaultOption)});
					this.target.disabled = false;
					this.target.selectedIndex = 0;
				}.bind(this));
			}
			else
			{
				this.target.updateOptions([this.options.targetDisabledMessage]);
				this.target.disabled = true;
			}
		}.bindAsEventListener(this));
	}
});


AvusMethods = {

	printElement: function(elementId) {
		element = $(elementId);
		
		if (Object.isElement($('avus_print_iframe')))
		{
			iframe = $('avus_print_iframe');
			iframe.contentWindow.document.body.insert(element.innerHTML);
			iframe.contentWindow.print();
		}
		else
		{
			iframe = new Element('iframe', {id:'avus_print_iframe', src: '/blank.htm'});
			iframe.setStyle({
				width: 0,
				height: 0
			});
			document.body.insert(iframe);

			iframe.contentWindow.onload = function(){
				iframe.contentWindow.document.body.insert(element.innerHTML);
				iframe.contentWindow.print();
			}
		}
	}
}

Object.extend(Avus, AvusMethods);

