var is_dom = (document.getElementById) ? true : false;
var is_ie  = (document.all) ? true : false;
var is_ie4 = (!is_dom && is_ie) ? true : false;
var is_nn  = (document.layers) ? true : false;
var is_moz = (is_dom && (navigator.appName=="Netscape")) ? true : false;
var is_opr = (is_dom && window.opera) ? true : false;
var is_op7 = (is_opr && document.readyState) ? true : false;


function getById(id) {
	if (is_dom) {
		return document.getElementById(id);
	} else if (is_ie) {
		return document.all[id];
	} else if (is_nn) {
		return document.layers[id];
	}
	return null;
}

function show_div(name) {
	var id = getById(name);
	if (!id) return;

	if (id.style) {
		id.style.display = "";
	} else {
		id.visibility = "show";
	}
}

function hide_div(name) {
	var id = getById(name);
	if (!id) return;

	if (id.style) {
		id.style.display = "none";
	} else {
		id.visibility = "none";
	}
}

function swap_div(name) {
	var id = getById(name);
	if (!id) return;

	if (id.style) {
		if (id.style.display == "none") id.style.display = "";
		else id.style.display = "none";
	} else {
		if (id.visibility == "none") id.visibility = "show";
		else id.visibility = "none";
	}
}

function flip_div(name1,name2) {
	var id1 = getById(name1);
	var id2 = getById(name2);
	if (!id1 || !id2) return;

	if (id1.style) {
		if (id1.style.display == 'none') {
			id1.style.display = '';
			id2.style.display = 'none';
		} else {
			id1.style.display = 'none';
			id2.style.display = '';
		}
	} else {
		if (id1.visibility == 'none') {
			id1.visibility = 'show';
			id2.visibility = 'none';
		} else {
			id1.visibility = 'none';
			id2.visibility = 'show';
		}
	}
}




document.onclick = float_menu_close;

var float_menus    = Array();
var float_menu_id  = '';
var float_menu_num = 0;

function reg_float_menu(id,menu_div,on_open_callback)
{
	var o = getById(id);
	var d = getById(menu_div);
	if (!o || !d) return;

    o.onclick = _event_menu_onclick;
    d.onclick = _cancel_bubble;

    float_menus[id] = Array(id,menu_div,on_open_callback);

    float_menu_num ++;
    return true;
}


function _cancel_bubble(obj, extra)
{
	if ( ! obj || is_ie)
	{
		if ( extra )
		{
			window.event.returnValue = false;
		}
		window.event.cancelBubble = true;
		return window.event;
	}
	else
	{
		if (obj.target.type == 'submit')
		{
			obj.target.form.submit();
		}
		obj.stopPropagation();
		if ( extra )
		{
			obj.preventDefault();
		}
		return obj;
	}
}

function _event_menu_onclick(e)
{
	e = _cancel_bubble(e, true);
	float_menu_open(this);
}


function float_menu_open(e)
{
	var openmenu = float_menu_id;

    if (typeof(float_menus[e.id]) == 'undefined')
    {
    	return false;
    }

	float_menu_close();

	if ( openmenu == e.id )
	{
		return false;
	}

	var div_id      = getById(float_menus[e.id][1]);
	var obj_id      = getById(float_menus[e.id][0]);

    if (!div_id || !obj_id) return false;

    if (typeof(float_menus[e.id][2]) == 'function')
    {
		float_menus[e.id][2](e.id);
    }

	var left_px     = _get_obj_leftpos(obj_id);
	var top_px      = _get_obj_toppos(obj_id) + e.offsetHeight;

	float_menu_id   = e.id;

	var width = parseInt( div_id.style.width ) ? parseInt( div_id.style.width ) : div_id.offsetWidth;

	if ( (left_px + width) >= document.body.clientWidth )
	{
		left_px = left_px + e.offsetWidth - width;
	}

	div_id.style.left    = left_px + "px";
	div_id.style.top     = top_px  + "px";
	div_id.style.zIndex  = 100;
	div_id.style.display = "";

	if ( is_ie )
	{
		if (( iframeobj = getById( 'iframe_hidden') ) != null)
		{
			iframeobj.scrolling      = 'no';
			iframeobj.frameborder    = 'no';
			iframeobj.style.position = 'absolute';
			iframeobj.style.width    = parseInt(div_id.offsetWidth)  + 'px';
			iframeobj.style.height   = parseInt(div_id.offsetHeight) + 'px';
			iframeobj.style.top      = parseInt(div_id.style.top) + 'px';
			iframeobj.style.left     = parseInt(div_id.style.left) + 'px';
			iframeobj.style.zIndex   = 99;
			iframeobj.style.display  = '';
		}
	}

	return false;
}


