﻿if (typeof Effect == 'undefined')
	throw("grower.js requires including script.aculo.us' effects.js library!");

var grower = Class.create();
grower.prototype = {
	effect: null,
	width: 0,
	table: null,
	text: null,
	hoverPE: null,
	phoneWidth: 0,
	emailWidth: 0,
	outerdivni: null,

	tableCells: null,
	phones: null,
	emails: null,

	initialize: function (table, outerdivni) {
		this.table = $(table);
		this.table.setStyle({ fontSize: '90%' });
		this.outerdivni = $(outerdivni);

		Event.observe(this.table, 'mouseover', function () {
			if (this.hoverPE)
				this.hoverPE.stop();
			this.table.addClassName('availabilityhover');
			this.hoverPE = new PeriodicalExecuter(this.showit.bind(this), 0.4);
		} .bind(this));

		Event.observe(this.table, 'mouseout', function () {
			if (this.hoverPE)
				this.hoverPE.stop();
			this.table.removeClassName('availabilityhover');
			this.hoverPE = new PeriodicalExecuter(this.hideit.bind(this), 0.9);
		} .bind(this));

		this.phones = this.table.select('.subphone');
		this.phones.each(function (element) {
			element.show().makeClipping();
			element.cleanWhitespace().setStyle({ whiteSpace: 'nowrap', width: '0px' });
			this.phoneWidth = Math.max(this.phoneWidth, element.scrollWidth);
		} .bind(this));

		this.emails = this.table.select('.subemail');
		this.emails.each(function (element) {
			element.show().makeClipping();
			element.cleanWhitespace().setStyle({ whiteSpace: 'nowrap', width: '0px' });
			this.emailWidth = Math.max(this.emailWidth, element.scrollWidth);
		} .bind(this));

		this.tableCells = this.table.select('.phone');
		this.tableCells = this.tableCells.concat(this.table.select('.email'));
		this.tableCells.each(function (element) {
			element.cleanWhitespace().setStyle({ width: '1px' }); //.hide();
		});

		this.emailWidth = this.emailWidth + 1;
		this.phoneWidth = this.phoneWidth + 1;

		this.table.setStyle({ fontSize: '60%' });
		this.outerdivni.setStyle({ width: this.table.scrollWidth + 'px', height: this.table.scrollHeight + 'px' });
		this.table.setStyle({ position: 'absolute', top: '0px', right: '0px' });
	},
	showit: function (evt) {
		if (this.hoverPE)
			this.hoverPE.stop();
		if (this.effect)
			this.effect.cancel();

		var effects = [];

		this.tableCells.each(function (element) {
			element.show();
		} .bind(this));

		effects.push(new Effect.Morph(this.table, { style: { fontSize: '90%' }, sync: true }));

		this.phones.each(function (element) {
			effects.push(new Effect.Morph(element, { style: { width: this.phoneWidth + 'px' }, sync: true }));
		} .bind(this));

		this.emails.each(function (element) {
			effects.push(new Effect.Morph(element, { style: { width: this.emailWidth + 'px' }, sync: true }));
		} .bind(this));

		this.effect = new Effect.Parallel(effects, { duration: 0.5 });
	},
	hideit: function (evt) {
		if (this.hoverPE)
			this.hoverPE.stop();
		if (this.effect)
			this.effect.cancel();

		var effects = [];

		effects.push(new Effect.Morph(this.table, { style: { fontSize: '60%' }, sync: true }));

		this.phones.concat(this.emails).each(function (element) {
			effects.push(new Effect.Morph(element, { style: { width: '0px' }, sync: true }));
		});

		this.effect = new Effect.Parallel(effects, { duration: Prototype.Browser.Opera ? 0 : 0.5,
			afterFinish: function () {
/*				this.tableCells.each(function (element) {
					element.hide();
				});*/
			} .bind(this)
		}
         );
	}
};
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
