/************************************************/
/* (T31) Moods Javascript						*/
/* -------------------------------------------- */
/* tb_moods.js - Moods popup changer code		*/
/* (c) Invision Byte 2010						*/
/* -------------------------------------------- */
/* Author: Terabyte								*/
/************************************************/

var _moods = window.IPBoard;

_moods.prototype.moods = {
	/* Init vars */
	menu:		null,
	popup:		null,
	chosenMood:	0,
	
	/* Constructor */
	init: function()
	{
		Debug.write("Initializing tb_moods.js");
		
		document.observe("dom:loaded", function(){
			//Create a new menu! =O
			ipb.moods.menu = new ipb.Menu( $('user_moods'), $('user_moods_menucontent') );
			
			if( $('user_moods') && $('mood_change') && $('user_moods').hasClassName('tb_moods') )
			{
				$('mood_change').observe('click', ipb.moods.initChangePopup );
				
				/* Update also ALL our moods on the page */
				$$('img.__my_mood').each(
					function(mood)
					{
						mood.addClassName('clickable').observe('click', ipb.moods.initChangePopup );
					}
				);
			}
		} );
	},
	
	/**
	 * Init the change mood popup
	 */
	initChangePopup: function()
	{
		/* Popup already exist? Skip the loading.. */
		if ( ipb.moods.popup == null )
		{
			ipb.moods.loadPopup();
			
			return true;
		}
		
		/* Show and setup it */
		ipb.moods.popup.show();
		
		return true;
	},
	
	/**
	 * Runs the ajax to load the moods popup
	 */
	loadPopup: function()
	{
		new Ajax.Request( ipb.vars['base_url'] + '&app=moods&module=ajax&section=change&do=getPopup&secure_key=' + ipb.vars['secure_hash'],
			{
				method: 'get',
				encoding: ipb.vars['charset'],
				evalJSON: 'force',
				onSuccess: function(s)
				{
					/* Check for Errors */
					if( s.responseJSON['error'] )
					{
						alert(s.responseJSON['error']);
						return false;
					}
					else
					{
						ipb.moods.popup	= new ipb.Popup( 'tbMoods',
										{
											type: 'pane',
											modal: true,
											w: '650px',
											h: 'auto',
											initial: s.responseJSON['text'],
											hideAtStart: false,
											close: '.cancel'
										}
									);
						
						/* Setup save button */
						$('moods_save').observe( 'click', ipb.moods.saveNewMood );
						$('moods_popup_cancel').observe( 'click', ipb.moods.closePopup.bindAsEventListener( this ) );
						
						return true;
					}
				}
			}
		);
	},
	
	/**
	 * Close the change mood popup
	 */
	closePopup: function(e)
	{
		Event.stop(e);
		
		ipb.moods.popup.hide();
	},
	
	/**
	 * Save the new mood
	 */
	getNewMood: function()
	{
		/* Reset mood to 0 */
		ipb.moods.chosenMood = 0;
		
		/* Loop all moods */
		$A( $('moods_gallery').down('td').childElements() ).each(
			function(div)
			{
				$A( div.childElements() ).each(
					function(entry)
					{
						if ( entry.down('input').checked )
						{
							ipb.moods.chosenMood = entry.down('input').value;
							throw $break;
						}
					}
				);
				
				if ( ipb.moods.chosenMood > 0)
				{
					throw $break;
				}
			}
		);
	},
	
	/**
	 * Save the new mood
	 */
	saveNewMood: function()
	{
		/* Get the ID */
		ipb.moods.getNewMood();
		
		/* Store it! */
		new Ajax.Request( ipb.vars['base_url'] + '&app=moods&module=ajax&section=change&do=saveMood&secure_key=' + ipb.vars['secure_hash'],
			{
				method: 'post',
				parameters: {
					newMood : ipb.moods.chosenMood
				},
				onSuccess: function(s)
				{
					/* Check for Errors */
					if( s.responseJSON['error'] )
					{
						alert(s.responseJSON['error']);
						return false;
					}
					else
					{
						/* Close the popup... */
						ipb.moods.popup.hide();
						
						/* ...update the current moods... */
						$$('img.__my_mood').each(
							function(img)
							{
								img.setAttribute('alt'  , s.responseJSON['newName']);
								img.setAttribute('title', s.responseJSON['newName']);
								img.setAttribute('src'  , s.responseJSON['newImage']);
							}
						);
						
						/* ...and open our menu to show it */
						setTimeout("ipb.moods.menu.doOpen();", 500 );
						
						return true;
					}
				}
			}
			
		);
		
		return true;
	}
};

ipb.moods.init();