var temp1 = '';

var bebees = {

	throbber: false,
	
	autocomplete_init: function( ac_name, options )
	{
			var json = new AutoComplete( ac_name, options );
			
			return true;
	},
	
	sid: function( url )
	{
		if( typeof( SID ) != 'undefined' ) {
						
			if( SID.length ) {
			
				if( SID.indexOf( '?' ) == -1 ) {
					
					url = ( url + '?' + SID );
				}
				else {
					
					url = ( url + '&' + SID );
				}
			}	
		}
		
		return url;
	},
	
	to_top: function() {
	
		window.setTimeout( function() {
		
			new Effect.ScrollTo(
				'top',
				{
					duration: 0.1
				}
			);
			
		}, 250 );
	},
	
	throbber_show: function()
	{
		if( bebees.throbber ) {
			//return false;
		}
		
		if( $('hive') && !$('hive').hasClassName( 'throbbing' ) ) {
			$('throbber').addClassName( 'throbbing' );
		}
		
		if( $('cells_swap_throbber') && !$('cells_swap_throbber').hasClassName( 'throbbing' ) ) {
			$('cells_swap_throbber').addClassName( 'throbbing' );
		}
		
		if( $('gelee_royal_throbber') && !$('gelee_royal_throbber').hasClassName( 'throbbing' ) ) {
			$('gelee_royal_throbber').addClassName( 'throbbing' );
		}
		
		if( $('short_time_throbber') && !$('short_time_throbber').hasClassName( 'throbbing' ) ) {
			$('short_time_throbber').addClassName( 'throbbing' );
		}
		
		//$('throbber').show();
		
		bebees.throbber = true;
		
		return true;
	},
	
	throbber_hide: function()
	{
		if( !bebees.throbber ) {
			//return false;
		}
		
		//$('throbber').hide();
		
		if( $('hive') && $('hive').hasClassName( 'overlay' ) ) {
			$('hive').removeClassName( 'overlay' );
		}
		
		if( $('throbber') && $('throbber').hasClassName( 'throbbing' ) ) {
			$('throbber').removeClassName( 'throbbing' );
		}
		
		if( $('cells_swap_throbber') && $('cells_swap_throbber').hasClassName( 'throbbing' ) ) {
			$('cells_swap_throbber').removeClassName( 'throbbing' );
		}
		
		if( $('gelee_royal_throbber') && $('gelee_royal_throbber').hasClassName( 'throbbing' ) ) {
			$('gelee_royal_throbber').removeClassName( 'throbbing' );
		}
		
		if( $('short_time_throbber') && $('short_time_throbber').hasClassName( 'throbbing' ) ) {
			$('short_time_throbber').removeClassName( 'throbbing' );
		}
		
		bebees.throbber = false;
		
		return true;
	},
	
	is_not_number: function( e ) 
	{
		
		if( $( 'gelee_royal_input' ) && !$( 'gelee_royal_input' ).disabled ) {
			if( $( 'gelee_royal_title' ) && $( 'gelee_royal_title_hidden' ) ) {
				$( 'gelee_royal_title' ).update( $( 'gelee_royal_title_hidden' ).innerHTML );
			}
		}
		if( $( 'short_time_input' ) && !$( 'short_time_input' ).disabled ) {
			if( $( 'short_time_title' ) && $( 'short_time_title_hidden' ) ) {
				$( 'short_time_title' ).update( $( 'short_time_title_hidden' ).innerHTML );
			}
		}
		
		var keycode;
		
		if ( window.event ) {
			keycode = window.event.keyCode;
		}
		else if (e) {
			keycode = e.which;
		}
		
		if( keycode ) {
			if( 1000 > keycode ) { 
				if( 8 != keycode ) { 
					if( 46 != keycode ) {
						if( 37 != keycode ) {
							if( 39 != keycode ) {
								if( 48 > keycode || 57 < keycode ) {
									return true;
								}
							}
						}
					}
				}
			}
		}
		
		return false;
	},
	
	url_encode: function( str ) 
	{
		// Check html class too!
		
		str = str.replace( /\//, '<s>' );
		str = str.replace( /\\/, '<b>' );
		
		return encodeURIComponent( str );
	},
	
	pager: function( e,url ) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Enter = 13;
		
		if( kC == Enter ) location.href = bebees.sid( url );
	},
	
	select_fields_show: function()
	{
		if( typeof( browser ) != 'undefined' && browser.is_ie() && browser.get_version() == 6 ) {
			$A( document.getElementsByTagName( 'select' ) ).each(
				function(s) {
	      			s.style.display = 'block';
	      		}
			);
		}
	},
	
	select_fields_hide: function()
	{
		if( typeof( browser ) != 'undefined' && browser.is_ie() && browser.get_version() == 6 ) {
			$A( document.getElementsByTagName( 'select' ) ).each(
				function(s) {
	      			s.style.display = 'none';
	      		}
			);
		}
	},
	
	screenshot_show: function( image )
	{
		if( $('message') ) {
		
			if( $('disable') ) {
				$('disable').show();
			}
		
			$('screenshot').show();
			
			if( $('screenshot_image') ) {
				$('screenshot_image').update( '<img src="' + image + '" />' );
			}
			
			if( $('screenshot_button') ) {
				$('screenshot_button').focus();
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			Event.observe( document, 'keypress', bebees.screenshot_bind.bindAsEventListener( bebees ) );
		}
	},
	
	screenshot_close: function()
	{
		if( $('message') ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			$('screenshot').hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			
			Event.stopObserving( document, 'keypress' );
		}
	},
	
	screenshot_bind: function(e) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( kC == Esc || kC == Enter ) bebees.screenshot_close();
	},
	
	message_show: function( header, text )
	{
		if( $('message') ) {
		
			bebees.select_fields_hide();
		
			if( $('disable') ) {
				$('disable').show();
			}
			
			$('message').show();
			
			bebees.to_top();
		
			if( $('message_header') ) {
				$('message_header').update( header );
			}
			
			if( $('message_text') ) {
				$('message_text').update( text );
			}
			
			if( $('message_button') ) {
				$('message_button').focus();
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			Event.observe( document, 'keypress', bebees.message_bind.bindAsEventListener( bebees ) );
		}
	},
	
	message_close: function()
	{
		if( $('message') ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			$('message').hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			
			bebees.select_fields_show();
		
			Event.stopObserving( document, 'keypress' );
		}
	},
	
	message_bind: function(e) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( kC == Esc || kC == Enter ) bebees.message_close();
	},
	
	confirm_show: function( header, text, action )
	{
		if( $('confirm') ) {
		
			bebees.select_fields_hide();
		
			if( $('disable') ) {
				$('disable').show();
			}
			
			if( typeof( action ) == 'function' ) {

				if( $('confirm_button') ) {
					
					$('confirm_button').stopObserving( 'click' );
				
					$('confirm_button').observe( 'click', function(e) { bebees.confirm_close(); action(); $('confirm_button').stopObserving( 'click' ); } );
				}
			}
			
			$('confirm').show();
			
			bebees.to_top();
		
			if( $('confirm_header') ) {
				$('confirm_header').update( header );
			}
			
			if( $('confirm_text') ) {
				$('confirm_text').update( text );
			}
			
			if( $('confirm_button') ) {
				$('confirm_button').focus();
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			Event.stopObserving( document, 'keypress' );
			
			Event.observe( document, 'keypress', bebees.confirm_bind.bindAsEventListener( bebees, action ) );
		}
	},
	
	confirm_close: function()
	{
		if( $('confirm') ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			$('confirm').hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			
			bebees.select_fields_show();
		
			Event.stopObserving( document, 'keypress' );
		}
	},
	
	confirm_bind: function( e, action ) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( kC == Enter ) {
			if( typeof( action ) == 'function' ) {
				action();
			}
		}
		if( kC == Esc || kC == Enter ) {
			bebees.confirm_close();
		}
	},
	
	gelee_royal_show: function( action )
	{
		if( $('gelee_royal') ) {
		
			bebees.select_fields_hide();
		
			if( $('disable') ) {
				$('disable').show();
			}
			
			if( typeof( action ) == 'function' ) {
				if( $('gelee_royal_button') ) {
					$('gelee_royal_button').stopObserving( 'click' );
					
					$('gelee_royal_button').observe( 'click', function(e) { action() } );
				}
			}
			
			if( $( 'gelee_royal_title' ) && $( 'gelee_royal_title_hidden' ) ) {
				$( 'gelee_royal_title' ).update( $( 'gelee_royal_title_hidden' ).innerHTML );
			}
			
			if( $( 'gelee_royal_content' ) && $( 'header_gelee_royal' ) ) {
				$( 'gelee_royal_content' ).update( $( 'header_gelee_royal' ).innerHTML );
			}	
		
			$('gelee_royal').show();
			
			bebees.to_top();
		
			if( $('gelee_royal_input') ) {
			
				$('gelee_royal_input').focus();	
				
				Event.observe( $('gelee_royal_input'), 'keydown', bebees.gelee_royal_bind.bindAsEventListener( bebees, function() { action(); Event.stopObserving( $('gelee_royal_input'), 'keydown' ); }, true ) );
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			Event.observe( document, 'keypress', bebees.gelee_royal_bind.bindAsEventListener( bebees, action ) );
		}
	},
	
	gelee_royal_close: function()
	{
		if( $('gelee_royal') ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			$('gelee_royal').hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			
			bebees.select_fields_show();
		
			Event.stopObserving( document, 'keypress' );
		}
	},
	
	gelee_royal_bind: function( e, action, esc_only ) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( typeof( esc_only ) == 'undefined' && kC == Enter ) {
			if( typeof( action ) == 'function' ) {
				action();
			}
		}
		if( kC == Esc ) {
			bebees.gelee_royal_close();
		}
	},	
	
	gelee_royal_change: function( type, cell_location )
	{
		if( $( 'gelee_royal_input' ) ) {
		
			if( $( 'gelee_royal_button' ) ) {
				$( 'gelee_royal_button' ).disabled = true;
			}
			
			bebees.throbber_show();
			
			var params = new Hash();

			params.set( 'gelee_royal', $( 'gelee_royal_input' ).value );
			
			new Ajax.Request( 
				
				'/ajax/gelee_royal_change/?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'post',
					parameters: params, 
					onSuccess: function( transport ) {
						if( transport.responseText.match( /homepage_screen_login/ ) ) {
							top.location = '/';
						}
						else if( transport.responseText.match( /[a-zA-Z]/ ) ) {
							
							if( $( 'gelee_royal_message' ) ) {
								$( 'gelee_royal_message' ).update( transport.responseText );
							}
							if( typeof( type ) != 'undefined' ) {
								
								if( $( 'action' ) && typeof( cell_location ) != 'undefined' ) {
									window.setTimeout( function() { bebees.cell_content_update( type, cell_location ); }, 1000 );
								}
								else if( $( 'specialist_action' ) ) {
									window.setTimeout( function() { bebees.bee_content_update( type ); }, 1000 );
								}
							}	
							else if( location.href.match( /hero/ ) ) {
							
								// Todo: Update train buttons
							}
							else if( location.href.match( /cell\/[0-9]/ ) ) {
								
								if( $( 'action' ) ) {
		
									var url = '/cell/main_content/';
									
									new Ajax.Updater( 
									
										'action', 
										url + '?' + SID, 
										
										{ 
											evalScripts: true, 
											method: 'get'
										} 
									);
								}
							}					
						}
					}
				} 
			);
		}
	},
	
	gelee_royal_calculate: function( rate ) 
	{
		if( typeof( rate ) != 'undefined' ) {
			
			if( $( 'gelee_royal_input' ) ) {
				
				$( 'gelee_royal_input' ).value = $( 'gelee_royal_input' ).value.replace( /[^0-9]/, '' ); 
				if( $( 'gelee_royal_input' ).value.length > 1 ) $( 'gelee_royal_input' ).value = $( 'gelee_royal_input' ).value.replace( /^0/, '' ); 
			
				if( $( 'gelee_royal_output' ) ) {
					if( parseInt( $( 'gelee_royal_input' ).value ) != $( 'gelee_royal_input' ).value ) {
						$( 'gelee_royal_output' ).value = '0';
					}
					else {
						$( 'gelee_royal_output' ).value = parseInt( $( 'gelee_royal_input' ).value ) * rate;
					}
				}
			}
		}
		return true;
	},
	
	short_time_rate: 0,
	
	short_time_counter: 0,
	
	short_time_timer_check: 0,
	
	short_time_show: function( counter, id, action, rate )
	{
		bebees.short_time_rate = rate;
		
		bebees.short_time_counter = counter;
		
		if( $('short_time') ) {
		
			bebees.select_fields_hide();
		
			if( $('disable') ) {
				$('disable').show();
			}
			
			if( typeof( action ) == 'function' ) {
				
				if( $('short_time_button') ) {
					
					$('short_time_button').stopObserving( 'click' );
					
					$('short_time_button').observe( 'click', function(e) { action() } );
				}
			}
			else {				
				return;
			}
			
			if( $( 'short_time_title' ) && $( 'short_time_title_hidden' ) ) {
				$( 'short_time_title' ).update( $( 'short_time_title_hidden' ).innerHTML );
			}
			
			if( $( 'short_time_content' ) && $( 'header_gelee_royal' ) ) {
				$( 'short_time_content' ).update( $( 'header_gelee_royal' ).innerHTML );
			}
			
			if( $( 'short_time_form' ) ) {
				$( 'short_time_form' ).show();
			}
			if( $( 'short_time_finished' ) ) {
				$( 'short_time_finished' ).hide();
			}	
			
			if( $( 'short_time_input' ) ) {
				$( 'short_time_input' ).disabled = false;
			}
			if( $( 'short_time_button' ) ) {
				$( 'short_time_button' ).disabled = false;
			}
			
			$('short_time').show();
			
			bebees.to_top();
		
			if( $('short_time_input') ) {
				
				$('short_time_input').focus();				
				
				Event.observe( $('short_time_input'), 'keydown', bebees.short_time_bind.bindAsEventListener( bebees, function() { action(); Event.stopObserving( $('short_time_input'), 'keydown' ); }, true ) );
			}
			if( $( 'short_time_rate' ) ) {
				$( 'short_time_rate' ).update( bebees.short_time_calculate_timer( rate ) );
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			Event.observe( document, 'keypress', bebees.short_time_bind.bindAsEventListener( bebees, action ) );
			
			bebees.short_time_check( id );
			
			if( $( 'gelee_royal_timer' ) && $( 'statusbox_timer_left_' + counter ) ) {
			
				$( 'gelee_royal_timer' ).update( $( 'statusbox_timer_left_' + counter ).innerHTML );
			
				if( counter < 1 ) {
				
					bebees.short_time_timer( bebees.short_time_timer_check, function() { bebees.short_time_close(); } );
				}
			}
		}
	},
	
	short_time_close: function()
	{
		if( $('short_time') ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			bebees.short_time_timer_check++;
			
			$('short_time').hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			
			bebees.select_fields_show();
		
			Event.stopObserving( document, 'keypress' );
		}
	},
	
	short_time_bind: function( e, action, esc_only ) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( typeof( esc_only ) == 'undefined' && kC == Enter ) {
			if( typeof( action ) == 'function' ) {
				if( !$( 'short_time_input' ) || !$( 'short_time_input' ).disabled ) {
					action();
				}
			}
		}
		if( kC == Esc ) {
			bebees.short_time_close();
		}
	},
	
	short_time_check: function( id )
	{
		if( $( id ) ) {
		
			window.setTimeout( 				
				function() { 				
					bebees.short_time_check( id );
				},
				500 
			);
		}
		else {
			
			if( $( 'short_time_input' ) ) {
				$( 'short_time_input' ).disabled = true;
			}
			if( $( 'short_time_button' ) ) {
				$( 'short_time_button' ).disabled = true;
			}
			if( $( 'short_time_form' ) ) {
				$( 'short_time_form' ).hide();
			}
			if( $( 'short_time_finished' ) && $( 'change_time_temp' ) ) {
				$( 'short_time_finished' ).update( $( 'change_time_temp' ).innerHTML );
				$( 'short_time_finished' ).show();
			}
		}
	},
	
	short_time_change: function( type, id, flush, bee_type_cell_location, cell_type  )
	{
		if( $( 'short_time_input' ) ) {
		
			if( $( 'short_time_button' ) ) {
				$( 'short_time_button' ).disabled = true;
			}
			
			bebees.throbber_show();
			
			var params = new Hash();

			params.set( 'gelee_royal', $( 'short_time_input' ).value );
			params.set( 'type', type )
			params.set( 'id', id );
			params.set( 'param', bee_type_cell_location );
			
			new Ajax.Request( 
				
				'/ajax/short_time/?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'post',
					parameters: params, 
					onSuccess: function( transport ) {
						if( transport.responseText.match( /homepage_screen_login/ ) ) {
							top.location = '/';
						}
						else if( transport.responseText.match( /[a-zA-Z]/ ) ) {
						
							if( $( 'short_time_output' ) ) {
								$( 'short_time_output' ).value = '- 0:00:00 h';
							}
						
							if( flush == 'hive' ) {								
								
								bebees.hive_statusbox_update();
								
								window.setTimeout( function() { bebees.hive_content_update(); }, 1000 );
							}
							else if( flush == 'bee' || cell_type == 'queen' ) {	
								
								bebees.specialist_statusbox_update();
								
								if( cell_type == 'queen' ) {
									
									window.setTimeout( function() { bebees.cell_content_update( cell_type, bee_type_cell_location ); }, 1000 ); 
								}
								else {
								
									window.setTimeout( function() { bebees.bee_content_update( bee_type_cell_location ); }, 1000 );
								}
							}
							else if( flush == 'cell' ) {
							
								bebees.cell_statusbox_update( bee_type_cell_location ); 
								
								window.setTimeout( function() { bebees.cell_content_update( cell_type, bee_type_cell_location ); }, 1000 );
							}
							if( $( 'short_time_message' ) ) {
								$( 'short_time_message' ).update( transport.responseText );
							}
							
							if( $( 'gelee_royal_timer' ) && $( 'statusbox_timer_left_' + bebees.short_time_counter ) ) {
							
								window.setTimeout( function() {	
								
									if( $( 'statusbox_timer_left_' + bebees.short_time_counter ) ) {			
										$( 'gelee_royal_timer' ).update( $( 'statusbox_timer_left_' + bebees.short_time_counter ).innerHTML );
									}
								
									if( bebees.short_time_counter < 1 ) {
										
										bebees.short_time_timer_check++;
				
										bebees.short_time_timer( bebees.short_time_timer_check, function() { bebees.short_time_close(); } );
									}
									
								}, 1100 );
							}													
						}
					}
				} 
			);
		}
	},
	
	short_time_calculate: function() 
	{
		if( $( 'short_time_input' ) ) {
		
			$( 'short_time_input' ).value = $( 'short_time_input' ).value.replace( /[^0-9]/, '' ); 
			if( $( 'short_time_input' ).value.length > 1 ) $( 'short_time_input' ).value = $( 'short_time_input' ).value.replace( /^0/, '' ); 
		
			if( $( 'short_time_output' ) ) {
				if( !bebees.short_time_rate || parseInt( $( 'short_time_input' ).value ) != $( 'short_time_input' ).value ) {
					$( 'short_time_output' ).value = '- 0:00:00 h';
				}
				else {
					$( 'short_time_output' ).value = "-" + bebees.short_time_calculate_timer( parseInt( parseInt( $( 'short_time_input' ).value ) * bebees.short_time_rate ) ) + " h";
				}
			}
		}
		return true;
	},
	
	short_time_calculate_timer: function( s ) 
	{
		if( s > -1 )
		{
			hours = Math.floor(s/3600);
			//if (hours > 23) {hours = hours - 24};
			minutes = Math.floor(s/60) % 60;
			seconds = s % 60;
			t = hours + ":";
			if(minutes < 10){t += "0";}
			t += minutes  + ":";
			if(seconds < 10){t += "0";}
			t += seconds;
			
		}
		return t;
	},
	
	short_time_timer: function( check, on_complete ) 
	{	
		var sec;
		
		sec = bebees.statusbox_timer_format1( $( 'gelee_royal_timer' ) ) ;
				
		sec -= 1;

		if (sec < 0)
		{
			sec = 0;
			
			if( typeof( on_complete ) != 'undefined' ) {
				
				on_complete();
			}
		}
		else
		{
			sec = bebees.short_time_format2( sec ); 
			
			$( 'gelee_royal_timer' ).update( sec );
		}
		
		setTimeout( function() { if( bebees.short_time_timer_check == check ) bebees.short_time_timer( check, on_complete ); }, 1000 );
	},
	
	short_time_format1: function( myElement ) 
	{	
		p = myElement.innerHTML.split(":");
		
		hours = p[0];
		minutes =  p[1];
		seconds = p[2];
		
		sec = hours * 3600 + minutes * 60 + seconds * 1;
		
		return sec;
	},
		
	short_time_format2: function( s ) 
	{		
		if( s > -1 )
		{
			hours = Math.floor(s/3600);
			//if (hours > 23) {hours = hours - 24};
			minutes = Math.floor(s/60) % 60;
			seconds = s % 60;
			t = hours + ":";
			if(minutes < 10){t += "0";}
			t += minutes + ":";
			if(seconds < 10){t += "0";}
			t += seconds;
		}
		return t;
	},
	
	help_id: 'help',
	
	help_show: function( id, button_id )
	{
		if( typeof( id ) != 'undefined' && $( id ) ) {
			 bebees.help_id = id;
		}
		
		if( typeof( button_id ) == 'undefined' || !$( button_id ) ) {
			 button_id = 'help_button';
		}
		if( $( bebees.help_id ) ) {
		
			bebees.select_fields_hide();
		
			if( $('disable') ) {
				$('disable').show();
			}
			
			if( typeof( action ) == 'function' ) {
				if( $('help_action') ) {
					$('help_action').observe( 'click', function(e) { bebees.help_close(); action() } );
				}
			}
		
			$( bebees.help_id ).show();
			
			bebees.to_top();
		
			if( $( button_id ) ) {
				$( button_id ).focus();
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			//if( !location.href.match( /register/ ) ) {
			
				Event.stopObserving( document, 'keypress' );
			
				Event.observe( document, 'keypress', bebees.help_bind.bindAsEventListener( bebees ) );
			//}
		}
	},
	
	help_close: function()
	{
		if( $( bebees.help_id ) ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			$( bebees.help_id ).hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			bebees.select_fields_show();
		}
	},
	
	help_bind: function(e) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( kC == Esc || kC == Enter ) bebees.help_close();
	},
	
	cell_swap_id: 0,
	
	cell_swap_progress: false,
	
	cells_swap_show: function( id )
	{
		if( typeof( id ) != 'undefined' && $('cells_swap') ) {
		
			bebees.cell_swap_id = id;
						
			if( $('cells_swap_text') && $('cells_swap_temp') ) {
				$('cells_swap_text').update( $('cells_swap_temp').innerHTML );
			}
			
			if( $('disable') ) {
				$('disable').show();
			}
			
			if( $('cells_swap_hive') ) {
				$('cells_swap_hive').update( '' );	
			}
			
			bebees.select_fields_hide();
		
			$('cells_swap').show();
			
			bebees.to_top();
		
			if( $('cells_swap_button') ) {
				$('cells_swap_button').focus();
			}
			
			if( $('overlay') ) {
				$('overlay').addClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').addClassName( 'overlay' );
			}
			
			Event.observe( document, 'keypress', bebees.cells_swap_bind.bindAsEventListener( bebees ) );
			
			bebees.throbber_show();
		
			new Ajax.Request( 
			
				'/cell/swap_content/' + id + '?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'get',
					onSuccess: function( transport ) {
						
						if( transport.responseText.match( /homepage_screen_login/ ) ) {
							top.location = '/';
						}
						else if( transport.responseText.match( /[a-z]/ ) ) {
						
							window.setTimeout( function() { bebees.throbber_hide(); }, 1000 );	
						
							if( $('cells_swap_hive') ) {
								$('cells_swap_hive').update( transport.responseText );	
							}						
						}
					}
				}
			);
		}
	},
	
	cells_swap_close: function()
	{
		if( $('cells_swap') && !bebees.cell_swap_progress ) {
		
			if( $('disable') ) {
				$('disable').hide();
			}
			
			$('cells_swap').hide();
			
			if( $('overlay') ) {
				$('overlay').removeClassName( 'overlay' );
			}
			if( $('homepage_screen') ) {
				$('homepage_screen').removeClassName( 'overlay' );
			}
			
			bebees.select_fields_show();
		
			Event.stopObserving( document, 'keypress' );
		}
	},
	
	cells_swap_bind: function(e) 
	{
		var kC  = (window.event) ? event.keyCode : e.keyCode;
		var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		var Enter = 13;
		
		if( kC == Esc || kC == Enter ) bebees.cells_swap_close();
	},
	
	cells_swap_submit: function( location )
	{
		if( $( 'cells_swap_form' ) ) {
		
			bebees.submit_form( $( 'cells_swap_form' ) );
		}
		if ( bebees.cell_swap_progress ) {
			
			return false;
		}
		
		bebees.throbber_show();
		
		var params = new Hash();

		params.set( 'cell_id', bebees.cell_swap_id );
		params.set( 'cell_location', location );
		
		bebees.cell_swap_progress = true;
		
		new Ajax.Request( 
			
			'/cell/swap/?' + SID, 
			
			{ 
				evalScripts: true, 
				method: 'post',
				parameters: params, 
				onSuccess: function( transport ) {
					if( transport.responseText.match( /homepage_screen_login/ ) ) {
						top.location = '/';
					}
					else if( transport.responseText.match( /[0-9]/ ) ) {
					
						if( transport.responseText == 1 ) {
							if( $('cells_swap_text') && $('cells_swap_success') ) {
								$('cells_swap_text').update( $('cells_swap_success').innerHTML );
							}	
							new Ajax.Request( 
			
								'/cell/swap_content/' + bebees.cell_swap_id + '?' + SID, 
								
								{ 
									evalScripts: true, 
									method: 'get',
									onSuccess: function( transport ) {
										
										if( transport.responseText.match( /homepage_screen_login/ ) ) {
											top.location = '/';
										}
										else if( transport.responseText.match( /[a-z]/ ) ) {
										
											window.setTimeout( function() { bebees.throbber_hide(); }, 1000 );	
										
											if( $('cells_swap_hive') ) {
												$('cells_swap_hive').update( transport.responseText );	
											}						
										}
									}
								}
							);
						}
						else {
							if( $('cells_swap_text') && $('cells_swap_error') ) {
								$('cells_swap_text').update( $('cells_swap_error').innerHTML );
							}							
						}							
							
						window.setTimeout( function() { 
							
							bebees.throbber_hide(); 
							
							if( $( 'cells_swap_form' ) ) {		
								bebees.unsubmit_form( $( 'cells_swap_form' ) );
							}
							
							bebees.cell_swap_progress = false;
							
						}, 1000 );
						
						window.setTimeout( function() { 
							
							if( $('cells_swap_text') && $('cells_swap_temp') ) {
								$('cells_swap_text').update( $('cells_swap_temp').innerHTML );
							}
							
						}, 5000 );						
					}
				}
			}
		);
	
	},

	header_update: function ( no_repeat )
	{
		if( !$( 'population_honey' ) ) {
			return;
		}
		
		if( agent && agent == 'admin' ) {
			return;
		}	
		
		var title;
		
		if( $( 'title' ) ) {
			title = $( 'title' ).innerHTML;
		}
		
		if( !no_repeat ) {
		
			bebees.throbber_show();
		}
				
		new Ajax.Request( 
			
			'/hive/header/?' + SID, 
			
			{ 
				method: 'get', 
				onSuccess: function( transport ) {
					if( transport.responseText.match( /homepage_screen_login/ ) ) {
						top.location = '/';
					}
					else if ( transport.responseText.isJSON() )
					{
						json = transport.responseText.evalJSON( true );
						
						$('population').innerHTML = json.population + ' / ' + json.population_max;
						$('free_workers').innerHTML = json.free_workers;
						$('honey_production').innerHTML = json.honey_production;
						$('honey').innerHTML = json.honey + ' / ' + json.honey_max;
						$('header_gelee_royal').innerHTML = json.gelee_royal;
						
						if ( $('menu_news') )
						{
							if ( json.new_mails == 'true' )
							{
								$('menu_news').addClassName( 'new' );
							}
							else
							{
								$('menu_news').removeClassName( 'new' );
							}
						}
						
						if( json.header_messages )
						{
							$('notice').innerHTML = '';
							
							for(var i =0; i < json.header_messages.length; i ++)
							{
								var new_p = new Element( 'p' );
								
								if ( json.header_messages[i].type == 'notice' )
								{
									new_p.addClassName('notice');
								}
								else
								{
									new_p.addClassName('message');
								}

								if ( json.header_messages[i].read == 'true' )
								{
									new_p.addClassName('message_read');
								}

								var new_a = new Element( 'a', { 'href' : json.header_messages[i].href, 'title' : json.header_messages[i].title} );
								
								new_a.innerHTML = json.header_messages[i].content;
								
								new_p.insert( new_a );
								
								$('notice').insert( new_p );
							}
						}
					}
				}
			}
		);
		
		if( !no_repeat ) {
		
			window.setTimeout( function() { bebees.throbber_hide(); }, 1000 );
		
			setTimeout( function() { bebees.header_update(); }, 60000 );
		}
	},
	
	statusbox_type: false,
	
	statusbox_location: 0,
	
	statusbox_update: function( url ) 
	{
		if( !$( 'statusbox' ) ) {
			return;
		}
				
		new Ajax.Request( 
			
			url + '?' + SID, 
			
			{ 
				evalScripts: true, 
				method: 'get', 
				onSuccess: function( transport ) {
					if( transport.responseText.match( /homepage_screen_login/ ) ) {
						top.location = '/';
					}
					else if( transport.responseText.match( /[a-zA-Z]/ ) ) {
						$( 'statusbox' ).update( transport.responseText );
					}
					else if( $( 'statusbox' ).innerHTML.match( /[a-zA-Z]/ ) ) {
						$( 'statusbox' ).fade();
						setTimeout( function() { $( 'statusbox' ).update( transport.responseText ); }, 1000 );
					}
				}
			} 
		);
	},

	statusbox_timer: function( on_complete ) 
	{	
		var sec;
		
		for( var i = 0;; i++ ) {
			
			if( $( "statusbox_timer_" + i ) )
			{
				sec = bebees.statusbox_timer_format1( $( "statusbox_timer_" + i ) ) ;
				
				sec -= 1;
		
				if (sec < 0)
				{
					sec = 0;
					
					if( typeof( on_complete ) != 'undefined' ) {
						
						on_complete();
					}
				}
				else
				{
					sec = bebees.statusbox_timer_format2( sec ); 
					
					$( "statusbox_timer_" + i ).update( sec );
				}
				
				if (i < 1)
				{
					if( $( "statusbox_timer_left_" + i ) )
					{
						sec = bebees.statusbox_timer_format1( $( "statusbox_timer_left_" + i ) ) ;
				
						sec -= 1;
				
						if (sec < 0)
						{
							sec = 0;
						}
						else
						{
							sec = bebees.statusbox_timer_format2( sec ); 
							
							$( "statusbox_timer_left_" + i ).update( sec );
						}
					}
				}
			}
			else {
				break;
			}
		}
		setTimeout( function() { bebees.statusbox_timer( on_complete ); }, 1000 );
	},
	
	statusbox_timer_format1: function( myElement ) 
	{	
		p = myElement.innerHTML.split(":");
		
		hours = p[0];
		minutes =  p[1];
		seconds = p[2];
		
		sec = hours * 3600 + minutes * 60 + seconds * 1;
		
		return sec;
	},
		
	statusbox_timer_format2: function( s ) 
	{		
		if( s > -1 )
		{
			hours = Math.floor(s/3600);
			//if (hours > 23) {hours = hours - 24};
			minutes = Math.floor(s/60) % 60;
			seconds = s % 60;
			t = hours + ":";
			if(minutes < 10){t += "0";}
			t += minutes + ":";
			if(seconds < 10){t += "0";}
			t += seconds;
		}
		return t;
	},
	
	infobox_show: function( info ) 
	{
		if( $( 'infobox' ) ) {
			$( 'infobox' ).update( info );
			$( 'infobox' ).show();
		}	
	},
	
	infobox_hide: function() 
	{
		if( $( 'infobox' ) ) {
			$( 'infobox' ).hide();
		}	
	},
	
	hive_orders: new Array(),

	hive_update: function()
	{
		bebees.hive_statusbox_update();
					
		window.setTimeout( function() { bebees.hive_content_update(); }, 1000 );		
		
		window.setTimeout( function() {	bebees.hive_update(); }, 60000 );
	},
	
	hive_statusbox_timer: function() 
	{
		bebees.statusbox_timer( 
			
			function() {
				
				if( bebees.throbber_show() ) {
				
					if( $('hive') ) {
						$('hive').addClassName( 'overlay' );
					}		
					
					window.setTimeout( 
							
						function() {	
							
							bebees.hive_statusbox_update();
								
							window.setTimeout( function() { bebees.hive_content_update(); }, 1000 );
						
							window.setTimeout( function() { bebees.throbber_hide(); }, 2000 );
						}
							
					, 2000 );
				}
			}
		);
	},
	
	hive_statusbox_update: function() 
	{
		bebees.statusbox_update( '/hive/statusbox/' );
	},
	
	hive_content_update: function() 
	{
		if( !$( 'hive' ) ) {
			return;
		}
		
		new Ajax.Updater( 
			
			'hive', 
			'/hive/content/?' + SID, 
			
			{ 
				evalScripts: true, 
				method: 'get'
			} 
		);
		
		bebees.header_update( true );
	},
	
	cancelation_in_progress: false,
	
	hive_cancel_order: function( cell_id ) 
	{
		if( bebees.cancelation_in_progress ) {
		
			return false;
		}
		bebees.cancelation_in_progress = true;
		
		bebees.throbber_show();
		
		if( $('hive') ) {
			$('hive').addClassName( 'overlay' );
		}		
					
		window.setTimeout( 
				
			function() {	
	
				new Ajax.Request( 
					
					'/hive/cancel_order/' + cell_id + '?' + SID, 
					
					{ 
						evalScripts: true, 
						method: 'get', 
						onSuccess: function() {
							
							bebees.hive_statusbox_update();
					
							window.setTimeout( function() { bebees.hive_content_update(); }, 1000 );
							
							window.setTimeout( function() { bebees.throbber_hide(); }, 2000 );
						}
					} 
				);
			}
		, 1000 );
	},
	
	hive_cancel_recruitment: function( recruitment_id, type ) 
	{
		if( bebees.cancelation_in_progress ) {
		
			return false;
		}
		bebees.cancelation_in_progress = true;
		
		bebees.throbber_show();
		
		window.setTimeout( 
				
			function() {	
	
				new Ajax.Request( 
					
					'/bee/cancel_recruitment/' + recruitment_id + '?' + SID, 
					
					{ 
						evalScripts: true, 
						method: 'get', 
						onSuccess: function() {
							
							bebees.specialist_statusbox_update();
							
							bebees.header_update( true );
					
							window.setTimeout( function() { bebees.throbber_hide(); }, 1000 );
						}
					} 
				);
			}
		, 1000 );
	},
	
	hive_move: function( map_id, x, y, move_type )
	{
		if( typeof( map_id ) == 'undefined' || !$( 'move_map_id' ) ) {
			
			return false;
		}
		
		$( 'move_map_id' ).value = map_id;
		
		$( 'move_map_x' ).value = x;
				
		$( 'move_map_y' ).value = y;
		
		$( 'move_type' ).value = move_type;
		
		$( 'form_move' ).submit();
	},
	
	cell_statusbox_update: function( location ) 
	{
		bebees.statusbox_update( '/cell/statusbox/' + location );
	},
	
	cell_cancel_order: function( location, type, cell_id, bee_type ) 
	{
		if( bebees.cancelation_in_progress ) {
		
			return false;
		}
		bebees.cancelation_in_progress = true;
		
		bebees.throbber_show();
		
		if( $('hive') ) {
			$('hive').addClassName( 'overlay' );
		}
		
		window.setTimeout( 
				
			function() {
			
				new Ajax.Request( 
					
					'/hive/cancel_order/' + cell_id + '?' + SID, 
					
					{ 
						evalScripts: true, 
						method: 'get', 
						onSuccess: function() {
							
							if( $( 'action' ) ) {
							
								if( type == 'queen' ) {
									bebees.specialist_statusbox_update();									
								}else {
									bebees.cell_statusbox_update( location );
								}
								
								window.setTimeout( function() { bebees.cell_content_update( type, location ); }, 1000 );
							}
							else if( $( 'specialist_action' ) && typeof( bee_type ) != 'undefined' ) {
							
								bebees.specialist_statusbox_update();	
							}
					
							window.setTimeout( function() { bebees.throbber_hide(); }, 2000 );
						}
					} 
				);
			}
		, 1000 );
	},
	
	cell_content_update: function( type, location ) 
	{
		if( $( 'action' ) ) {
		
			var url = '/cell/' + type + '_content/' + location;
			
			new Ajax.Updater( 
			
				'action', 
				url + '?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'get'
				} 
			);
		}
		
		bebees.header_update( true );
	},
	
	bee_content_update: function( type ) 
	{
		if( $( 'specialist_action' ) && $( 'specialist_action_' + type ) ) {
		
			var url = '/bee/' + type + '_content/';
						
			new Ajax.Updater( 
			
				'specialist_action', 
				url + '?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'get'
				} 
			);
		}
		
		bebees.header_update( true );
	},
	
	queen_statusbox_timer: function() 
	{
		bebees.statusbox_timer( 
			
			function() {
				
				if( bebees.throbber_show() ) {
					
					window.setTimeout( 
							
						function() {
						
							bebees.queen_statusbox_update();
							
							window.setTimeout( 
									
								function() { 
								
									if( $( 'action' ) ) {
										
										new Ajax.Updater( 
										
											'action', 
											'/cell/queen_content/?' + SID, 
											
											{ 
												evalScripts: true, 
												method: 'get'
											} 
										);
									}
									bebees.header_update( true );
								}
								
							, 1000 );
						
							window.setTimeout( function() { bebees.throbber_hide(); }, 2000 );
						}
							
					, 2000 );
				}
			}
		);
	},
	
	queen_statusbox_update: function() 
	{
		bebees.statusbox_update( '/cell/statusbox/' );
	},
	
	specialist_statusbox_timer: function( type ) 
	{
		bebees.statusbox_type = type;
		
		bebees.statusbox_timer( 
			
			function() {
				
				if( bebees.throbber_show() ) {
					
					window.setTimeout( 
							
						function() {
						
							bebees.specialist_statusbox_update();
							
							if( bebees.statusbox_type ) {
															
								window.setTimeout( 
									
									function() { 
									
										if( $( 'specialist_action' ) ) {
											
											new Ajax.Updater( 
											
												'specialist_action', 
												'/bee/' + bebees.statusbox_type + '_content/' + '?' + SID, 
												
												{ 
													evalScripts: true, 
													method: 'get'
												} 
											);
										}
																				
										bebees.header_update( true );
									}
									
								, 1000 );
							}
						
							window.setTimeout( function() { bebees.throbber_hide(); }, 2000 );
						}
							
					, 2000 );
				}
			}
		);
	},
	
	specialist_statusbox_update: function() 
	{
		var type = '';
		
		if( bebees.statusbox_type ) {
			
			type = bebees.statusbox_type;
		}
		bebees.statusbox_update( '/bee/statusbox/' + type );
	},
	
	bees_statusbox_timer: function( type, location ) 
	{
		bebees.statusbox_type = type;
		
		bebees.statusbox_location = location;
		
		var url = '/cell/' + bebees.statusbox_type + '_content/' + bebees.statusbox_location;
		
		bebees.statusbox_timer( 
			
			function() {
				
				if( bebees.throbber_show() ) {
					
					window.setTimeout( 
							
						function() {
						
							bebees.bees_statusbox_update();
							
							window.setTimeout( 
								
								function() { 
									
									if( $( 'action' ) ) {
										
										new Ajax.Updater( 
										
											'action', 
											url + '?' + SID, 
											
											{ 
												evalScripts: true, 
												method: 'get'
											} 
										);
									}
									
									bebees.header_update( true );
								}
								
							, 1000 );
							
							window.setTimeout( function() { bebees.throbber_hide(); }, 2000 );
						}
							
					, 2000 );
				}
			}
		);
	},
	
	bees_statusbox_update: function() 
	{
		var location = '';
		
		if( bebees.statusbox_location ) {
			
			var location = bebees.statusbox_location;
		}
		
		bebees.statusbox_update( '/cell/statusbox/' + location  );
	},
	
	cell_cancel_training: function( location, type, training_id ) 
	{
		if( bebees.cancelation_in_progress ) {
		
			return false;
		}
		bebees.cancelation_in_progress = true;
		
		bebees.throbber_show();
		
		window.setTimeout( 
				
			function() {	
	
				new Ajax.Request( 
					
					'/cell/cancel_training/' + location + '?id=' + encodeURIComponent( training_id ) + '&' + SID, 
					
					{ 
						evalScripts: true, 
						method: 'get', 
						onSuccess: function() {
							
							bebees.bees_statusbox_update(); 
					
							window.setTimeout( function() { bebees.cell_content_update( type, location ); }, 1000 );
					
							window.setTimeout( function() { bebees.throbber_hide(); }, 1000 );
						}
					} 
				);
			}
		, 1000 );
	},
	
	form_submitted: false,
	
	submit_form: function( form ) 
	{	
		if( bebees.form_submitted ) {
			
			return false;
		}
		
		bebees.form_submitted = true;
		
		for ( i = 0; i < form.length; i++ ) {
			
			var temp = form.elements[i];

			if ( temp.type.toLowerCase() == "submit" || temp.type.toLowerCase() == "reset" ) {
				
				temp.disabled = true;
			}
		}
		
		return true;	
	},
	
	unsubmit_form: function( form ) 
	{	
		for ( i = 0; i < form.length; i++ ) {
			
			var temp = form.elements[i];

			if ( temp.type.toLowerCase() == "submit" || temp.type.toLowerCase() == "reset" ) {
				
				temp.disabled = false;
			}
		}
		
		return true;	
	},
	
	set_max: function( id, value )
	{
		if( !$( id ) ) return;
		
		if( value > 0 ) $( id ).value = value;
	},

	set_max_within: function( id, value, search_in )
	{
		selector = '#' + search_in + ' #' + id;
		
		control = $$(selector);

		control = control[0];
		
		if( !control ) return;
		
		if( value > 0 ) control.value = value;
	},
	
	check_mails: function()
	{
		new Ajax.Request( 
			
			'/messaging/check_mails/?' + SID, 
			
			{ 
				method: 'get', 
				onSuccess: function( transport ) {
				
					if( transport.responseText.match( /true/ ) ) {
						$( 'menu_news' ).className = 'new';
					}
					else {
						$( 'menu_news' ).className = '';
					}
				}
			} 
		);
	},
	
	admin_get_max_honey: function( user_id )
	{
		if( $( 'user_max_honey_' + user_id ) ) {
										
			bebees.throbber_show();
			
			new Ajax.Updater( 
			
				'user_max_honey_' + user_id, 
				'/admin/get_max_honey/' + user_id + '?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'get',
					onSuccess: function() {
						window.setTimeout( function() { bebees.throbber_hide(); }, 250 );
					}
				} 
			);
		}
	},
	
	admin_get_max_bees: function( user_id )
	{
		if( $( 'user_max_bees_' + user_id ) ) {
										
			bebees.throbber_show();
			
			new Ajax.Updater( 
			
				'user_max_bees_' + user_id, 
				'/admin/get_max_bees/' + user_id + '?' + SID, 
				
				{ 
					evalScripts: true, 
					method: 'get',
					onSuccess: function() {
						window.setTimeout( function() { bebees.throbber_hide(); }, 250 );
					}
				} 
			);
		}
	},
	
	preload_images: function()
	{
	    document.preload = new Array();
	
	    if(document.images)
	    {
	        for(var i = 0; i < bebees.preload_images.arguments.length; i++)
	        {
	            document.preload[i] = new Image();
	            document.preload[i].src = bebees.preload_images.arguments[i];
	        }
	    }
	},
	
	open_payment: function( address ) {
	
		var width = 840;
		var height = 680;
	
		var left = ( screen.width / 2 - width / 2 ) ;
		var top = ( screen.height / 2 - height / 2 );
		
		var external = window.open( address.replace(/\+/g,"%2B"),"paymentglobal","width="+width+",height="+height+",left="+left+",top="+top);
		
		external.focus();
	},
	
	htmlspecialchars_decode: function(string, quote_style) {

		string = string.toString();
		
		string = string.replace(/&amp;/g, '&');
		string = string.replace(/&lt;/g, '<');
		string = string.replace(/&gt;/g, '>');
		
		if (quote_style == 'ENT_QUOTES')
		{
			string = string.replace(/&quot;/g, '"');
			string = string.replace(/&#039;/g, '\'');
		}
		else if (quote_style != 'ENT_NOQUOTES')
		{
			string = string.replace(/&quot;/g, '"');
		}
		
		return string;
	},
	
	select_map_target: function( source, target, value_target )
	{
		if ( $(source) && $(target) && $(source).value.length > 0 )
		{
			for(var i=0; i<$(source).options.length; i++)
			{
				if ( $(source).options[i].selected )
				{
					$(target).value = $(source).options[i].text;
					
					if ( $(value_target) )
					{
						$(value_target).value = $(source).options[i].value;
					}
				};
			}
		}
		
		$('selected_map_target').value = $(source).value;
	},
	
	add_map_target: function( type, target )
	{
		var url = '/map/add_target/?' + SID;

		var params = new Hash();

		params.set('type', type);
		params.set('target', target);

		new Ajax.Request( url,
		{
			method: 'post',
			parameters: params,
			onSuccess: function( transport ) {

				if( transport.responseText.match( /homepage_screen_login/ ) )
				{
					top.location = '/';
				};
				
				json = transport.responseText.evalJSON( true );
				
				var text = '';
				
				if (json && json.result == 'true' )
				{
					text = map_target_add_success;
					
					if ( $('select_map_target') )
					{
						$('select_map_target').options[ $('select_map_target').options.length ] = new Option( json.title, json.value );
					}
				}
				else
				{
					if ( json.error == 'map_target_add_fail_overflow' )
					{
						text = map_target_add_fail_overflow;
					}
					else if ( json.error == 'map_target_add_fail_already_there' )
					{
						text = map_target_add_fail_already_there;
					}
					else
					{
						text = map_target_add_fail;
					}
				}
				bebees.message_show( map_target_header, text );
			}
		} );
		
		return false;
	},

	delete_map_target: function( control )
	{
		if ( $(control) && $F(control) )
		{
			var target = $F(control);
					
			var url = '/map/delete_target/?' + SID;
	
			var params = new Hash();
				
			params.set('target', target );
	
			new Ajax.Request( url,
			{
				method: 'post',
				parameters: params,
				onSuccess: function( transport ) {
	
					if( transport.responseText.match( /homepage_screen_login/ ) )
					{
						top.location = '/';
					};
					
					json = transport.responseText.evalJSON( true );
					
					var text = '';

					if (json && ( json.result == 'true' || json.result == true ) )
					{
						text = map_target_delete_success;

						$A($$('select._map_target')).each(
						
						  function( item )
						  {
								for(var i=0; i<item.options.length; i++)
								{
									if ( item.options[i].value == target )
									{
										item.options[i] = null;
									};
								}
							}			
						);						
					}
					else
					{
						text = map_target_delete_fail;
					}

					bebees.message_show( map_target_header, text );
				}
			} );
		}
	},
	
	increment_server_time: function()
	{
		if ( $('server_time') )
		{
			var current_time = $('server_time').innerHTML.split(':');
			
			current_time[2]++;
			
			if ( current_time[2] > 59 )
			{
				current_time[1]++;

				if ( current_time[1] < 10 )
				{
					current_time[1] = '0' + current_time[1];
				}
				
				current_time[2] = '00';
			}
			else if ( current_time[2] < 10 )
			{
				current_time[2] = '0' + current_time[2];
			}

			if ( current_time[1] > 59 )
			{
				current_time[0]++;

				if ( current_time[0] < 10 )
				{
					current_time[0] = '0' + current_time[0];
				}
							
				current_time[1] = 0;
			}

			if ( current_time[0] > 23 )
			{
				current_time[0] = 0;
			}
			
			var time_string = '' + current_time[0] + ':' + current_time[1] + ':' + current_time[2];
			
			$('server_time').update( time_string );
			
			window.setTimeout( 'bebees.increment_server_time()', 1000 );
		}
	}

}