var in_drag = false;
var in_drag_counter = 0;
function init(){
var i, coll = document.getElementsByTagName( "div" );
for( i = 0; i < coll.length; i ++ ){
if( coll[ i ].getAttribute("drag") == 1 ){
coll[ i ].onmousedown = function( evnt ){
evnt = evnt || event;
in_drag = [ this, evnt.offsetX || evnt.layerX, evnt.offsetY || evnt.layerY ];
this.style.zIndex = ++in_drag_counter;
}
}
}
document.onmouseup = function(){
in_drag = false;
}
document.onmousemove = function( evnt ){
if( ! in_drag ){
return false;
}
evnt = evnt || event;
in_drag[ 0 ].style.left = ( document.body.scrollLeft + ( evnt.x || evnt.clientX ) - in_drag[ 1 ] ) + "px";
in_drag[ 0 ].style.top  = ( document.body.scrollTop  + ( evnt.y || evnt.clientY ) - in_drag[ 2 ] ) + "px";
}
}


function update(id_page,id_div,name_page) {
$.ajax({
type: 'GET',
url: '/template/ajax/get_type.php?id_page='+id_page+'&id_type='+id_div+'&name_page='+name_page+'',
timeout: 100000,
success: function(data) {
$("#some_div_"+id_div+"").html(data);
//window.setTimeout(update, 1000);
}, 
error: function (XMLHttpRequest, textStatus, errorThrown) {
//window.setTimeout(update, 10000);
}
})}$(document).ready(update);

function doLoad(force,id_div,save) {
var query = '' + document.getElementById(''+id_div+'_query').value;
var req = new JsHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) {
document.getElementById(''+id_div+'_result').innerHTML = 
''+req.responseJS.q +'';
document.getElementById('debug').innerHTML = 
req.responseText;
}
}
req.caching = true;
req.open('POST', '/template/ajax/post_type.php?id_type='+id_div+'&save='+save+'', true);
req.send({ q: query });
if (save == 'yes'){
document.body.removeChild(document.getElementById(id_div));
}
}
var timeoutH = null;
function doLoadUpPhp() {
if (timeoutH) clearTimeout(timeoutH);
timeoutH = setTimeout(doLoad, 1000);
}
  
function send(id_page,name_type,id_div,name_page)
{
update(id_page,id_div,name_page);
if(document.getElementById(id_div) != null) {
close_div = document.getElementById(id_div);
close_div.parentNode.removeChild(close_div);
} else {
var div = document.createElement("div");
div.id = id_div;
//div.setAttribute('drag',1);
div.setAttribute('class', 'new');
div.style.zIndex=11111;
div.style.position='absolute';
div.style.display='block'; 
div.style.background='#ffffff'; 
div.style.top='10%';
div.style.left='20%';
div.innerHTML="<div><div id='new_name'>Редактировать <a href='/a.php?id_page="+id_page+"#type_"+id_div+"'>"+name_type+"</a></div><div id='close'><a onclick=\"hide('"+id_div+"','"+name_type+"')\">свернуть</a> <a onclick=\"dell('"+id_div+"')\">закрыть</a></div></div><form onsubmit=\"return false\"><div class='get' id='some_div_"+id_div+"'></div><div id='save'><a onclick=\"doLoad(true,'"+id_div+"','yes')\" >Сохранить</a></div></form>";
document.body.appendChild(div);
//init();
}
}



function dell(id_div)
{
document.body.removeChild(document.getElementById(id_div));
shortcut("Esc",function() {
document.body.removeChild(document.getElementById(id_div));
});
}

function hide(id_div,name_type)
{
var hide = document.getElementById(id_div);
hide.style.display='none';
var open_admin_bar = document.getElementById('admin_bar');
open_admin_bar.style.display='block';
var a = document.createElement("b");
a.innerHTML="<a id=\""+id_div+"_hide\" onclick=\"nohide('"+id_div+"');\">"+name_type+"</a>";
document.getElementById('hide_window').appendChild(a);
}

function nohide(id_div)
{
var nohide = document.getElementById(id_div);
nohide.style.display='block';
hidemenu = document.getElementById(id_div+"_hide");
hidemenu.parentNode.removeChild(hidemenu);
}
