var ImageSource = new Image();
var ImageLoading = new Image();
var ImageViewerStatus = "off";
var padding_corrector = (document.all) ? 2 : 0; 

ImageLoading.src = WEBDIR+"images/load.gif";

function calcThumbDimensions(nowwidth,nowheight,maxwidth,maxheigth)
{
    var newwidth=maxwidth;
    var newheight=maxheigth;

    var width = nowwidth;
    var height= nowheight;
    
    if(width > height)
    {
                     if(width > newwidth)
                       {
                           var newheight2=newheight;
                           newheight=height / (width / newwidth);
                           if (newheight>newheight2)
                           {
                              newheight=newheight2;
                              newwidth=width/(height/newheight);
                            }
                         }
                    else
                    {
                       newheight=height;
                       newwidth=width;
                     }                           
    }
    else if (width < height)
    {
        if(height>newheight)
        {
          var newwidth2=newwidth;
          newwidth = width / (height / newheight);
          if(newwidth > newwidth2)
          {
            newwidth=newwidth2;
            newheight=height / (width / newwidth);
          }
         }
         else
         {
           newheight=height;
           newwidth=width;
         }                               
    }
    else
    {
        if(width > newwidth)
        {
          newwidth = newheight;
        }
        else
        {
          newheight=height;
          newwidth=width;
        }                           
    }
    imgwidth = Math.round(newwidth);
    imgheigth= Math.round(newheight);
    return imgwidth + '-' + imgheigth;
}


function loadImage(start, end, mom, objectid)
{
//    ImageViewerStatus = ImageViewerStatus == "loading" ? "loading" : "start_loading";
    ImageViewerStatus = "start_loading";
    ImageSource = new Image();
    var imgSrc = view[objectid+'-'+mom+'-bild'];
    ImageSource.src = imgSrc;
    checkImage(start, end, mom, objectid);
}

function checkImage(start, end, mom, objectid)
{
    if(ImageViewerStatus == "off")
    {
        return true;
    }
    if(ImageSource.complete == true)
    {
        /*
        if(ImageSource.width>398)
        {
            var newWidth=398;
            var newHeight=Math.round((398*ImageSource.height)/ImageSource.width);
        }
        else
        {
            var newWidth=ImageSource.width;
            var newHeight=ImageSource.height;        
        }
        */
        
        /*showImage(ImageSource.src, ImageSource.width, ImageSource.height);*/
        var wertekomplett = calcThumbDimensions(ImageSource.width,ImageSource.height,398,398)
        var werte = wertekomplett.split("-");
        
        var newWidth=werte[0];
        var newHeight=werte[1];
        
        var preload=false;
        showImage(start, end, mom, newWidth, newHeight, objectid, preload);
        ImageViewerStatus = "showing";
        return true;
    }
    if(ImageViewerStatus == "start_loading")
    {
        ImageViewerStatus = "loading";
        var preload=true;
        showImage(start, end, mom, newWidth, newHeight, objectid, preload);        
        //showImage2(ImageLoading.src, ImageLoading.width, ImageLoading.height);
    }
    if(ImageViewerStatus == "loading")
    {
        window.setTimeout("checkImage("+start+","+end+","+mom+","+objectid+","+preload+")", 150);
    }
    return true;
}

