var cm_active_folder = 'new';

function showCastingMessage(msg_id) {
	new Ajax.Request("/castingmanager/show_message/"+msg_id, {method: "GET", onSuccess: doShowCastingMessage});
}

var doShowCastingMessage = function (t) {
	hidePops();
	fillPopup(t.responseText);
	showCenteredPopup(true);

	reloadCounters();
}

function reloadCastingFolder(folder) {

	cm_active_folder = folder;

	if (folder == 'new') {
		$("cm_new").className = 'selected';
		//if ($("cm_old")) $("cm_old").className = '';
		if ($("cm_sent")) $("cm_sent").className = '';
	}else if (folder == 'old') {
		$("cm_new").className = '';
		//$("cm_old").className = 'selected';
		$("cm_sent").className = '';
	}else if (folder == 'inbox') {
		$("cm_new").className = 'selected';
		//$("cm_old").className = 'selected';
		$("cm_sent").className = '';
	}else {
		$("cm_new").className = '';
		//$("cm_old").className = '';
		$("cm_sent").className = 'selected';
	}

	reloadCounters();

	showPreloader();

	new Ajax.Request("/castingmanager/get_casting_messages/"+folder, {onSuccess: doReloadCastingFolder});
}

var doReloadCastingFolder = function (t) {
	hidePops();

	$('casting_messages_list').innerHTML = t.responseText;
}

function deleteCastingMessage(msg_id) {
	new Ajax.Request('/castingmanager/delete_message/'+msg_id, {onSuccess: doDeleteCastingMessage});
}

var doDeleteCastingMessage = function (t) {
	hideDimmer(true);

	//Reload folder
	reloadCastingFolder(cm_active_folder);
}

function composeCastingMsg(to_id, to_name, subject, casting_state, casting_city) {
	if (typeof to_id == 'undefined') 			to_id = '';
	if (typeof to_name == 'undefined') 			to_name = '';
	if (typeof subject == 'undefined') 			subject = '';
	if (typeof casting_state == 'undefined') 	casting_state = '';
	if (typeof casting_city == 'undefined') 	casting_city = '';

	hidePops();

	fillPopup('<div style="width: 555px">'+
	'<div class="green" style="margin: 0 0 10px 0; border-bottom: 1px solid #303030; font-size: 12px; padding-bottom: 4px">'+languages['send_casting_msg']+'</div>'+
	'<div style="margin: 0 0 10px 0; color: #b0b0b0">As a "client", you can use this form to send the Casting Manager your ad/casting that you want them to post for you. We recommend that you do that AFTER you`ve already spoken with that person already and have established your business relationship.</div>'+
	'<table>'+
		'<tr><td>'+languages['recipient']+':</td>'+
		'<td>ModelRun ID:</td>'+
		'<td>'+languages['state']+':</td>'+
		'<td>'+languages['city']+':</td></tr>'+
		'<tr><td><input type="text" id="cmmsg_to_user_id" maxlength="255" class="x-form-text" style="width: 165px" value="'+to_name+'" /></td>'+
		'<td><input type="text" id="to_user_id" maxlength="11" class="green x-form-text" style="width: 75px" value="'+to_id+'" /></td>'+
		//'<td><input type="text" id="casting_state" maxlength="11" style="width: 134px" value="'+casting_state+'" /></td>'+
		'<td><select id="casting_state" class="x-form-select">'+casting_state+'</select></td>'+
		'<td><input type="text" id="casting_city" maxlength="11" class="x-form-text" style="width: 134px" value="'+casting_city+'" /></td></tr>'+
		'<tr><td colspan="4">'+languages['subject']+':</td></tr>'+
		'<tr><td colspan="4"><input type="text" id="cmmsg_subject" maxlength="255" class="x-form-text" style="width: 550px" value="'+subject+'" /></td></tr>'+
		'<tr><td colspan="4">'+languages['body']+':</td></tr>'+
		'<tr><td colspan="4"><textarea id="cmmsg_body" class="x-form-text" style="width:550px; height:300px; overflow: auto"></textarea></td></tr>'+
		'<tr><td colspan="4" align="right"><a href="javascript:;" onclick="sendCastingMsg()" class="send s40x20" alt="Send" >&nbsp;</a>&nbsp;&nbsp;<a href="javascript:;" onclick="hideDimmer(true)" class="close s40x20" title="Close">&nbsp;</a></td></tr>'+
	'</table></div>');
	showCenteredPopup(true);

	new bsn.AutoSuggest('cmmsg_to_user_id', {
		script:"/users/returnUserListXML/11/1?",
		varname:"input",
		json:true,
		callback: function (obj) { document.getElementById('to_user_id').value = obj.id; }
	});
}

function sendCastingMsg() {
	var to_user_id = $('to_user_id').value;
	var subject = $('cmmsg_subject').value;
	var body = $('cmmsg_body').value;
	var casting_state = $('casting_state').value;
	var casting_city = $('casting_city').value;

	if (to_user_id == '' || to_user_id == 0) {
		alert(languages['to_user_required']);
		return false;
	}

	if (subject == '') {
		alert(languages['subject_required']);
		return false;
	}

	var request = "to_user_id="+encodeURIComponent(to_user_id)+"&subject="+encodeURIComponent(subject)+"&body="+encodeURIComponent(body)+"&casting_state="+encodeURIComponent(casting_state)+"&casting_city="+encodeURIComponent(casting_city);

	new Ajax.Request("/castingmanager/send_message", {method: "POST", postBody: request, onSuccess: doSendCastingMsg});
}

var doSendCastingMsg = function (t) {
	hideDimmer(true);
	fillPopup(t.responseText);
	showCenteredPopup();
	setTimeout('hidePops()', popupTimeout);
}

function reloadCounters() {
	new Ajax.Request('/castingmanager/get_counters', {onSuccess: doReloadCounters});
}

var doReloadCounters = function (t) {
	var data = t.responseText.split(":");

	if ($('counter_cm_new')) $('counter_cm_new').innerHTML  = data[0];
	//if ($('counter_cm_old'))  $('counter_cm_old').innerHTML  = data[1];
	if ($('counter_cm_sent')) $('counter_cm_sent').innerHTML = data[3];
}