// **********************************************************************************************
// Random-Image-Function
// **********************************************************************************************
function DynamicRandomImage(pUrl, pImgName, pInterval, pImagePath, pImageCount, pDynamic, pRecursive, pIgnore, pSort, pSortOrder)
{
  this.url = pUrl;
  this.imgName = pImgName;
  this.interval = pInterval;
  this.imagePath = pImagePath;
  this.imageCount = pImageCount;
  this.dynamic = pDynamic;
  this.recursive = pRecursive;
  this.ignore = pIgnore;
  this.sort = pSort;
  this.sortorder = pSortOrder;

  this.arrImg = new Array(pImageCount);
  this.imgNumber = 0;
  this.zufallsZahl = 0;
  this.xmlhttp = GetXMLObject();

  DynamicRandomImage.prototype.randomize = function()
  {
    var thisObject = this;

    if (this.imgNumber != 0) {if (this.imgNumber > this.imageCount) {this.imgNumber = 1;}}
    if (this.imgNumber == 0) {var urlArgs = this.url + "?path=" + this.imagePath + "&count=" + this.imageCount + "&dynamic=" + this.dynamic + "&recursive=" + this.recursive + "&ignore=" + this.ignore + "&t=" + new Date().getTime();} else {var urlArgs = this.url + "?path=" + this.imagePath + "&dynamic=" + this.dynamic + "&recursive=" + this.recursive + "&count=1&ignore=" + this.ignore + "&sort=" + this.sort + "&sortorder=" + this.sortorder + "&t=" + new Date().getTime();}
    for (var i = 1; i <= this.imageCount; i++) {urlArgs = urlArgs + "&img" + i + "=" + this.arrImg[i-1];}

    try
    {
        if (this.imgNumber != 0)
        {
          // get one new random image
          this.xmlhttp.open("GET", urlArgs, false);
          this.xmlhttp.send(null)
          var xmlResponse = this.xmlhttp.responseText;

          // get random image number to replace
          var a = Math.random();
          a *= this.imageCount;
          a = Math.ceil(a);
          if (a == this.zufallsZahl) {if (a+1 > this.imageCount) {a = 1;} else {a++;}};

          // replace image source and update current image array
          document.getElementById(this.imgName.concat(a)).src = xmlResponse;
          this.arrImg[a-1] = xmlResponse;
          this.imgNumber = a;
          this.zufallsZahl = a;
        }
        else
        {
          // first time -> save current images in array
          for (var i=0; i < this.imageCount; i++)
          {
            var img = document.getElementById(this.imgName.concat(i+1)).src.toString().substr(document.getElementById(this.imgName.concat(i+1)).src.toString().indexOf("fileadmin"));
            this.arrImg[i] = img;
          }
          this.imgNumber++;
        }
    }
    catch(e) {  }
    window.setTimeout( function () { thisObject.randomize(); }, this.interval);
  }
  
  function GetXMLObject()
  {
    var xmlhttp=false;
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
    if (!xmlhttp && typeof XMLHttpRequest!="undefined") {
      try {
        xmlhttp = new XMLHttpRequest();
      } catch (e) {
        xmlhttp=false;
      }
    }
    if (!xmlhttp && window.createRequest) {
      try {
        xmlhttp = window.createRequest();
      } catch (e) {
        xmlhttp=false;
      }
    }
    return xmlhttp;
  }  
}

function RandomImage_MouseOver(pId, pIdImage, pIdMessage, pImg, pMarginLeft, pMarginTop, pImagePath, pBigImagePath, pPositionLeft, pPositionTop)
{
  this.id = pId;
  this.idImage = pIdImage;
  this.idMessage = pIdMessage;
  this.img = pImg;
  this.marginLeft = pMarginLeft;
  this.marginTop = pMarginTop;
  this.imagePath = pImagePath;
  this.bigImagePath = pBigImagePath;
  this.positionLeft = pPositionLeft;
  this.positionTop = pPositionTop;

  this.bigImage = null;
  this.bigImage = new Image();
  
  RandomImage_MouseOver.prototype.Main = function()
  {
    // set position of div
    RandomImage_SetPosition(this.id, this.positionLeft, this.marginLeft, this.positionTop, this.marginTop, this.img);
  
    // Show "Loading-Image ..." message
    document.getElementById(this.idImage).style.display = "none";
    document.getElementById(this.id).style.display = "block";
    document.getElementById(this.idMessage).style.display = "block";

    // load big image
    this.bigImage.src = this.img.src.toString().replace(this.imagePath, this.bigImagePath);
    this.bigImage.load = this.loadCompleted();  
  }

  // Loading Completed function
  RandomImage_MouseOver.prototype.loadCompleted = function()
  {
    if (this.bigImage.complete == false)
    {
      var thisObject = this;
      setTimeout(function() { thisObject.loadCompleted() }, 50);
      return (false);
    }
    document.getElementById(this.idMessage).style.display = "none";
    document.getElementById(this.idImage).src = this.bigImage.src.toString();
    document.getElementById(this.idImage).style.display = "block";  
  }
}

// Hide big image when mouse out
function RandomImage_MouseOut(pId, pIdImage, pIdMessage)
{
  document.getElementById(pId).style.display = "none";
  document.getElementById(pIdMessage).style.display = "none";
  document.getElementById(pIdImage).style.display = "none";
}

// Show big image on load
function RandomImage_ShowOnLoad(id, positionLeft, marginLeft, positionTop, marginTop, img, imgID)
{
  // set position of div and display the div
  RandomImage_SetPosition(id, positionLeft, marginLeft, positionTop, marginTop, img);
  document.getElementById(id).style.display = "block";
  document.getElementById(imgID).style.display = "block";  
}


// Helper: set position of div for big image
function RandomImage_SetPosition(id, positionLeft, marginLeft, positionTop, marginTop, img)
{
  document.getElementById(id).style.position = "absolute";
  if (positionLeft == 'relative') {
    document.getElementById(id).style.left = marginLeft + findPosX(img) + img.width + "px";
  } else {
    document.getElementById(id).style.left = marginLeft + "px";
  }
  if (positionTop == 'relative') {
    document.getElementById(id).style.top = marginTop + findPosY(img) + "px";
  } else {
    document.getElementById(id).style.top = marginTop + "px";  
  }
}

// Helper: find x-position of a object
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


// Helper: find y-position of a object
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
