//------------------------------------------
// IP.Tracker v1.2
// JS File
// (c) 2007 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;

function projects_send_marker_update( pid, is_subforum )
{
	//----------------------------------
	// Get current image...
	//----------------------------------

	try
	{
		var imgsrc = document.getElementById( 'p-'+pid ).innerHTML;
		
		if ( imgsrc != '' )
		{
			//----------------------------------
			// Find out what img its using...
			//----------------------------------

			var regex  = new RegExp( "src=['\"](.*/)(" + regex_markers + ")['\"]" );

			var results = imgsrc.match( regex );

			//----------------------------------
			// Got a replacement?
			//----------------------------------

			if ( img_markers[ results[2] ] )
			{
				imgsrc = imgsrc.replace( regex, "src='$1"+img_markers[ results[2] ]+"'" );

				document.getElementById( 'p-' + pid ).innerHTML = imgsrc;
			}
		}
	}
	catch(e){}

	var text_return = 0;

	/*--------------------------------------------*/
	// 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() )
		{
			// Could do a little loading graphic here?
			return;
		};

		//----------------------------------
		// Do nothing
		//----------------------------------

		text_return = xmlobj.xmlhandler.responseText;
	};

	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );

	xmlobj.process( ipb_var_base_url + 'autocom=tracker&req=xml&do=mark-project-read&pid=' + pid + '&sf=' + is_subforum );
}

/*--------------------------------------------*/
// Show hidden post
/*--------------------------------------------*/

function issue_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 issue_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 );
	}
}

/*--------------------------------------------*/
// Add onload event
/*--------------------------------------------*/

add_onload_event( fix_linked_image_sizes );

/*--------------------------------------------*/
// Fix linked images (attach thumbs)
/*--------------------------------------------*/

function fix_linked_image_sizes_attach_thumb( attach_id, width, height, file_size )
{
	//-----------------------------------------
	// INIT
	//-----------------------------------------

	var _img    = '<img src="' + ipb_var_image_url + '/img-resized.png" style="vertical-align:middle" border="0" alt="" />';
	var img_obj = document.getElementById( 'ipb-attach-img-'   + attach_id );
	var div_obj = document.getElementById( 'ipb-attach-div-'   + attach_id );
	var ct_obj  = document.getElementById( 'ipb-attach-ct-'    + attach_id );
	var cb_obj  = document.getElementById( 'ipb-attach-cb-'    + attach_id );
	var url_obj = document.getElementById( 'ipb-attach-url-'   + attach_id );
	var tbl_obj = document.getElementById( 'ipb-attach-table-' + attach_id );

	var _width   = parseInt( img_obj.width )  ? parseInt( img_obj.width )  : parseInt( img_obj.style.width );
	var _height  = parseInt( img_obj.height ) ? parseInt( img_obj.height ) : parseInt( img_obj.style.height );
	var _padding = 5;
	var _percent = 0;

	if ( is_ie )
	{
		//-----------------------------------------
		// Well done MS.. more bugs and workarounds
		//-----------------------------------------

		_width  = parseInt( img_obj.currentStyle['width'] );
		_height = parseInt( img_obj.currentStyle['height'] );
	}

	if ( is_safari )
	{
		//-----------------------------------------
		// Nice. Safari you suck! I hate you!
		// You're the black sheep of the apple family!
		//-----------------------------------------

		tbl_obj.style.display = 'inline-block';
	}

	//-----------------------------------------
	// Got width / height?
	//-----------------------------------------

	if ( width && height )
	{
		_percent = Math.ceil( 100 - parseInt( _width / width * 100 ) );
	}

	//-----------------------------------------
	// Stylize
	//-----------------------------------------

	// Remove IMG class to stop gray border
	img_obj.className     = 'ipb';
	img_obj.title         = ipb_global_lang['click_to_view'];
	img_obj.onmouseover   = fix_attach_images_mouseover;
	img_obj.onclick       = function(event) { ipsclass.pop_up_window( url_obj.href, width + 20, height + 20 ); ipsclass.cancel_bubble(event); };

	// Re-style the main div obj
	div_obj.style.width          = ( _width > 140 ? _width : 140 ) + 'px';
	div_obj.style.height         = _height + ( _padding * 2 ) + 28 + 'px';
	div_obj.className            = 'resized-linked-image';
	div_obj.style.paddingTop     = _padding + "px";
	div_obj.style.paddingBottom  = _padding + "px";
	div_obj.style.paddingLeft    = _padding + 3 + "px";
	div_obj.style.paddingRight   = _padding + 3 + "px";
	div_obj.style.textAlign      = 'center';
	div_obj.title                = ipb_global_lang['click_to_view'];
	//div_obj.onmouseover          = fix_linked_images_mouseover;
	//div_obj.onclick              = function(event) { ipsclass.pop_up_window( url_obj.href, width + 20, height + 20 ); ipsclass.cancel_bubble(event); };

	// Add content to the top div
	ct_obj.style.fontSize  = '9px';
	cb_obj.style.fontSize  = '9px';
	cb_obj.style.textAlign = 'center';

	if ( _percent > 0 )
	{
		ct_obj.innerHTML = _img + '&nbsp;' + ipsclass.lang_build_string( ipb_global_lang['image_attach_percent'], _percent );
	}
	else
	{
		ct_obj.innerHTML = _img + '&nbsp;' + ipb_global_lang['image_attach_no_percent'];
	}

	cb_obj.innerHTML     = "";

	if ( width && height )
	{
		cb_obj.innerHTML = ipsclass.lang_build_string( ipb_global_lang['image_attach_dims'], width, height );
	}

	cb_obj.innerHTML += ipsclass.lang_build_string( ipb_global_lang['image_attach_size'], file_size );
}

