//------------------------------------------
// Invision Power Board v2.1
// Topics JS File
// (c) 2005 Invision Power Services, Inc.
//
// http://www.invisionboard.com
//------------------------------------------

var mystored_selection 	= '';
var post_cache			= new Array();
var ajax_loaded			= 1;
var ignore_cache		= new Array();
var rate_value			= 0;

/*--------------------------------------------*/
// Show hidden post
/*--------------------------------------------*/

function topic_show_ignored_post( pid )
{
	try
	{
		// Set up
		var post_main   = document.getElementById( 'post-main-'   + pid );
		var post_ignore = document.getElementById( 'post-ignore-' + pid );
		
		// Show it
		post_main.innerHTML = ignore_cache[ pid ];
	}
	catch( e )
	{
		//alert( e );
	}
	
	return false;
}


/*--------------------------------------------*/
// Initiate topic hide
/*--------------------------------------------*/

function topic_init_ignored_post( pid )
{
	try
	{
		// Set up
		var post_main   = document.getElementById( 'post-main-'   + pid );
		var post_ignore = document.getElementById( 'post-ignore-' + pid );
		
		// Cache it...
		ignore_cache[ pid ] = post_main.innerHTML;
		
		// Display "ignored" msg
		post_main.innerHTML = post_ignore.innerHTML;
	}
	catch( e )
	{
		//alert( e );
	}
}


/*--------------------------------------------*/
// Ajax: Use full editor
/*--------------------------------------------*/

function ajax_fulleditor_for_edit( post_id )
{
	if ( post_id && ipb_input_t && ipb_input_f )
	{
		var _form = document.getElementById( 'quick-edit-form-' + post_id );
		var _url  = ipb_var_base_url + 'act=post&do=edit_post&f=' + ipb_input_f + '&t=' + ipb_input_t + '&p=' + post_id + '&st=' + ipb_var_st + '&_from=quickedit';
		
		_form.action = _url;
		_form.method = 'POST';
		
		_form.submit();
		
		xmlobj.show_loading();
		
		return false;
	}
	else
	{
		return false;
	}
}

/*--------------------------------------------*/
// Ajax: Cancel for edit
/*--------------------------------------------*/

function ajax_cancel_for_edit( post_id )
{
	if ( post_cache[ post_id ] != "" )
	{
		document.getElementById( 'post-'+post_id ).innerHTML = post_cache[ post_id ];
	}
	
	return false;
}

/*--------------------------------------------*/
// Ajax: Save for edit
/*--------------------------------------------*/

function ajax_save_for_edit( post_id )
{
	//----------------------------------
	// INIT
	//----------------------------------
	
	var url    = ipb_var_base_url+'act=xmlout&do=post-edit-save&p='+post_id+'&t='+ipb_input_t+'&f='+ipb_input_f;
	var fields = new Array();

	//----------------------------------
	// Populate fields
	//----------------------------------
	
	fields['md5check']         = ipb_md5_check;
	fields['t']                = ipb_input_t;
	fields['f']                = ipb_input_f;
	fields['p']                = post_id;
	fields['act']              = 'xmlout';
	fields['do']               = 'post-edit-save';
	fields['Post']             = document.getElementById( post_id + '_textarea' ).value;
	fields['std_used']         = 1;  // Make sure STD BBCode parser is used
	
	//-----------------------------------------
	// Got a reason to be jolly?
	//-----------------------------------------
	
	try
	{
		fields['post_edit_reason'] = document.getElementById( 'post_edit_reason' ).value;
	}
	catch(err)
	{
	}
	
	//----------------------------------
	// Is there a post?
	//----------------------------------
	
	var post_check = fields['Post'];
	
	if ( post_check.replace( /^\s*|\s*$/g, "" ) == "" )
	{
		alert( js_no_empty_post );
		return false;
	}
	
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		//-----------------------------------------
		// Execute JS...
		//-----------------------------------------
		
		if ( html == 'nopermission' )
		{
			alert( js_error_no_permission );
			document.getElementById( 'post-'+post_id ).innerHTML = post_cache[ post_id ];
		}
		else if ( html != 'error' )
		{
			document.getElementById( 'post-'+post_id ).innerHTML = html;
			xmlobj.execute_javascript( html );
			fix_linked_image_sizes();
		}
	};
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	
	return false;
}

