var req, nonAjaxId, nonAjaxPos, nonAjaxThumbPos;

function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        try {
        	req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) { // activeX ist deaktiviert, oder nicht vorhanden (IE5 mac)
    		var docUrl = new Array();
    		var urlAddOn = "";
			docUrl = document.location.href.split(".php");

        	if (nonAjaxId) {
        		urlAddOn += "id=" + nonAjaxId;
        	} else if (nonAjaxPos) {
        		urlAddOn += "positionD=" + nonAjaxPos;
        	} else if (document.forms[0].id.value > 0) {
        		urlAddOn += "id=" + document.forms[0].id.value;
        	}
        
        	if (nonAjaxThumbPos) {
        		if (urlAddOn != "") {
        			urlAddOn += "&";
        		}
        		urlAddOn += "thumbPos=" + nonAjaxThumbPos;
        	}
        
        	document.location.href = docUrl[0] + ".php?" + urlAddOn;
        }
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
			response  = req.responseXML.documentElement;
			method    = response.getElementsByTagName('method')[0].firstChild.data;
			eval(method + '(response)');
        } else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function detailsById(newId) {
	if (document.forms[0].id.value != newId) {
		nonAjaxId = newId;
		newDetail(newId, 0);
	}
}

function detailsByPosition(position) {
	nonAjaxPos = position;
	newDetail("", position);
}

function newDetail(targetId, positionD) {
	var now = new Date();
	url = '/newDetail.php?id=' + targetId + '&positionD=' + positionD + '&q=' + document.forms[0].q.value + '&' + now.getTime();
	loadXMLDoc(url);
}

function detailsArrived(response) {
	if (self.screenTop && self.screenX) { // fix safari redraw bug - part 1
		oldHeight = document.getElementById('center').clientHeight;
	}

	details	= response.getElementsByTagName('details')[0].firstChild.data;
	document.getElementById('center').innerHTML = details;

	newId = response.getElementsByTagName('newid')[0].firstChild.data;
	curpos = response.getElementsByTagName('curpos')[0].firstChild.data;
	delpos = response.getElementsByTagName('delpos')[0].firstChild.data;

	document.forms[0].curPos.value = curpos;
	document.forms[0].delPos.value = delpos;

	if (oldThumb = document.getElementById("t" + document.forms[0].id.value)) {
		oldThumb.src = "/img/thumb_overlay" + oldThumb.alt + ".gif";
	}
	if (newThumb = document.getElementById("t" + newId)) {
		newThumb.src = "/img/current_thumb" + newThumb.alt + ".gif";
	} else {
		thumbsById(newId);
	}

	document.forms[0].id.value = newId;
	
	if (self.screenTop && self.screenX) { // fix safari redraw bug - part 2
		if (self.innerHeight < (oldHeight + 25) && self.innerHeight >= (document.getElementById('center').clientHeight + 25)) {
        	window.resizeTo(self.outerWidth + 1, self.outerHeight);
        	window.resizeTo(self.outerWidth - 1, self.outerHeight);
		}
    }
}

function thumbsById(newId) {
	newThumbs(newId, "");
}

function thumbsByPosition(position) {
	nonAjaxThumbPos = position;
	newThumbs("", position);
}

function newThumbs(targetId, positionT) {
	var now = new Date();
	url = '/newThumbs.php?targetId=' + targetId + '&positionT=' + positionT + '&q=' + document.forms[0].q.value + '&' + now.getTime();
	loadXMLDoc(url);
}

function thumbsArrived(response) {
	newThumbsArrived = response.getElementsByTagName('thumbs')[0].firstChild.data;
	document.getElementById('thumbs').innerHTML = newThumbsArrived;
	activateCurrentThumb();
}

function activateCurrentThumb() {
	if (document.forms[0].id.value > 0) {
		if (newThumb = document.getElementById("t" + document.forms[0].id.value)) {
			newThumb.src = "/img/current_thumb" + newThumb.alt + ".gif";
		}
	}
}

function pop(file) {
	helpwindow = window.open(file,"popupwin","toolbar=no,scrollbars=no,location=no,status=no,directories=no,menubar=no,resizable=no,width=730,height=584").focus();
}

function externalPreview() {
	pop("externalPreview.php?q=" + document.forms[0].q.value + "&curPos=" + document.forms[0].curPos.value);
}

function branchMenu(branchMenu) {
	if (branchMenu) {
		document.getElementById("mainNav").style.display = "none";
		document.getElementById("brancheNav").style.display = "block";
	} else {
		document.getElementById("brancheNav").style.display = "none";
		document.getElementById("mainNav").style.display = "block";
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
