// JavaScript Document

function setFilename(obj, id) {
	var val = obj.value;
	
	document.getElementById(id).innerHTML = val;
}

var counter = 1;

function addMore(path) {
	var div = document.createElement("div");
	
	
	var temp = '<div style="position:relative; width:200px;">';
    temp += '<input type="file" style="z-index:100; opacity:0.0; filter:alpha(opacity=0); position:relative; cursor:pointer;" onchange="setFilename(this, \'file_name_'+counter+'\')" name="images[]"/>';
    temp += '<div style="position:absolute; top:0px; z-index:50; left:0px;">';
    temp += '<div style="float:left; overflow:hidden;" class="fake_file" id="file_name_'+counter+'">';
    temp += '</div>';
    temp += '<div style="float:right; margin-left:5px; cursor:pointer;">';
    temp += '<img src="'+path+'images/admin/image.png" width="22" height="22"/>';
    temp += '</div>';
    temp += '<div style="clear:both; height:0px;"></div>';
    temp += '</div>';
	temp += '</div>';
	
	div.innerHTML = temp;
	document.getElementById("file_inputs").appendChild(div);
	
	counter++;
	
}

var drag = false;
var startX = 0;
var startY = 0;
function startDrag() {
	drag = true;
	startX = X;
	startY = Y;
	
}

function dragImage(obj, image, id) {

	if (drag == true) {
		
		var maxi = document.getElementById(image).offsetWidth-obj.offsetWidth;
		var maxi_top  = document.getElementById(image).offsetHeight-obj.offsetHeight;
		
		moveX = startX-X;
		moveY = startY-Y;
		
		var top = parseInt(obj.offsetTop);		
		var left = parseInt(obj.offsetLeft);
		
		
		document.getElementById("corY_"+id).value = top;	
		document.getElementById("corX_"+id).value = left;
		
		if (top >= 0 & top <= maxi_top) {
			if (((-1)*moveY) >= 0 & ((-1)*moveY) <= maxi_top) {
				obj.style.top = ((-1)*moveY)+"px";
			}
		}
		if (left >= 0 & left <= maxi) {
			if (((-1)*moveX) >= 0 & ((-1)*moveX) <= maxi) {
				obj.style.left = ((-1)*moveX)+"px";
			}
		}
		
		if (top < 0) {
			obj.style.top = "0px";
		}
		if (top >  maxi_top) {
			obj.style.top = "0px";
		}
		
		if (left < 0) {
			obj.style.left = "0pxpx";
		}
		if (left >  maxi) {
			obj.style.left = "0px";
		}
		
		
	}
}

function stopDrag() {
	drag = false;	
	startX = 0;
	startY = 0;
}


function setThumbHoverSize(id, x, y) {


	document.getElementById("thumb_hover_"+id).style.top = y+"px";
	document.getElementById("thumb_hover_"+id).style.left = x+"px";
}


function saveImage(id, path) {
	
	var req = createXMLHttpRequest();
			
	req.onreadystatechange=function()
	{
		if(req.readyState == 4){  
				var text = req.responseText;
				alert (text);
		}
	}

	var name = document.getElementById("image_name_"+id).value;
	var X = document.getElementById("corX_"+id).value;
	var Y = document.getElementById("corY_"+id).value;
	
		
	req.open('GET', path+'save_image.php?X='+X+'&Y='+Y+'&name='+name+'&id='+id); 
	req.send(null);	
}
