var lsinterval = 7;
var _lsInterval;
var luinterval = 120;
var _luInterval;

var saved_uid;
var saved_lounge_id;
var saved_emote_id = 0;
var saved_inactive = 0;

var new_lounge_posts_lock = 0;

window.onunload = function()
{
	lounge_exit();
}

function open_lounge(lounge_id)
{
	var url = '/lounge/' + lounge_id;
	
	saved_lounge_id = lounge_id;

	var loungeWindow = window.open(url, '_lounge_'+lounge_id);
	loungeWindow.focus();
	return false;
}

function lounge_exit(close)
{
	// cancel shout and userlist updates
	if (typeof _lsInterval != "undefined")
		clearInterval(_lsInterval);
	if (typeof _luInterval != "undefined")
		clearInterval(_luInterval);

 	var ajax = new sack(); ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("part", 1);
	ajax.requestFile = "/new_lounge.php";
	if (close == 1)
	{
		ajax.onCompletion = lounge_close;
	}
	else if (close == 0)
	{
		ajax.onCompletion = lounge_relocate;
	}
	ajax.runAJAX();

}

function lounge_close()
{
    window.close();
}

function lounge_relocate()
{
	// cancel shout and userlist updates
	if (typeof _lsInterval != "undefined")
		clearInterval(_lsInterval);
	if (typeof _luInterval != "undefined")
		clearInterval(_luInterval);

	window.onunload = function()
	{
	}

	window.location = "/home.php";
}

function lounge_say()
{
	var ajax = new sack();
	var msg = document.new_lounge_enter_data_form.enter_data.value;

	ajax.method = "POST";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("msg", msg);
	ajax.requestFile = "/new_lounge_shouts.php";
	ajax.element = "new_lounge_notice_div";
	ajax.onCompletion = process_msg_response;
	ajax.runAJAX();
}

function process_msg_response()
{
	update_new_lounge_shouts();

	if (this.response.length > 0)
        $('new_lounge_notice_div').innerHTML = this.response;

    _lsInterval = setInterval('update_new_lounge_shouts()',lsinterval * 1000); 
}


function update_new_lounge_users(forced)
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("olist", 1);
	if (forced != null)
	{
		//alert('forced refresh!');
		ajax.setVar("forced", 1);
	}
	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_online_user_list_div";
	ajax.runAJAX();
}

function update_new_lounge_motd()
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("motd", 1);

	//alert('refreshing motd!');

	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_motd_div";
	ajax.runAJAX();
}

function update_new_lounge_info()
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("info", 1);

	//alert('refreshing info!');

	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_info_div";
	ajax.runAJAX();
}

function update_new_lounge_opts()
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("opts", 1);

	//alert('refreshing opts!');

	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_opts_div";
	ajax.runAJAX();
}

function update_new_lounge_music()
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("music", 1);

	//alert('refreshing music!');

	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_music_div";
	ajax.runAJAX();
}

function update_new_lounge_custom(content_num)
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("custom", content_num);

	//alert('refreshing custom'+content_num+'!');

	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_custom"+content_num+"_div";
	ajax.runAJAX();
}

function update_new_lounge_comments()
{
    var tmpdiv = $('lounge_comments');
    if (tmpdiv) {
        var textdivs = tmpdiv.getElementsBySelector('textarea');
        for (var index = 0; index < textdivs.length; ++index) {
            var div = textdivs[index];
            if (!div.getValue().empty() || div.getValue().focus()) {
                return ;
            }
        }

        load_lounge_comments("lounge_comments",saved_lounge_id,0)
    }
}


