/**
* Provides following functionalities:
*
* 1) Generating Code for displaying a popup-image
* 2) ID needs to start with "image"
*
*/
var ww = "";
var id = 0;
var t  = "";
var token = true;
var aktiv = window.setInterval("verifyImagesLoop()", 200);
var wasRunning =0;
var defaultStartWidth = 50;
var ipHeaderLocal = "172.1";
var magnifyServer = "";
var defaultClassName = "imageLeftFloatCaptionPopup";
//var uriPrefix = "/Home/Basic/Magnify_Image/index.htm?";
var uriPrefix = "/idcplg?IdcService=SS_GET_PAGE&nodeId=368&";


function showImageWithDownload(urlThumb, caption, downloadUrl, downloadCaption, size)
{

    showImage2(urlThumb, caption,
    "<br /><img src='/groups/jsp/documents/fragmentassets/icon_download_coloured.gif' class='navigationIcon' alt='[Enlarge picture]' border='0'>"
    +"<a class='contentColouredAnkerBox' href=\""+downloadUrl+"\">"+downloadCaption+"</a>"
    ,
    defaultStartWidth
    );
}

function showMagnifiedImageNoP(url, urlLarge, caption, captionLarge, thumbSize)
{
	//thumbSize = !thumbSize ? 50:thumbSize;
	className = defaultClassName;
	var keepSize = thumbSize!=-1 && thumbSize > defaultStartWidth;
	id++;
	captionLarge = captionLarge ? captionLarge:caption;
	var anImage = new Image();
	anImage.src = url;
	var width = !thumbSize || thumbSize==-1
	            ?
	            (
	              (anImage.width >= defaultStartWidth
	               ?
	               anImage.width
	               :
	               defaultStartWidth
	               )
	            )
	            :
	            thumbSize;

	document.writeln("<div class=\""+className+"\">");
	document.writeln("<div id='"+(keepSize ? "XX":"")+"image"+id+"div'  style='width: " + width + "px;'>");
	document.writeln("<img id='"+(keepSize ? "XX":"")+"image"+id+"' style='cursor:pointer;' src='"+url+"' "
	  +(keepSize ? "width='" + width + "'":"")+" alt='"+caption+"' border='0' "
	  +"onclick='javascript:"+getMagnifyLink(urlLarge, captionLarge)+"' />");
	document.write(getCaptionMagnify(urlLarge, caption, captionLarge, false, true));

}

function showMagnifiedImage(url, urlLarge, caption)
{
  showMagnifiedImage(url, urlLarge, caption, caption, -1);
}

function showMagnifiedImage(url, urlLarge, caption, caption2)
{
  showMagnifiedImage(url, urlLarge, caption, caption2, -1);
}

function showMagnifiedImage(url, urlLarge, caption, captionLarge, thumbSize)
{
  showMagnifiedImage(url, urlLarge, caption, captionLarge, thumbSize, defaultClassName);
}
function showMagnifiedImage(url, urlLarge, caption, captionLarge, thumbSize, className)
{
	//thumbSize = !thumbSize ? 50:thumbSize;
	className = className ? className:defaultClassName;
	var keepSize = thumbSize!=-1 && thumbSize > defaultStartWidth;
	id++;
	captionLarge = captionLarge ? captionLarge:caption;
	var anImage = new Image();
	anImage.src = url;
	//if(!anImage)alert("Image Object '"+url+"' is not loadable/readable!");
	var width = !thumbSize || thumbSize==-1
	            ?
	            (
	              (anImage.width >= defaultStartWidth
	               ?
	               anImage.width
	               :
	               defaultStartWidth
	               )
	            )
	            :
	            thumbSize;

	//document.writeln((className+": anImage.width="+anImage.width+ " / width=" + width + " / defaultStartWidth="+defaultStartWidth);
	document.writeln("<div class=\""+className+"\">");
	document.writeln("<div id='"+(keepSize ? "XX":"")+"image"+id+"div'  style='width: " + width + "px;addding:10px'>");
	document.writeln("<img id='"+(keepSize ? "XX":"")+"image"+id+"' style='cursor:pointer;argin:5px' src='"+url+"' "
	  +(keepSize ? "width='" + width + "'":"")+" alt='"+caption+"' border='0' "
	  +"onclick='javascript:"+getMagnifyLink(urlLarge, captionLarge)+"' />");
	//fnFitImage(id, width);
	putCaptionMagnify(urlLarge, caption, captionLarge);
}

//#####################################
/**
* No Magnify, only caption
*/
function showImage(url, caption)
{
  showImage2(url, caption, caption, 30);
}

function showImage(url, caption, thumbSize)
{
  showImage2(url, caption, "", thumbSize);
}
function showImage2(url, caption, caption2, thumbSize)
{
	caption2 = caption2 ? caption2:"";
	id++;
	//captionLarge = captionLarge ? captionLarge:caption;
	var anImage = new Image();
	anImage.src = url;
	var width = (anImage.width >= thumbSize ) ? anImage.width : thumbSize;
	document.writeln("<div class=\"imageLeftFloatCaptionPopup\">");
	document.writeln("<div id='image"+id+"div'     style='width: " + width + "px;'>");
	document.writeln("<img id='image"+id+"' src='"+url+"' idth='" + width + "' alt='"+caption+"' border='0' />");
	putCaption(caption+caption2);
}

