// ******************************************************
//  Copyright 2006 by
//
//          Matthias Marquardt
//
//  Version 0.4
// ******************************************************

var xmlHttp = false;
var WindowIsOpen = false;
var lastopen = "";
var ie  = ((document.all)&&(!document.layers)) ? true : false;
var ns  = ((document.layers)&&(!document.getElementById)) ? true : false;
var moz = ((document.getElementById)&&(!document.all)) ? true : false;

try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
    try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        xmlHttp = false;
    }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}

function getPosLeft(l) {
  if (l.offsetParent) return (l.offsetLeft + getPosLeft(l.offsetParent));
  else return (l.offsetLeft);
}
function getPosTop(l) {
  if (l.offsetParent) return (l.offsetTop + getPosLeft(l.offsetParent));
  else return (l.offsetTop);
}

function go2(link) {
    window.location.href = DOMAIN + "index.php?f=" + link;
}

function GetFile(link) {
    window.location.href = DOMAIN + "index.php?file=" + link;
}
function CloseLastOpen(xdiv) {
    if (xdiv) {
        document.getElementById("p_" + xdiv).style.visibility = "hidden";
        lastopen = "";
    }
}
function GetImage(link) {
    var de = document.documentElement;
    var imgTitel  = "";
    var imgNewW   = 480;
    //var imgNewH   = 320;
    var imgOrgW   = 0;
    var imgOrgH   = 0;
    var imgNext   = "";
    var imgBack   = "";
    var imgPos    = "";
    var EXIFL  = "";
    var EXIFD  = "";
    var INFOL  = "";
    var INFOD  = "";
    
    CloseLastOpen(lastopen);
    
    if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {  
        yScr = window.innerHeight + window.scrollMaxY;
        xScr = window.innerWidth + window.scrollMaxX;
        var wff = (de&&de.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
        var hff = (de&&de.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
        xScr -= (window.innerWidth - wff);
        yScr -= (window.innerHeight - hff);
    } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){
        yScr = document.body.scrollHeight;
        xScr = document.body.scrollWidth;
    } else {
        yScr = document.body.offsetHeight;
        xScr = document.body.offsetWidth;
    }
    
    if (xmlHttp) {
        xmlHttp.open('GET', DOMAIN + 'index.php?file=' + link + '&ajax=IMAGEINFO&imgsi=' + xScr, true);
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    var contX    = xmlHttp.responseXML.getElementsByTagName("Width").item(0);
                    //var contY    = xmlHttp.responseXML.getElementsByTagName("Height").item(0);
                    var contTi   = xmlHttp.responseXML.getElementsByTagName("Titel").item(0);
                    var contNext = xmlHttp.responseXML.getElementsByTagName("ImgNext").item(0);
                    var contBack = xmlHttp.responseXML.getElementsByTagName("ImgBack").item(0);
                    var contPos  = xmlHttp.responseXML.getElementsByTagName("AnzahlPos").item(0);
                    var contINL  = xmlHttp.responseXML.getElementsByTagName("INFOLink").item(0);
                    var contIND  = xmlHttp.responseXML.getElementsByTagName("INFOData").item(0);
                    var contEXL  = xmlHttp.responseXML.getElementsByTagName("EXIFLink").item(0);
                    var contEXD  = xmlHttp.responseXML.getElementsByTagName("EXIFData").item(0);
                    if (xmlHttp.responseXml) {
                        imgNewW  = parseInt(contX.firstChild.nodeValue);
                        //imgNewH  = parseInt(contY.firstChild.nodeValue);
                        imgTitel = contTi.firstChild.nodeValue;
                        imgNext  = contNext.firstChild.nodeValue;
                        imgBack  = contBack.firstChild.nodeValue;
                        imgPos   = contPos.firstChild.nodeValue;
                        EXIFL = contEXL.firstChild.nodeValue;
                        EXIFD = contEXD.firstChild.nodeValue;
                        INFOL = contINL.firstChild.nodeValue;
                        INFOD = contIND.firstChild.nodeValue;
                    } else if (xmlHttp.responseText) {
                        imgNewW  = parseInt(contX.textContent);
                        //imgNewH  = parseInt(contY.textContent);
                        imgTitel = contTi.textContent;
                        imgNext  = contNext.textContent;
                        imgBack  = contBack.textContent;
                        imgPos   = contPos.textContent;
                        EXIFL = contEXL.textContent;
                        EXIFD = contEXD.textContent;
                        INFOL = contINL.textContent;
                        INFOD = contIND.textContent;
                    }
                    var AWinExtra = EXIFL + 
                                    INFOL + 
                                    "<a href='" + DOMAIN + 'index.php?file=' + link + "' target='_blank'><img src='" + DOMAIN + "img/hb_download.gif' border='0' alt='Download / Zoom' title='Download / Zoom' \></a>" +
                                    "<a href='#'><img onclick=closeWidget('AWin') src='" + DOMAIN + "img/hb_close.gif' border='0' alt='Close' title='Close' \></a>";
                    
                    var HTMLInhalt   = "<img id='AWBild' src='" + DOMAIN + "img/galloader.gif' border='0' alt='" + imgTitel + "' title='" + imgTitel + "' \>";
                    var AWinNextBack = imgBack + " " + imgPos + " " + imgNext;
                    if (WindowIsOpen == false) {
                        var wstr="<table id='AWin' style='filter:alpha(opacity=100); -moz-opacity: 1.00; opacity: 1.00; -khtml-opacity: 1.00; position: absolute; z-index: 7; top: 0px; left: 0px; width: 100%;' cellspacing='0' cellpadding='0'>"+
                                   "<tr>" +
                            	     "<td id='AWinTitel'>" + imgTitel + "</td>" +
                            	     "<td id='AWinNextBack'>" + AWinNextBack + "</td>" +
                            	     "<td id='AWinExtra'>" + AWinExtra + "</td>" +
                            	   "</tr>" +
                                   "<tr>" +
                            	     "<td id='AWinInhalt' colspan='3'>" + HTMLInhalt + "</td>" +
                            	   "</tr>" +
                                   "<tr>" +
                            	     "<td id='AWinCopy' colspan='3'>MMFile by Matthias M. - <a href='http://www.mattmarr.com' target='_blank'>www.mattmarr.com</a></td>" +
                                   "<tr>" +
                                 "<div id='p_Info_' class='dropdown'>" + INFOD + "</div>" + 
                                 "<div id='p_Exif_' class='dropdown'>" + EXIFD + "</div>" +
                            	 "</table>";
                        // Hingerund abdunkeln
                        var ref = document.getElementById("MMWin");
                    	ref.innerHTML += "<div id='wid002' style='background-color: #000000; filter:alpha(opacity=80); -moz-opacity: 0.80; opacity: 0.80; -khtml-opacity: 0.80; position: absolute; z-index: 6; top: 0px; left: 0px; width: " + xScr + "px; height: " + yScr + "px;'>";
                    	ref.innerHTML += wstr;
                    	ref.innerHTML += "</div>";
                    	WindowIsOpen = true;
                    	
                    	// FadeInOut(ID, From, To, Speed, Pause);
                    	
                	} else {
                	   document.getElementById("p_Info_").innerHTML      = INFOD;
                	   document.getElementById("p_Exif_").innerHTML      = EXIFD;
                	   document.getElementById("AWinInhalt").innerHTML   = HTMLInhalt;
                	   document.getElementById("AWinTitel").innerHTML    = imgTitel;
                	   document.getElementById("AWinExtra").innerHTML    = AWinExtra;
                	   document.getElementById("AWinNextBack").innerHTML = AWinNextBack;
                    }
                    //document.getElementById("AWinInhalt").style.height = imgNewH + 10;
                    var bild = new Image;
                    bild.onload = function() {
                        document.getElementById("AWBild").src = bild.src;
                        //document.getElementById("AWBild").style.height = imgNewH;
                    };
                    bild.src = DOMAIN + "index.php?file=" + link + "&sm=a" + imgNewW;
                } else {
                    alert('Es ist ein Problem aufgetreten.');
                }
            }
        };
        xmlHttp.send(null);
    }
}

