/* Highlight steps script by hovering list */
/* Script by Hesido.com                    */
/* All rights reserved                     */

function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
} else var expires = "";
//alert (name+"="+value+expires+"; path=/");
document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
//alert (name);
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
/*
if (document.getElementById && document.getElementsByTagName) {
if (window.addEventListener) window.addEventListener('load', setUpScripts, false);
else if (window.attachEvent) window.attachEvent('onload', setUpScripts);
}
function attachEv (elemName, eventName, functionName) {
//alert (elemName.attachEvent);
if (document.getElementById && document.getElementsByTagName) {
if (elemName.addEventListener) elemName.addEventListener(eventName, functionName, false);
//else if (elemName.attachEvent) elemName.attachEvent(eventName, functionName);
}
}

*/

function setGlobalOnLoad(f) {
	var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null
	if (root){
		if(root.addEventListener) root.addEventListener("domcontentready", f, false)
		if(root.addEventListener) root.addEventListener("load", f, false)
		else if(root.attachEvent) root.attachEvent("onload", f)
		}
	else {
		if(typeof window.onload == 'function') {
			var existing = window.onload
			window.onload = function() {
				existing()
				f()
				}
			}
		else {
    	window.onload = f
      }
		}
	}

var addHandler = (function(){
       if (document.addEventListener){
               return function(obj, type, fn){
                       obj.addEventListener(type, fn, false);
               }
       } else if (document.attachEvent) {
       	return function (obj, type, fn) {
					obj["e"+type+fn] = fn;
					obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
					obj.attachEvent( "on"+type, obj[type+fn] );
       		}
       } else {
       	return function (object, event, handler) {
    var handlersProp = '_handlerStack_' + event;
    var eventProp = 'on' + event;
    if (typeof object[handlersProp] == 'undefined')
    {
      object[handlersProp] = [];
      if (typeof object[eventProp] != 'undefined')
        object[handlersProp].push(object[eventProp]);
      object[eventProp] = function(e)
      {
        var ret = true;
        for (var i = 0; ret != false && i < object[handlersProp].length; i++)
          ret = object[handlersProp][i](e);
        return ret;
      }
    }
    object[handlersProp].push(handler);
       		}
       	}
})();

var removeHandler = (function() {
	if ( document.addEventListener ) {
		return function (object, event, handler) {
	    object.removeEventListener(event, handler, false);
			}
		} else if (document.detachEvent) {
		return function (object, event, handler) {
					object.detachEvent( "on"+event, object[event+handler] );
					object["e"+event+handler] = null;
					object[event+handler] = null;
			}
		} else {
			return function (object, event, handler) {
    var handlersProp = '_handlerStack_' + event;
    if (typeof object[handlersProp] != 'undefined')
    {
      for (var i = 0; i < object[handlersProp].length; i++)
      {
        if (object[handlersProp][i] == handler)
        {
          object[handlersProp].splice(i, 1);
          return;
        }
      }
    }

				}
			}
	}
)();
function removeHandler(object, event, handler)
{
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else
  {
    var handlersProp = '_handlerStack_' + event;
    if (typeof object[handlersProp] != 'undefined')
    {
      for (var i = 0; i < object[handlersProp].length; i++)
      {
        if (object[handlersProp][i] == handler)
        {
          object[handlersProp].splice(i, 1);
          return;
        }
      }
    }
  }
}

setGlobalOnLoad(setUpScripts);
function setUpScripts (){
if (window.addEventListener) {
//	initStepHighlight();
	}
externalLinks()
replaceAlert();

if ( typeof (gRun) != 'undefined' ) {
gRun();
}
if ( typeof (sortListSetup) != 'undefined' ) {
sortListSetup();
}
if ( typeof (parseLinks) !='undefined') {
	parseLinks();
	}
//	applyDropDownF('early','menuI','',true,1,-10,0,'hoveredspan',450,150);
//applyDropDownF('early','menuI','',true,1,-10,0,'hoveredspan',450,150,1,20,10);
}
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i < anchors.length; i++) {
if (anchors[i].getAttribute("href") &&
anchors[i].getAttribute("rel") == "external") {
anchors[i].target = "_blank";
}
}
}


