var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}


function createPost(myId, x,y, myClass, myContent) {

	var closeButton = '<div class="close"><a href="#" onclick="closeDiv(\''+myId+'\'); return true;">close window<\/a><\/div>';
	//var newContent = closeButton+myContent;
	var newContent = myContent;
	
	
	if(xGetElementById(myId)) {
		moveUp(myId);
	} else {
		var theC = xGetElementById('container');
		var newContainer = '<div id="'+myId+'" class="'+myClass+'" style="left: '+x+'px; top: '+y+'px;">';
		
		theC.innerHTML = theC.innerHTML+newContainer+closeButton+newContent+"<\/div>";
	
		myDivs.push(myId);
		moveUp(myId);
		myZ++;
		enableDrag();
	}
}

function getPost(theId) {
	// new
	divname = "div"+theId;

	if(xGetElementById(divname)) {
		xDisplay(divname, "block");
		moveUp(divname);
	} else {
		var url = "./getPost.php?id="+theId;
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = addPost;
		xmlHttp.send(null);
	}
}

function addPost() {
	if (xmlHttp.readyState == 4) {
    	var response = xmlHttp.responseText.split("@@@");
    	theId = response[0];
    	// alert(theId);
    	theCat = response[2];
    	divName = "div"+theId;
    	// x+y dann random
    	var xpos = Math.floor(Math.random()*maxx);
    	var ypos = Math.floor(Math.random()*maxy);
    	// 
    	divClass = "post kat"+theCat;
		createPost(divName, xpos, ypos,divClass,response[1]);
	}
}


function enableDrag() {
	for(i=0;i<myDivs.length;i++) {
		if(xGetElementById(myDivs[i])) xEnableDrag(myDivs[i],moveUp);
	}
}

function moveUp(ele) {
	xZIndex(ele, myZ++);
}

function closeDiv(myId) {
	var theD = xGetElementById(myId);
	if (theD) theD.parentNode.removeChild(theD);
}

function shCat(catId) {
	if(disCat[catId] == "none") disCat[catId] = "block";
	else disCat[catId] = "none";

	for(i=0;i<allkats[catId].length;i++) {
		divname = "div"+allkats[catId][i];
		if(xGetElementById(divname)) xDisplay(divname, disCat[catId]);
		moveUp(divname);
	}
}

/* positionen auslesen und speichern */

function whatsUp() {
	allPos = "";
	
	katSwitch = "";
	
	// kategorie-switches
	for(i=0;i<catNrs.length;i++) {
		nr = catNrs[i];
		state = disCat[nr];
		katSwitch += nr+":"+state+"/";
	}
	
	allPos += katSwitch + "@@@";
	
	// offene divs und positionen
	for(i=0;i<myDivs.length;i++) {
		if(xGetElementById(myDivs[i])) {
			divId = myDivs[i];
			divXPos = xLeft(myDivs[i]);
			divYPos = xTop(myDivs[i]);
			divZ = xZIndex(myDivs[i]);
			allPos += "b"+divId+"a"+divXPos+"a"+divYPos+"a"+divZ;
		}
	}
	return(allPos);
}

function savePos() {
	// anordnung merken, in db speichern, cookie
	// overrides dann is_visible settings
	// kategorie switches auch merken
	// neue aktuell posts trotzdem darstellen
	// eventuell auch ein paar standard-setups speichern
	// die dann zufllig dargestellt werden
	// alert(whatsUp());
	
	
	
	cookieDivs = whatsUp();

	var url = "./setCookie.php?divs="+cookieDivs;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = cookieSet;
	xmlHttp.send(null);
	xDisplay("delPos","block");
}

function cookieSet() {
	if (xmlHttp.readyState == 4) {
    	var response = xmlHttp.responseText;
    	// alert(response);
    	
	}
}


function delPos() {
	var url = "./setCookie.php?del=1";
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = cookieSet;
	xmlHttp.send(null);
	xDisplay("delPos","none");
}


function cookieReset() {
	if (xmlHttp.readyState == 4) {
    	var response = xmlHttp.responseText;
    	//alert(response);
    	parent.location = "index.php";
	}
}

function resetPos() {
	var url = "./setCookie.php?del=1";
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = cookieReset;
	xmlHttp.send(null);
	// xDisplay("delPos","none");
}

// var xChildWindow = null;
var bofWindows = new Array();
var wct = 0;
function bofWinOpen(sUrl,w,h)
{
	var xChildWindow = null;
	
	bofWindows[wct] = null;
	var features = "left=0,top=0,width="+w+",height="+h+",location=0,menubar=0," +
	"resizable=1,scrollbars=1,status=0,toolbar=0";
	/* if (xChildWindow && !xChildWindow.closed) {xChildWindow.location.href = sUrl;}
	else { */
	bofWindows[wct] = window.open(sUrl, "bofWindow"+wct , features);
	// }
	bofWindows[wct].focus();
	wct++;
	
	return false;
}


var myDivs = new Array;

window.onload = function() {
	// getting divs via php, making dragable here
	for(i=0;i<is_visible.length;i++) {
		divname = "div"+is_visible[i];
		myDivs.push(divname);
		myZ++;
		moveUp(divname);
	}
	
	// verlinktes div nach vorne
	if(linkp) moveUp("div"+linkp);
	
	enableDrag();	
	
	// check checkboxes, für browser dies nicht checken
	for(i=0;i<catNrs.length;i++) {
		state = disCat[catNrs[i]]; 
		filter = xGetElementById('catswitch'+catNrs[i]);
		if(state == "none") filter.checked = false;
		else filter.checked = true;
	}
	
	// xAddEventListener('container','mouseup',savePos,false);
	// xAddEventListener('navigation','click',savePos,false);
}


var myZ = 10;
var myDivNr = 0;

var imgX = 130; var imgY = 20;

var biX = 300; var biY = 10;
