/**
 * File: http://www.facilitywarehouse.com/gallery/js/EmailPopup.js
 */

var EmailPopup = new Class({
	
	target: null,
	content: null,
	
	initialize: function(pup) {
		this.target = new Element('div', {
			'id': 'emailpopupcontainer'
		});
		
		this.content = new Element('div', {
			'class': 'emailpopup'
		});
		
		this.content.injectInside(this.target);
		
		pup.appendChild(this.target);
	},
	
	placePopup: function() {
		var ts = this.target.getSize();
		var ws = window.getSize();
	},

	toggle: function() {
		if (this.target.getStyle('display') == 'block')
			this.hide();
		else
		{
			this.show();
			var xhr = new XHR({method: 'post', onSuccess: function(response){
				popup.emailpopup.content.setHTML(response);
				popup.emailpopup.show();
			}}).send('/gallery/js/async/product.php', 'pid=' + popup.product.id + '&action=email');
		}
	},
	
	show: function() {
		if (this.target.getStyle('display') == 'none')
		{
			this.target.setStyles({
				'display': 'block',
				'visibility': 'visible'
			});
		}
	},

	hide: function() {
		if (this.target.getStyle('display') == 'block')
		{
			this.content.empty();
			this.target.setStyles({
				'display': 'none',
				'visibility': 'hidden'
			});
		}
	},

	send: function() {
		var s = Object.toQueryString({pid: popup.product.id, send: $('productemailsend').value, action: 'email', productemailto: $('productemailto').value, productemailfrom: $('productemailfrom').value, productemailmessage: $('productemailmessage').value});
		var xhr = new Ajax('/gallery/js/async/product.php', {method: 'post', data: s, onComplete: function(response) {
			if (this.getHeader('Error') == '1')
			{
				popup.emailpopup.content.setHTML(response);
				popup.emailpopup.show();
			}else{
				popup.messagebar.setStyles({'display': 'block', 'visibility': 'visible'});
				popup.messagebar.setHTML('email sent');
				popup.emailpopup.hide();
			}
		}}).request();
	}
	


});