function float_menu_close()
{
	if ( float_menu_id != '' )
	{
		getById(float_menus[float_menu_id][1]).style.display = "none";

		if ( is_ie )
		{
			iframeobj = getById( 'iframe_hidden' );
			if (iframeobj) iframeobj.style.display = "none";
		}
	}

	float_menu_id = '';
}


function _get_obj_leftpos(obj)
{
	var left = obj.offsetLeft;

	while( (obj = obj.offsetParent) != null )
	{
		left += obj.offsetLeft;
	}

	return left;
}

function _get_obj_toppos(obj)
{
	var top = obj.offsetTop;

	while( (obj = obj.offsetParent) != null )
	{
		top += obj.offsetTop;
	}

	return top;
}


//==========================================
// Pop up Meters Settings
// Insert by Wizard
//==========================================

function meters_pop()
{
	window.open( ipb_var_base_url + 'act=meters','MetersSets','width=650,height=500,resizable=yes,scrollbars=yes');
}


// ----------------------------------------
// jpage_jump.js
// ----------------------------------------
var pj_total_posts = 0;
var pj_per_page    = 0;
var pj_base_url    = '';
var pj_cur_st      = 0;


function do_page_jump() {
	var o = getById('goto_page_jump');
	if (!o) return;

	var pages    = 1;
	var userPage = parseInt(o.value);
    pj_cur_st    = parseInt(pj_cur_st);

	if ( pj_total_posts % pj_per_page == 0 ) {
		pages = pj_total_posts / pj_per_page;
	} else {
		pages = Math.ceil( pj_total_posts / pj_per_page );
	}

	url_bit = pj_base_url.replace( new RegExp( "&amp;", "g" ) , '&' );

	if ( userPage > 0  )
	{
		if ( userPage < 1 ) {
			userPage = 1;
		}
		if ( userPage > pages ) {
			userPage = pages;
		}
		if ( userPage == 1 ) {
			start = 0;
		} else {
			start = (userPage - 1) * pj_per_page;
		}

        if (start != pj_cur_st) {
			window.location = url_bit + "&st=" + start;
			return false;
		}
	}
}



// ----------------------------------------
// jreply_form.js
// ----------------------------------------

// Prepare quick quote
function copyQ(authorpost,datapost) 
{
	txt='';
	if (document.getSelection) 
	{
		txt=document.getSelection()
	}
	else if (document.selection) 
	{
		txt=document.selection.createRange().text;
	}
	else if ( window.getSelection )
	{
		txt=window.getSelection();
	}

	if ( txt != '' && txt != null )
	{
		txt='[quote='+authorpost+', '+datapost+']'+txt+'[/quote]\n';
	}
}

// Paste quick quote
function pasteQ()
{
	if(document.REPLIER.Post)document.REPLIER.Post.value += txt
}

// Nick insert
function ins(name){
	if (name != "") paste("[b]"+name+"[/b], "+"");
}


//function Insert(text){
//	if (text!="") paste("[quote]"+text+"[/quote]\n", 0);
//}

function Insert(text,authorpost,datapost){
        if( text!="")
                    paste("[quote="+authorpost+", "+datapost+"]"+text+"[/quote]\n", 0);
}

function paste(text, flag){
	if (document.REPLIER) {
		if ((document.selection)&&(flag)) {
			document.REPLIER.Post.focus();
			document.REPLIER.document.selection.createRange().text = text;
		} else document.REPLIER.Post.value += text;
	}
}

function get_selection() {
	if (document.getSelection){
		selection = document.getSelection();
		selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
		selection = selection.replace(/\r\n/gi, " ");
    	while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, "");
		selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
	  } else selection = document.selection.createRange().text;
}