function update_new_lounge_posts()
{
	//alert('lock='+new_lounge_posts_lock);

	if (new_lounge_posts_lock == 1)
		return;

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("discuss", 1);

	//alert('refreshing posts!');

	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function process_lounge_shouts()
{
	var content = filter_shouts(this.response);

	$("new_lounge_shoutbox_div").innerHTML = content;
}

function filter_shouts(shouts)
{
	var fucmd_escape = "!:!:!";
	var fucmd_escape_re = new RegExp(fucmd_escape, "g");
	var re = new RegExp(fucmd_escape+'(\\w+)'+fucmd_escape, "g");

	cmds = shouts.match(re);
	if (cmds != null)
	{
		var cnt;

		for (cnt = 0; cnt < cmds.length; cnt++)
		{
			cmd = cmds[cnt].replace(fucmd_escape_re, '');
			//alert('cmd='+cmd);
			handle_cmd(cmd);
		}

		shouts = shouts.replace(re,'');
	}

	return shouts;
}

function handle_cmd(cmd)
{
	if (cmd == "FU_UPDATE_UL")
	{
		update_new_lounge_users("1");
	} 
	else if (cmd == "FU_UPDATE_MOTD")
	{
		update_new_lounge_motd();
	} 
	else if (cmd == "FU_UPDATE_INFO")
	{
		update_new_lounge_info();
	} 
	else if (cmd == "FU_UPDATE_OPTS")
	{
		update_new_lounge_opts();
	} 
	else if (cmd == "FU_UPDATE_MUSIC")
	{
		update_new_lounge_music();
	} 
	else if (cmd == "FU_UPDATE_C1")
	{
		update_new_lounge_custom(1);
	} 
	else if (cmd == "FU_UPDATE_C2")
	{
		update_new_lounge_custom(2);
	} 
	else if (cmd == "FU_UPDATE_C3")
	{
		update_new_lounge_custom(3);
	} 
	else if (cmd == "FU_UPDATE_COMMENTS")
	{
		update_new_lounge_comments();
	} 
	else if (cmd == "FU_UPDATE_POSTS")
	{
		update_new_lounge_posts();
	} 
	else if (cmd == "FU_UPDATE_IBOX")
	{
		refresh_question();
	} 
	else if (cmd == "FU_EJECT")
	{
		lounge_ejection();
	} else
	{
		//alert('unknown server cmd:'+cmd);
	}
}


function update_new_lounge_shouts(initial)
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("shouts", 1);	
	if (initial != null)
		ajax.setVar("init", 1);
	ajax.requestFile = "/new_lounge_shouts.php";
	ajax.onCompletion = process_lounge_shouts;
	ajax.runAJAX();
}


function new_lounge_entsub(e,form) 
{
	if (saved_lounge_id == undefined)
		return false;

	if(window.event && window.event.keyCode == 13) {
		lounge_say(document.new_lounge_shoutbox_data.lounge_id.value);
		clear_lounge_shout_input();
  	} else if (e && e.which == 13) {
		lounge_say(document.new_lounge_shoutbox_data.lounge_id.value);
		clear_lounge_shout_input();
	} else {
		return true;
	}

}
function fakesubmit() {
	return false;
}

function clear_lounge_shout_input() {
	document.new_lounge_enter_data_form.enter_data.value='';
	document.new_lounge_enter_data_form.enter_data.focus();
}

function new_lounge_shouts_init(lounge_id, uid) {
//	saved_lounge_id = lounge_id;
//	saved_uid = uid;

	update_new_lounge_users();
    _luInterval = setInterval('update_new_lounge_users()',luinterval * 1000);

	update_new_lounge_shouts();
    _lsInterval = setInterval('update_new_lounge_shouts()',lsinterval * 1000); 

	//clear_lounge_shout_input();
}

function do_nothing()
{
}

function owner_hire(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("uid", uid);
	ajax.setVar("m", 1);
	ajax.setVar("tab", 1);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_loungemembers.php";
	ajax.runAJAX();
}

function owner_fire(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("uid", uid);
	ajax.setVar("m", 0);
	ajax.setVar("tab", 1);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_loungemembers.php";
	ajax.runAJAX();
}

function owner_unsubscribe_user(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("uid", uid);
	ajax.setVar("unsub", 1);
	ajax.setVar("tab", 1);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_loungemembers.php";
	ajax.runAJAX();
}

