var web_root = "http://www.cladverts.com";
function DialogOverlay(content, container) {

	// Manage arguments and assign defaults,
	if (typeof container == 'undefined' ) container = document.body;
	if (null == (this.container = $(container))) throw("container is not valid");

	// Assign instance variables
	this.content = content;
	this.overlay = new Element('div', { 'class': 'overlay' }).hide();
	this.dialog = new Element('div', { 'class': 'dialog' }).hide();

	// Hide the overlay when clicked. Ignore clicks on the dialog.
	Event.observe(this.overlay, 'click', this.hide.bindAsEventListener(this));
	Event.observe(this.dialog, 'click',  function(event) { Event.stop(event) });

	// Insert the elements into the DOM
	this.dialog.insert(this.content);
	this.container.insert(this.overlay);
	this.container.insert(this.dialog);

	// Content may have been hidden if it is embedded in the page
	content.show();
	this.dialog.hide();
}

function send(mid){
    var url = web_root + '/messages/xsend_email/?ad='+ mid +"&"+$('sef').serialize();
    new Ajax.Request(url,{
        method:'get',
        onComplete:
        function(transport){
            if (transport.responseText == 'ok'){
                this.dialog.hide();
                this.overlay.hide();
            }else{
                $('reply').innerHTML = transport.responseText;
            }
        },
        onLoading:
        function(){
            $('reply').innerHTML = '<b>Sending... Please wait!</b>';
        },
        onFailure: function(){
            $('reply').innerHTML = "Something went wrong...";
        }

    }
    );

}

function submitForm(img,mid,$sct){
    var ret = '<div id="reply"></div>';
    ret += '<div id="eform" class="eform">';
    ret += '<form name="sef" id="sef" >\n\
    <p>Your mail:</p><p><input type="text" name="comment-email" class="required validate-email txt20" style="width:200px;" /></p>';
    ret += '<p>Subject:</p><p><input type="text" name="subject" class="txt20" value="'+ $sct +'" style="width:420px;" /></p>';
    ret += '<p>Message text:</p>';
    ret += '<p><textarea rows="4" cols="77" name="comment" class="required textarea20"></textarea></p>';
    ret += '<p>Enter text in the image:</p>';
    ret += '<p><input class="required txt20" type="text" name="captcha" /></p>';
    ret += img;
    ret += '<p><input type="submit" value="Send Email" class="ijoin" onclick="send('+mid+');" /></p>';
    ret += '</form></div>';
    return ret;
}

DialogOverlay.prototype.show = function() {
	new Effect.Appear(this.overlay, { duration: 0.5,  to: 0.8 });
	this.dialog.show();
	return this;
};
DialogOverlay.prototype.hide = function(event) {
	this.dialog.hide();
	this.overlay.hide();
	return this;
};