function showImageNoP(url, caption, caption2, thumbSize)
{
	caption2 = caption2 ? caption2:"";
	id++;
	var anImage = new Image();
	anImage.src = url;
	var width = (anImage.width >= thumbSize ) ? anImage.width : thumbSize;
	document.writeln("<div class=\"imageLeftFloatCaptionPopup\">");
	document.writeln("<div id='image"+id+"div'     style='width: " + width + "px;'>");
	document.writeln("<img id='image"+id+"' src='"+url+"' idth='" + width + "' alt='"+caption+"' border='0' />");
	putCaption2(caption, false, true);
}

function putCaption(caption)
{
  var temp = ""
  +(caption ? "<p>"+caption+"</p>":"")
  +"</div></div>";
  document.writeln(temp);
}

function putCaption2(caption, embedP, terminate, useClass)
{
  var useClass_ = useClass ? true:false;
  var temp = ""
  +(embedP ? "<p>"+(caption ? caption:"")+"</p>":(caption ? "<span class='contentColouredAnkerBox'>"+caption+"</span>":""))
  +(terminate ? "</div></div>":"");
  document.writeln(temp);
}

function putCaptionMagnify(url, caption, captionLarge)
{
  document.writeln(getCaptionMagnify(url, caption, captionLarge, true, true));
}
function getCaptionMagnify(url, caption, captionLarge)
{
  return getCaptionMagnify(url, caption, captionLarge, true, true);
}
function getCaptionMagnify(url, caption, captionLarge, embedP)
{
  return getCaptionMagnify(url, caption, captionLarge, embedP, true);
}
function getCaptionMagnify(url, caption, captionLarge, embedP, terminate)
{

  var temp =  url.toLowerCase();
  var isNotIMG =   temp.indexOf(".gif")==-1
                && temp.indexOf(".png")==-1
                && temp.indexOf(".jpg")==-1;
  var server = location.href.indexOf(ipHeaderLocal)==-1 ? "":magnifyServer;
  var temp = ""
  + (embedP ? "<p>":"")+"<a href='"+(
                    //isNotIMG
                    //?
                    url
                    //:
                    //"javascript:" + getMagnifyLink(url, captionLarge)
                    )
  +"'"
  +" class='contentColouredAnkerBox' title='"+caption+"' onclick='"+"javascript:" + getMagnifyLink(url, captionLarge)+";return false;'>"
  +"<img src='/groups/jsp/documents/fragmentassets/icon_link_coloured_picture.gif' class='navigationIcon' alt='[Enlarge picture]' border='0'>"
  +caption+"</a>"+ (embedP ? "</p>":"")+(terminate ? "</div></div>":"");
  return (temp);
}

/**
 Providing the javascript call to open Window with Image-viewer URL*/
function getMagnifyLink(url, caption)
{
  var server = location.href.indexOf(ipHeaderLocal)==-1 ? "":magnifyServer;
  return "var magnifyWin = openWindow(\""
  +server + uriPrefix
  +"imageUrl="    + encodeURIComponent(url)
  +"&imageCaption="+ encodeURI(caption)
  +"\","
  +" 400, 200, \"XXX\", \"resizable=yes,scrollbars=yes\", false);"
}

function openWindow(url, w, h, winName, winDescr, centered)
{
	//winDescr = dependent=
	if(!winName){
		winName = '';
	}
	if(!winDescr){
		winDescr = '';
	}
	else{
		winDescr = ',' + winDescr;
	}
	var availX = screen.availWidth;
	var availY = screen.availHeight;
	if(availX < w) w = availX;
	if(availY < h) h = availX;
	var x = Math.round((availX - w)/ 2);
	var y = 20;//Math.round((availY - h) / 2);
	if(centered)
	  winDescr = 'left=' + x + ',top=' + y + ',width=' + w + ',height=' + h + winDescr;
	else
	  winDescr = 'left=' + 0 + ',top=' + 0 + ',width=' + w + ',height=' + h + winDescr;

	ww = open (url, winName, winDescr);
	ww.focus();
}

/**
* Automatically adjusts the width of the DIV-tag surrounding the image.
* Requirements:
* 1) ID of the name tag needs to be unique
* 2) ID needs to start with "image"
*
*/
var loadingImages = 0;
function verifyImagesLoop()
{

  //if(token)return;
  var Status = "";
  var Status2 = "";
  var loaded = 0;
  var loading = 0;
  for (i = 0; i < document.images.length; ++i) {
    if(document.images[i].id.indexOf("image")!=0)
    {
      Status2 +="# SKIP: id="+document.images[i].id;
      continue;
    }
    loadingImages++;
    Status +=" id="+document.images[i].id+" ("+document.images[i].src+")\n";
    if (document.images[i].complete == true)
    {
      fnFitImage2(document.images[i].id, document.images[i].width);
      loaded++;
    } else {
      loading++;
    }

  }

  if(loadingImages==loaded)
  {
    window.clearInterval(aktiv);
    //alert("Statistik: "+loaded+" loaded, calls: "+wasRunning+"\n"+Status);
    //alert(Status2);
    //document.writeln((Status2);
  }

}

function fnFitImage2(id2, width)
{
	wasRunning++;
	t +=("Fitting: "+id2+"<br>");
	var NS = (navigator.appName=="Netscape")?true:false;
	width = (width >= defaultStartWidth ) ? width : defaultStartWidth;
	document.getElementById(id2+"div").style.width = width + 'px';
	//alert(document.getElementById(id2+"div").style.width);
	document.getElementById(id2+"").width = width;
}

