window.onload = AnchorPopupWindows; 

function popupWindow() 
{
    if (popupWindow.arguments.length < 1) return false;
    var popupWin;
	
	popupWin = window.open("",popupWindow.arguments[1], popupWindow.arguments[2]);

	if (popupWin.location.pathname == "")
		popupWin.location.href = popupWindow.arguments[0];
	else
	{
		popupWin.close();
		popupWin = window.open(popupWindow.arguments[0],popupWindow.arguments[1], popupWindow.arguments[2]);		
	}
}

function AnchorPopupWindows() 
{
    if(!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    var currentAnchor;
	var screen_w = screen.availWidth ? screen.availWidth : screen.width;
	var screen_h = screen.availHeight ? screen.availHeight : screen.height;
	var path;
	
	if (window.location.href.lastIndexOf('/'))
		path = window.location.href.substring(0, window.location.href.lastIndexOf('/') + 1);
	else
		path = window.location.href.substring(0, window.location.href.lastIndexOf('\\') + 1);
	
    for (var anchorIndex = 0; anchorIndex < anchors.length; anchorIndex++) 
	{
        var targetLink, relArray, relInformation, windowAttributes, javascriptTargetLink, windowName, phpTarget;
		var windowPos = "";
        var location = 0, menubar = 0, resizable = 0, scrollbars = 0, status = 0, toolbar = 0;
        var width, height, windowType, windowName;
		var center = 0;
		var call_php = 0;
		var title = "demmel products";
		var lang;
		var relpath;
						  
        currentAnchor = anchors[anchorIndex];
		
		relpath = currentAnchor.getAttribute("href", 2);
		
		if (relpath && relpath.substr(0, 7) == "http://")
			targetLink = relpath;
		else
	        targetLink = path + relpath;	// get's href as is on MSIE (not expanded path)
		
        relInformation = currentAnchor.getAttribute("rel");
		currentAnchor.setAttribute("target", "_self");	// change target (predefined for no java available)
		
        if (relInformation && targetLink) 
		{
            relArray = relInformation.split("|");
            // Make sure there are at least 4 parameters
            if (relArray[0] == "popup" && relArray.length >= 4) 
			{
                width = parseInt(relArray[1]) ? parseInt(relArray[1]) : 400;
                height = parseInt(relArray[2]) ? parseInt(relArray[2]) : 400;
									
                windowType = relArray[3];

                if (windowType == "custom") 
				{
                    if (relArray.length < 11) 
						return false; // IF custom window type
						
                    location = parseInt(relArray[4]) ? parseInt(relArray[4]): 0;
                    menubar = parseInt(relArray[5]) ? parseInt(relArray[5]): 0;
                    resizable = parseInt(relArray[6]) ? parseInt(relArray[6]): 0;
                    scrollbars = parseInt(relArray[7]) ? parseInt(relArray[7]): 0;
                    status = parseInt(relArray[8]) ? parseInt(relArray[8]): 0;
                    toolbar = parseInt(relArray[9]) ? parseInt(relArray[9]): 0;
                    center = parseInt(relArray[10]) ? parseInt(relArray[10]): 0;
					
                    if (relArray.length == 12) 
						windowName = relArray[11]; // Determine Window name
                    else 
						windowName = "popupWindow";						
                } 
				else 
				{
                    // Defined Window Type
                    // Determine Window name
                    if (relArray.length >= 6) 
						windowName = relArray[5];
                    else 
						windowName = "popupWindow";
						
                    // Determine attributes
                    if (windowType == "standard") 
					{
						// everything is already = 0
					}
                    else if (windowType == "resize") 
						resizable = 1;
                    else if (windowType == "scrollbar") 
						scrollbars = 1;
                    else if (windowType == "resizescroll") 
					{
						resizable = 1;
						scrollbars = 1;
					}
                    else if (windowType == "img" || windowType == "imge" || windowType == "imgg")
					{
						call_php = 1;
						
	                    if (relArray.length >= 6) 
							title = relArray[5];
							
		                width += 42;
    		            height += 65;
							
						windowName = "imgWindow";
						
						if (windowType == "imgg")
							lang = "gr";
						else
							lang = "en";
					}
					else
						return false;

                    if (relArray.length >= 5) 
	                    center = parseInt(relArray[4]) ? parseInt(relArray[4]): 0;
                }
				
				if (width > screen.availWidth - 100)
				{
					width = screen.availWidth - 100;
					resizable = 1;
					scrollbars = 1;
					height += 30;	// add some space for the scrollbar
				}
				if (height >= screen.availHeight - 100)
				{
					height = screen.availHeight - 100;
					resizable = 1;
					scrollbars = 1;
					width += 30;	// add some space for the scrollbar					
				}

				// Set window size
                windowAttributes = "width=" + width + ",height=" + height;
				
                // Set attributes
                windowAttributes += ",location="+ location +",menubar=" + menubar +",resizable=" + resizable + ",scrollbars=" + scrollbars + ",status=" + status + ",toolbar="+ toolbar;
								
				if (center)
					windowPos = ",top=" + Math.round((screen_h - height) / 2) + ",left=" + Math.round((screen_w - width) / 2);
				
				if (call_php)
				{
					phpTarget = "http://www.demmel.com/php/showimg.php?img=" + targetLink + "&title=" + title + "&lang=" + lang;
					javascriptTargetLink = "javascript:popupWindow('" + phpTarget + "','" + windowName + "','" + windowAttributes + windowPos + "');";
    	            currentAnchor.setAttribute("href", javascriptTargetLink);
				}
				else
				{
					javascriptTargetLink = "javascript:popupWindow('" + targetLink + "','" + windowName + "','" + windowAttributes + windowPos + "');";
    	            currentAnchor.setAttribute("href", javascriptTargetLink);
				}
            }
        }
    }
}
