﻿ function getElementLeft(Elem) {
      xPos = Elem.offsetLeft;
      tempEl = Elem.offsetParent;
      while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
      }
      return xPos;
    }
 
    function getElementTop(Elem) {
      yPos = Elem.offsetTop;
      tempEl = Elem.offsetParent;
      while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
      }
      return yPos;
    }
 
    var growTimeoutId;
    var randTimeoutId;
 
    var startWidth = 120;
    var startHeight = 90;
    var growWidth = 150;
    var growHeight = 112;
    var extraWidth = 15;
    var extraHeight = 15;
 
    var curWidth;
    var curHeight;
    var curLeft;
    var curTop;
 
    var baseWidth;
    var baseHeight;
    var baseLeft;
    var baseTop;
 
    var cLeft;
    var cRight;
    var cTop;
    var cBottom;
 
    var growTimeoutId;
    
    //****For image popup
    var idOfTheOwner;
    var listOrderOfTheImage;
    var typeOfTheImage;
    //****
    
    function enlargeThumb(imgObj,imgOwnerID,imgListOrder,imgImageType) {
      
      idOfTheOwner = imgOwnerID;
      listOrderOfTheImage = imgListOrder;
      typeOfTheImage = imgImageType;
      
      var cDiv = document.getElementById("container");
      var otDiv = document.getElementById("overThumbDiv");
      if (!cDiv || !otDiv)
        return false;
 
      var otImg = otDiv.getElementsByTagName("img")[0];
      var otLink1 = otDiv.getElementsByTagName("a")[0];
      var otLink2 = imgObj.parentNode;
 
      cLeft = getElementLeft(cDiv);
      cRight = cLeft + 689;
      cTop = getElementTop(cDiv);
      cBottom = cTop + 1789;
 
      baseWidth = imgObj.width;
      baseHeight = imgObj.height;
      baseLeft = getElementLeft(imgObj);
      baseTop = getElementTop(imgObj);
 
      curWidth = startWidth;
      curHeight = startHeight;
      curLeft = baseLeft - ((curWidth - baseWidth) / 2);
      curTop = baseTop - ((curHeight - baseHeight) / 2);
 
      if (curLeft < cLeft) curLeft = cLeft;
      if (curLeft + curWidth > cRight) curLeft = cRight - curWidth;
      if (curTop < cTop) curTop = cTop;
      if (curTop + curHeight > cBottom) curTop = cBottom - curHeight;
 
      otDiv.style.left = curLeft+'px';
      otDiv.style.top = curTop+'px';
      otDiv.style.width = curWidth+'px';
      otDiv.style.height = curHeight+'px';
 
      otImg.style.width = otDiv.style.width;
      otImg.style.height = otDiv.style.height;
 
      otLink1.href = otLink2.href;
      otLink1.target = otLink2.target;
      otLink1.title = otLink2.title;
      otLink1.onclick = otLink2.onclick;
 
      otImg.src = "";
      otImg.src = imgObj.src;
 
      showThumb();
      growTimeoutId = window.setTimeout('growThumb()', 500);
    }
 
    function growThumb() {
      otDiv = document.getElementById("overThumbDiv");
      otImg = otDiv.getElementsByTagName("img")[0];
 
      window.clearTimeout(growTimeoutId);
 
      if (curWidth < growWidth + extraWidth || curHeight < growHeight + extraHeight) {
        curWidth += Math.round(((growWidth + extraWidth) - curWidth) / 2);
        curHeight += Math.round(((growHeight + extraHeight) - curHeight) / 2);
 
        curLeft = baseLeft - ((curWidth - baseWidth) / 2);
        curTop = baseTop - ((curHeight - baseHeight) / 2);
 
        growTimeoutId = window.setTimeout('growThumb()', 25);
      } else {
        curWidth = growWidth;
        curHeight = growHeight;
 
        curLeft = baseLeft - ((curWidth - baseWidth) / 2);
        curTop = baseTop - ((curHeight - baseHeight) / 2);
      }
 
      if (curLeft < cLeft) curLeft = cLeft;
      if (curLeft + curWidth > cRight) curLeft = cRight - curWidth;
      if (curTop < cTop) curTop = cTop;
      if (curTop + curHeight > cBottom) curTop = cBottom - curHeight;
 
      otDiv.style.left = curLeft+'px';
      otDiv.style.top = curTop+'px';
      otDiv.style.width = curWidth+'px';
      otDiv.style.height = curHeight+'px';
 
      otImg.style.width = otDiv.style.width;
      otImg.style.height = otDiv.style.height;
    }
 
    function hideThumb() {
      window.clearTimeout(growTimeoutId);
      window.clearTimeout(randTimeoutId);
 
      document.getElementById("overThumbDiv").style.display = "none";
 
      //randTimeoutId = window.setTimeout("popRandom()", 500);
    }
 
    function showThumb() {
      window.clearTimeout(randTimeoutId);
 
      document.getElementById("overThumbDiv").style.display = "inline";
    }
 
    var thumbs = new Array();
 
    function collectThumbs() {
      var divs = document.getElementsByTagName("div");
 
      var counter = 0;
      for (var i=0; i<divs.length; i++) { 
        var thisClass = divs[i].getAttribute("class"); 
        if (!thisClass) 
          thisClass = divs[i].getAttribute("className"); 
 
        if (thisClass == 'thumb') { 
          thumbs[counter] = divs[i]; 
          counter++; 
        } 
      } 
    } 
 
    function popRandom() { 
      if (!thumbs) 
        return false; 
 
      var rand = Math.round(Math.random() * thumbs.length); 
      var thumbDiv = thumbs[rand]; 
 
      if (!thumbDiv) 
        return false; 
 
      var thumbImg = thumbDiv.getElementsByTagName("img")[0]; 
 
      if (thumbImg != null) 
        enlargeThumb(thumbImg); 
 
      window.clearTimeout(randTimeoutId); 
      randTimeoutId = window.setTimeout("hideThumb()", 3000); 
    }
    
    function OpenNewWindow(url, width, height)
    {
	var randomnumber=Math.floor(Math.random()*5001)
	window.open(url, randomnumber, "top=10,left=10,menubar=0,resizable=0,scrollbars=0,width=" + width + ",height=" + height)
    }
    
    //****For image popup
    function openImagePop() {
    OpenNewWindow('../../ImagePop.aspx?ownerID='+idOfTheOwner+'&currentImageLO='+listOrderOfTheImage+'&type='+typeOfTheImage,380, 360);
    }
    //****
window.onload = function() {collectThumbs();}