// -------------------------------------------------------------------------------
// ---  найти родительский объект с заданным tagName
	function rGetParent(elmChild,tagName){
		var elmParent = elmChild;
		while(elmParent.tagName!=tagName && elmParent.tagName!="BODY"){
			elmParent = elmParent.parentNode;
		}
		return (elmParent.tagName==tagName)?elmParent:null;
	}
// -------------------------------------------------------------------------------
// ---  вызвать ColorPicker
	function rGetColorer(color){
		return window.showModalDialog(orPath.base+"richedit.color.html",color,"dialogWidth:350px; dialogHeight:370px; resizable: no; help: no; status: no; scroll: no;");
	}
// -------------------------------------------------------------------------------
// ---  очистка стилей objecta
	function rClearObjectFormat(oObject,oDocument){
		var newTag,orTags;
		if(orTags=oObject.childNodes){
			for(var i=orTags.length-1;i>=0;i--){ 
				rClearObjectFormat(orTags[i],oDocument);
			}
		}
		if(oObject.tagName){
			if(	oObject.tagName == "I" ||
					oObject.tagName == "B" ||
					oObject.tagName == "U" ||
					oObject.tagName == "A" ||
					oObject.tagName == "P" ||
					oObject.tagName == "OL" ||
					oObject.tagName == "UL" ||
					oObject.tagName == "LI" ||
					oObject.tagName == "TR" ||
					oObject.tagName == "TD" ||
					oObject.tagName == "TH" ||
					oObject.tagName == "HR" ||
					oObject.tagName == "BR" ||
					oObject.tagName == "SUP" ||
					oObject.tagName == "SUB" ||
					oObject.tagName=="IMG"	||
					oObject.tagName=="MAP"	||
					oObject.tagName=="AREA"	||
					oObject.tagName == "TBODY" ||
					oObject.tagName=="TABLE"
			){
				oObject.removeAttribute("className");
				oObject.removeAttribute("style");
				oObject.removeAttribute("type");
				oObject.removeAttribute("dir");
				oObject.removeAttribute("lang");
				if(oObject.tagName!="IMG"){
					oObject.removeAttribute("width");
					oObject.removeAttribute("height");
				}
				if(oObject.tagName=="TABLE"){ // для всех тегов TABLE
					oObject.setAttribute("cellSpacing","0");
					oObject.setAttribute("cellPadding","0");
					oObject.setAttribute("border","0");
					oObject.setAttribute("className","reTable");
				}else if(oObject.tagName=="IMG"){ // для всех тегов IMG
					oObject.removeAttribute("vSpace");
					oObject.removeAttribute("hSpace");
					oObject.removeAttribute("align");
					oObject.setAttribute("border","0");
				}
			}else if(oObject.tagName == "BODY"){
			}else if(oObject.tagName == "SCRIPT"){
				 oObject.parentNode.removeChild(oObject);
			}else if(oObject.tagName == "STRONG"){
				var fChar = oObject.innerText.substr(0,1);
				newTag = oDocument.createElement("B");
				newTag.innerHTML = oObject.innerHTML;
				oObject.parentNode.replaceChild(newTag,oObject);
				if(fChar==" ") newTag.parentNode.insertBefore(oDocument.createTextNode(" "),newTag);
			}else if(oObject.tagName == "EM"){
				var fChar = oObject.innerText.substr(0,1);
				newTag = oDocument.createElement("I");
				newTag.innerHTML = oObject.innerHTML;
				oObject.parentNode.replaceChild(newTag,oObject);
				if(fChar==" ") newTag.parentNode.insertBefore(oDocument.createTextNode(" "),newTag);
			}else if(oObject.canHaveHTML){
				var fChar = oObject.innerText.substr(0,1);
				newTag = oDocument.createElement("");
				newTag.innerHTML = oObject.innerHTML;
				oObject.parentNode.replaceChild(newTag,oObject);
				if(fChar==" ") newTag.parentNode.insertBefore(oDocument.createTextNode(" "),newTag);
			}
			if(oObject.canHaveHTML && oObject.tagName!="TABLE" && oObject.tagName!="TR" && oObject.tagName!="TBODY"){
				oObject.innerHTML = oObject.innerHTML.replace(/(&nbsp;)+/g," ");
			}
		}
	}