//==========================================
// Translit
//==========================================
var rusBig = new Array( "Ý", "×", "Ø", "¨", "¨", "Æ", "Þ", "Þ", "\ß", "\ß", "À", "Á", "Â", "Ã", "Ä", "Å", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ù", "Ú", "Û", "Ü");
var rusSmall = new Array("ý", "÷", "ø", "¸", "¸","æ", "þ", "þ", "ÿ", "ÿ", "à", "á", "â", "ã", "ä", "å", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ù", "ú", "û", "ü" );
var engBig = new Array("E'", "CH", "SH", "YO", "JO", "ZH", "YU", "JU", "YA", "JA", "A","B","V","G","D","E", "Z","I","J","K","L","M","N","O","P","R","S","T","U","F","H","C", "W","~","Y");
var engSmall = new Array("e'", "ch", "sh", "yo", "jo", "zh", "yu", "ju", "ya", "ja", "a", "b", "v", "g", "d", "e", "z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s",  "t", "u", "f", "h", "c", "w", "~", "y");
var rusRegBig = new Array( /Ý/g, /×/g, /Ø/g, /¨/g, /¨/g, /Æ/g, /Þ/g, /Þ/g, /ß/g, /ß/g, /À/g, /Á/g, /Â/g, /Ã/g, /Ä/g, /Å/g, /Ç/g, /È/g, /É/g, /Ê/g, /Ë/g, /Ì/g, /Í/g, /Î/g, /Ï/g, /Ð/g, /Ñ/g, /Ò/g, /Ó/g, /Ô/g, /Õ/g, /Ö/g, /Ù/g, /Ú/g, /Û/g, /Ü/g);
var rusRegSmall = new Array( /ý/g, /÷/g, /ø/g, /¸/g, /¸/g, /æ/g, /þ/g, /þ/g, /ÿ/g, /ÿ/g, /à/g, /á/g, /â/g, /ã/g, /ä/g, /å/g, /ç/g, /è/g, /é/g, /ê/g, /ë/g, /ì/g, /í/g, /î/g, /ï/g, /ð/g, /ñ/g, /ò/g, /ó/g, /ô/g, /õ/g, /ö/g, /ù/g, /ú/g, /û/g, /ü/g);
var engRegBig = new Array( /E'/g, /CH/g, /SH/g, /YO/g, /JO/g, /ZH/g, /YU/g, /JU/g, /YA/g, /JA/g, /A/g, /B/g, /V/g, /G/g, /D/g, /E/g, /Z/g, /I/g, /J/g, /K/g, /L/g, /M/g, /N/g, /O/g, /P/g, /R/g, /S/g, /T/g, /U/g, /F/g, /H/g, /C/g, /W/g, /~/g, /Y/g, /'/g);
var engRegSmall = new Array(/e'/g, /ch/g, /sh/g, /yo/g, /jo/g, /zh/g, /yu/g, /ju/g, /ya/g, /ja/g, /a/g, /b/g, /v/g, /g/g, /d/g, /e/g, /z/g, /i/g, /j/g, /k/g, /l/g, /m/g, /n/g, /o/g, /p/g, /r/g, /s/g, /t/g, /u/g, /f/g, /h/g, /c/g, /w/g, /~/g, /y/g, /'/g);


function rusLang(str) {
var textar = str;
if (textar) {
   for (i=0; i<engRegSmall.length; i++) {
    textar = textar.replace(engRegSmall[i], rusSmall[i])
    }
   for (var i=0; i<engRegBig.length; i++) {
    textar = textar.replace(engRegBig[i], rusBig[i])
    }
return textar;
}
}

function check_input() {
var input = new Array();
input = document.REPLIER.Post.value.split(' ');
for(var j=0;j<input.length;j++) {
   if(input[j].indexOf("http://") > -1 || input[j].indexOf("www.") > -1 || input[j].indexOf("@") > -1 || (input[j].charAt(0)==":" && input[j].charAt(input[j].length-1)==":") ){
   input[j] = input[j];
   }
else { input[j] = rusLang(input[j]) }

}

document.REPLIER.Post.value = input.join(' ');

}


document.onkeydown=function(e){
if (e) event=e
if ((event.keyCode==13)&&(event.ctrlKey)){
     document.REPLIER.submit.click();}
}

//====================
// Link to a post
//====================

function link_to_post(pid)
{
	temp = prompt( ipb_lang_tt_prompt, pid );
	return false;
}

//====================
// Link to a comment
//====================

function link_to_comment(cid)
{
	temp = prompt( ipb_lang_tt_prompt, ñid );
	return false;
}


//=====================
// Floatalign attach
//=====================

function insert_attach(id,floatalign)
{
    document.REPLIER.Post.focus();
    if(!floatalign)
    {
        floatalign='';
    }    
    doInsert( "[attachid="+id+floatalign+"]", "" );
        //paste( "[attachid="+id+floatalign+"]", "" );

    document.REPLIER.Post.focus();
}