/*--------------------------------------------*/
// Ajax: Prep for edit
/*--------------------------------------------*/

function ajax_prep_for_edit( post_id, event )
{
	//----------------------------------
	// Cancel bubble (Prevent IE scroll...)
	//----------------------------------
	
	global_cancel_bubble( event, true );
	
	var post_main_obj = document.getElementById( 'post-main-' + post_id );
	var post_box_top  = _get_obj_toppos( post_main_obj );
	
	//----------------------------------
	// INIT
	//----------------------------------
	
	var url = ipb_var_base_url+'act=xmlout&do=post-edit-show&p='+post_id+'&t='+ipb_input_t+'&f='+ipb_input_f;
	
	post_cache[ post_id ] = document.getElementById( 'post-'+post_id ).innerHTML;

	//----------------------------------
	// Attempt to close open menus
	//----------------------------------
	
	try
	{
		menu_action_close();
	}
	catch(e)
	{
		//alert( e );
	}
	
	/*--------------------------------------------*/
	// Main function to do on request
	// Must be defined first!!
	/*--------------------------------------------*/
	
	do_request_function = function()
	{
		//----------------------------------
		// Ignore unless we're ready to go
		//----------------------------------
		
		if ( ! xmlobj.readystate_ready_and_ok() )
		{
			xmlobj.show_loading();
			return;
		}
		
		xmlobj.hide_loading();
		
		//----------------------------------
		// INIT
		//----------------------------------
		
		var html = xmlobj.xmlhandler.responseText;
		
		if ( html == 'nopermission' )
		{
			alert( js_error_no_permission );
		}
		else if ( html != 'error' )
		{
			if ( post_box_top )
			{
				scroll( 0, post_box_top - 30 );
			}
			
			document.getElementById( 'post-' + post_id ).innerHTML = html;

			//-----------------------------------------
			// Set up new editor
			//-----------------------------------------
			
			IPS_Lite_Editor[ post_id ] = new ips_text_editor_lite( post_id );
			IPS_Lite_Editor[ post_id ].init();
		}
	};
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	
	xmlobj.process( url );
	
	return false;
}

/*--------------------------------------------*/
// check selection
/*--------------------------------------------*/

function checkselection()
{
	var myselection = '';
	
	if ( window.getSelection )
	{
		myselection = window.getSelection();
	}
	else if ( document.selection )
	{
		myselection = document.selection.createRange().text;
	}
	else if ( document.getSelection )
	{
		myselection = document.getSelection();
	}
	
 	if ( myselection != '' && myselection != null )
	{
		if ( myselection != mystored_selection )
		{
			document.getElementById('fastreply-pastesel').style.display = '';
			mystored_selection = (myselection.toString() != '') ? myselection.toString() : null;
		}
	}
	else
	{
		mystored_selection = null;
	}
}

/*--------------------------------------------*/
// Paste selection
/*--------------------------------------------*/

function pasteselection()
{
	if ( mystored_selection != '' && mystored_selection != null )
	{
		var fr = document.getElementById('fastreplyarea');
		if (fr)
		{
			fr.value += '[quote]'+mystored_selection+'[/quote]\n';
			fr.focus();
		}
	}

	return false;
}

/*--------------------------------------------*/
// Delete post
/*--------------------------------------------*/

function delete_post(theURL)
{
	if (confirm( ipb_lang_js_del_1 ))
	{
		window.location.href=theURL;
	}
}

/*--------------------------------------------*/
// Multi quote
/*--------------------------------------------*/