// -------------------------------------------------------------------------------
// ---  открыть окно с изображением
	var wrImageWindow;
	function rOpenImageWindow(src,width,height){ 
		wWidth = (width)?width+4:470;
		wHeight = (height)?height+4:470;
		wrImageWindow=window.open("","blankImageWindow", 'status=no,scrollbars=no,resizable=yes,width='+(wWidth)+',height='+(wHeight)+'');
			wrImageWindow.document.write("<html><head><title>Изображение</title>");			
			wrImageWindow.document.write("<style>.visible{display:block;} .hide{display:none;} div,a{font:10px verdana,tahoma,sans-serif;text-decoration:none;}</style>");


			wrImageWindow.document.write("<script>function printshow(over){document.getElementById('menu').setAttribute(classFix,(over)?'visible':'hide');}</script>");


			wrImageWindow.document.write("<body class=popupWnd topmargin=0 marginheight=0 leftmargin=0 marginwidth=0 bgcolor=white text=black link=black alink=black vlink=black>");

			wrImageWindow.document.write("<script>var classFix = (document.body && document.body.getAttribute('className'))?'className':'class';</script>");

			wrImageWindow.document.write("<table border=0 cellspacing=0 cellpadding=0 width=100% height=100%><tr><td align=center onmouseover='printshow(true);' onmouseout='printshow(false);'>");
			wrImageWindow.document.write("<table border=0 cellspacing=1 cellpadding=1 bgcolor=#333333><tr><td bgcolor=white>");
			wrImageWindow.document.write("<div id='menu' class='hide' style='position:absolute;z-index:1;background:white;top:2px;left:2px;padding:5px;border:#333333 1px solid;'>");
				wrImageWindow.document.write("[<a href='javascript:;' onclick='printshow(false);self.print();' title='распечатать'>распечатать</a>] &nbsp;");
				wrImageWindow.document.write("[<a href='javascript:;' onclick='self.close();' title='закрыть окно'>закрыть</a>]");
			wrImageWindow.document.write("</div>");
			wrImageWindow.document.write("<img src='"+src+"' width='"+width+"' height='"+height+"' border=0>");
			wrImageWindow.document.write("</td></tr></table>");
			wrImageWindow.document.write("</td></tr></table>");
			wrImageWindow.document.write("</body></html>");
			wrImageWindow.document.close();
		wrImageWindow.resizeTo(wWidth+30,wHeight+40);
		wrImageWindow.focus();
	}
// -------------------------------------------------------------------------------
// ---   преобразовать RGB то Hex
	function color(r,g,b){
		return toHex((r<<16)+(g<<8)+(b<<0));
	}
// ---  преобразовать число (0 - 16) к шеснадцатеричному виду 
	function enHex(aDigit){ 
		return("0123456789ABCDEF".substring(aDigit, aDigit+1)); 
	}
// --- преобразовать число к шеснадцатеричному виду
	function toHex(n){
		return (enHex((0xf00000 & n) >> 20) +
						enHex((0x0f0000 & n) >> 16) +
						enHex((0x00f000 & n) >> 12) +
						enHex((0x000f00 & n) >>  8) +
						enHex((0x0000f0 & n) >>  4) +
						enHex((0x00000f & n) >>  0));
	}
// ---
	function colorToRGB(color){
		return ((color & 0x0000ff) << 16)+
		  			(color & 0x00ff00)+
					 ((color & 0xff0000) >> 16);
	} 
// -------------------------------------------------------------------------------
// --- класс позиция
	function rPosition(){
		this.row = -1;
		this.col = -1;
		this.copy = rPositionCopy;
	}
// ---
	function rPositionCopy(mPos){
		this.row = mPos.row;
		this.col = mPos.col;
	}
// -------------------------------------------------------------------------------
// --- класс преобразования таблицы в матрицу
	function rMatrix(elmTABLE){
		this.matrix = new Array();
		this.table	= elmTABLE;
	// --- иницализация масива
		var yCell=0;
		var cellIndex=0;
		for(var i=0; i<this.table.rows.length; i++) this.matrix[i]=new Array();
		for(var i=0; i<this.table.rows.length; i++){
			var xCell=0;
			for(var j=0; j<this.table.rows[i].cells.length; j++){
				while(this.matrix[yCell][xCell]>=0) xCell++;
				for(var k=0;k<this.table.rows[i].cells[j].colSpan;k++){
					for(var l=0;l<this.table.rows[i].cells[j].rowSpan;l++){
						this.matrix[yCell+l][xCell]=cellIndex;
					} xCell++;
				} cellIndex++;
			} yCell++;
		}
	// --- 
		this.mrows	= this.matrix.length;
		this.mcols	= this.matrix[0].length;
	// --- 
		this.index = rMatrixGetCellIndex;
		this.position = rMatrixGetPosition;
		this.nearest	= rMatrixGetNearest;
	}
// -------------------------------------------------------------------------------
// --- найти ближаший елемент в матрице
	function rMatrixGetNearest(mPos,direction){	
		var mNearPos = new rPosition(); mNearPos.copy(mPos);
			 	 if(direction == 'left')	while(mNearPos.col>=0					&& this.index(mNearPos)==this.index(mPos)) mNearPos.col--;
		else if(direction == 'right')	while(mNearPos.col<this.mcols && this.index(mNearPos)==this.index(mPos)) mNearPos.col++;
		else if(direction == 'top')		while(mNearPos.row>=0					&& this.index(mNearPos)==this.index(mPos)) mNearPos.row--;
		else													while(mNearPos.row<this.mrows	&& this.index(mNearPos)==this.index(mPos)) mNearPos.row++;
		var nearIndex = this.index(mNearPos);
		if(nearIndex!=-1 && nearIndex!=this.index(mPos)){
			mNearPos = this.position(this.table.cells[nearIndex]);
			//alert(this.index(mPos)+" > "+mPos.row+","+mPos.col+"\n"+this.index(mNearPos)+" > "+mNearPos.row+","+ mNearPos.col);
			return mNearPos;
		}else{
			return false;
		}
	}
