var contactWindowActive = false;
//var loginURL='/contact/mailer.php';
var loginURL='/mailer.php';

var ALIGN_RIGHT = 1;
var ALIGN_LEFT = 2;

function showContactWindow(position, align) {
    if (contactWindowActive) {
        hideContactWindow();
        return;
    }
    contactWindowActive = true;

    var verticalPosition = findPosY(position) - 100;
    //var verticalPosition = findPosY(position) + position.offsetHeight - 1;
    var horizontalPosition = align == ALIGN_RIGHT? '' : (align==ALIGN_LEFT? 'left : '+findPosX(position)+'px;' : '');		

    Element.update('contactWindow',
    '<div id="innerContactWindow" class="popup contact-popup" style="display:none; '+
    'z-index: 25550; position:absolute; top: '+verticalPosition+'px; '+
    horizontalPosition+' "><div class="popup-wrapper">'+
    '<p class="popup-close"><a href="javascript:void(0);" onClick="javascript:hideContactWindow();">'+JSResource.close+'</a></p>'+
    '<h2>'+JSResource.contact_getContactArea_title+'</h2><div id="contactArea">'+		
    getContactArea()+
    '</div></div></div>');					

    Element.show('contactWindow');

    if (align == ALIGN_RIGHT) {
        horizontalPosition = 'left:'+Number(findPosX(position)-Element.getDimensions('innerContactWindow').width+
        Element.getDimensions(position).width+8)+'px;';

        Element.update('contactWindow',
        '<div id="innerContactWindow" class="popup contact-popup" style="display:none; '+
        'z-index: 25550; position:absolute; top: '+verticalPosition+'px; '+
        horizontalPosition+' "><div class="popup-wrapper">'+
        '<p class="popup-close"><a href="javascript:void(0);" onClick="javascript:hideContactWindow();">'+JSResource.close+'</a></p>'+
        '<h2>'+JSResource.contact_getContactArea_title+'</h2><div id="contactArea">'+		
        getContactArea()+
        '</div></div></div>');							
    }	
    Element.show('innerContactWindow');
}

function getContactArea() {
    return '<div id="contactUpperArea">'+
    '<p><label for="email">'+JSResource.contact_getContactArea_email+'</label> <input type="text" id="email" name="email" style="width: 100%" /></p>'+
    '<p><label for="body">'+JSResource.contact_getContactArea_message+'</label> <input type="text" id="body" name="body" style="width: 100%" onkeydown="if ((event.which && event.which == 13)||(event.keyCode && event.keyCode == 13)){doSendEmail();return false;} else return true;"/></p>'+
    '<p><input value="'+JSResource.contact_getContactArea_send+'" type="button" onClick="doSendEmail()"/></p></div>'+
    '<div style="display:none; text-align: left;" id="emailMessage"></div>';
    //'<div style="display:none; text-align: left;" id="emailMessage"></div>'+
    //getRegister();
}

function hideContactWindow() {
    Element.hide('innerContactWindow');	
    Element.hide('contactWindow');
    contactWindowActive = false;
}

function doSendEmail() {
    var handlerFunc = function(t) {
        var response = trim(t.responseText);
//alert(response);

        if (response.substring(0,6)=='error:') {
            Element.update('emailMessage',response.substring(6,response.length));
        } else if (response.substring(0,2)=='ok') {
            Element.update('emailMessage','<p>'+JSResource.contact_doSendEmail_emailOK+'<p>');	
            //decoratePage();
            hideContactWindow();
        }
    }
	
    var errFunc = function(t) {
        alert(JSResource.error + ' ' + t.status + ' -- ' + t.statusText);
    }
	
    Element.update('emailMessage',JSResource.contact_sendingEmail);		
    Element.show('emailMessage');
    var tempValue = $(('email')).value;
    $(('email')).value = '';
    $(('email')).value = tempValue;
    var tempValue = $(('body')).value;
    $(('body')).value = '';
    $(('body')).value = tempValue;

    new Ajax.Request(loginURL, 
        { method:'post', 
          parameters:'email='+fullEscape($(('email')).value)+'&body='+fullEscape($(('body')).value), 
          onSuccess:handlerFunc, onFailure:errFunc});
}