/*--------------------------------------------*/
// Fix linked images (normal IMGs)
/*--------------------------------------------*/

function fix_linked_image_sizes()
{
	if ( ipsclass.settings['do_linked_resize'] != 1 )
	{
		return true;
	}

	var images   = document.getElementsByTagName( 'IMG' );
	var _padding = 2;
	var _count   = 0;
	var _img     = '<img src="' + ipb_var_image_url + '/img-resized.png" style="vertical-align:middle" border="0" alt="" />';
	var _img2    = '<img src="' + ipb_var_image_url + '/folder_attach_images/attach_zoom_in.png" style="vertical-align:middle" border="0" alt="" />';
	var _sw      = screen.width * ( parseInt( ipsclass.settings['resize_percent'] ) / 100 );

	for ( var i = 0 ; i < images.length; i++ )
	{
		if ( images[i].className == 'linked-image' )
		{
			//-----------------------------------------
			// Inc. counter
			//-----------------------------------------

			_count++;

			//-----------------------------------------
			// Set up basics...
			//-----------------------------------------

			if ( images[i].width > _sw )
			{
				//-----------------------------------------
				// Grab some details...
				//-----------------------------------------

				var _width   = images[i].width;
				var _height  = images[i].height;
				var _percent = 0;

				//-----------------------------------------
				// Work out percentage
				//-----------------------------------------

				images[i].width  = _sw;

				if ( images[i].width < _width && _width > 0 && images[i].width > 0 )
				{
					_percent = Math.ceil( parseInt( images[i].width / _width * 100 ) );
				}

				images[i].id          = '--ipb-img-resizer-' + _count;
				images[i]._resized    = 1;
				images[i]._width      = _width;

				//-----------------------------------------
				// Add in wrapper
				//-----------------------------------------

				var div = document.createElement( 'div' );

				div.innerHTML            = _img + '&nbsp;' + ipsclass.lang_build_string( ipb_global_lang['image_resized'], _percent, _width, _height );
				div.style.width          = images[i].width - ( _padding * 2 ) + 'px';
				div.className            = 'resized-linked-image';
				div.style.paddingTop     = _padding + "px";
				div.style.paddingBottom  = _padding + "px";
				div.style.paddingLeft    = _padding + "px";
				div.style.paddingRight   = _padding + "px";
				div._is_div              = 1;
				div._resize_id           = _count;
				div.onclick              = fix_linked_images_onclick;
				div.onmouseover          = fix_linked_images_mouseover;
				div.title                = ipb_global_lang['click_to_view' ];
				div._src                 = images[i].src;

				//-----------------------------------------
				// Add it all...
				//-----------------------------------------

				//if ( is_ie && ! is_ie7 )
				//{
				//	images[i].outerHTML = div.outerHTML + images[i].outerHTML;
				//}
				//else
				//{
					images[i].parentNode.insertBefore( div, images[i] );
				//}
			}
		}
	}
}

