var g_DescSize=100;
var g_iPad=10;
var g_iimg;
var g_aData=new Array();
var g_bOccupied = false;
var g_bLoading = false;
var g_is=null;



//-----------------------------------------------
// setDescSize
//   set size of description area
//
function setDescSize(iSize) {
    g_DescSize = iSize;
}


//-----------------------------------------------
// buildWaitNotice
//   set size of description areacreate and show a wait notice
//
function buildWaitNotice() {

    var eT = document.getElementById(g_sTxtCont);
    iX = eT.offsetLeft;
    iY = eT.offsetTop; 
    iW = eT.offsetWidth; 
    iH = eT.offsetHeight; 
    //    alert("txtcont: "+iW+"x"+iH+" @("+iX+","+iY+")");


    if (g_browser.isIE6) {
       iX=20;
    }
    var eC = document.createElement("div");
    eC.setAttribute("id", "waiter");
    sStyle = "margin:0px; padding:0px; " +
	     "position:absolute;top:"+(iY-20)/2+"px; " +
             "height:"+20+"px; " +
             "left:"+(iX)+"px; " +
             "width:"+(iW)+"px; " +
             "z-index:3; overflow:hidden; background-color: #ffffa0;"+
             "padding-top:3px; padding-bottom:3px;text-align:center";
    eC.style.cssText=sStyle; 
    eC.innerHTML="Bild wird geladen. Bitte warten...";
    var eB = document.getElementById(g_sContainer);
    eB.appendChild(eC);

}

//-----------------------------------------------
// destroyWaitNotice
//   remove the wait notice
//
function destroyWaitNotice() {
    var eXX = document.getElementById("waiter");
    var eXB = document.getElementById(g_sContainer);
    eXB.removeChild(eXX);
}

//-----------------------------------------------
// display
//   do request for showing image
//
function display(imgid) {
    if (!g_bLoading) {
        g_bLoading = true;
        
        if (g_bOccupied) {
            _removeChild(g_sContainer, g_sImgCont);
        }

        buildWaitNotice();


        doRequest('get_info', imgid, showImage);
    }
}




//-----------------------------------------------
// closeContainer
//   remove iimage container
//
function closeContainer(bMode) {
    _removeChild(g_sContainer, g_sImgCont);
    var eT = document.getElementById(g_sTxtCont);
    eT.style.visibility="visible";    

    g_bOccupied = false;
}


//-----------------------------------------------
// bigImage
//   open the image in an own window
//
function bigImage() {
    window.open(aData["image"], "_blank");
}


//-----------------------------------------------
// showImage
//   callback for httprequest
//   loads image and passes control to function finish
//
function showImage(aData) {
      //alert("showImage Doing : "+aData["image"]);

    g_aData = aData;

    // must load image to get size
    g_iimg = new Image();
    g_iimg.src=aData["image"];
    g_iimg.onload=finish;
}




//-----------------------------------------------
// finish
//   create image content (same size as text content)
//   display image
//   display data
//   loads image and passes control to function finish
//
function finish() {
    // alert("finishing");

    // create image container
    var eC = buildImgCont();

    // remove wait notice
    destroyWaitNotice();

    // loading is finished
    g_bLoading = false;
  

    g_is = new ScaledImage(g_sImgCont+"_img");
    
    g_is.displayData();
    g_is.setImage(aData["image"]);
  

    // data  
    var eD = eC.lastChild;
    
    var eCC = document.createElement("div");
    // disgusting IE6 hack: need to give width
//    sStyle = "position:absolute; top: 0px; left:0px; right:0px; height:27px; " + 
//             "background-image:url(images/transback.png);";
    sStyle = "position:absolute; top: 0px; left:0px; width:"+eD.offsetWidth+"px; height:27px; " + 
             "background-image:url(images/transback.png);";
    eCC.style.cssText=sStyle;

    eD.appendChild(eCC);

    /*
    var oi0 = new ImgButton("but-left", TYPE_LEFT, STATE_DISABLED, nop);
    oi0.setStyle("position:absolute; top: 2px; right: 110px;");
    eCC.appendChild(oi0.getImg());	
    */
    var oi1 = new ImgButton("but-bigger", TYPE_BIGGER, STATE_NORMAL, bigImage);
//    oi1.setStyle("position:absolute; top: 2px; right: 29/*83*/px; cursor: pointer;");
    oi1.setStyle("position:absolute; top: 2px; right: 29px; cursor: pointer;");
    eCC.appendChild(oi1.getImg());	

    var oi2 = new ImgButton("but-close", TYPE_CLOSE, STATE_NORMAL, closeContainer);
//    oi2.setStyle("position:absolute; top: 2px; right: 2/*56*/px; cursor: pointer;");
    oi2.setStyle("position:absolute; top: 2px; right: 2px; cursor: pointer;");
    eCC.appendChild(oi2.getImg());	
    /*
    var oi3 = new ImgButton("but-smaller", TYPE_SMALLER, STATE_DISABLED, nop);
    oi3.setStyle("position:absolute; top: 2px; right: 29px; cursor: pointer;");
    eCC.appendChild(oi3.getImg());	

    var oi4 = new ImgButton("but-right", TYPE_RIGHT, STATE_DISABLED, nop);
    oi4.setStyle("position:absolute; top: 2px; right: 2px; cursor: pointer;");
    eCC.appendChild(oi4.getImg());	
    */

    var eCD = document.createElement("div");
    sStyle = "position:absolute; top: 27px; left:0px; right:0px; bottom: 0px;";
    eCD.style.cssText=sStyle;
    
    eD.appendChild(eCD);

    eCD.innerHTML = /*"["+g_aData["id"]+"] "+g_aData["image"]+*/"<br><b>"+g_aData["img_title"]+"</b><br>"+g_aData["img_group"]+" ("+g_aData["img_date"]+")<br/><br/>"+g_aData["img_comment"];

    
    g_bOccupied = true;
	
}

