
	function PicMenu() {
	
		this.init = function() {
			var attThumbs = document.getElementsByClassName("attachmentThumbnail", "fieldset");
			for (var i=0; i<attThumbs.length; i++) {
				var thumbs = attThumbs[i].getElementsByTagName("img");
				
				for (var j=0; j<thumbs.length; j++){
					if (typeof thumbs[j].parentNode.href == "undefined")
						continue;
					var picSrc = thumbs[j].parentNode.href;
					var picID  = picSrc.substring(picSrc.lastIndexOf("?")).replace(/\D+/ig,"");
					
					var picMenu = new PicMenuTable();
					var picPos = getPosition( thumbs[j] );
					picMenu.setPos( picPos.x, picPos.y );
					picMenu.setToParent( attThumbs[i] );
										
					thumbs[j].picMenu = picMenu;
					thumbs[j].onmouseover = function(e) {
						this.picMenu.setVisible(true);
					}
					

					thumbs[j].onmouseout = function(e) {
						if(!e) e = window.event;
						var obj = e.relatedTarget?e.relatedTarget:e.toElement;
						if (obj == null || (obj.nodeName != "TABLE" && obj.nodeName != "TBODY" && obj.nodeName != "TD"))
							this.picMenu.setVisible(false);
					}

					picMenu.id = picID;
					var savePMI = new PicMenuItem( 
									picID,
									"./images/menuIcons/save_icon.png", 
									function(id) {
										window.location.href = "#" + id;
									},
									"Bild speichern"
								);
					
					var printPMI = new PicMenuItem( 
									picID,
									"./images/menuIcons/print_icon.png", 
									function(id) {
										var printWin = window.open("", "Druckvorschau");
										printWin.document.open();
										printWin.document.write('<html><title>JavArea.de</title><body onload="window.print();"><img alt="" src="attachment.php3?attachmentid=' + id + '"></body></html>');
										printWin.document.close();
									},
									"Bild drucken"
								);
								
					var zoomPMI = new PicMenuItem( 
									picID,
									"./images/menuIcons/zoom_icon.png", 
									function(id) {
										var printWin = window.open("", "Ansicht");
										printWin.document.open();
										printWin.document.write('<html><title>JavArea.de</title><body><img alt="" src="attachment.php3?attachmentid=' + id + '"></body></html>');
										printWin.document.close();
										//ajaxRequest.openGetRequest(BOARD_AJAXPATH + '?action=AttachmentRead&attachmentid='+id);
										//window.location.href = "attachment.php3?attachmentid=" + id;
									},
									"Vollbild im neuen Fenster"
								);
					/*
					var delPMI = new PicMenuItem( 
									picID,
									"./images/menuIcons/zoom_icon.png", 
									function(id) {
										//ajaxRequest.openGetRequest(BOARD_AJAXPATH + '?action=AttachmentRead&attachmentid='+id);
										
									},
									"Anhang löschen"
								);
					*/
					picMenu.addMenuItem( savePMI );
					picMenu.addMenuItem( printPMI );
					picMenu.addMenuItem( zoomPMI );
					//picMenu.addMenuItem( delPMI );
					
				}
			}
		
		}
	
	}

	function PicMenuTable() {
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr    = document.createElement("tr");
		
		this.addMenuItem = function( item ) {
			tr.appendChild( item.getCell() );
		}
		
		this.setToParent = function( par ) {
			par.appendChild( table );
		}
		
		this.setPos = function(x,y) {
			table.style.left = (x+5)+"px";
			table.style.top  = (y+5)+"px";
		}
		
		this.setVisible = function(setVisible) {
			table.style.visibility = setVisible?"visible":"hidden";
		}
		
		table.appendChild( tbody );
		tbody.appendChild( tr );
		table.style.position = "absolute";
		table.style.background = "#ECE9D8";
		table.style.borderTop    = "1px solid #FFFFFF";
		table.style.borderLeft   = "1px solid #FFFFFF";
		table.style.borderBottom = "1px solid #ACA899";
	  table.style.borderRight  = "1px solid #ACA899";
		this.setVisible(false);

	}
	
	
	function PicMenuItem( id, imgSrc, func, toolTip ) {
		this.id = id;
		var td = document.createElement("td");
		td.appendChild(document.createTextNode(String.fromCharCode(160)));
		td.title = toolTip
		td.style.background = "#ECE9D8 url(" + imgSrc + ") center center no-repeat";
		td.style.border = "1px solid #ECE9D8";
		
		td.style.cursor = "default";
		td.style.height = "17px";
		td.style.width  = "17px";
		
		td.onmouseover = function(e) {
			td.style.borderTop    = "1px solid #FFFFFF";
			td.style.borderLeft   = "1px solid #FFFFFF";
			td.style.borderBottom = "1px solid #ACA899";
			td.style.borderRight  = "1px solid #ACA899";
			td.style.backgroundColor = "#ECE9D8";
		};
		
		td.onmouseout = function(e) {
			td.style.border = "1px solid #ECE9D8";
			td.style.backgroundColor = "#ECE9D8";
		};
		
		td.onmousedown = function(e) {
			td.style.borderTop    = "1px solid #ACA899";
			td.style.borderLeft   = "1px solid #ACA899";
			td.style.borderBottom = "1px solid #FFFFFF";
			td.style.borderRight  = "1px solid #FFFFFF";
			td.style.backgroundColor = "#FFFFFF";
		};
		
		td.Instanz = this;
		td.onclick = function(e) {
			func(this.Instanz.id);
		};
		
		this.getCell = function() {
			return td;
		}
	}
	
	document.getElementsByClassName = function( className, tagName ) {
		tagName = tagName||false;
		var elements=[];
		var docObj = tagName?document.getElementsByTagName(tagName):document.getElementsByTagName("*");	
			for(var i=0; i<docObj.length; i++) {
				if(docObj[i].className.indexOf(className)!=-1) {
					var testStr=","+docObj[i].className.split(" ").join(",")+",";
					if(testStr.indexOf(","+className+",")!=-1)
						elements.push(docObj[i]);
				}
			}
	
		return elements;
	}
	
	/*
	HTMLElement.prototype.contains = function (oEl) {
		if (oEl == this) return true;
		if (oEl == null) return false;
		return this.contains(oEl.parentNode);		
	};
	
	Array.prototype.inArray = function(val){
		for (var i=0; i<this.length; i++)
			if (typeof(this[i]) == "object" && typeof this[i].length != 'undefined'){
				return this[i].inArray(val);
			}
			else if (this[i] == val)
					return true;
		return false;
	}
	*/
	
	function getPosition(element) {
		var tmpEl=element;
		var pos = new Object();
		pos.x = 0;
		pos.y = 0;	
		do {
			pos.y += tmpEl.offsetTop;
			pos.x += tmpEl.offsetLeft;
			if (typeof(tmpEl)=="object" && typeof(tmpEl.offsetParent)=="object")
				tmpEl=tmpEl.offsetParent;
		} while ((typeof(tmpEl)=="object") && (typeof(tmpEl.tagName)!="undefined") && tmpEl.tagName.toLowerCase() != "body") 
				
		return pos;
	}
		
	/*window.onload = function() {
		new PicMenu().init();		
	};*/
	
var PicMenu = new PicMenu();
	
	
var isDOMContentLoaded = false;
function addContentLoadListener () {
	if (document.addEventListener) {
	var DOMContentLoadFunction = function () {
		isDOMContentLoaded = true;
		PicMenu.init();
	};
	document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
}
var oldonload = (window.onload || new Function());
	window.onload = function () {
		if (!isDOMContentLoaded) {
			oldonload();
			PicMenu.init();
		}
	};
}
addContentLoadListener();	