function initStepHighlight() {
	var contElements = document.getElementById("menuI").getElementsByTagName("a");
	for(var i=0; i<contElements.length; i++) {
		if (contElements[i].href&&contElements[i].href!=''){
			addHandler(contElements[i], 'mouseover',hiliteStep);
			addHandler(contElements[i],'click', function(){		doBGFade(this,[204,204,204],[204,153,153],[255,0,255],10,30,4)});
			addHandler(contElements[i], 'mouseout', hilite1);//function() {doBGFade(this,[204,153,153],[204,204,204],[255,0,255],10,30,4)});
			}
		else {
		  contElements[i].style.backgroundColor = "rgb(184,133,133)";
			}
		}
	function hilite1 () {
		doBGFade(this,[204,153,153],[204,204,204],'transparent',5,30,4);
	}
	function hiliteStep() {
		doBGFade(this,[204,204,204],[184,133,133],[255,0,255],5,30,4)
  	}
	}
//FleXiFade Lite By Hesido
function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr){
//FleXiFade Lite v1.0
//BG Fader by www.hesido.com
if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
var actStep = 0;
elem.bgFadeInt = window.setInterval(
	function() {
		elem.style.backgroundColor = "rgb("+
			easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
			easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
			easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")";
		actStep++;
		if (actStep > steps) {
		elem.style.backgroundColor = finalColor;
		window.clearInterval(elem.bgFadeInt);
		}
	}
	,intervals)
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr){
//Generic Animation Step Value Generator By www.hesido.com
var delta = maxValue - minValue;
var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
return Math.ceil(stepp)
}

function sortListSetup ( ul, ulNum ) {
	if ( !ul ) {
		ul = document.getElementsByTagName('ul');
		for ( i=0, j = ul.length ; i<j; i++) {
			if ( ul[i].className=='sortable-list' ) {
				sortListSetup(ul[i], i );
				}
			}
		return ( true );
		}
  inpP = document.createElement('p');
  inpP.appendChild(document.createTextNode('Введите часть названия для поиска '));
  inp = document.createElement('input');
	inp.type = 'text';
	inpP.appendChild(inp);
/*
	inpA = document.createElement('a');
	inpA.setAttribute('href','')
	inpA.appendChild(document.createTextNode(' Только новые документы'))
	inpP.appendChild(inpA);
	addHandler(inpA,'click',function(){
	  inp.value='new';
	  inp.keyUp();
	  return ( false );
		})
	*/
	inpNOFOUND = document.createElement('span');
	inpNOFOUND.style.fontWeight = 'bold';
	inpNOFOUND.style.color = 'red';
	inpNOFOUND.style.display = 'none';
	inpNOFOUND.appendChild(document.createTextNode('Нет совпадений.'));
	inpP.appendChild(inpNOFOUND);
	ul.parentNode.insertBefore(inpP,ul);
	var lis = ul.getElementsByTagName('li');
	addHandler(inp,'keyup', function(e) {
		var hasFound = false;
		var ulsFound = [];
		for ( i=0, j=lis.length ; i<j ; i++ ) {
			cP = lis[i].parentNode;
			if ( lis[i].innerText.toLowerCase().indexOf(inp.value.toLowerCase())==-1 ) {
				lis[i].style.display = 'none';
				}
			else {
				hasFound = true;
				lis[i].style.display = '';
				while (cP !=ul) {
					cpId = cP.getAttribute('ulId');
					if ( ulsFound[cpId] ) {
					//	alert (ulsFound);
						break;
						}
					else {
						ulsFound[cpId] = '1';
						if ( inp.value=='' ) {
							if (document.ulsState[cpId]=='1') {
								cP.style.display = 'none';
								setSwitchState(cP.parentNode.firstChild,false);
								}
							else {
								cP.style.display = '';
								setSwitchState(cP.parentNode.firstChild,true);
								}
							}
						else if ( cP.style.display=='none' && cP.parentNode.firstChild.tagName.toLowerCase()== 'img' ) {
							cP.style.display = '';
							setSwitchState(cP.parentNode.firstChild,true);
							}
						cP = cP.parentNode.tagName.toLowerCase()=='li'?cP.parentNode.parentNode:cP.parentNode;
						}
					}
				}
			}
		inpNOFOUND.style.display = hasFound ? 'none' : 'block';
		}

	)
	var uls = ul.getElementsByTagName('ul');
	switcher0 = document.createElement('img');
	//inp = document.createElement('span');
	//inp.type='button';
	switcher0.className='transparent switcher';
	//inp.appendChild(document.createTextNode(' - '));
  var j = uls.length;
  document.ulsState = new Array;
  t = getCookie(document.body.id+'-'+ulNum);
	if ( t ) {
		document.ulsState = t.split('');
		}
	else {
		;
		}
	for ( i=0 ; i<j ; i++) {
		inpN = switcher0.cloneNode(true);
		inpN.id=i;
		addHandler(inpN,'click',function() {
		switcher = this.event?this.event.srcElement : this;
		  if ( uls[switcher.id].style.display=='none' ) {
		  	uls[switcher.id].style.display='';
		  	setSwitchState(switcher, true);
		  	document.ulsState[switcher.id] = '0';
		//  	this.firstChild.nodeValue=' - ';
		  	}
		  else {
		  	uls[switcher.id].style.display = 'none';
		  	setSwitchState(switcher, false)
		  	document.ulsState[switcher.id] = '1';
		  	}
		  setCookie(document.body.id+'-'+ulNum,document.ulsState.join('').toString(),30);

			});

		uls[i].parentNode.insertBefore(inpN,uls[i].parentNode.firstChild);
		uls[i].parentNode.className+=' empty';
		uls[i].setAttribute ? uls[i].setAttribute('ulId',i) : uls[i].ulId = i;
		if ( document.ulsState[i] && document.ulsState[i]=='1' ) {
			uls[i].style.display = 'none';
			setSwitchState(inpN,false)
			}
		else {
			document.ulsState[i] = '0';
			setSwitchState(inpN,true);
			}
		}
//		  setCookie(document.body.id+'--'+ulNum,"23r",30);
	//	setCookie(document.body.id+'-'+ulNum,document.ulsState.join('').toString(),30);
	}
	function setSwitchState ( switcher, isOpen) {
		if ( isOpen) {
			switcher.alt =' - ';
			switcher.src = '/book.png';
			}
		else {
			switcher.alt=' + ';
			switcher.src='/book-close.png';
			}
		}

