if(!PB) var PB = {};
/*________________________________________
	
		COMPOSE FORM
*/

var Inbox = Class.create({
	initialize:function()
	{
		this.num_recipients = 2;
		$$('#inbox_messages .message .title').each(function(item){
			item.observe('click',this.open.bindAsEventListener(this));
		}.bind(this));
		
		$$('#inbox_messages .icon').each(function(item){
			item.observe('click',this.star.bindAsEventListener(this));
		}.bind(this));
		
		//$$('#inbox_messages .mark').each(function(item){
		//	item.observe('click',this.star.bindAsEventListener(this));
		//}.bind(this));
		if($$('.reply_btn').length > 0)
		{
			$$('.reply_btn').each(function(item){
				item.stopObserving('click');
				item.observe('click',this.reply.bindAsEventListener(this));
			}.bind(this));
		}
		$$('#inbox_messages .trash_btn').each(function(item){
			item.observe('click',this.sendToTrash.bindAsEventListener(this));
		}.bind(this));
		
		
	},
	star:function(element)
	{
		if(typeof(element.element) == 'function')
			element = element.element();
		
		if(element.hasClassName('starred'))
		{
			element.removeClassName('starred');
			element.addClassName('unstarred');
			$ajax('/user',{action:'inbox',what:'unstar',message_id:element.up('.message_wrap').readAttribute('id').gsub('message_','')},this.handleServerResponse.bindAsEventListener(this));
		}
		else
		{
			element.removeClassName('unstarred');
			element.addClassName('starred');
			$ajax('/user',{action:'inbox',what:'star',message_id:element.up('.message_wrap').readAttribute('id').gsub('message_','')},this.handleServerResponse.bindAsEventListener(this));
		}
	},
	unread:function(mess_id)
	{
		$(mess_id).removeClassName('read');
		$(mess_id).addClassName('unread');
		$ajax('/user',{action:'inbox',what:'unread',message_id:mess_id.gsub('message_','')},this.handleServerResponse.bind(this));
	},
	read:function(mess_id)
	{
		$(mess_id).removeClassName('unread');
		$(mess_id).addClassName('read');
		$ajax('/user',{action:'inbox',what:'read',message_id:mess_id.gsub('message_','')},this.handleServerResponse.bind(this));
	},
	handleServerResponse:function(response)
	{
		
	},
	removeRecipient:function(which)
	{
		if($('message[to]['+which+']'))
			$('message[to]['+which+']').remove();
		
		if($('to_handle_'+which))
			$('to_handle_'+which).remove();
	},
	addSelectedRecipients:function()
	{
		$$('.friend_check').each(function(item)
		{
			this.removeRecipient(this.num_recipients);
			if(item.checked)
			{
				uname = item.up('.friend_request').down('.title').innerHTML;
				$('message_recipients').insert('<input type="hidden" id="message[to]['+this.num_recipients+']" name="message[to]['+this.num_recipients+']" value="'+uname+'"/>');
				$('message_recipients').insert('<p id="to_handle_'+this.num_recipients+'">'+uname+'<a href="javascript:PB.Inbox.removeRecipient('+this.num_recipients+');">remove</a></p>');
			}
			this.num_recipients++;
		}.bind(this));
		
		PB.FriendSelecter.close();
	},
	open:function(evt)
	{
		$$('#inbox_messages > div > li > div').each(function(item)
		{
			item.removeClassName('opened');
			item.addClassName('closed');
		});
		
		if(evt.element().up('.message_wrap').hasClassName('unread'))
			this.read(evt.element().up('.message_wrap').readAttribute('id'));
		
		evt.element().up('.message_wrap').removeClassName('closed');
		evt.element().up('.message_wrap').addClassName('opened');
	},
	reply:function(element)
	{
		//window.thee = element;
		if(typeof(element.element) == 'function')
		{
			element = element.element().up('.message_wrap');
		}
		myLightWindow.deactivate();
		document.location = '/'+PB.sessData.user_handle+'/messages/reply_'+element.readAttribute('id').gsub('message_body_','')+'/#compose';
	},
	sendToTrash:function(element)
	{
		if(typeof(element.element) == 'function')
		{
			element = element.element().up('.message_wrap');
		}
		element = $(element);
		message_id = element.readAttribute('id').gsub('message_','');
		
		$ajax('/user',{action:'inbox',what:'trash',message_id:message_id},this.handleServerResponse.bind(this));
		element.up('li').remove();
		myLightWindow.deactivate();
	},
	deleteForever:function(element)
	{
		if(typeof(element.element) == 'function')
		{
			element = element.element().up('.message_wrap');
		}
		
		message_id = element.readAttribute('id').gsub('message_','');
		
		$ajax('/user',{action:'inbox',what:'delete',message_id:message_id},this.handleServerResponse.bind(this));
		element.up('li').remove();
	},
	sendToInbox:function(element)
	{
		if(typeof(element.element) == 'function')
		{
			element = element.element().up('.message_wrap');
		}
		
		message_id = element.readAttribute('id').gsub('message_','');
		
		$ajax('/user',{action:'inbox',what:'untrash',message_id:message_id},this.handleServerResponse.bind(this));
		element.up('li').remove();
	},
	reloadMessages:function(which)
	{
		$ajax('/user',{action:'inbox',what:'load',which:which},this.handleServerResponse.bind(this));
	},
	selectAll:function()
	{
			if(!$('select_all_messages').checked)
			{
				$$('.mark').each(function(item){
					item.checked = false;
				});
			}
			else
			{
				$$('.mark').each(function(item){
					item.checked = true;
				});
			}
	
	},
	doFriends:function(action,friend_id,mess_id)
	{
		switch(action)
		{
			case 'accept':
				PB.Action.acceptFriend(friend_id);
			break;
			case 'reject':
				PB.Action.rejectFriend(friend_id);
			break;
			case  'block':
				PB.action.blockFriend(friend_id);
			break;
		}
		
		$(mess_id).up('li').remove();
	},
	page:function()
	{
		switch($('inbox_page').value)
		{
			case 'inbox':
			//	$$('.message_wrap').each(function(mw){
			//		mw.up('li').show();});
				document.location = '/'+PB.sessData.user_handle+'/messages/inbox';
			break;
			case 'trash':
				document.location = '/'+PB.sessData.user_handle+'/messages/trash';
			break;
			case 'sent':
				document.location = '/'+PB.sessData.user_handle+'/messages/sent';
			break;
			case 'friend_requests':
			  document.location = '/'+PB.sessData.user_handle+'/messages/friend_requests';
			break;
			
			case 'new_mailers':
			  document.location = '/'+PB.sessData.user_handle+'/messages/new_mailers';
			break;
			case 'involvement_requests':
			  document.location = '/'+PB.sessData.user_handle+'/messages/involvement_requests';
			break;
			case 'event_invites':
			  document.location = '/'+PB.sessData.user_handle+'/messages/event_invites';
			break;
			case 'new_testimonials':
			  document.location = '/'+PB.sessData.user_handle+'/messages/new_testimonials';
			break;
			case 'sponsorship_offers':
			  document.location = '/'+PB.sessData.user_handle+'/messages/sponsorship_offers';
			break;
			case 'org_registration_requests':
			  document.location = '/'+PB.sessData.user_handle+'/messages/org_registration_requests';
			break;
			
			case 'all':
				document.location = '/'+PB.sessData.user_handle+'/messages/all';
			break;
		}
	},
	filter:function()
	{
		$('no_messages').hide();
		count = 0;
		switch($('inbox_filter').value)
		{
			case 'favorites':
			$$('.message_wrap').each(function(mw){
				if(mw.down('.starred'))
				{
					mw.up('li').show();
					count++;
				}
				else
					mw.up('li').hide();});
			break;
			case 'read':
				$$('.message_wrap').each(function(mw){
				if(mw.hasClassName('read'))
				{
					mw.up('li').show();
					count++;
				}
				else
					mw.up('li').hide();});
			break;
			case 'unread':
				$$('.message_wrap').each(function(mw){
				if(mw.hasClassName('unread'))
				{
					mw.up('li').show();
					count++;
				}
				else
					mw.up('li').hide();});
			break;
			case 'all':
				$$('.message_wrap').each(function(mw){
					mw.up('li').show();
					count++;
				});
			break;
		}
		if(count <= 0)
			$('no_messages').show();
			
	},
	action:function()
	{
		switch($('inbox_actions').value)
		{
			case 'mark_read':
				$$('#inbox_messages input[type=checkbox]').each(function(item)
				{
					if(item.checked)
					{
						this.read(item.up('.message_wrap').readAttribute('id'));
					}
				}.bind(this));
			break;
			case 'mark_unread':
				$$('#inbox_messages input[type=checkbox]').each(function(item)
				{
					if(item.checked)
					{
						this.unread(item.up('.message_wrap').readAttribute('id'));
					}
				}.bind(this));
			break;
			case 'mark_favorite':
				$$('#inbox_messages input[type=checkbox]').each(function(item)
				{
					if(item.checked)
					{
						this.star(item.up('div').down('.icon'));
					}
				}.bind(this));
			break;
			case 'trash':
				$$('#inbox_messages input[type=checkbox]').each(function(item)
				{
					if(item.checked)
					{
						this.sendToTrash(item.up('.message_wrap'));
					}
				}.bind(this));
			break;
			case 'untrash':
				$$('#inbox_messages input[type=checkbox]').each(function(item)
				{
					if(item.checked)
					{
						this.sendToInbox(item.up('.message_wrap'));
					}
				}.bind(this));
			break;
			case 'delete':
				$$('#inbox_messages input[type=checkbox]').each(function(item)
				{
					if(item.checked)
					{
						this.deleteForever(item.up('.message_wrap'));
					}
				}.bind(this));
			break;
		}
	}
});

function initInbox()
{
	PB.Inbox = new Inbox();
}
var InboxComposeForm = Class.create(PB.Form,{
	initialize:function($super,element,options)
	{
		$super(element,options);
	},
	onSuccess:function(response)
	{
		if(response.info)
		{
			window.growler.info(response.info,{header:'Success.'});
			
		}
		if(response.location)
			document.location = response.location;
	}
});

function initInboxComposeForm()
{
	if($('inbox_compose_form'))
	PB.Forms.inbox_compose_form = new InboxComposeForm('inbox_compose_form');
}

PB.addToDomLoad('initInboxComposeForm','');
PB.addToDomLoad('initInbox','');


