/* start functions update content */

function updateElements(linkname)
{
	var currentHeight = checkHeight();
	var motionBlock = document.getElementById('revealing_content_block').className;
	if (currentHeight == 1)	{
		updateGo(linkname);
	}
	else if (motionBlock == 'static') {
		opLost(0, linkname);
	}
}

function updateGo(linkname)
{
  var req = new JsHttpRequest();
  req.open(null, "index.php?reference=" + linkname, false);
	var c = document.getElementById("content");
  //var b = document.getElementById("buttons");
	//var lm = document.getElementById("left_menu");
  req.onreadystatechange = function()  // многократное вхождение!?
	{
    if ( req.readyState == 4 ) /*req.readyState > 1 при такой конструкции вхождение происходит многократное*/
		{
			c.innerHTML = req.responseJS.content;
			//b.innerHTML = req.responseJS.buttons;
			//lm.innerHTML = req.responseJS.topMenu;
			document.title = req.responseJS.title;
			opGo(); //-> поехали!
    }
  }
	req.send( { reference: linkname } );
}

function opGo(flag)
{
	var step = 0;
	document.getElementById('loading_screen').className = '';
	document.getElementById('border_for_nav').style.visibility = 'visible';
	var motionBlock = document.getElementById('revealing_content_block');
	motionBlock.className = 'motion';
	var neededHeight = document.getElementById('revealing_inner').offsetHeight;
	var currentHeight = checkHeight();
	var move_a = setInterval(function() {
		if ( currentHeight < neededHeight  ) {
			step = currentHeight/10;
			if (step < 20){ step = 20; }
			currentHeight+=step;
			if (currentHeight > neededHeight){currentHeight = neededHeight;}
			setHeight(currentHeight);
		}
		else {
			clearInterval(move_a);
			motionBlock.className = 'static';
			if (flag == 1) {
				document.getElementById("roll_nav").innerHTML = '<a href="javascript: opLost(1);" title="свернуть"><img src="../img/op_lost_4.gif" width="11" height="11" alt="свернуть"></a>';
			}
			else {
				document.getElementById('loading_screen').className = '';
				document.getElementById("roll_nav").innerHTML = '<a  href="javascript: opLost(1);" title="свернуть" ><img src="../img/op_lost_4.gif" width="11" height="11" alt="свернуть"></a>';
			}
		}
	}, (10));

	//return true // !? вызывает ошибку при разворачиваниии [+]
}

function opLost(flag, linkname)
{
	var step = 0;
	var motionBlock = document.getElementById('revealing_content_block');
	motionBlock.className = 'motion';
	var currentHeight = checkHeight();
	if (document.addEventListener) { windowScroll(); }

	var move_b = setInterval(function() {
		if ( currentHeight > 10 ) {
			step = currentHeight/10;
			if (step < 10){ step = 10; }
			currentHeight-=step;
			setHeight(currentHeight);
		}
		else {
			clearInterval(move_b);
			motionBlock.style.height = 1+'px';
			motionBlock.className = 'static';
			if (flag == 1) {
				document.getElementById("roll_nav").innerHTML = '<a  href="javascript: opGo(1);" title="развернуть" ><img src="../img/op_go_4.gif" width="11" height="11" alt="развернуть"></a>';
			}
			else {
				document.getElementById('loading_screen').className = 'load';
				document.getElementById('border_for_nav').style.visibility = 'hidden';
				updateElements(linkname);
			}
		}

	}, (10));
}

function checkHeight() {
 var heightY = document.getElementById('revealing_content_block').style.height;
 return(parseInt(heightY));
}

function setHeight(currentHeight) {
	document.getElementById('revealing_content_block').style.height = currentHeight+'px';
}

function windowScroll() {
	var y = pageYOffset;
	parseInt(y);
	for (i=y; i>=0; i=i-20)
	{
		if (i < 20){ i = 0; }
		self.scrollTo(0,i); //parent.scroll(1,i) || window.scroll(0,i);
	}
	return true;
}

