//------------------------------------------
// 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();

/*--------------------------------------------*/
// 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 );
		}
	}
}

/*--------------------------------------------*/
// 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 );
	}
}

/*--------------------------------------------*/
// Window Resize
/*--------------------------------------------*/

function ajax_std_window_resize( pix, pid )
{
	var box        = document.getElementById( 'post-edit-'+pid );
	var cur_height = parseInt( box.style.height ) ? parseInt( box.style.height ) : 300;
	var new_height = cur_height + pix;
	
	if ( new_height > 0 )
	{
		box.style.height = new_height + "px";
	}
	
	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-edit-'+post_id ).value;
	fields['std_used'] = 1;  // Make sure STD BBCode parser is used
	
	/*--------------------------------------------*/
	// 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 != 'error' )
		{
			document.getElementById( 'post-'+post_id ).innerHTML = html;
		}
	}
	
	//----------------------------------
	// LOAD XML
	//----------------------------------
	
	xmlobj = new ajax_request();
	xmlobj.onreadystatechange( do_request_function );
	var xmlreturn = xmlobj.process( url, 'POST', xmlobj.format_for_post(fields) );
	
	post_cache[ post_id ] = '';
	
	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 );
	/* 
		Alyth Addon 
		Don't change height of Post with post edit textarea
	*/
	var post_height = post_main_obj.offsetHeight - 10;
	/* <- */
	
	//----------------------------------
	// INIT
	//----------------------------------
	
	var url = ipb_var_base_url+'act=xmlout&do=post-edit-show&p='+post_id+'&t='+ipb_input_t+'&f='+ipb_input_f;
	
	if ( ! post_cache[ post_id ] || post_cache[ post_id ] == '' )
	{
		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 )
			{
				//stupid scroll!
				//scroll( 0, post_box_top - 30 );
			}
			
			document.getElementById( 'post-'+post_id ).innerHTML = html;
			
			/* 
				Alyth Addon 
				Don't change height of Post with post edit textarea
			*/
			document.getElementById( 'post-edit-'+post_id ).style.height = document.getElementById( 'post-edit-'+post_id ).offsetHeight + (post_height - post_main_obj.offsetHeight) + 'px';
			/* <- */
			
		}
		
	}
	
	//----------------------------------
	// 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 + "showtopic=" + ipb_input_t + "&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");
	}
	else
	{
		eval(" document.mad_"+id+".src=addquotebutton");
	}
	
	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;
}

var float_selection = "";

function get_selection(e, post_author) {
  var str = "";
  if (window.getSelection) {
    str = window.getSelection();
  } else if (document.getSelection) {
    str = document.getSelection();
  } else if (document.selection && document.selection.createRange) {
    var range = document.selection.createRange();
    str = range.text;
  } else {
    str = "Sorry, this is not possible with your browser.";
  }
  var float_paste = document.getElementById("float_paste");
  if (str != "") {
    var coords = get_mouse_coords(e);
    //float_selection = "[quote name='"+post_author+"' date='"+post_date+"' post='"+post_pid+"']"+str+"[/quote]";
    float_selection = "[fastquote="+post_author+"]"+str+"[/fastquote]\n";
    //alert(post_author + " " + str);
    //alert(coords[0]+":"+coords[1]);
    //alert(document.documentElement.scrollTop+":"+document.body.scrollTop+":"+window.pageYOffset+":"+window.scrollY);
    coords[0]+=5; coords[1]+=5;
    float_paste.style.left = coords[0]+"px";
    float_paste.style.top = coords[1]+"px";
    float_paste.style.display = "";
    //setTimeout("float_paste.style.display = 'none'", 5000);
  } else {
    float_paste.style.display = 'none';
  }
}

function post_selection() {
    if (float_selection != "") {
    	//alert(float_selection);
	my_show_div(my_getbyid('qr_open'));
    	doInsert(float_selection);
        var float_paste = document.getElementById("float_paste");
    	float_paste.style.display = 'none';
    	float_selection = "";
    }
}

function quote_name(txt) {
	my_show_div(my_getbyid('qr_open'));
    	doInsert("[b]"+txt+": [/b]\n");
}

function showExif(txt, e) {
	 var float_exif = document.getElementById("float_exif");
	 if (float_exif.style.display == 'none') {
	    if (txt.indexOf("|") > 0) {
	       var data = txt.split("|");
	       txt =     "File Time: "+data[0]+
	    	   "<br />Mime-Type: "+data[1]+
	    	   "<br />Camera Manufacturer: "+data[2]+
	    	   "<br />Camera Model: "+data[3]+
	    	   "<br />Software: "+data[4]+
	    	   "<br />Exposure Time: "+data[5]+
	    	   "<br />F Number: "+data[6]+
	    	   "<br />ISO: "+data[7]+
	    	   "<br />Original Date: "+data[8];
	    }	
	    float_exif.innerHTML = txt;
	    var coords = get_mouse_coords( e );
	    float_exif.style.left = coords[0]+'px';
	    float_exif.style.top = coords[1]+'px';
	    float_exif.style.display = '';
  	 } else 
  	    float_exif.style.display = 'none';
}
/*
function get_mouse_coords(e) {
	var scrollY = 10;

	if ( document.documentElement && document.documentElement.scrollTop )
	{
		scrollY = document.documentElement.scrollTop;
	}
	else if ( document.body && document.body.scrollTop )
	{
		scrollY = document.body.scrollTop;
	}
	else if ( window.pageYOffset )
	{
		scrollY = window.pageYOffset;
	}
	else if ( window.scrollY )
	{
		scrollY = window.scrollY;
	}

	return scrollY;
} */
/*

function get_mouse_coords(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		if (document.documentElement.scrollTop) {
  		       posx = e.clientX + document.documentElement.scrollLeft;
		       posy = e.clientY + document.documentElement.scrollTop;
		} else {
  		       posx = e.clientX + document.body.scrollLeft;
		       posy = e.clientY + document.body.scrollTop;
		}
	}
    	return [ posx, posy ];
	// posx and posy contain
	// the mouse position relative to the document
	// Do something with this information
}  */

function get_mouse_coords(evt) {
	posx = 0;
	posy = 0;
	if (evt.pageX) return [ evt.pageX, evt.pageY ];
	else if (evt.clientX) {
   	     posx = evt.clientX + (document.documentElement.scrollLeft ?
   	     	  document.documentElement.scrollLeft :
   	     	  document.body.scrollLeft);
   	     posy = evt.clientY + (document.documentElement.scrollTop ?
   	     	  document.documentElement.scrollTop :
   	     	  document.body.scrollTop);
   	     return [posx, posy ];
	}
 	else return null;
}
/*
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function get_mouse_coords(e) {
   var posX = 0, posY = 0;
   if( !e ) { e = window.event; }
   if( !e ) { return [ 0, 0 ]; }
   if( typeof( e.pageX ) == 'number' ) {
      posX = e.pageX;
      posY = e.pageY;
   } else {
      if( typeof( e.clientX ) == 'number' ) {
         posX = e.clientX;
	 posY = e.clientY;
         if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
               posX += document.body.scrollLeft;
	       posY += document.body.scrollTop;
         }  else {
            if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
               posX += document.documentElement.scrollLeft;
	       posY += document.documentElement.scrollTop;
            }
         }
      }
   }
   return [ posX, posY ];
} */

