var SummaryRecord = function() {

	var globalDiv;
	var glinId = -1;
	var emailSummaryDisplayed = false;
	var textDirection;
	
}

SummaryRecord.prototype.Create = function(_glinId, _textDirection) {

	this.globalDiv = $('global');
	this.glinId = _glinId;
	this.textDirection = _textDirection;
	
	this.closeEmailSummaryClickedListener = this.closeEmailSummaryClicked.bindAsEventListener(this);
	
	// Activate the context elements
	this.globalDiv.select('div.cawContainer').each(function(contextElem) {
		Event.observe(contextElem, 'mouseover', this.ContextMouseOver.bindAsEventListener(this));
		Event.observe(contextElem, 'mouseout', this.ContextMouseOut.bindAsEventListener(this));
	}.bind(this));
	
	// Bind the email summary link
	Event.observe($('emailSummaryLink'), 'click', this.emailSummaryClicked.bindAsEventListener(this));
	
}

SummaryRecord.prototype.emailSummaryClicked = function(evt) {
	
	if (this.emailSummaryDisplayed) {
		return;
	}
	
	var emailLink = $(Event.element(evt));
	
	var container = document.createElement('div');
	container.id = 'emailSummary';
	container.style.display = 'none';
	
	this.globalDiv.appendChild(container);	
	
	new Ajax.Request('emailSummaryRecord.action', 
		{ method: 'get',
		parameters: 'legalResource=' + this.glinId,
		onComplete: this.displayEmailSummary.bind(this) });
	
}

SummaryRecord.prototype.emailSummaryReloaded = function() {
	Event.stopObserving($('closeEmailSummaryLink'), 'click', this.closeEmailSummaryClickedListener);
	Event.observe($('closeEmailSummaryLink'), 'click', this.closeEmailSummaryClickedListener);
}

SummaryRecord.prototype.emailSummarySuccess = function(responseNote) {

	this.emailSummaryDisplayed = false;
	
	Element.remove($('emailSummary'));
	
	$$('h1')[0].insert( { after: '<div id="pageNotice" style="display: none"><div>' + responseNote + '</div></div>' });
	
	Effect.toggle('pageNotice', 'blind', {
		afterFinish: function() { setTimeout("Effect.toggle('pageNotice', 'blind')", 3000); } });
	
	
}

SummaryRecord.prototype.displayEmailSummary = function(request) {
	var emailSummary = $('emailSummary');
	var emailIcon = $('emailSummaryIcon');
	Element.update(emailSummary, request.responseText);
	
	var offset = emailIcon.cumulativeOffset();
	var emailIconDimensions = emailIcon.getDimensions();
	var emailSummaryDimensions = emailSummary.getDimensions();
	
	var effectDirection;
	
	if (this.textDirection == 'ltr') {
		emailSummary.style.left = offset[0] + 'px';
		effectDirection = 'top-left';
	} else {
		emailSummary.style.left = (offset[0] + emailIconDimensions.width - emailSummaryDimensions.width) + 'px';
		effectDirection = 'top-right';
	}
	
	emailSummary.style.top = (offset[1] + emailIconDimensions.height + 6) + 'px';
	
	Effect.Grow(emailSummary, {direction: effectDirection,
			afterFinish: function() {
				try {
					// clear latent styles
					emailSummary.style.height = '';
					emailSummary.style.width = '';
					emailSummary.style.font = '';
					// emailSummary.style.left = '';
					// emailSummary.style.top = '';
					emailSummary.style.opacity = '';
				}
				catch (e) { }
	}});
	
	this.emailSummaryReloaded();
	
	this.emailSummaryDisplayed = true;
}

SummaryRecord.prototype.closeEmailSummaryClicked = function(evt) {

	var closeLink = Event.element(evt);
	var emailSummary = $('emailSummary');	
	var effectDirection;
	
	if (this.textDirection == 'ltr') {
		effectDirection = 'top-left';
	} else {
		effectDirection = 'top-right';
	}
	Effect.Shrink(emailSummary, { direction: effectDirection, 
			afterFinish: function(effect) { Element.remove(emailSummary); } });
	
	this.emailSummaryDisplayed = false;
	
}

SummaryRecord.prototype.ContextMouseOver = function(evt) {

	var containerElem = Event.element(evt);
	if (!Element.hasClassName(containerElem, 'cawContainer')) {
		containerElem = containerElem.findParentWithClass('div', 'cawContainer');
	}
	
	var wrapperElem = $(containerElem.select('div.cawWrapper')[0]);
	var contentElem = $(wrapperElem.select('div.cawContent')[0]);

	// close others
	$A(this.globalDiv.select('div.cawWrapper')).each(function(clearElem) {
		if (clearElem != wrapperElem) {
			clearElem.style.visibility = 'hidden';
		}
	});
	
	var coords = containerElem.cumulativeOffset();
	
	var l = coords[0];
	if ((wrapperElem.offsetWidth + coords[0]) > this.globalDiv.offsetWidth) {
		l = this.globalDiv.offsetWidth - wrapperElem.offsetWidth - 25;
	}
	
	var h = contentElem.getHeight() - 2;
	var t = (coords[1] + 1);
	if ((t + h) > document.offsetHeight) {
		t = document.offsetHeight - h;
	}
	wrapperElem.style.left = l + 'px';
	wrapperElem.style.top = t + 'px';
	contentElem.style.height = h + 'px';
	wrapperElem.style.visibility = 'visible';
	
}

SummaryRecord.prototype.ContextMouseOut = function(evt) {
	var containerElem = Event.element(evt);
	if (!Element.hasClassName(containerElem, 'cawContainer')) {
		containerElem = containerElem.findParentWithClass('div', 'cawContainer');
	}
	if (!Position.within(containerElem, Event.pointerX(evt), Event.pointerY(evt))) {
		var wrapperElem = containerElem.select('div.cawWrapper')[0];
		wrapperElem.style.visibility = 'hidden';
	}
}