function fix_linked_images_onclick(e)
{
	//-----------------------------------------
	// Div clicked 
	//-----------------------------------------

	PopUp( this._src, 'popup',screen.width,screen.height,1,1,1);

	e = ipsclass.cancel_bubble_all( e );

	return false;
};

function fix_attach_images_mouseover(e)
{
	//-----------------------------------------
	// Change cursor...
	//-----------------------------------------

	try
	{
		this.style.cursor='pointer';
	}
	catch(acold)
	{
	}
}

function fix_linked_images_mouseover(e)
{
	try
	{
		this.style.cursor='pointer';
	}
	catch(acold)
	{
	}

}

/*--------------------------------------------*/
// Fast reply stuff
/*--------------------------------------------*/

function emoticon( ecode, eobj, eurl )
{
	ecode      = ' ' + ecode + ' ';
	var obj_ta = document.getElementById( 'fastreplyarea' );

	if ( (ua_vers >= 4) && is_ie && is_win)
	{
		if (obj_ta.isTextEdit)
		{
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;

			if((sel.type == "Text" || sel.type == "None") && rng != null)
			{
				if(ecode != "" && rng.text.length > 0)
				{
					ecode = rng.text + ecode ;
				}

				rng.text = ecode;
			}
		}
		else
		{
			obj_ta.value += ecode;
		}
	}
	//----------------------------------------
	// It's MOZZY!
	//----------------------------------------
	else if ( obj_ta.selectionEnd )
	{ 
		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;

		if (es <= 2)
		{
			es = obj_ta.textLength;
		}

		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);

		//-----------------------------------
		// text range?
		//-----------------------------------

		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
		{
			middle = ecode + middle;
		}
		else
		{
			middle = ecode + middle;
		}

		obj_ta.value = start + middle + end;

		var cpos = ss + (middle.length);

		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;
	}
	//----------------------------------------
	// It's CRAPPY!
	//----------------------------------------
	else
	{
		obj_ta.value += ecode;
	}

	obj_ta.focus();

	if ( (ua_vers >= 4) && is_ie && is_win && emowindow != 'undefined' )
	{
		emowindow.focus();
	}

	//document.getElementById( 'fastreplyarea' ).value += ' ' + ecode + ' ';
}

/*--------------------------------------------*/
// Topic: Scroll to post: IE fix
/*--------------------------------------------*/

function topic_scroll_to_post( post_id )
{
	//----------------------------------
	// Check for PID
	//----------------------------------

	if ( ! post_id )
	{
		return false;
	}

	try
	{
		var post_main_obj = document.getElementById( 'post-main-' + post_id );
	}
	catch(error)
	{
		var post_main_obj;
	}

	if ( post_main_obj )
	{
		var post_box_top  = _get_obj_toppos( post_main_obj );

		if ( post_box_top )
		{
			scroll( 0, post_box_top - 30 );
		}
	}
}

/*--------------------------------------------*/
// 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 + 'autocom=tracker&req=postedit&pid=' + ipb_input_pid + '&iid=' + ipb_input_iid + '&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+'autocom=tracker&req=xml&do=post-edit-save&p='+post_id+'&iid='+ipb_input_iid+'&pid='+ipb_input_pid;
	var fields = new Array();

	//----------------------------------
	// Populate fields
	//----------------------------------

	fields['md5check']         = ipb_md5_check;
	fields['iid']              = ipb_input_iid;
	fields['pid']              = ipb_input_pid;
	fields['p']                = post_id;
	fields['autocom']          = 'tracker';
	fields['req']              = 'xml';
	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+'autocom=tracker&req=xml&do=post-edit-show&p='+post_id+'&iid='+ipb_input_iid+'&pid='+ipb_input_pid;

	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;
}

/*--------------------------------------------*/
// Link to a post
/*--------------------------------------------*/

function link_to_post(pid)
{
	temp = prompt( ipb_lang_tt_prompt, ipb_var_base_url + "autocom=tracker&showissue=" + ipb_input_iid + "&view=findpost&p=" + pid );
	return false;
}

/*--------------------------------------------*/
// Delete post
/*--------------------------------------------*/

function delete_post(theURL)
{
	if (confirm( ipb_lang_js_del_1 ))
	{
		window.location.href=theURL;
	}
	else
	{
		alert ( ipb_lang_js_del_2 );
	} 
}

/*--------------------------------------------*/
// 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;
}
