

function picturebar(gallery, position, code, selected) {
	var picturebar = document.getElementById("picturebar");
	
	var xmlhttp = new ajaxRequest("response/picturebar.php?gallery=" + gallery + "&position=" + position + "&code=" + code + "&selected=" + selected);
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			picturebar.innerHTML = xmlhttp.req.responseText;
		}
	}
	xmlhttp.doRequest();
}

function picturebar_no_pics() {
	//function gibt Abrfrage aus 
	layer(400);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/picturebar_no_pics.php");
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function comment(type, code) {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/comment.php?code=" + code + "&type=" + type);
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function send_comment() {
	var comment = formattxt(document.getElementsByName("comment")[0].value);
	var code = document.getElementsByName("mediacode")[0].value;
	var type = document.getElementsByName("type")[0].value;
	
	//function gibt Abrfrage aus 
	var lc = document.getElementById("lcontent");
	//divcontainer
	lc.removeChild(lc.lastChild);
	insertAjaxLoader(lc, 600);
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/comment.php?code=" + code + "&type=" + type + "&comment=" + comment);
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function show_upload(code) {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/uploadmedia.php?code=" + code );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function startUpload() {
	var datei = document.getElementById("upfile").value.split("\\");
	if (datei[datei.length-1].length == 0) {
		window.alert("Bitte gib eine Datei zum hochladen an.");
	} else {
		var table = document.getElementById("upload_status");
		var tbody = table.lastChild;
		
		var tr = document.createElement("tr");
		var td = document.createElement("td");
		var txt = document.createTextNode("Datei: ");
		td.appendChild(txt);
		var b = document.createElement("b");
		var txt = document.createTextNode(datei[datei.length-1]);
		b.appendChild(txt);
		td.appendChild(b);
		tr.appendChild(td);
		var td = document.createElement("td");
		tr.appendChild(td);
		
		var td = document.createElement("td");
		var div = document.createElement("div");
		div.setAttribute("id", datei[datei.length-1]);
		var img = document.createElement("img");
		img.setAttribute("src", "../img/gif/ajaxloader.gif");
		div.appendChild(img);
		td.appendChild(div);
		
		var count = ((tbody.childNodes.length) + 2);
		var target = "upload_target" + count;
		
		var iframe = document.createElement("iframe");
		iframe.setAttribute("id", target);
		iframe.setAttribute("name", target);
		iframe.setAttribute("src", "#");
		iframe.style.width = 0;
		iframe.style.height = 0;
		iframe.style.border = 0;
		
		td.appendChild(iframe);
		tr.appendChild(td);
		
		var ie = navigator.userAgent.toLowerCase();
		if (ie.indexOf("msie") > -1) {
			isIE = true;
		} else {
			isIE = false;;
		}
		
		if (isIE) {
			tbody.appendChild(tr);
		} else {
			table.appendChild(tr);
		}
		window.setTimeout("newUpload('" + target + "')", 2000);
		return true;
	}
}

function newUpload(target) {
	document.getElementById("upfile").value="";
	document.getElementById("rights").checked=false;
	document.getElementById("upload_form").target = target;
}

function stopUpload(success, string, filename){  
	if (filename.length > 0) { 
		var div = document.getElementById(filename);
		div.innerHTML = "";
		if (success == 1) {  
			var span = document.createElement("span");
			span.style.color = "green";
			var txt = document.createTextNode("Erfolgreich hochgeladen!");
			span.appendChild(txt); 
			div.appendChild(span);
		} else {  
			var span = document.createElement("span");
			span.style.color = "red";
			var txt = document.createTextNode("Fehler - " + string);
			span.appendChild(txt); 
			div.appendChild(span);
		}     
		return true; 
	}
}

function show_diashow(code) {
	//function gibt Abrfrage aus 
	layer(800);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/diashow.php?code=" + code );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function create_gallery() {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/creategallery.php");
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function save_create_gallery(code) {
	var name = document.getElementsByName("name")[0].value;
	
	var lc = document.getElementById("lcontent");
	//divcontainer
	
	lc.removeChild(lc.lastChild);
	insertAjaxLoader(lc, 600);
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/creategallery.php?name=" + name );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function rename_gallery(code) {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/renamegallery.php?code=" + code );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function save_rename_gallery(code) {
	var code = document.getElementsByName("gallerycode")[0].value;
	var name = document.getElementsByName("name")[0].value;
	
	var lc = document.getElementById("lcontent");
	//divcontainer
	
	lc.removeChild(lc.lastChild);
	insertAjaxLoader(lc, 600);
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/renamegallery.php?code=" + code + "&name=" + name );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function delete_gallery(code) {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/deletegallery.php?code=" + code );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function save_delete_gallery(code) {
	var code = document.getElementsByName("gallerycode")[0].value;
	
	var lc = document.getElementById("lcontent");
	//divcontainer
	
	lc.removeChild(lc.lastChild);
	insertAjaxLoader(lc, 600);
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/deletegallery.php?code=" + code + "&delete=on" );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function edit_media(type, code) {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/editmedia.php?type=" + type + "&code=" + code );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function save_edit_media() {
	var code = document.getElementsByName("mediacode")[0].value;
	var type = document.getElementsByName("type")[0].value;
	var name = document.getElementsByName("name")[0].value;
	var keys = document.getElementsByName("keys")[0].value;
	
	var lc = document.getElementById("lcontent");
	//divcontainer
	
	lc.removeChild(lc.lastChild);
	insertAjaxLoader(lc, 600);
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/editmedia.php?type=" + type + "&code=" + code + "&name=" + name + "&keywords=" + keys );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function delete_media(type, code) {
	//function gibt Abrfrage aus 
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/deletemedia.php?type=" + type + "&code=" + code );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function save_delete_media() {
	var code = document.getElementsByName("mediacode")[0].value;
	var type = document.getElementsByName("type")[0].value;
	
	var lc = document.getElementById("lcontent");
	//divcontainer
	
	lc.removeChild(lc.lastChild);
	insertAjaxLoader(lc, 600);
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/deletemedia.php?type=" + type + "&code=" + code + "&delete=delete" );
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			removeAjaxLoader(lc);
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function getPositionOnDocument (o) {
    var pos = { top:0, left:0 };
    if(!o)
		return pos;
	else if(typeof o == 'string') 
		o = document.getElementById(o);
    
    if( typeof o != 'object'  || typeof o.offsetTop == 'undefined') 
		return pos;
    while (o && o.tagName != 'BODY') {
        pos.top  += parseInt( o.offsetTop );
        pos.left += parseInt( o.offsetLeft );
        o = o.offsetParent;
    }
    return pos;
} 

function getMousePosition(evt) {
    if(!evt) 
		evt = window.event;
    var pos = {left: evt.clientX, top: evt.clientY};
    var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? 
    window.document.documentElement : window.document.body || null;
    if (body) {
        pos.left += body.scrollLeft;
        pos.top += body.scrollTop;
    }
    return pos;
}  

function getPositionOnImage(el, evt) {
    var m = getMousePosition(evt);
    var p = getPositionOnDocument(el);
	/*window.alert(m.left);
	window.alert(p.left);*/
    return {posX: (m.left - p.left), posY: (m.top - p.top)};
}


function iLink(event) {
	var el = document.getElementById("media");
	if (!document.getElementById("newLink")) {
		var posX = getPositionOnImage(el, event).posX;
		var posY = getPositionOnImage(el, event).posY;
		/*window.alert(posY);
		window.alert(posX);*/
		var media = document.getElementById("media");
		var img = document.createElement("img");
		img.setAttribute("id", "newLink");
		img.className = "mark";
		img.setAttribute("src", "../img/png/marked.png");
		img.style.position = "absolute";
		if (posY < 25) {
			posY = 0;	
		} else {
			posY = posY - 25;
		}
		if ((posY+50) > (media.style.height.substring(0, (media.style.height.length-2)))) {
			posY = media.style.height.substring(0, (media.style.height.length-2)) - 50;
		}
		if (posX < 25) {
			posX = 0;	
		} else {
			posX = posX - 25;
		}
		if ((posX+50) > (media.style.width.substring(0, (media.style.width.length-2)))) {
			posX = media.style.width.substring(0, (media.style.width.length-2)) - 50;
		}
		img.style.top = posY + "px";
		img.style.left = posX + "px";
		media.appendChild(img);
		
		var div = document.createElement("div");
		div.className = "selection";
		div.setAttribute("id", "selection");
		div.style.top = posY + "px";
		div.style.left = (posX+60) + "px";
		
		var txt = document.createTextNode("Freund/in markieren:");
		var b = document.createElement("b");
		b.appendChild(txt);
		div.appendChild(b);
		var br = document.createElement("br");
		div.appendChild(br);
		var br = document.createElement("br");
		div.appendChild(br);
		var selection = document.createElement("select");
		selection.setAttribute("name", "markedPerson");
		selection.setAttribute("size", "10");
		
		var xmlhttp = new ajaxRequest("response/markinguser.php");
		xmlhttp.wState = function() {
			if (xmlhttp.req.readyState==4) {
				//window.alert(xmlhttp.req.responseText);
				var daten = xmlhttp.req.responseText;
				daten = eval("(" + daten + ")");			
				for(var i = 0; i < daten.length; i++) {
					var option = document.createElement("option");
					option.setAttribute("value", daten[i].code);
					if (i == 0) {
						option.setAttribute("selected", "selected");	
					}
					var txt = document.createTextNode(daten[i].name);
					option.appendChild(txt);
					selection.appendChild(option);
				}			
			}
		}
		xmlhttp.doRequest();
		
		div.appendChild(selection);
		
		var input = document.createElement("input");
		input.setAttribute("type", "button");
		input.setAttribute("value", "Speichern");
		input.className = "button";
		input.onclick = marking;
		div.appendChild(input);
		
		var input = document.createElement("input");
		input.setAttribute("type", "button");
		input.setAttribute("value", "Abbrechen");
		input.className = "button";
		input.onclick = closeLink;
		div.appendChild(input);
		
		/*var but = document.createElement("div");
		but.className="button";
		var a = document.createElement("a");
		a.setAttribute("href", "javascript:marking();");
		var txt = document.createTextNode("OK");
		a.appendChild(txt);
		but.appendChild(a);
		div.appendChild(but);
		
		var but = document.createElement("div");
		but.className="button";
		var a = document.createElement("a");
		a.setAttribute("href", "javascript:closeLink();");
		var txt = document.createTextNode("Abbrechen");
		a.appendChild(txt);
		but.appendChild(a);
		div.appendChild(but);*/
		
		media.appendChild(div);	
	}
}

function closeLink() {
	var media = document.getElementById("media");
	var newLink = document.getElementById("newLink");
	media.removeChild(newLink);
	var selection = document.getElementById("selection");
	media.removeChild(selection);
}

function marking() {	
	var picturecode = document.getElementById("media").firstChild.id;
	var usercode = document.getElementsByTagName("select")[0].value;
	var newLink = document.getElementById("newLink");
	var posY = newLink.style.top.substring(0, (newLink.style.top.length-2));
	var posX = newLink.style.left.substring(0, (newLink.style.left.length-2));
	var media = document.getElementById("media");
	var selection = document.getElementById("selection");
	
	selection.innerHTML = '<b>Freund/in markieren:</b><br><br><img src="../img/gif/ajaxloader.gif" style="margin: 20px 84px;"/>';
	
	var linkstr = "response/markeduser.php?picturecode=" + picturecode + "&usercode=" + usercode + "&posx=" + posX + "&posy=" + posY;
	var xmlhttp = new ajaxRequest(linkstr);
		xmlhttp.wState = function() {
			if (xmlhttp.req.readyState==4) {
				var daten = xmlhttp.req.responseText;
				daten = eval("(" + daten + ")");
				
				var mark = document.getElementById("newLink");
				if (!isIE) {
					mark.setAttribute("id", "mark"+daten[0].code);
					mark.setAttribute("onmouseover", "showMark('"+daten[0].code+"', true)");
					mark.setAttribute("onmouseout", "showMark('"+daten[0].code+"', true)");
				} else {
					mark.id = "mark"+daten[0].code;
					mark.onmouseover = function() {showMark("'" + daten[0].code + "'", true);}
					mark.onmouseout = function() {showMark("'" + daten[0].code + "'", true);}
				}
				mark.src = "../img/png/marked_empty.png";
				
				var div = document.createElement("div");
				div.className="markinfo";
				if (!isIE) {
					div.setAttribute("id", "info"+daten[0].code);
					div.setAttribute("onmouseover", "showMark('"+daten[0].code+"', true)");
					div.setAttribute("onmouseout", "showMark('"+daten[0].code+"', true)");
				} else {
					div.id = "info"+daten[0].code;
					div.onmouseover = function() {showMark("'" + daten[0].code + "'", true);}
					div.onmouseout = function() {showMark("'" + daten[0].code + "'", true);}
				}
				div.style.top = (parseInt(posY)+50) + "px";
				div.style.left = posX + "px";
				
				var a = document.createElement("a");
				a.href="account.php?user=" + daten[0].code;
				a.className="marklink";
				var txt = document.createTextNode(daten[0].name);
				a.appendChild(txt);
				div.appendChild(a);
				
				var br = document.createElement("br");
				div.appendChild(br);
				var br = document.createElement("br");
				div.appendChild(br);
				var txt = document.createTextNode("markiert von:");
				div.appendChild(txt);
				var br = document.createElement("br");
				div.appendChild(br);
				
				var a = document.createElement("a");
				a.href="account.php?user=" + daten[1].code;
				a.className="marklink";
				var txt = document.createTextNode(daten[1].name);
				a.appendChild(txt);
				div.appendChild(a);
				var test = div.id;
				media.appendChild(div);
				
				var a = document.createElement("a");
				a.href="account.php?user=" + daten[0].code;
				if (!isIE) {
					a.setAttribute("onmouseover", "showMark('"+daten[0].code+"', false)");
					a.setAttribute("onmouseout", "showMark('"+daten[0].code+"', false)");
				} else {
					a.onmouseover = function() {showMark("'" + daten[0].code + "'", false);}
					a.onmouseout = function() {showMark("'" + daten[0].code + "'", false);}
				}
				a.className="marklink";	
				var txt = document.createTextNode(" "+daten[0].name);
				a.appendChild(txt);
				
				document.getElementById("linked").insertBefore(a, document.getElementById("hr"));
				var txt = document.createTextNode(";");
				document.getElementById("linked").insertBefore(txt, document.getElementById("hr"));
				media.removeChild(selection);
			}
		}
		xmlhttp.doRequest();
}

function showMark(c, add) {
	if (isIE) 
		c = c.replace(/'/g, "");
	var m = document.getElementById("mark"+c);
	if (m.src.search('../img/png/marked_empty.png') > 0) {
		m.src = "../img/png/marked.png";
		if (add)
			document.getElementById("info"+c).style.display = "block";
	} else {
		m.src = "../img/png/marked_empty.png";
		document.getElementById("info"+c).style.display = "none";
	}	
}

function deleteMark() {
	layer(600);
	var lc = document.getElementById("lcontent");
	//divcontainer
	var container = document.createElement("div");
	container.className = "layercontainer";
	
	var xmlhttp = new ajaxRequest("response/markdelete.php");
	xmlhttp.wState = function() {
		if (xmlhttp.req.readyState==4) {
			//divcontainer
			var container = document.createElement("div");
			container.className = "layercontainer";
			container.innerHTML = xmlhttp.req.responseText;
			lc.appendChild(container);
		}
	}
	xmlhttp.doRequest();
}

function deletePersonalMark() {
	//window.alert(window.location.href);
	window.location.href = window.location.href + "&mark=delete"
}

function startMarking(c) {
	var pic = document.getElementById(c);
	var att = document.getElementById("mediaattention");
	var a = document.getElementById("marking");
	if (att.style.display != "block") {
		if (!isIE) {
			pic.setAttribute("onclick", "iLink(event);");
		} else {
			pic.onclick = function() {
							iLink(window.event);
						}
		} 
		pic.style.cursor = "crosshair";
		att.style.display = "block";
		var txt = document.createTextNode("Fertig markiert");
		a.innerHTML = "";
		a.appendChild(txt);
	} else {
		if (document.getElementById("newLink")) {
			closeLink();	
		}
		pic.setAttribute("onclick", "");
		pic.style.cursor = "";
		att.style.display = "none";
		var txt = document.createTextNode("Personen markieren");
		a.innerHTML = "";
		a.appendChild(txt);
	}
}