/* end functions update content ------------------------------------------------------- */

function actionSet(msg) {
	switch (msg) {
		case '1':
			document.login.action = 'http://editor.59p.ru/';
			break;
		case '2':
			document.login.action = 'http://editor.byte-kuzbass.ru/';
			break;
		case '3':
			document.login.action = 'http://editor.inmarket.ru/';
			break;
		case '4':
			document.login.action = 'http://editor.ur66.ru/';
			break;
		case '5':
			document.login.action = 'http://editor.ural66.ru/';
			break;
		default:
			document.login.action = 'http://editor.ural66.ru/';
			break;
	}
}

function addBookmark(url, title)
{
 if (!url) url = location.href;
 if (!title) title = document.title;

 //Gecko
 if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
 //IE4+
 else if (typeof window.external == "object") window.external.AddFavorite(url, title);
 //Opera7+
 else if (window.opera && document.createElement)
 {
   var a = document.createElement('A');
   if (!a) return false; //IF Opera 6
   a.setAttribute('rel','sidebar');
   a.setAttribute('href',url);
   a.setAttribute('title',title);
   a.click();
 }
 else return false;

 return true;
}

function showChildDynmicMenu(menuitem) {
  var childmenu = menuitem.getElementsByTagName("ul")[0];
  if (childmenu) {
	childmenu.style.visibility = "visible";
  }
}

function hideChildDynmicMenu(menuitem) {
  var childmenu = menuitem.getElementsByTagName("ul")[0];
  if (childmenu) {
	childmenu.style.visibility = "hidden";
  }
}

function setInputValue(formName, inputName, value, minus)
{
    var inputValue = eval("document." + formName + ".elements[inputName].value");
    if (minus) {
        inputValue = eval(inputValue) - eval(value);
        if (inputValue < 0) inputValue = 0 ;
    } else {
        inputValue = eval(inputValue) + eval(value);
    }
    eval("document." + formName + ".elements[inputName].value = inputValue;");
}

function getSubmit(form)
{
    eval("document." + form + ".submit()");
}

function replaceImgDiv(obj, path, imgName) {
    var i = obj.selectedIndex;
    var name = obj.options[i].value;
    var e = document.getElementById('dinamicImg');
    e.innerHTML = '<img src="'+ path + name +'/' + imgName + '" width="300" height="200" alt="' + name +'" />';
    return true;
}

function imgOver(obj, name, txt) {
    if (name) {
        obj.className = name;
    } else {
        obj.className = '';
    }
    var e = document.getElementById('dImageText');
    if (txt) {
        e.innerHTML = txt;
    } else {
        e.innerHTML = '&#160;';
    }
    return true;
}

function Element(name, rule, error) {
    this.name = name
    this.rule = rule
    this.error = error
}

function checkForm(form) {
    desc = eval(form.name);
    error = -1;
    for(var i = 0; i < desc.length; i++) {
        elementName =  form.name + '.' + desc[i].name
        if(desc[i].rule == 1) {
            if (form.elements[elementName].value == '') {
                errorIn = elementName
                error = i
            }
        } else {
            if(desc[i].rule != 0 && !desc[i].rule.test(form.elements[elementName].value)) {
                errorIn = elementName
                error = i
            }
        }
        if(error != -1) {
            alert(desc[error].error)
            form.elements[errorIn].focus()
            return false
        }
    }
    return true
}

function openWin (href,x,y,Wtitle) {
    msgWin=window.open ("","_blank","width="+x+",height="+y+"toolbars=no,status=no,location=no");
    with (msgWin) {
        document.write ('<ht'+'ml><he'+'ad><titl'+'e>'+Wtitle+'<'+'/titl'+'e><scrip'+'t>'+'window.moveTo(50,50);<'+'/script></'+'head><bo'+'dy topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0>');
        document.write ('<img src='+href+' title="'+Wtitle+'" vspace=0 hspace=0>');
        document.write ('</bo'+'dy></ht'+'ml>');
    }
}