function multiquote_add(id)
{
	saved = new Array();
	clean = new Array();
	add   = 1;
	
	//-----------------------------------
	// Get any saved info
	//-----------------------------------
	
	if ( tmp = my_getcookie('mqtids') )
	{
		saved = tmp.split(",");
	}
	
	//-----------------------------------
	// Remove bit if exists
	//-----------------------------------
	
	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != "" )
		{
			if ( saved[i] == id )
			{
				 add = 0;
			}
			else
			{
				clean[clean.length] = saved[i];
			}
		}
	}
	
	//-----------------------------------
	// Add?
	//-----------------------------------
	
	if ( add )
	{
		clean[ clean.length ] = id;
		eval("document.mad_"+id+".src=removequotebutton");
		eval("document.mad_"+id+".title='-'");
	}
	else
	{
		eval(" document.mad_"+id+".src=addquotebutton");
		eval("document.mad_"+id+".title='+'");
	}
	
	my_setcookie( 'mqtids', clean.join(','), 0 );
	
	return false;
}

/*--------------------------------------------*/
// Check delete
/*--------------------------------------------*/

function checkdelete()
{
	if ( ! document.modform.selectedpids.value )
	{
		return false;
	}
	
	isDelete = document.modform.tact.options[document.modform.tact.selectedIndex].value;
	
	if (isDelete == 'delete')
	{
		formCheck = confirm( lang_suredelete );
		
		if (formCheck == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

/*--------------------------------------------*/
// Toggle selection
//*--------------------------------------------*/

function topic_toggle_pid( pid )
{
	//-----------------------------------
	// Got a number?
	//-----------------------------------
	
	if ( isNaN( pid ) )
	{
		return false;
	}
	
	saved = new Array();
	clean = new Array();
	add   = 1;
	
	//-----------------------------------
	// Get form info
	//-----------------------------------
	
	tmp = document.modform.selectedpids.value;
	
	saved = tmp.split(",");
	
	//-----------------------------------
	// Remove bit if exists
	//-----------------------------------
	
	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != "" )
		{
			if ( saved[i] == pid )
			{
				 add = 0;
			}
			else
			{
				clean[clean.length] = saved[i];
			}
		}
	}
	
	//-----------------------------------
	// Add?
	//-----------------------------------
	
	if ( add )
	{
		clean[ clean.length ] = pid;
		eval("document.img"+pid+".src=selectedbutton");
	}
	else
	{
		eval(" document.img"+pid+".src=unselectedbutton");
	}
	
	newvalue = clean.join(',');
	
	my_setcookie( 'modpids', newvalue, 0 );
	
	document.modform.selectedpids.value = newvalue;
	
	newcount = stacksize(clean);
	
	document.modform.gobutton.value = lang_gobutton + ' (' + newcount + ')';
	
	return false;
};


function topic_rate()
{
	/**
	* Settings
	*/
	this.settings = {
						'allow_rating'           : 0,
						'default_rating'         : 3,
						'img_star_on'            : 'star_filled.gif',
						'img_star_selected'      : 'star_selected.gif',
						'img_star_off'           : 'star_empty.gif',
						'img_main_star_0'        : 'rating_0.gif',
						'img_main_star_1'        : 'rating_1.gif',
						'img_main_star_2'        : 'rating_2.gif',
						'img_main_star_3'        : 'rating_3.gif',
						'img_main_star_4'        : 'rating_4.gif',
						'img_main_star_5'        : 'rating_5.gif',
						'img_base_url'           : '',
						'div_rating_wrapper'     : 'topic-rating-wrapper',
						'text_rating_image'      : 'topic-rating-img-',
						'topic-rating-img-main'  : 'topic-rating-img-main',
						'topic-rating-my-rating' : 'topic-rating-my-rating',
						'topic-rating-hits'      : 'topic-rating-hits'
	 				};
	
	this.languages = {
						'img_alt_rate'       : '',
						'rate_me'            : ''
					 };
	
	/**
	* INIT rating images
	*/
	this.init_rating_images = function()
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------

		var html = '';

		//-----------------------------------------
		// Can rate this member?
		//-----------------------------------------

		if ( ! this.settings[ 'allow_rating' ] )
		{
			return false;
		}

		//-----------------------------------------
		// Still here? OK...
		//-----------------------------------------

		for( var i = 1 ; i <= 5 ; i++ )
		{
			var _onmouseover = '';
			var _onmouseout  = '';
			var _onclick     = '';
			var _title       = '';

			_onmouseover = ' onmouseover="this.style.cursor=\'pointer\'; topic_rate.show_rating_images(' + i + ', 0)"';
			_onmouseout  = ' onmouseout="topic_rate.show_rating_images(-1, 1)"';
			_onclick     = ' onclick="topic_rate.send_rating(' + i + ')"';
			_title       = this.languages['img_alt_rate'];

			html += "<img style='vertical-align:top' src='" + this.settings['img_base_url'] + '/' + this.settings['img_star_off'] + "' " + _onmouseover + _onmouseout + _onclick + "id='" + this.settings['text_rating_image'] + i + "' alt='-' title='" + _title + "' />";
		}

		document.getElementById( this.settings['div_rating_wrapper'] ).innerHTML = this.languages['rate_me'] + ' ' + html;

		//-----------------------------------------
		// Now set the image...
		//-----------------------------------------

		this.show_rating_images( this.settings['default_rating'], 1 );
	};
	
	/**
	* Send rating..
	*/
	this.send_rating = function( rating )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		rating = rating ? rating : 0;
		
		//-----------------------------------------
		// Got a rating?
		//-----------------------------------------
		
		if ( rating )
		{
			//----------------------------------
			// INIT
			//----------------------------------

			var url = ipb_var_base_url+'act=xmlout&do=topic_rate&t='+ipb_input_t+'&rating='+rating;

			/*--------------------------------------------*/
			// Main function to do on request
			// Must be defined first!!
			/*--------------------------------------------*/

			do_request_function = function()
			{
				//----------------------------------
				// Ignore unless we're ready to go
				//----------------------------------

				if ( ! xmlobj.readystate_ready_and_ok() )
				{
					xmlobj.show_loading( '' );
					return;
				}

				xmlobj.hide_loading();

				//----------------------------------
				// INIT
				//----------------------------------

				var html = xmlobj.xmlhandler.responseText;
				
				if ( html == 'no_permission' )
				{
					alert( js_error_no_permission );
				}
				else if ( html != 'error' )
				{
					var _result    = html.split(',');
					var _new_value = _result[0];
					var _new_hits  = _result[1];
					var _new_stars = _result[2];
					var _type      = _result[3];
					
					//-----------------------------------------
					// Now set the image...
					//-----------------------------------------
					
					topic_rate.settings['default_rating'] = parseInt( _new_stars );
				
					topic_rate.show_rating_images( topic_rate.settings['default_rating'], 1 );
					
					menu_action_close();
					
					//-----------------------------------------
					// Update counts
					//-----------------------------------------
				
					document.getElementById('topic-rating-hits').innerHTML      = _new_hits;
					document.getElementById('topic-rating-my-rating').innerHTML = rating;
					
					show_inline_messages_instant( 'rating_updated' );
				}
			};

			//----------------------------------
			// LOAD XML
			//----------------------------------

			xmlobj = new ajax_request();
			xmlobj.onreadystatechange( do_request_function );

			xmlobj.process( url );

			return false;
		}
	};
	
	/**
	* Show rating images..
	*/
	this.show_rating_images = function( rating, restore_default )
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------
		
		rating   = restore_default ? this.settings['default_rating'] : parseInt( rating );
		var star = restore_default ? this.settings['img_star_on'] : this.settings['img_star_selected'];
		
		//-----------------------------------------
		// Set to 0
		//-----------------------------------------
		
		for( var i = 1 ; i <= 5 ; i++ )
		{
			var _img = document.getElementById( this.settings['text_rating_image'] + i );
			_img.src = this.settings['img_base_url'] + '/' + this.settings['img_star_off'];
		}
		
		//-----------------------------------------
		// Show ones coloured...
		//-----------------------------------------
		
		for( var i = 1 ; i <= rating ; i++ )
		{
			var _img = document.getElementById( this.settings['text_rating_image'] + i );
			_img.src = this.settings['img_base_url'] + '/' + star;
		}
		
		//-----------------------------------------
		// Set main image
		//-----------------------------------------
		
		document.getElementById( this.settings['topic-rating-img-main'] ).src = this.settings['img_base_url'] + '/' + this.settings['img_main_star_' + rating ];
	};
	
};