var counters = 0;
var group;
var woof;
var coordinates = ToolMan.coordinates();
var drag = ToolMan.drag();
var magneto = ToolMan.iebitch();
var win, dhxWins;
var mbWinL = false;
var mbWinC = false;

function make_element(x, y, id, w, h) {
	id += '_id_' + counters++;
	var olala = ToolMan.coordinates().topLeftOffset(document.getElementById("dragBox"));
	var drag = ToolMan.drag();

	var imgnode = document.createElement('img');
	imgnode.setAttribute("style", "position: absolute; opacity: 1; z-index: 15; top: " + (y - olala.y) +"px; left: " + (x - olala.x) + "px; cursor: pointer; padding: 0; margin: 0;");
	imgnode.setAttribute("id", id);
	imgnode.setAttribute('src', 'desinger/module/m25/surfaces/additional/cursor.gif');
	imgnode.setAttribute('alt', 'img');

	document.getElementById("dragBox").appendChild(imgnode);
	
	drag.createSimpleGroup(document.getElementById(id))
}

function setWPORT(disable) {
	if (disable) {
 		dhxWins.setViewport(0, 0, 2, 2, document.getElementById('page'));
	} else {
		woff = ToolMan.coordinates().bottomRightOffset(document.getElementById("dragBox"));
 		dhxWins.setViewport(80, woff.y + 140, 250, 195, document.getElementById('page'));
	}
}

function m25SetDnd(){
	setDndFields();
	
	var j, i;
	
	dhxWins = new dhtmlXWindows();
 	dhxWins.setImagePath('desinger/js/dhtmlxWindows/imgs');
 	dhxWins.enableAutoViewport(false);
	
	setWPORT(false);
		
 	win = dhxWins.createWindow('mbForm', 0, 0, 228, 198);
 	win.setText('Multibox');
	win.hide();
   	win.clearIcon();
    win.denyResize();
    win.denyMove();
    win.denyPark();
    win.button("close").disable();
	
 	//win.attachObject('mbWindow');
	
	j = document.getElementById("mbcount").value;
	
	for (i = 0; i < j; i++)
		drag.createSimpleGroup(document.getElementById("multiboxi_" + i), null, true);
		
	
}

function setDndFields() {
	var id = document.getElementById('shelf_id');

	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';

	var postvars = 'action=getFields';
    
	if (!send_xmlhttprequest(setDndFieldsService, 'POST', '/m25_handle_dnd.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function setDndFieldsService(xmlhttp) {
	var obj, nodes, i, j, x, y, index, filled;
	
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.responseXML != null) {
			obj = xmlhttp.responseXML.getElementsByTagName('dropField');
			
			for (i = 0; i < obj.length; i++) {
				nodes = obj[i].childNodes;

				for (j = 0; j < nodes.length; j++){
					if (nodes[j].nodeName == 'x')
						x = parseInt(nodes[j].firstChild.data);
						
					if (nodes[j].nodeName == 'y')
						y = parseInt(nodes[j].firstChild.data);
						
					if (nodes[j].nodeName == 'index')
						index = nodes[j].firstChild.data;
						
					if (nodes[j].nodeName == 'filled')
						filled = nodes[j].firstChild.data;
				}

				magneto.addElement(x, y + 20, index); 
				
				if (filled == 'yes')
					make_element(x, y + 40, index);				
			}
		}
	}
}

function setDndButtons(stdmenu) {
	var id = document.getElementById('shelf_id');

	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';

	var postvars = 'action=getFields';
    
    if (!ignore) {
		if (!send_xmlhttprequest(setDndButtonsService, 'POST', '/m25_addrmbtn.php?id=' + id.value, postvars, headers)) {
			alert('Error in servise');
		}	
	}
}

function setDndButtonsService(xmlhttp) {
    var obj, width, hght, height, nodes, i, j, x, index, type, target, node;
    
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.responseXML != null) {
            width = xmlhttp.responseXML.getElementsByTagName('width');
            height = xmlhttp.responseXML.getElementsByTagName('height');
            
            target = document.getElementById('btnBox');
            target.innerHTML = '';
            
            hght = parseInt(height[0].firstChild.data);
            
            target.style.width = width[0].firstChild.data + 'px';

    /// Pridat odebrat buttonny            
            target2 = document.getElementById('btnBoxTop');
            target2.innerHTML = '';
            
            target2.style.width = target.style.width;
            
            node = document.createElement('div');
            node.setAttribute('onclick', 'add(\''+ T_COLUMN +'\'); magnetoReset = true;');            
            node.className = "prid_sl";
            node.style.left = (width[0].firstChild.data) + 'px';
            node.style.top = (hght) + "px";
            node.title = T_ADDCOLUMN;
            node.innerHTML = T_ADDCOLUMN;

            target2.appendChild(node);
            
            node = document.createElement('div');
            node.setAttribute('onclick', 'remove(); magnetoReset = true;');            
            node.className = "odebr_sl";
            node.style.left = (width[0].firstChild.data) + 'px';
            node.style.top = (hght + 22) + "px";
            node.title = T_RMCOLUMN;
            node.innerHTML = T_RMCOLUMN;
            
            target2.appendChild(node);
                        
    
    ////***********************
    
            obj = xmlhttp.responseXML.getElementsByTagName('button');
            
            for (i = 0; i < obj.length; i++) {
                nodes = obj[i].childNodes;

                for (j = 0; j < nodes.length; j++){
                    if (nodes[j].nodeName == 'x')
                        x = parseInt(nodes[j].firstChild.data);
                        
                    if (nodes[j].nodeName == 'index')
                        index = nodes[j].firstChild.data;
                        
                    if (nodes[j].nodeName == 'type')
                        type = nodes[j].firstChild.data;
                }

                node = document.createElement('img');
                
                if (type == 'add') {
                    node.setAttribute('src', '/images/nav-plus.gif');
                    node.setAttribute('onclick', 'change_shelf_count_ar(' + index +', \'add\'); magnetoReset = true;');
                    node.title = T_ADDSHELF;
                } else {
                    node.setAttribute('src', '/images/nav-minus.gif');
                    node.setAttribute('onclick', 'change_shelf_count_ar(' + index +', \'rm\'); magnetoReset = true;');
                    node.title = T_RMSHELF;
                }
                
                node.style.cursor = 'pointer';
                node.style.position = 'absolute';
                node.style.left = x + 'px';

                target.appendChild(node);
            }
        }
    }
}