var testimonialAuthorText="";
var MAIL_FORM = $('#mail_form');
var MAIL_LAYER = $('#mail_layer');


function onMailtoClick(event) {
    var mailTo = $(event.target).attr('href').split('mailto:')[1];
    var mailInput = '<input type="hidden" name="mail_to" value="'+ mailTo +'" />';
    if($('#'+MAIL_FORM.attr('id')+' input[name="mail_to"]').length==1){
		$(MAIL_FORM).find('input[name="mail_to"]').val(mailTo);
	}else{
		MAIL_FORM.append(mailInput);
	}
    try{
        if($(event.target).hasClass('noAdvertiser')){
			var old_text = $(MAIL_LAYER).find('h2').text();
			$(MAIL_LAYER).find('h2').text(testimonialAuthorText)
			popup.showPopup(MAIL_LAYER.attr('id'), "", "0.4");
			$('#'+MAIL_LAYER.attr('id')+' .hide_form').click(function(){
				$(MAIL_LAYER).find('h2').text(old_text);
				popup.hidePopup(MAIL_LAYER.attr('id'));
			})
		}else{
			popup.showPopup(MAIL_LAYER.attr('id'), "hide_form", "0.4");
		}
    } catch(e) {
        showMailPopup();
    };
    return false;
};

function showMailPopup() {
    var positionTop = $(document).scrollTop() - ($(window).height() - MAIL_LAYER.height()) / 2;
    //var positionLeft = ($(window).width() - MAIL_LAYER.width()) / 2;
    MAIL_LAYER.css('position', 'absolute'
            ).css('z-index', 20
            //).css('left', positionLeft
            ).css('top', positionTop
            ).slideDown();
    $('.hide_form[name="mail"]').click(function() {
        $(this).unbind('click');
        MAIL_LAYER.slideUp();
        return false;
    });
};

$(document).ready(function() {
    if (!MAIL_FORM) {
        return false;
    };
    $('a[href^="mailto"]').each(function() {
		$(this).click(onMailtoClick);			
    });
    $('#show_mail_layer').click(function() {
        MAIL_FORM.find('input[name="mail_to"]').each(function() {
            $(this).remove();
        });
        return true;
    });
});