function closeWidget(id) {

    // FadeInOut(ID, From, To, Speed, Pause);
    
    CloseLastOpen(lastopen);
    
	var wind  = document.getElementById("MMWin");
	var node2 = document.getElementById('wid002');
	wind.removeChild(node2);
	var node  = document.getElementById(id);
	wind.removeChild(node);
	WindowIsOpen = false;
}


function dropdown(wdiv){

    var PopUpid = document.getElementById("p_" + wdiv);
    var Menu_id = document.getElementById("m_" + wdiv);
    var Mainid  = document.getElementById("AWin");

    // ################################################
    // Schliesse altes Fenster wenn eins offen ist
    if(lastopen != wdiv) {
        CloseLastOpen(lastopen);
    }
    var statdiv = PopUpid.style.visibility;
    var pWidth  = PopUpid.offsetWidth;          // Breite des PopFenster
    var pHeight = PopUpid.offsetHeight;         // Hoehe des PopFenster
    var mLeft   = getPosLeft(Menu_id);          // X Position des Menu
    var mTop    = getPosTop(Menu_id);           // Y Position des Menu
    var mWidth  = Menu_id.offsetWidth;          // Breite des Menu
    var mHeight = Menu_id.offsetHeight;         // Höhe des Mneu
    
    //var BWidth  = Mainid.offsetWidth; //document.body.clientWidth;  // Breite des Browserfenster
    if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
        BWidth = window.innerWidth + window.scrollMaxX;
        var wff = (de&&de.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
        BWidth -= (window.innerWidth - wff);
    } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){
        BWidth = document.body.scrollWidth;
    } else {
        BWidth = document.body.offsetWidth;
    }
    
    // ################################################
    // Menü position neuberechnen
    var neuXTop   = mTop  + mHeight;
    var merkRight = mLeft + pWidth;
    
    if (merkRight >= BWidth) {
        mLeft = (mLeft + mWidth) - pWidth;
    }
    // ################################################
    // Menu neu positionieren
    PopUpid.style.top  = neuXTop + "px";
    PopUpid.style.left = mLeft   + "px";
    // ################################################
    // Öffne neues Menu bei Klick
    if(ie) {
        if (statdiv == "visible") {
            PopUpid.style.visibility="hidden";
        } else {
            PopUpid.style.visibility="visible";
        }
    }
    if(ns) {
        if (statdiv == "show") {
            PopUpid.style.visibility="hidden";
        } else {
            document.layers[wdiv].visibility="show";
        }
    }
    if(moz) {
        if (statdiv == "visible") {
            PopUpid.style.visibility="hidden";
        } else {
            PopUpid.style.visibility="visible";
        }
    }

    lastopen = wdiv;
}