// -------------------------------------------------------------------------------
// --- значение елемента в матрице по его позиции
	function rMatrixGetCellIndex(mPos){ 
		return (mPos.row<this.mrows && mPos.col<this.mcols)?this.matrix[mPos.row][mPos.col]:-1;	
	}
// -------------------------------------------------------------------------------
// --- позиция елемента в матрице
	function rMatrixGetPosition(elmTD){			
		var mPos = new rPosition();
		for(var i=0;i<this.matrix.length;i++)
			for(var j=0;j<this.matrix[i].length;j++){
				if(this.table.cells[this.matrix[i][j]]==elmTD){
					mPos.row =i;
					mPos.col =j;
			    return mPos;
				}
			}
    return mPos;
	} 

//========================================================================================================//
// ---  функции для флеш	- модуль AC_RunActiveContent.js
//========================================================================================================//
 //v1.7
// Flash Player Version Detection
// Detect Client Browser type
// Copyright 2005-2007 Adobe Systems Incorporated.  All rights reserved.
var AC_FL_RunContent = 0;
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '';
  if (isIE && isWin && !isOpera)
  {
    str += '<object ';
    for (var i in objAttrs)
    {
      str += i + '="' + objAttrs[i] + '" ';
    }
    str += '>';
    for (var i in params)
    {
      str += '<param name="' + i + '" value="' + params[i] + '" /> ';
    }
    str += '</object>';
  }
  else
  {
    str += '<embed ';
    for (var i in embedAttrs)
    {
      str += i + '="' + embedAttrs[i] + '" ';
    }
    str += '> </embed>';
  }
  document.write(str);
}


// -------------------------------------------------------------------------------
// ---  Показать флеш
function rGetHtmlForFlash(movie,name,width,height,bgcolor,flashVars){
	var filename = movie.split("/");
	filename = filename[filename.length-1];
	var str='<div class=reFlash flashVars="'+flashVars+'" height="'+height+'" width="'+width+'" bgcolor="'+bgcolor+'" movie="'+movie+'" name="'+name+'" style="width:'+width+'px;height:'+height+'px;">'+filename+'</div>';
	//document.write(str); 
	return str; 
}
// ---  Показать video
function rGetHtmlForVideo(movie,name,width,height,bgcolor,flashVars){
	var str='<img class=reVideo flashVars="'+flashVars+'" height="'+height+'" width="'+width+'" bgcolor="'+bgcolor+'" movie="'+movie+'" name="'+name+'" style="width:'+width+'px;height:'+height+'px;" src="'+flashVars+'.jpg" border="0" alt="">';
	//document.write(str); 
	return str; 
}
// ---  Показать флеш
function rGetHtmlForShowFlash(movie,name,width,height,bgcolor,flashVars){
	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContent(
			"codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0",
			"width", width,
			"height", height,
			"src", movie,
			"quality", "high",
			"pluginspage", "http://www.macromedia.com/go/getflashplayer",
			"align", "middle",
			"play", "true",
			"loop", "true",
			"scale", "showall",
			"wmode", "transparent",
			"devicefont", "false",
			"id", name,
			"bgcolor", bgcolor,
			"name", name,
			"menu", "true",
			"allowFullScreen", "false",
			"allowScriptAccess","sameDomain",
			"movie", movie,
			"flashVars", flashVars,
			"salign", ""
			);
	}
}
// ---  Показать флеш
function rGetHtmlForFlashShow(path,width,height){ 
	var str='<object class=reFlash width="'+width+'" id=reFlashObject height="'+height+'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" align="middle" name="reFlashObject">'; 
	str+='<param name="allowScriptAccess" value="sameDomain" />'; 
	str+='<param name="movie" value="'+path+'" />'; 
	str+='<param name="bgcolor" value="#ffffff" />'; 
	str+='<param name="flashVars" value="" />'; 
	str+='<param name="quality" value="high" />'; 
	str+='<param name="wmode" value="transparent" />'; 
	str+='<embed src="'+path+'" quality="high" bgcolor="#ffffff" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+width+'" height="'+height+'" wmode="transparent" />'; 
	str+='</embed></object>'; 
	//document.write(str); 
	return str; 
}
// ---  Показать флеш
function rGetHtmlForVideoShow(path,width,height){ 
	var str='<img class=reVideo id=reVideoObject bgcolor="#ffffff" width="'+width+'" id=reFlashObject height="'+height+'" videoVars="'+path+'" movie="./scripts/richedit/img/re_player.swf" name="reVideoObject" src="'+path+'.jpg" border="0" alt="">'; 
	//document.write(str); 
	return str; 
}
//========================================================================================================//