function showImage(start, end, mom, boxwidth, boxheight, objectid, preload)
{
    select1 = document.getElementById("select1");
    if(select1)
    {   
        select1.style.display="none";
    }   
    
    viewer     = document.getElementById("ImageViewer");
    if(!viewer)
    {
        document.body.innerHTML = document.body.innerHTML + '<div id="ImageViewer"><\/div>';
        viewer     = document.getElementById("ImageViewer");
    }
    if(viewer)
    {
        viewer.align = "left";
        viewer.style.display="none";
        viewer.style.position="absolute";
        viewer.style.border="1px solid black";
        viewer.style.textAlign="center";
        viewer.style.backgroundColor="#fff";
        
        
        if(preload)
        {
          var xcolor = 'FFFFFF';
        }
        else
        {
          var xcolor = 'E2E2E2';
        }
        
        textInhalt ='<table border="0" cellpadding="0" cellspacing="0" width="550" height="440" style="width:550px;vertical-align:top;margin-left:auto;margin-right:auto">'
          
        + '<tr><td colspan="2"><div style="padding:10px;padding-right:3px;text-align:right"><a href="#" onclick="closeImage(); return false;"><img style="border-width:0px; width:38px; height:9px;" src="' + WEBDIR + 'images/close3.gif" alt="Schlie&szlig;en"><\/a></div>'
        + '</td></tr>'
        
        + '<tr><td valign="top" height="400" width="400" style="text-align:center;vertical-align:middle;background-color:#' + xcolor + ';border:1px solid #B1B1B1;"><div style="padding: 0px; margin: 0px;" id="ImageViewerContent"><\/div></td>'
        
        + '<td style="vertical-align:top;text-align:left;">'
        
        + '<table border="0" cellspacing="0" cellpadding="0" width="150" style="width:150px;">'
        
        + '<tr>'
        
        + '<td><form action="wohnansichten.php" method="post">'
        + '<input type="hidden" name="id" value="' + view[objectid+'-'+mom+'-id'] + '">'
        + '<input type="hidden" name="vote" value="1">'
        
        + '<table cellspacing="0" cellpadding="0" border="0"><tr><td colspan="2"><b>' + VOTE_HEADER + '</b></td></tr>'
        
        + '<tr><td>&nbsp</td></tr>';
        
        if(view[objectid+'-'+mom+'-votingok']==1)
        {
        textInhalt = textInhalt + '<tr><td><input style="width:20px;" type="radio" name="ergebnis" value="1" checked="checked"></td><td valign="top"><span class="starboxOutside"><span class="starInside" ><img src="' + WEBDIR + 'images/px_trans.gif" width="16" height="15" border="0" alt="' + RADIO1 + '" title="' + RADIO1 + '"></span><img src="' + WEBDIR + 'images/px_trans.gif" width="63" height="15" border="0" alt="' + RADIO1 + '" title="' + RADIO1 + '"></span></td></tr>'
        
        + '<tr><td><input style="width:20px;" type="radio" name="ergebnis" value="2"></td><td valign="top"><span class="starboxOutside"><span class="starInside" ><img src="' + WEBDIR + 'images/px_trans.gif" width="33" height="15" border="0" alt="' + RADIO2 + '" title="' + RADIO2 + '"></span><img src="' + WEBDIR + 'images/px_trans.gif" width="46" height="15" border="0" alt="' + RADIO2 + '" title="' + RADIO2 + '"></span></td></tr>'
        
        + '<tr><td><input style="width:20px;" type="radio" name="ergebnis" value="3"></td><td valign="top"><span class="starboxOutside"><span class="starInside" ><img src="' + WEBDIR + 'images/px_trans.gif" width="47" height="15" border="0" alt="' + RADIO3 + '" title="' + RADIO3 + '"></span><img src="' + WEBDIR + 'images/px_trans.gif" width="34" height="15" border="0" alt="' + RADIO3 + '" title="' + RADIO3 + '"></span></td></tr>'
        
        + '<tr><td><input style="width:20px;" type="radio" name="ergebnis" value="4"></td><td valign="top"><span class="starboxOutside"><span class="starInside" ><img src="' + WEBDIR + 'images/px_trans.gif" width="63" height="15" border="0" alt="' + RADIO4 + '" title="' + RADIO4 + '"></span><img src="' + WEBDIR + 'images/px_trans.gif" width="16" height="15" border="0" alt="' + RADIO4 + '" title="' + RADIO4 + '"></span></td></tr>'
        
        + '<tr><td><input style="width:20px;" type="radio" name="ergebnis" value="5"></td><td valign="top"><span class="starboxOutside"><span class="starInside" ><img src="' + WEBDIR + 'images/px_trans.gif" width="79" height="15" border="0" alt="' + RADIO5 + '" title="' + RADIO5 + '"></span></span></td></tr>'
        
        + '<tr><td>&nbsp</td></tr>'
        
        + '<tr><td colspan="2"><input type="submit" class="submit" value="&nbsp;&nbsp;&nbsp;' + VOTE_BUTTON + '&nbsp;&nbsp;&nbsp;" style="width:auto;"></td></tr>'
        }
        else
        {
        textInhalt = textInhalt + '<tr><td>' + VOTE_ALLREADY + '</td></tr>';
        }
        
        + '</form></td>'
        
        + '</tr>'
        
        + '</table>'
        
        + '</td></tr>';
        /*   
        
        if(!preload)
        {
          textInhalt += '<tr><td style="vertical-align:top;color:#000000; text-align:left;">';
  
  
          textInhalt += '<table border="0" cellpadding="0" cellspacing="0" width="400" style="width:400px">';
          textInhalt += '<tr><td colspan="2" align="center" style="padding-top:10px">' + view[objectid+'-'+mom+'-beschreibung'] + '</td></tr>';
          textInhalt += '<tr><td align="left" width="50%" style="padding:10px 0px;">';
          
          if(mom>1)
          {
              zuruck=mom-1;
              textInhalt += '<br><a style="background-color:#E2E2E2;border:1px solid #B1B1B1;text-decoration:none;color:#333;padding-right:20px;padding-left:20px;" href="#" onclick="loadImage(' + start + ',' + end + ',' + zuruck + ',' + objectid + ');return false;"><b>&laquo; zurück</b></a>';
          }
          else
          {
              textInhalt += '&nbsp;';                        
          }
          textInhalt += '</td><td align="right" width="50%" style="padding:10px 0px;">';        
          if(mom<end)
          {
              weiter=mom+1;
              textInhalt += '<br><a style="background-color:#E2E2E2;border:1px solid #B1B1B1;text-decoration:none;color:#333;padding-right:20px;padding-left:20px;" href="#" onclick="loadImage(' + start + ',' + end + ',' + weiter + ',' + objectid + ');return false;"><b>weiter &raquo;</b></a>';
          }
          else
          {
              textInhalt += '&nbsp;';
          }
  
          textInhalt += '</td></tr>';
        }
        else
        {
          textInhalt += '<tr><td style="vertical-align:top;color:#000000; text-align:left;" height="100">';
          textInhalt += '&nbsp;';          
          textInhalt += '</td></tr>';                  
        }  
        */      
        
        textInhalt += '</table>';        
        
       viewer.innerHTML = textInhalt; 

        if(document.all)
        {
            boxtop    = (document.body.scrollTop > document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) + (-52);
            boxleft   = (document.body.scrollLeft > document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) + 60;
        } else {
            boxtop    = window.pageYOffset + (-52);
            boxleft   = window.pageXOffset + 60;
        }

        imgheight = boxheight;
        imgwidth  = boxwidth;
        //boxheight = boxheight + 99;
        //boxwidth  = boxwidth + 40 + padding_corrector;
        boxheight = 480;
        boxwidth=590;

        viewer.style.top  = boxtop;
        viewer.style.left = boxleft;
        viewer.style.width  = boxwidth;
        viewer.style.height = boxheight;
        viewer.style.zIndex = 2000;
        

        viewer_con = document.getElementById("ImageViewerContent");
        if(preload)
        {
          viewer_con.innerHTML = '<img width="106" height="36" src="' + WEBDIR + 'images/load.gif">';
          //window.alert(viewer_con.innerHTML);
        }
        else
        {
          viewer_con.innerHTML = '<img width="' + imgwidth +'" height="' + imgheight + '" src="' + view[objectid+'-'+mom+'-bild'] + '">';        
        }

        viewer.style.display = "inline";
    }
    return true;
}