function buildImgCont() {

    var eT = document.getElementById(g_sTxtCont);
    iX = eT.offsetLeft;
    iY = eT.offsetTop; 
    iW = eT.offsetWidth; 
    iH = eT.offsetHeight; 
    //    alert("txtcont: "+iW+"x"+iH+" @("+iX+","+iY+")");

    // disgusting IE6 hack
    if (g_browser.isIE6) {
      iX = 20;
    }

    var eC = document.createElement("div");
    eC.setAttribute("id", g_sImgCont);
    sStyle = "margin:0px; padding:0px; " +
             "position:absolute;top:"+iY+"px; " +
             "height:"+iH+"px; " +
             "left:"+iX+"px; " +
             "width:"+iW+"px; " +
             "z-index:3; overflow:hidden; /*background-color: #ff00ff;*/";
    eC.style.cssText=sStyle; 

    var eI = document.createElement("div");
    eI.setAttribute("id", g_sImgCont+"_img");
    sStyle = "position:absolute; " +
    // disgusting IE6 hack: need to give height explicitly
//             "top:0px; bottom:0px; " +
             "top:0px; height:"+iH+"px; " +
             "left:0px; " +
             "width:"+(iW-(g_DescSize+g_iPad))+"px; " +
             "z-index:4;" +
             "overflow: hidden; /*background-color: #88ff88;*/";
    eI.style.cssText=sStyle; 

    var eD = document.createElement("div");
    eD.setAttribute("id", g_sImgCont+"_dsc");
    sStyle = "position:absolute; " +
    // disgusting IE6 hack: need to give height explicitly
//             "top:0px; bottom:0px; " +
             "top:0px; height:"+iH+"px; " +
             "width:"+g_DescSize+"px; " +
             "z-index:4;" +
             "right:0px; overflow: auto; /*background-color: #8888ff;*/";
    eD.style.cssText=sStyle; 

    eC.appendChild(eI);
    eC.appendChild(eD);


    var eB = document.getElementById(g_sContainer);
    eB.appendChild(eC);
    if (!g_bOccupied) {
       eT.style.visibility="hidden";
    }


    return eC;
}


function handleResize2() {
    // adjust cover & imgstuff
    if (g_bOccupied) {
        var eT = document.getElementById(g_sTxtCont);
        iX = eT.offsetLeft;
        iY = eT.offsetTop; 
        iW = eT.offsetWidth; 
        iH = eT.offsetHeight; 
     
        var eC = document.getElementById(g_sImgCont);
        sStyle = "position:absolute;top:"+iY+"px; height:"+iH+"px; left:"+iX+"px;width:"+iW+"px; z-index:3; overflow:hidden; background-color: #ffffff;";
        eC.style.cssText=sStyle; 

        var eI = document.getElementById(g_sImgCont+"_img");
        sStyle = "position:absolute;top:0px; bottom:0px; left:0px; right:"+(g_DescSize+g_iPad)+"px; overflow: hidden; /*background-color: #88ff88;*/";
        eI.style.cssText=sStyle; 
    
        var eD = document.getElementById(g_sImgCont+"_dsc");
        sStyle = "position:absolute;top:0px; bottom:0px; width:"+g_DescSize+"px; right:0px; overflow: auto; /*background-color: #8888ff;*/";
        eD.style.cssText=sStyle; 

        var eC = document.getElementById(g_sImgCont);

	// scale and place image
        var eI = eC.firstChild;

        var ei0 = eI.firstChild;

	// the following 4 lines must be done  - 
        // otherwise IE7 will never get the offsetXXX the second time
        w = eI.offsetWidth;
        h = eI.offsetHeight;
	ei0.setAttribute("width", 0);
	ei0.setAttribute("height",0);

        // image size & aspect
        wi = g_iimg.width;
        hi = g_iimg.height;
        fi = 1.0*wi/hi;

	// container size & aspect
	w0 = eI.offsetWidth;
        h0 = eI.offsetHeight;
        f = (1.0*w0)/h0;
	
	// scale image
	if (f > fi) {
	    w0 = h0*fi;
	    h0 = h0;
	} else {
	    w0 = w0;
            h0 = w0/fi;
        }

	ei0.setAttribute("width", w0);
	ei0.setAttribute("height",h0);


    } 
}

