var prevImage = [];
function setPrevHrefId(imgID, cnt)
{
   prevImage[imgID] = cnt;
}

function changeImage(imgID, cnt)
{
   var Img = document.getElementById('img'+imgID);
   var hrefObj = document.getElementById('aimg'+imgID+'['+cnt+']');
   
   if (!Img || !hrefObj) return;

   if (prevImage[imgID]){
      var prevHref = document.getElementById('aimg'+imgID+'['+prevImage[imgID]+']');
      if (prevHref){
         prevHref.onclick = function(){return true}
         prevHref.style.textDecoration = 'underline';
         prevHref.style.cursor = 'pointer';
         prevImage[imgID] = cnt;
      }
   }

   if (Img && hrefObj){
      Img.src = hrefObj.getAttribute("imgurl");
      var imgtext = hrefObj.getAttribute("imgtext");
      var imgtextObj = document.getElementById("imgtext"+imgID);
      if (typeof(imgtext)!='undefined' && imgtextObj) imgtextObj.innerHTML = imgtext;
      hrefObj.style.textDecoration = 'none';
      hrefObj.style.cursor = 'default';
      hrefObj.onclick = function(){return false}
   }
}

var win_array = [];

// open classic popup window
function openWin(loc, winName, winWidth, winHeight, winParams)
{
   if (!win_array[winName]) win_array[winName] = null;
   if (!win_array[winName] || (win_array[winName] && win_array[winName].closed)){
      if (winParams=='full'){
         winParams = 'resizable=yes,menubar=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,directories=yes,scrollbars=yes';
      }
      win_array[winName] = window.open(loc, winName, "width="+winWidth+",height="+winHeight+","+winParams);
   }

   win_array[winName].location = loc;
   win_array[winName].focus();
   return win_array[winName];
}

function winPosition()
{
  // visible window size
  var visibleWinWidth = document.documentElement.offsetWidth;
  // total window size
  var totalWinWidth = typeof(document.width)!='undefined' ? document.width : document.body.offsetWidth;
  
  if (visibleWinWidth>totalWinWidth)
     var lpos = parseInt((visibleWinWidth - totalWinWidth)/2);
  else
     var lpos = 10+(document.width ? -10 : 0);
  
  var leftPos = typeof(window.screenLeft)!='undefined' ? window.screenLeft+lpos-30 : parseInt(window.top.screenX)-20+lpos,
      topPos = typeof(window.screenTop)!='undefined' ? window.screenTop+25 : parseInt(window.top.screenY)+130;
  
  return {leftPos:leftPos, topPos:topPos};
      
}

function showGallery(imgID, loc, imgNum, winName, winWidth, winHeight, winParams)
{
  loc = prevImage[imgID] ? loc + "/" +prevImage[imgID] : loc + "/" + imgNum;
  
  var posObj = winPosition();
  
  openWin(loc, winName, winWidth, winHeight, winParams+',left='+parseInt(200+posObj.leftPos)+',top='+parseInt(40+posObj.topPos));
}

function rotateImages()
{  var i, imgID, numargs = arguments.length;

   for (i = 0; i < numargs; i++)
   {
       imgID = arguments[i];
       window.setInterval('startRotation('+imgID+')',5000);
   }
}

function startRotation(imgID)
{  
   var cnt = br = parseInt(prevImage[imgID]);
   if (br>=10) br = 1; else br++;
   for(var i=br; i<=10; i++){
      if (i==cnt) break;
      var hrefObj = document.getElementById('aimg'+imgID+'['+i+']');
      if (hrefObj){
         changeImage(imgID, i);
         return;
      }
      if (i>=10) i = 0;
   }
}

function showMedia(loc, winName, winWidth, winHeight, winParams)
{
   var posObj = winPosition();
   openWin(loc, winName, winWidth, winHeight, winParams+',left='+parseInt(200+posObj.leftPos)+',top='+parseInt(40+posObj.topPos));
}