function parseLinks ( ) {
	links = document.getElementById('content').getElementsByTagName('a');
	for ( var i = 0,  j = links.length ; i<j ; i++ ) {
		liCN = links[i].className;
		if ( liCN.indexOf('needConfirm')>-1 ) {
			if ( liCN.indexOf('delete')>-1 ) {
				addHandler(links[i], 'click', function(e) { if (confirm('Вы действительно хотите удалить страницу?')) { return true}  e.preventDefault(); return false} )

				}
			else {
				addHandler(links[i], 'click', function(e) { if (confirm('Вы уверены?')) { return true}  e.preventDefault(); return false} )
				}
			}
		else if ( liCN.indexOf('showurl')>-1 ) {
			addURLInput(links[i])
			}
		}
	}
function addURLInput ( toA ) {
	if ( !toA.firstChild ) {
		return ;
		}
	var i = document.createElement('input');
	i.type = 'text';
	tmp = toA.getAttribute('href');
	tmp = tmp.slice(tmp.lastIndexOf(location.host)+location.host.length+1);

//	topic =
	if ( tmp.search(/\?topic|&page/)>-1 ) {
		;
		}
	else {
		tmp1 = tmp.split('/');
		topic =tmp1.length==2 ? tmp1[0] : '';
		if ( tmp.indexOf('.html')==-1 ) {
			page = '';
			}
		else {
			page = tmp1.length==2 ? tmp1[1].slice(0,-5) : tmp1[0].slice(0,-5)  ;
			}
		}
	i.setAttribute('value',  '[[#'+topic+':'+page + '|' +toA.firstChild.nodeValue +']]');
	i.style.marginLeft = '1em';
	i.className='comment';
	addHandler(i,'click', function(){this.select()});

	toA.parentNode.insertBefore(i, toA.nextSibling);
	}

// replace alert()
var nativeAlert = window.alert;

function replaceAlert ( ) {
	window.alert = function ( arg ) {


divAlert = document.getElementById('__alert__');
//nativeAlert(divAlert);
	if ( !divAlert ) {
		divAlert = document.createElement('div');
		divAlert.id = '__alert__';
		document.documentElement.appendChild(divAlert);
		with (divAlert.style) {
		  position='fixed';
		  zIndex='999';
		  opacity='.8';
		  bottom= '0';
		  left= '0';
		  height='100px';
		  padding='.4em';
		  background='#f0f0f0';
		  width='300px';
		  font='.75em/1 Tahoma';
		  overflow='auto'
			}
		}
	p = document.createElement('p');
	divAlert.appendChild(p);
	divAlert.lastChild.appendChild(document.createTextNode(arg));
//	nativeAlert(arg);
	}
//	alert (2);
	}
//alert('1')