// Contact Overlay

dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.SimpleTextarea");
dojo.require("dijit.form.Button");


dojo.addOnLoad (initContact);

function initContact() {
	var contactLink = dojo.byId("Contact");
	dojo.connect(contactLink, "onclick", newContactWindow);
}

function newContactWindow(event) {
	dojo.stopEvent(event);
	var contactWindow = new dijit.Dialog({
		id: "Contacting",
		draggable: false,
		autofocus: false,
		style: "width: 586px; text-align: right;"
	});
	dojo.style(contactWindow.closeButtonNode, "display", "none");
	dijit.byId("Contacting").titleNode.innerHTML = '<img src="/images/headers/header_contact.png" width="225" height="15" border="0" style="position: relative; top: 3px; left: -339px;" />';
	
	var address = new dijit.form.TextBox({
 		id: 'address',
 		name: 'address',
 		trim: true,
 		style: 'width: 554px; height: 20px; background-image: url(/images/backgrounds/email_address.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D;'
 	});
 	
	var subject = new dijit.form.TextBox({
 		id: 'subject',
 		name: 'subject',
 		trim: true,
 		style: 'width: 554px; height: 20px; background-image: url(/images/backgrounds/email_subject.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D;'
 	});

	var body = new dijit.form.SimpleTextarea({
		id: "body",
		name: "body",
		style: 'width: 554px; height: 188px; background-image: url(/images/backgrounds/email_body.gif); background-repeat: no-repeat; font: 12px helvetica; padding: 6px; color: #4D4D4D;'
		},
		"body"
	);

	var sendButton = new dijit.form.Button({label: "SEND", id: "Send", style: "color: green;" });
	sendButton.attr('disabled', true);
	var cancelButton = new dijit.form.Button({label: "CANCEL", id: "Cancel"});

	contactWindow.startup();
	contactWindow.containerNode.appendChild(address.domNode);	
	contactWindow.containerNode.appendChild(subject.domNode);	
	contactWindow.containerNode.appendChild(body.domNode);	
	contactWindow.containerNode.appendChild(cancelButton.domNode);
	contactWindow.containerNode.appendChild(sendButton.domNode);
	contactWindow.show();

	dojo.connect(dojo.byId('Contacting'), 'onkeyup', function() {
		var testSubject = dijit.byId('subject').getValue();
		var testAddress = dijit.byId('address').getValue();
		var testBody = dijit.byId('body').getValue();
		if(testSubject!='' && testAddress!= '' && testBody!=''){
			sendButton.attr('disabled', false);
		} else {
			sendButton.attr('disabled', true);
		}
	});

	dojo.connect(dojo.byId("Send"), "onclick", function(){
		var parameters = {
			url: "/helpers/contact.php",
			preventCache: true,
			load: function(data){
				var closeButton = new dijit.form.Button({label: "CLOSE", id: "Close"});
				contactWindow.destroyDescendants();
				contactWindow.containerNode.innerHTML = '<img src="/images/email_sent.gif" width="586" height="259" border="0" /><br />';
				contactWindow.containerNode.appendChild(closeButton.domNode);
				dojo.connect(dojo.byId("Close"), "onclick", destroyContact);
			},
			error: function(data){
				contactWindow.destroyDescendants();
				contactWindow.containerNode.innerHTML = '<img src="/images/email_sent_error.gif" width="586" height="259" border="0" /><br />';
				contactWindow.containerNode.appendChild(closeButton.domNode);
				dojo.connect(dojo.byId("Close"), "onclick", function(){ contactWindow.destroyRecursive(); closeButton.destroy(); contactWindow.hide(); });
			},
			timeout: 2000,
			content: {
				address: address.value,
				subject: subject.value,
				body: body.value
			}
		};
		dojo.xhrPost(parameters);
	});
	dojo.connect(dojo.byId("Cancel"), "onclick", destroyContact);
 	dojo.connect(dojo.byId('address'), 'onfocus', function() {
 		dojo.byId('address').style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('address'), 'onblur', function() {
		var entry = dijit.byId('address').getValue();
		if(entry==''){
			dojo.byId('address').style.backgroundImage = 'url(/images/backgrounds/email_address.gif)';
		}
	});
 	dojo.connect(dojo.byId('subject'), 'onfocus', function() {
 		dojo.byId('subject').style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('subject'), 'onblur', function() {
		var entry = dijit.byId('subject').getValue();
		if(entry==''){
			dojo.byId('subject').style.backgroundImage = 'url(/images/backgrounds/email_subject.gif)';
		}
	});
 	dojo.connect(dojo.byId('body'), 'onfocus', function() {
 		dojo.byId('body').style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('body'), 'onblur', function() {
		var entry = dijit.byId('body').getValue();
		if(entry==''){
			dojo.byId('body').style.backgroundImage = 'url(/images/backgrounds/email_body.gif)';
		}
	});
}

function destroyContact() {
	var dialog = dijit.byId("Contacting");
	dialog.destroyRecursive();
	dialog.hide();
}