function admin_devoice(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("devoice", uid);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function admin_revoice(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("revoice", uid);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function admin_ban(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("ban", uid);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function admin_unban(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("unban", uid);
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function admin_eject(uid)
{
    hide_allowed = 1; hidetip();

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("eject", uid);
	ajax.element = "new_lounge_notice_div";
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function member_emote_menu(to_uid)
{
    new Ajax.Request("/new_lounge.php", {
            method: 'get',
            parameters: { lid: saved_lounge_id, r: to_uid, emenu: 1 },
            onSuccess: function(response) {
                $('emote_swap').innerHTML = response.responseText;
            }
    });
}

function member_emote(to_uid, emote_id)
{
    hide_allowed = 1; hidetip();

    new Ajax.Request("/new_lounge.php", {
            method: 'get',
            parameters: { lid: saved_lounge_id, r: to_uid, emote: emote_id },
            onSuccess: function(response) {
                $('new_lounge_notice_div').innerHTML = response.responseText;
                update_new_lounge_shouts();
            }
    });
}

function member_subscribe(lounge_id)
{
	var ajax = new sack();

	saved_lounge_id = lounge_id;

	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("sub", 1);
	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_notice_div";
	ajax.onCompletion = update_new_lounge_users;
	ajax.runAJAX();
}

function member_unsubscribe(lounge_id)
{
	var ajax = new sack();

	saved_lounge_id = lounge_id;

	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("unsub", 1);
	ajax.requestFile = "/new_lounge.php";
	ajax.element = "new_lounge_notice_div";
	ajax.onCompletion = update_new_lounge_users;
	ajax.runAJAX();
}

function lounge_ejection()
{
	// cancel shout and userlist updates
	if (typeof _lsInterval != "undefined")
		clearInterval(_lsInterval);
	if (typeof _luInterval != "undefined")
		clearInterval(_luInterval);

	window.onunload = function()
	{
	}

	window.location = "/mylounges.php?ejected=1";
}

function create_new_lounge_topic(lounge_id)
{
	new_lounge_posts_lock = 1;

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("new_topic", 1);
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function new_lounge_topic_index(lounge_id, didx)
{
	new_lounge_posts_lock = 0;

	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);

	if (didx == null)
		didx = 0;

	ajax.setVar("didx", didx);
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function process_msg_response()
{
	update_new_lounge_shouts();

	if (this.response.length > 0)
    	$('new_lounge_notice_div').innerHTML = this.response;

}

function open_nl_staff_menu(lounge_id, uid)
{
	var ajax = new sack();

	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("nlsm", 1);
	ajax.setVar("r", uid);
	ajax.requestFile = "/new_lounge.php";
	ajax.element = "staff_tt_div";
	ajax.runAJAX();
}

function post_topic(lounge_id, obj)
{
	new_lounge_posts_lock = 0;

	var ajax = new sack();

	ajax.method = "POST";
	ajax.setVar("lid", lounge_id);

	for(i=0; i<obj.form.elements.length; i++)
	{
		var fieldname = obj.form.elements[i].name;
		var fieldvalue = obj.form.elements[i].value;
	
		if (fieldname == "subject") {
			if (fieldvalue.length == 0) {
				set_post_status('<span style="color:red">Please enter a subject!</span>');
				return false;
			}
		}

		if (fieldname == "ajaxpost")
			continue;

		ajax.setVar(fieldname, fieldvalue);
	}
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();

	return false;
}

function set_post_status(status)
{
	var post_status_box = $('post_status_div');
	post_status_box.innerHTML = status;
}

function get_post_topic(lounge_id, tid)
{
	new_lounge_posts_lock = 1;

	var ajax = new sack();
	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("tid", tid);
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function reply_post(lounge_id, post_id, tid, quote)
{
	new_lounge_posts_lock = 1;

	var ajax = new sack();
	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	if (quote == 0)
	{
		ajax.setVar("reply", post_id);
	}
	else
	{
		ajax.setVar("reply_quote", post_id);
	}
	ajax.setVar("tid", tid);
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function staff_delete_topic(lounge_id, tid)
{
	new_lounge_posts_lock = 0;

	var ajax = new sack();
	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("thread_delete", tid);
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function staff_delete_post(lounge_id, tid, pid)
{
	new_lounge_posts_lock = 0;

	var ajax = new sack();
	ajax.method = "GET";
	ajax.setVar("lid", lounge_id);
	ajax.setVar("tid", tid);
	ajax.setVar("delete", pid);
	ajax.setVar("tab", 1);
	ajax.requestFile = "/lounge_posts.php";
	ajax.element = "new_lounge_discuss_div";
	ajax.runAJAX();
}

function load_lounge_comments(div_id,i,idx) {
    $(div_id).innerHTML = '<br><br><p align=center style="padding-left:3em;font-weight:bold;color:red;">... LOADING ...</p>';

    new Ajax.Request("/a_profile_comments.php", {
            method: 'get',
            parameters: { i: i, idx: idx, type: 'lounge' },
            onSuccess: function(response) {
                handle_comment_reformt(div_id,response);
            }
    });
}

function edit_new_lounge(lounge_id, obj)
{
	var ajax = new sack();

	ajax.method = "POST";
	for(i=0; i<obj.form.elements.length; i++)
	{
		var fieldname = obj.form.elements[i].name;
		var fieldvalue = obj.form.elements[i].value;
	
		/*
		if (fieldname == "ajaxpost")
			continue;
		*/

		ajax.setVar(fieldname, fieldvalue);
	}
	ajax.setVar("tab", 1);
	ajax.requestFile = "/edit_new_lounge.php";
	ajax.element = "content_div";
	ajax.runAJAX();

	return false;
}

function get_new_lounges(by, direction, idx)
{
	if (idx == null)
		idx = 0;	

    if (saved_inactive == undefined) {
        saved_inactive = 0;
    }

	var ajax = new sack();
	ajax.method = "GET";
	ajax.setVar("by", by);
	ajax.setVar("direction", direction);
	ajax.setVar("idx", idx);
	ajax.setVar("tab", 1);
	ajax.setVar("inactive", saved_inactive);
	ajax.requestFile = "/browse_new_lounges.php";
	ajax.element = "new_lounges_div";
	ajax.runAJAX();
}

function toggle_inactive()
{
    if (saved_inactive == 0) {
        saved_inactive = 1; 
    } else {
        saved_inactive = 0; 
    }
}

function search_new_lounges(idx, search_id, search_type, active_filter)
{
	if (idx == null)
		idx = 0;	

	var ajax = new sack();
	ajax.method = "GET";
	ajax.setVar("tab", 1);
	ajax.setVar("search_id", search_id);
	if (search_type == 0) {
		ajax.setVar("search_type", "name");
	} else {
		ajax.setVar("search_type", "keyword");
	}
	if (active_filter == 1) {
		ajax.setVar("active_filter", "on");
	}
	ajax.setVar("idx", idx);
	ajax.requestFile = "/lounge_search.php";
	ajax.element = "new_lounges_div";
	ajax.runAJAX();

	//alert('debug sid='+search_id+'; type='+search_type+'; active='+active_filter);
}

function delete_new_lounge_shout(lounge_id, uid, timestamp)
{
    new Ajax.Request("/new_lounge.php", {
            method: 'get',
            parameters: { lid: lounge_id, del_uid: uid, del_time: timestamp },
            onSuccess: function(response) {
                update_new_lounge_shouts();
            }
    });
}

function process_round_selection(obj, members_inside, drinkid)
{
	var drinkname = $('name['+drinkid+']').value;
	var drinkcost = $('cost['+drinkid+']').value;

	$('drink_calc_name').value = drinkname;
	$('drink_calc_cost').value = drinkcost;
	$('drink_calc_total').value = drinkcost * members_inside;
}

function validate_round_form(obj)
{
	var drinkname = $('drink_calc_name').value;

	if (drinkname.length == 0)
	{	
		$('lounge_round_status').innerHTML = '<span style="color:red">You must select a drink, first!</span>';
		return false;
	}

	return true;
}

function bar_select(obj, timer, selected)
{
	clear_timer(timer);

	if (selected == 1) {
		obj.id="active";
	} else if (selected == 0) {
		obj.id="alter";
	}
}

function clear_timer(timer)
{
    if (typeof timer!="undefined") {
		clearTimeout(timer)
	}
}

function delayed_bar_select(obj, timer, selected)
{
	timer = setTimeout(function () { bar_select(obj, timer, selected); }, 25);
}

function close_interview_box()
{
	$('interview_box_div').style.display = 'none';
}

function ask_question()
{
	var ajax = new sack();
	var msg = $('question_textarea').value;

	ajax.method = "POST";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("q", msg);
	ajax.onCompletion = process_question_asked;
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function process_question_asked()
{
	if (this.response == '0') {
		$('question_textarea').value = '';
		$('interview_box_form_label').innerHTML = '<font color=red>Sorry, you already have a question waiting! Please wait.</font>';
	} else {
		$('question_textarea').value = '';
		$('interview_box_form_label').innerHTML = '<font color=green>YOUR QUESTION HAS BEEN SUBMITTED!</font>';
		refresh_question();
	}
}

function refresh_question()
{
	var ajax = new sack();

	ajax.method = "POST";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("iq", 1);
	ajax.onCompletion = process_iq_response;
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function next_question()
{
	var ajax = new sack();

	ajax.method = "POST";
	ajax.setVar("lid", saved_lounge_id);
	ajax.setVar("iq", 2);
	ajax.onCompletion = process_iq_response;
	ajax.requestFile = "/new_lounge.php";
	ajax.runAJAX();
}

function process_iq_response()
{
	if (this.response.length == 0) {
		$('interview_box_question_user_div').innerHTML = '';
		$('interview_box_question_div').innerHTML = 'The interview question list is currently empty.';
		return;
	}

	var myObject = eval('(' + this.response + ')');

	$('interview_box_question_user_div').innerHTML = 
			'<span style=\"color:gray\">question from:</span><br /><br />' +
			myObject['userbox'];

	$('interview_box_question_div').innerHTML = myObject['question_text'];
}

function disable_search_button()
{
	$('submit_button').disabled = true;
}