function showImage2(imgSrc, boxwidth, boxheight)
{
    
    viewer     = document.getElementById("ImageViewer");
    if(!viewer)
    {
        document.body.innerHTML = document.body.innerHTML + '<div id="ImageViewer"><\/div>';
        viewer     = document.getElementById("ImageViewer");
    }
    if(viewer)
    {
        viewer.align = "right";
        viewer.style.display="none";
        viewer.style.position="absolute";
        viewer.style.border="1px solid black";
        viewer.style.backgroundColor="white";
        viewer.innerHTML = 
          '<div style="padding: 10px;"><a class="admin_link" href=""' 
        + 'onclick="closeImage(); return false;"><img style="border-width: 0px;'
        + 'width:38px; height: 9px;" src="'+WEBDIR+'images/close3.gif" alt="Schlie&szlig;en"><\/a><br>'
        + '<div style="padding: 0px; margin: 0px;" id="ImageViewerContent"><\/div><\/div>';

        if(document.all)
        {
            boxtop    = document.body.scrollTop + 50;
            boxleft   = document.body.scrollLeft + 55;
        } else {
            boxtop    = window.pageYOffset + 50;
            boxleft   = window.pageXOffset + 55;
        }
        //boxheight = boxheight + 59;
        //boxwidth  = boxwidth + 40 + padding_corrector;
        boxheight = 500;
        boxwidth=440;

        viewer.style.top  = boxtop;
        viewer.style.left = boxleft;
        viewer.style.width  = boxwidth;
        viewer.style.height = boxheight;

        viewer_con = document.getElementById("ImageViewerContent");
        viewer_con.innerHTML = '<img style="margin-top:10px;margin-right:10px;" src="' + imgSrc + '">';

        viewer.style.display = "inline";
    }
    return true;
}


function closeImage()
{
    select1 = document.getElementById("select1");
    if(select1)
    {   
        select1.style.display="inline";
    }        
    
    ImageViewerStatus = "off";
    viewer = document.getElementById("ImageViewer");
    if(viewer)
    {
        viewer.style.display = "none";
        return true;
    }
    return false;
}


/*
    if(!viewer)
    {
        document.body.innerHTML = document.body.innerHTML 
        + '<div align="right" id="ImageViewer" style="display: none;'
        + 'z-index: 1000; background-color: white;'
        + 'border: 1px solid black;">'
        + '<div style="padding: 20px;"><a href=""' 
        + 'onclick="closeImage(); return false;"><img style="border-width: 0px;'
        + 'width:38px; height: 9px;" src="images/close3.gif" alt="Schlie&szlig;en"><\/a><br>'
        + '<div style="padding: 0px; margin: 0px;" id="ImageViewerContent"><\/div><\/div><\/div>';
//        alert(document.body.innerHTML);
        showImage(imgSrc, boxwidth, boxheight);
        return false;
    }
*/    
