// **********************************************************************************************
// Random-Image-Function
// **********************************************************************************************
// Global Variables
var objLastPosition = new Array();

// Dynamic Change of random images
function DynamicRandomImage(pUrl, pImgName, pInterval, pImagePath, pImageCount, pDynamic, pRecursive, pIgnore, pIgnoreFolder, pIgnoreFile, pSort, pSortOrder, pImageMaxW, pImageMaxH, pBigImageMaxW, pBigImageMaxH)
{
  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.ignoreFolder = pIgnoreFolder;
  this.ignoreFile = pIgnoreFile;
  this.sort = pSort;
  this.sortorder = pSortOrder;
  this.imageMaxW = pImageMaxW;
  this.imageMaxH = pImageMaxH;
  this.bigImageMaxW = pBigImageMaxW;
  this.bigImageMaxH = pBigImageMaxH;
  this.tempImage = null;
  this.a = -1;

  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 + "&ignorefolder=" + this.ignoreFolder + "&ignorefile=" + this.ignoreFile + "&t=" + new Date().getTime();} else {var urlArgs = this.url + "?path=" + this.imagePath + "&dynamic=" + this.dynamic + "&recursive=" + this.recursive + "&count=1&ignore=" + this.ignore + "&ignorefolder=" + this.ignoreFolder + "&ignorefile=" + this.ignoreFile + "&sort=" + this.sort + "&sortorder=" + this.sortorder + "&imw=" + this.imageMaxW + "&imh=" + this.imageMaxH + "&bmw=" + this.bigImageMaxW + "&bmh=" + this.bigImageMaxH + "&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;
          
          if (xmlResponse != '')
          {
            // 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++;}};

            // save big imagesource in div and replace image source of current image
            var arrTemp = xmlResponse.split("x;x");
            this.a = a;
            this.tempImage = new Image();
            this.tempImage.src = arrTemp[1];
            this.tempImage.load = this.loadCompleted();
            document.getElementById('bi_'.concat(this.imgName).concat('_').concat(a)).innerHTML = arrTemp[2];
          
            // update current image array
            this.arrImg[a-1] = arrTemp[0];
            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("fi_".concat(this.imgName).concat("_").concat(i+1)).innerHTML.toString();
            this.arrImg[i] = img;
          }
          this.imgNumber++;
        }
    }
    catch(e) {  }
    window.setTimeout( function () { thisObject.randomize(); }, this.interval);
  }
  
  // Image Loading Completed function
  DynamicRandomImage.prototype.loadCompleted = function()
  {
    if (this.tempImage.complete == false)
    {
      var thisObject = this;
      setTimeout(function() { thisObject.loadCompleted() }, 50);
      return (false);
    }
    if (document.getElementById(this.imgName.concat(this.a)))
    {
      document.getElementById(this.imgName.concat(this.a)).src = this.tempImage.src;
      document.getElementById(this.imgName.concat(this.a)).width = this.tempImage.width;
      document.getElementById(this.imgName.concat(this.a)).height = this.tempImage.height;
    }
  }

  // Get xml object (crossbrowser valid)
  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, pImgName, pMarginLeft, pMarginTop, pPositionLeft, pPositionTop, pImgNumber)
{
  this.id = pId;
  this.idImage = pIdImage;
  this.idMessage = pIdMessage;
  this.img = pImg;
  this.imgName = pImgName;
  this.marginLeft = pMarginLeft;
  this.marginTop = pMarginTop;
  this.positionLeft = pPositionLeft;
  this.positionTop = pPositionTop;
  this.imageNumber = pImgNumber;

  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, this.imgName);
  
    // 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 = document.getElementById('bi_'.concat(this.imgName).concat('_').concat(this.imageNumber)).innerHTML;
    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, smallImageName)
{
  // set position of div and display the div
  RandomImage_SetPosition(id, positionLeft, marginLeft, positionTop, marginTop, img, smallImageName);
  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, smallImageName, objLastPos)
{
  if (objLastPos)
  {
    id = objLastPos.id;
    positionLeft = objLastPos.positionLeft;
    marginLeft = objLastPos.marginLeft;
    positionTop = objLastPos.positionTop;
    marginTop = objLastPos.marginTop;
    img = objLastPos.img;
    smallImageName = objLastPos.smallImageName;
  }

  document.getElementById(id).style.position = "absolute";

  if (positionLeft == 'relative') {
    document.getElementById(id).style.left = marginLeft + findPosX(img) + img.width + "px";
  } else if (positionLeft == 'fixed') {
    document.getElementById(id).style.left = marginLeft + "px";
  } else if (positionLeft == 'relativefixed') {
    tempImage = document.getElementById(smallImageName + '1');
    document.getElementById(id).style.left =  marginLeft + findPosX(tempImage) + "px";
  }

  if (positionTop == 'relative') {
    document.getElementById(id).style.top = marginTop + findPosY(img) + "px";
  } else if (positionTop == 'fixed') {
    document.getElementById(id).style.top = marginTop + "px";  
  } else if (positionTop == 'relativefixed') {
    tempImage = document.getElementById(smallImageName + '1');
    document.getElementById(id).style.left =  marginTop + findPosY(tempImage) + "px";
  }

  if (!objLastPosition[id]) {objLastPosition[id] = new RandomImage_LastPosition();}
  objLastPosition[id].setValues(id, positionLeft, marginLeft, positionTop, marginTop, img, smallImageName);
}

// Helper: object for last position of big image
function RandomImage_LastPosition()
{
  this.id = "";
  this.positionLeft = "";
  this.marginLeft = "";
  this.positionTop = "";
  this.marginTop = "";
  this.img = "";
  this.smallImageName = "";

  RandomImage_LastPosition.prototype.setValues = function(pId, pPositionLeft, pMarginLeft, pPositionTop, pMarginTop, pImg, pSmallImageName)
  {
    this.id = pId;
    this.positionLeft = pPositionLeft;
    this.marginLeft = pMarginLeft;
    this.positionTop = pPositionTop;
    this.marginTop = pMarginTop;
    this.img = pImg;
    this.smallImageName = pSmallImageName;
  }
}

// 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;
}