// gotta have this everywhere lctooltip is, so I'm rellocating from a larger script
var saved_tt_html;
var saved_loungeinv_idx = 0;
var newbie = 0;
function istr(str,color)
{
	var result = '<tr><td colspan="2"><span style="color:'+color+
		'">'+str+'</span></tr></td>';

	return result;
}

function whenLoading()
{
	var e = $('invite_status_div'); 
	if(e) e.innerHTML = istr('Sending invite request...', 'orange');
}
function whenLoaded()
{
	var e = $('invite_status_div');
	if(e) e.innerHTML = istr('Invite request pending...', 'orange');
}

function whenCompleted()
{
	var e = $('invite_status_div');
	if(e) {
		if (this.response.length == 0)
			e.innerHTML = istr('Invite Sent!', 'green');
		else	
			e.innerHTML = istr(this.response, 'red');
	}
	hide_allowed = 1;
}

function lounge_invite_menu(to_uid)
{
	saved_tt_html = '';

	var e = $('toggle_invite_div');
	if (e) saved_tt_html = e.innerHTML;

	saved_loungeinv_idx = 0;
	hide_allowed=0;

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar('invmenu', to_uid);
	ajax.requestFile = "/mylounges.php";
	ajax.element = "toggle_invite_div";
	ajax.runAJAX();
}

function member_invite(to_uid)
{
	var ajax = new sack();

	check_select_index(); 

	ajax.method = "GET";
	ajax.setVar("lid", saved_loungeinv_idx);
	ajax.setVar("invite", to_uid);
	ajax.requestFile = "/new_lounge.php";
	ajax.onLoading = whenLoading;
	ajax.onLoaded = whenLoaded; 
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
	hide_allowed = 1; 

	//"dropmenuobj.blur();\">";
}

function cancel_invite()
{
	restore_tt_html();
	hide_allowed = 1; 
}

function check_select_index()
{
	if (saved_loungeinv_idx == 0)
	{
		box = document.forms[0].loungeinv_idx;
		loungeinv_idx = box.options[box.selectedIndex].value;

		saved_loungeinv_idx = loungeinv_idx;
	}
}

function clear_invite_status()
{
	var e = $('invite_status_div'); 
	if(e) e.innerHTML = '';
}

function restore_tt_html()
{
	var e = $('toggle_invite_div');
	if (e) e.innerHTML = saved_tt_html;
}


function hide_homepage_invite(newbie)
{
	if(newbie){
	pitch = '<div align="center">';
	pitch += 'Earn 14,000 fuBucks for every friend that joins.';
	pitch += ' (<a href="javascript:show_homepage_invite('+newbie+')">';
	pitch += 'show me!';
	pitch += '</a>) <span class="tabfaq" style="font-size:11px;font-weight:normal;">[<a href="/bible.php#invite_bonus" style="color:red">?</a>]</span><div>';
	}else{
	pitch = 'Earn 14,000 fuBucks for every friend that joins.';
        pitch += ' (<a href="javascript:show_homepage_invite('+newbie+')">';
	pitch += 'Tell me how!';
	pitch += '</a>)';
	}

	$('homepage_invite_pitch').innerHTML = pitch;
	$('homepage_invite_box').style.display = 'none';

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar('hib', 1);
	ajax.requestFile = "/a_homepage.php";
	ajax.runAJAX();
}

function show_homepage_invite(newbie)
{
	
	if(newbie){
	pitch = '<div align="center">Psst!! Did you know you could earn 14,000 fuBucks for every friend that joins?';
	pitch += ' (<a href="javascript:hide_homepage_invite('+newbie+');">hide</a>)';
	pitch += '<span class="tabfaq" style="font-size:11px;font-weight:normal;">[<a href="/bible.php#invite_bonus" style="color:red">?</a>]</span></div>';
	}else{
	pitch = 'Psst!! Did you know you could earn 14,000 fuBucks for every friend that joins?';
	pitch += ' <span class="tabfaq" style="font-size:11px;font-weight:normal;">[<a href="/bible.php#invite_bonus" style="color:red">?</a>]</span>';
	}

	$('homepage_invite_pitch').innerHTML = pitch;
	$('homepage_invite_box').style.display = 'block';

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar('hib', 0);
	ajax.requestFile = "/a_homepage.php";
	ajax.runAJAX();
}

function invite_resolve_friends() {
    new Ajax.Request('/lb_invite.php', {
	    method: 'get',
	    parameters: { resolve_friends: 1,
	                  email: $('invite_to').value.gsub(/([\n\r\t ])+/,',').strip()
			},
	    onComplete: function (transport) {
		    var data = transport.responseText.evalJSON();
            if (data.emails.length > 0) {
		        $('invite_to').value = data.emails.join("\n");
		        if (data.friends_html.length > 0) {
			        $('invite_blurb').hide();
			        $('invite_friends').show();
			        $('invite_friends_list').insert(data.friends_html); 
		                Modalbox.resizeToContent();
		        } else {
			        $('invite_blurb').update('Oh noes! None of your friends are members of Fubar yet. Send them a invitation via email.');
                }
            } else {
			    $('invite_blurb').update('Sorry! Coulnd\'t find anything in your addressbook. Type someting in instead.');
            }
	    }
    });
}

function invite_submit_friends() {
    new Ajax.Request('/invite.php', {
	parameters: Form.serializeElements($('invite_form').getInputs('', 'add[]')),
	onComplete: function (transport) {
	    $('invite_friends_list').update('');
	    $('invite_friends').hide();
	    $('invite_blurb').update('Friend request(s) sent!');
	    $('invite_blurb').show();
	    Modalbox.resizeToContent();
	}
    });

}

function invite_loadContacts(list) {
	var to_field = $('invite_to');
	if (to_field.value != "") {
		to_field.value = to_field.value + "\n";
	}
	to_field.value = to_field.value + list.join("\n");
	invite_resolve_friends();
}

function invite_form_init() {
	var form = $('invite_form');
	if (form.ready) {
		return;
	}
	form.observe('submit', function(e) {
	    e.stop();
	    if ($('invite_to').value == "") {
		alert('Please enter at least one email address.');
		return;
	    }
	    $('invite_to').value = $('invite_to').value.gsub(/([\n\r\t ])+/,',').strip();
	    $(Event.element(e)).request({
		onComplete: function(transport) {
		    Modalbox.notification('Thanks for inviting more people to fubar!', { autoClose: 5000} );
		},
		onCreate: function() {
		    pageTracker._trackEvent('invite', 'submit');
		    $('invite_submit').value = "Sending...";
		    $('invite_submit').disable();
		}
	    });
	});
	form.ready = true;
}

