var asImagesAvailable          = new Array () ;
var cxLayerB                   = 0 ;
var cyLayerB                   = 0 ;
var cxLayerT                   = 0 ;
var cyLayerT                   = 0 ;
var cxWindow                   = 0 ;
var cyWindow                   = 0 ;
var iBrowserIsIE               = (window.navigator.userAgent.toLowerCase ().indexOf ('msie') != - 1) ? true : false ;
var iCurrTransition            = 0 ;
var iFramesPerTrans            = 0 ;
var iImagesOnServer            = 0 ;
var iImagesOnServer_NoChange   = 0 ;
var iImagesOnServer_SpiderDone = 0 ;
var iImagesShown               = 0 ;
var iLastImageIndex            = 0 ;
var iLayerMargin               = 8 ;
var iNextImageReady            = 0 ;
var iShowId                    = 0 ;
var iSlideshowDelayMs          = 10000 ;
var iSlideshowReady            = 0 ;
var iSlideshowRunning          = 0 ;
var iTransitionLengthMs        = 3000 ;
var iTransitionStartedMs       = 0 ;
var iTransitionsEnabled        = 0 ;
var imgNextImage               = new Image () ;
var oImg1                      = document.getElementById ("img1") ;
var oImg2                      = document.getElementById ("img2") ;
var oLayer1                    = document.getElementById ("show1") ;
var oLayer2                    = document.getElementById ("show2") ;
var oLayerB                    = document.getElementById ("showb") ;
var oLayerT                    = document.getElementById ("showt") ;
var oTransitionLayer           = oLayer1 ;
var oTransitionLayer_style     = oTransitionLayer.style ;
var pxLayerB                   = 0 ;
var pyLayerB                   = 0 ;
var pxLayerT                   = 0 ;
var pyLayerT                   = 0 ;
var sRes                       = "1680x1050" ;
var sUrlPicsBase               = sNSPBaseUrl + "pics/" ;
var sUrlSaveSettings           = sNSPBaseUrl + "scripts_public/show_savesettings.php" ;
var sUrlShowStatus             = sNSPBaseUrl + "scripts_public/show_status.php?kw=" + sNSPKw + "&es=" + emb ;

function ajaxRequest (sMethod, sUrl, bAsync, fCallback)
{
  var xmlhttp ;

  if (window.XMLHttpRequest)
  {
    // IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest () ;
  }
  else
  {
    // IE6, IE5
    xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP") ;
  }

  if (fCallback) xmlhttp.onreadystatechange = function ()
  {
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) fCallback (xmlhttp.responseText) ;
  }

  xmlhttp.open (sMethod, sUrl, bAsync) ;
  xmlhttp.send (null) ;
}

function bodyResized ()
{
  if (!iSlideshowRunning) return ;

  if (typeof (window.innerWidth) == 'number')
  {
    // Non-IE
    cxWindow = window.innerWidth ;
    cyWindow = window.innerHeight ;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    // IE 6+ in 'standards compliant mode'
    cxWindow = document.documentElement.clientWidth ;
    cyWindow = document.documentElement.clientHeight ;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    // IE 4 compatible
    cxWindow = document.body.clientWidth ;
    cyWindow = document.body.clientHeight ;
  }
  else { cxWindow = 800 ; cyWindow = 600 ; }

  if (naauth != "")
  {
    oLayerT.style.left   = "0px" ;
    oLayerT.style.top    = "0px" ;
    oLayerT.style.width  = cxLayerT = 0 ;
    oLayerT.style.height = cyLayerT = 0 ;

    oLayerB.style.left   = "0px" ;
    oLayerB.style.top    = "0px" ;
    oLayerB.style.width  = pxStr (cxLayerB = cxWindow) ;
    oLayerB.style.height = pxStr (cyLayerB = cyWindow) ;

    oLayer1.style.left   = "0px" ;
    oLayer1.style.top    = "0px" ;
    oLayer1.style.width  = pxStr (cxLayerB) ;
    oLayer1.style.height = pxStr (cyLayerB) ;

    oLayer2.style.left   = "0px" ;
    oLayer2.style.top    = "0px" ;
    oLayer2.style.width  = pxStr (cxLayerB) ;
    oLayer2.style.height = pxStr (cyLayerB) ;
  }
  else
  {
    oLayerT.style.left  = pxStr (pxLayerT = iLayerMargin) ;
    oLayerT.style.top   = pxStr (pyLayerT = iLayerMargin) ;
    oLayerT.style.width = pxStr (cxLayerT = cxWindow - (2 * iLayerMargin)) ;

    cyLayerT = oLayerT.offsetHeight ;

    oLayerB.style.left   = pxStr (pxLayerB = iLayerMargin) ;
    oLayerB.style.top    = pxStr (pyLayerB = pyLayerT + cyLayerT + iLayerMargin) ;
    oLayerB.style.width  = pxStr (cxLayerB = cxWindow - (2 * iLayerMargin)) ;
    oLayerB.style.height = pxStr (cyLayerB = cyWindow - pyLayerB - iLayerMargin) ;

    oLayer1.style.left   = "0px" ;
    oLayer1.style.top    = "0px" ;
    oLayer1.style.width  = pxStr (cxLayerB) ;
    oLayer1.style.height = pxStr (cyLayerB) ;

    oLayer2.style.left   = "0px" ;
    oLayer2.style.top    = "0px" ;
    oLayer2.style.width  = pxStr (cxLayerB) ;
    oLayer2.style.height = pxStr (cyLayerB) ;
  }

  var layerBAspectRatio = cxLayerB / cyLayerB ;

  if (oImg1.src && oImg1.width && oImg1.height)
  {
    var imageAspectRatio  = oImg1.width / oImg1.height ;

    if (imageAspectRatio > layerBAspectRatio)
    {
      oImg1.width      = cxLayerB ;
      oImg1.height     = cxLayerB / imageAspectRatio ;
      oImg1.style.left = "0px" ;
      oImg1.style.top  = pxStr (Math.round ((cyLayerB - oImg1.height) / 2)) ;
    }
    else if (imageAspectRatio < layerBAspectRatio)
    {
      oImg1.height     = cyLayerB ;
      oImg1.width      = cyLayerB * imageAspectRatio ;
      oImg1.style.left = pxStr (Math.round ((cxLayerB - oImg1.width) / 2)) ;
      oImg1.style.top  = "0px" ;
    }
    else
    {
      oImg1.width      = cxLayerB ;
      oImg1.height     = cyLayerB ;
      oImg1.style.left = "0px" ;
      oImg1.style.top  = "0px" ;
    }
  }

  if (oImg2.src && oImg2.width && oImg2.height)
  {
    var imageAspectRatio  = oImg2.width / oImg2.height ;

    if (imageAspectRatio > layerBAspectRatio)
    {
      oImg2.width      = cxLayerB ;
      oImg2.height     = cxLayerB / imageAspectRatio ;
      oImg2.style.left = "0px" ;
      oImg2.style.top  = pxStr (Math.round ((cyLayerB - oImg2.height) / 2)) ;
    }
    else if (imageAspectRatio < layerBAspectRatio)
    {
      oImg2.height     = cyLayerB ;
      oImg2.width      = cyLayerB * imageAspectRatio ;
      oImg2.style.left = pxStr (Math.round ((cxLayerB - oImg2.width) / 2)) ;
      oImg2.style.top  = "0px" ;
    }
    else
    {
      oImg2.width      = cxLayerB ;
      oImg2.height     = cyLayerB ;
      oImg2.style.left = "0px" ;
      oImg2.style.top  = "0px" ;
    }
  }

  sRes = "1680x1050" ;
  if (cxLayerB <= 800) sRes = "0800x0500" ;
  if (cxLayerB <= 640) sRes = "0640x0400" ;
  if (cxLayerB <= 480) sRes = "0480x0300" ;
  if (cxLayerB <= 320) sRes = "0320x0200" ;
}

function doTransition ()
{
  var oDate = new Date () ;
  var iTransitionCurrentMs = oDate.getTime () - iTransitionStartedMs ;
  delete oDate ;

  iFramesPerTrans ++ ;
  if (iTransitionCurrentMs < iTransitionLengthMs)
  {
    var fProgress  = iTransitionCurrentMs / iTransitionLengthMs ;
    var fProgressI = 1 - fProgress ;

    if (!iCurrTransition)
    {
      if (!iBrowserIsIE) oTransitionLayer_style.opacity  = fProgress ;
      else               oTransitionLayer_style.filter   = "alpha (opacity = " + Math.floor (fProgress * 100) + ")" ;
    }
    else if (iCurrTransition == 1) { oTransitionLayer_style.left = "-" + Math.floor (cxLayerB * fProgressI) + "px" ; }
    else if (iCurrTransition == 2) { oTransitionLayer_style.left = ""  + Math.floor (cxLayerB * fProgressI) + "px" ; }
    else if (iCurrTransition == 3) { oTransitionLayer_style.top  = "-" + Math.floor (cyLayerB * fProgressI) + "px" ; }
    else if (iCurrTransition == 4) { oTransitionLayer_style.top  = ""  + Math.floor (cyLayerB * fProgressI) + "px" ; }

    setTimeout ("doTransition () ;", 20) ;
  }
  else
  {
    if (!iCurrTransition)
    {
      if (!iBrowserIsIE) oTransitionLayer_style.opacity  = 1 ;
      else               oTransitionLayer_style.filter   = "alpha (opacity = 100)" ;
    }
    else if ((iCurrTransition == 1) || (iCurrTransition == 2)) { oTransitionLayer_style.left = "0px" ; }
    else if ((iCurrTransition == 3) || (iCurrTransition == 4)) { oTransitionLayer_style.top  = "0px" ; }

    oTransitionLayer = (oTransitionLayer == oLayer1) ? oLayer2 : oLayer1 ;
    oTransitionLayer_style = oTransitionLayer.style ;

    do
    {
      iCurrTransition ++ ;
      if (iCurrTransition > 4) iCurrTransition = 0 ;
    } while (!(iTransitionsEnabled & (1 << iCurrTransition)))

    var iPendingDelayMs = iSlideshowDelayMs - iTransitionCurrentMs ;
    if (iPendingDelayMs < 50) iPendingDelayMs = 50 ;

    //if (iImagesShown == 10) alert (iFramesPerTrans) ;

    setTimeout ("showChanger () ;", iPendingDelayMs) ;
  }
}

function imageLoaded_error ()
{
  delete imgNextImage ;
  imgNextImage = new Image () ;

  var i ;
  do
  { i = Math.floor (Math.random () * asImagesAvailable.length) ;
  } while ((i == iLastImageIndex) && (asImagesAvailable.length > 1))
  iLastImageIndex = i ;

  imgNextImage.onload  = imageLoaded_ok ;
  imgNextImage.onabort = imageLoaded_error ;
  imgNextImage.onerror = imageLoaded_error ;

  imgNextImage.src = sUrlPicsBase   + "/" + asImagesAvailable [i].charAt (0) + "/" +
   asImagesAvailable [i].charAt (1) + "/" + asImagesAvailable [i].charAt (2) + "/" +
   asImagesAvailable [i].charAt (3) + "/" + asImagesAvailable [i].charAt (4) + "/" +
   asImagesAvailable [i].charAt (5) + "/" + asImagesAvailable [i] + "_" + sRes + ".jpg" ;
}

function imageLoaded_ok ()
{
  iNextImageReady = 1 ;

  if (!iSlideshowReady)
  {
    iSlideshowReady = 1 ;

    setTimeout ("launchSlideshow () ;", 50) ;
  }
}

function launchSlideshow ()
{
  if (!iSlideshowReady)
  {
    alert ("Slideshow not ready yet.") ;
    return ;
  }

  if (iSlideshowRunning)
  {
    alert ("Slideshow already running.") ;
    return ;
  }

  var val1 = 10 ; // document.getElementById ("SlideshowDelay").value ;
  var val2 = 5 ; // document.getElementById ("TransitionLength").value ;

  if ((val1 < 5) || (val1 > 60))
  {
    alert ("'Slideshow delay' must contain a value between 5 and 60 (both included).") ;
    return ;
  }

  if ((val2 < 1) || (val2 > 30))
  {
    alert ("'Transition length' must contain a value between 1 and 30 (both included).") ;
    return ;
  }

  if (val1 < (parseInt (val2) + 2))
  {
    alert ("'Slideshow delay' must be at least two seconds longer than 'Transition length'.") ;
    return ;
  }

  iTransitionsEnabled  = 31 ; /* (document.getElementById ("trans0").checked * 1) +
   (document.getElementById ("trans1").checked * 2) + (document.getElementById ("trans2").checked *  4) +
   (document.getElementById ("trans3").checked * 8) + (document.getElementById ("trans4").checked * 16) ; */

  ajaxRequest ("GET", sUrlSaveSettings + "?sd=" + val1 + "&tl=" + val2 + "&te=" + iTransitionsEnabled, true, 0) ;

  iSlideshowDelayMs   = val1 * 1000 ;
  iTransitionLengthMs = val2 * 1000 ;

  if (iTransitionsEnabled) while (!(iTransitionsEnabled & (1 << iCurrTransition)))
  {
    iCurrTransition ++ ;
    if (iCurrTransition > 4) iCurrTransition = 0 ;
  }

  iSlideshowRunning = 1 ;
  sUrlShowStatus += "&sr=1" ;

  if (naauth == "") var sAdHTML = document.getElementById ("adhtml").innerHTML ;

  document.getElementById ("launcher").innerHTML = "" ;

  document.body.style.background = "#000000" ;
  document.body.style.margin     = "0px" ;
  document.body.style.overflow   = "hidden" ;

  oLayerT.style.visibility = "visible" ;
  oLayerB.style.visibility = "visible" ;
  oLayer1.style.visibility = "visible" ;
  oLayer2.style.visibility = "visible" ;

  if (naauth == "") document.getElementById ("showta").innerHTML = sAdHTML ;

  bodyResized () ;
  showChanger () ;
}

function pxStr (iPx) { return "" + iPx + "px" ; }

function showChanger (iFirstChange)
{
  if (!iNextImageReady)
  {
    setTimeout ("showChanger () ;", 1000) ;
    return ;
  }

  iImagesShown ++ ;

  if (!iTransitionsEnabled) oTransitionLayer_style.visibility = "hidden" ;
  else
  {
    if (!iCurrTransition)
    {
      if (!iBrowserIsIE) oTransitionLayer_style.opacity  = 0 ;
      else               oTransitionLayer_style.filter   = "alpha (opacity = 0)" ;
    }
    else if (iCurrTransition == 1) oTransitionLayer_style.left = "-" + cxLayerB + "px" ;
    else if (iCurrTransition == 2) oTransitionLayer_style.left = ""  + cxLayerB + "px" ;
    else if (iCurrTransition == 3) oTransitionLayer_style.top  = "-" + cyLayerB + "px" ;
    else if (iCurrTransition == 4) oTransitionLayer_style.top  = ""  + cyLayerB + "px" ;
  }

  if (oTransitionLayer == oLayer1)
  {
    oLayer1.style.zIndex = 3 ;
    oLayer2.style.zIndex = 2 ;

    var el = oImg1 ;
  }
  else
  {
    oLayer2.style.zIndex = 3 ;
    oLayer1.style.zIndex = 2 ;

    var el = oImg2 ;
  }

  el.style.visibility = "visible" ;

  el.src = imgNextImage.src ;

  var layerBAspectRatio = cxLayerB / cyLayerB ;
  var imageAspectRatio  = imgNextImage.width / imgNextImage.height ;

  if (imageAspectRatio > layerBAspectRatio)
  {
    el.width      = cxLayerB ;
    el.height     = cxLayerB / imageAspectRatio ;
    el.style.left = "0px" ;
    el.style.top  = pxStr (Math.round ((cyLayerB - el.height) / 2)) ;
  }
  else if (imageAspectRatio < layerBAspectRatio)
  {
    el.height     = cyLayerB ;
    el.width      = cyLayerB * imageAspectRatio ;
    el.style.left = pxStr (Math.round ((cxLayerB - el.width) / 2)) ;
    el.style.top  = "0px" ;
  }
  else
  {
    el.width      = cxLayerB ;
    el.height     = cyLayerB ;
    el.style.left = "0px" ;
    el.style.top  = "0px" ;
  }

  if (!iTransitionsEnabled) oTransitionLayer_style.visibility = "visible" ;
  else
  {
    var oDate = new Date () ;
    iTransitionStartedMs = oDate.getTime () ;
    delete oDate ;

    //iFramesPerTrans = 0 ;
    doTransition () ;
  }

  iNextImageReady = 0 ;
  delete imgNextImage ;
  imgNextImage = new Image () ;

  var i ;
  do
  { i = Math.floor (Math.random () * asImagesAvailable.length) ;
  } while ((i == iLastImageIndex) && (asImagesAvailable.length > 1))
  iLastImageIndex = i ;

  imgNextImage.onload  = imageLoaded_ok ;
  imgNextImage.onabort = imageLoaded_error ;
  imgNextImage.onerror = imageLoaded_error ;

  imgNextImage.src = sUrlPicsBase   + "/" + asImagesAvailable [i].charAt (0) + "/" +
   asImagesAvailable [i].charAt (1) + "/" + asImagesAvailable [i].charAt (2) + "/" +
   asImagesAvailable [i].charAt (3) + "/" + asImagesAvailable [i].charAt (4) + "/" +
   asImagesAvailable [i].charAt (5) + "/" + asImagesAvailable [i] + "_" + sRes + ".jpg" ;
}

function showStatus (sStatus)
{
  if (!sStatus.length) return ;

  if (!iImagesOnServer_SpiderDone)
  {
    var asStatus    = sStatus.split (" ") ;
    var iSpiderDone = 0 ;

    if (asStatus [0] == 1) iSpiderDone = 1 ;
    else
    {
      if (iImagesOnServer != asStatus [1])
      {
        iImagesOnServer = asStatus [1] ;
        iImagesOnServer_NoChange = 0 ;
      }
      else
      {
        iImagesOnServer_NoChange ++ ;
        if (iImagesOnServer_NoChange >= 24) iSpiderDone = 1 ;
      }
    }

    if (iSpiderDone)
    {
      iImagesOnServer_SpiderDone = 1 ;
      sUrlShowStatus += "&sd=1" ;
    }

    if (asStatus [1] != 0)
    {
      if (asStatus [asStatus.length - 1] == "") asStatus.splice (asStatus.length - 1, 1) ;
      asStatus.splice (0, 2) ;
      asImagesAvailable = asStatus ;

      if (!iSlideshowRunning && !imgNextImage.src)
      {
        var i = Math.floor (Math.random () * asImagesAvailable.length) ;
        iLastImageIndex = i ;

        imgNextImage.onload  = imageLoaded_ok ;
        imgNextImage.onabort = imageLoaded_error ;
        imgNextImage.onerror = imageLoaded_error ;

        imgNextImage.src = sUrlPicsBase   + "/" + asImagesAvailable [i].charAt (0) + "/" +
         asImagesAvailable [i].charAt (1) + "/" + asImagesAvailable [i].charAt (2) + "/" +
         asImagesAvailable [i].charAt (3) + "/" + asImagesAvailable [i].charAt (4) + "/" +
         asImagesAvailable [i].charAt (5) + "/" + asImagesAvailable [i] + "_" + sRes + ".jpg" ;
      }
    }
  }

  setTimeout ("ajaxRequest (\"GET\", sUrlShowStatus + \"&is=\" + iImagesShown, true, showStatus) ;", 5000) ;
}

function showStatus_si (sStatus)
{
  if (!sStatus.length) return ;

  iShowId = parseInt (sStatus) ;
  if (!iShowId) return ;

  sUrlShowStatus += "&si=" + iShowId ;
  ajaxRequest ("GET", sUrlShowStatus, true, showStatus) ;
}

oLayerT.style.visibility = "hidden" ;
oLayerB.style.visibility = "hidden" ;
oLayer1.style.visibility = "hidden" ;
oLayer2.style.visibility = "hidden" ;

oImg1.style.visibility = "hidden" ;
oImg2.style.visibility = "hidden" ;

ajaxRequest ("GET", sUrlShowStatus, true, showStatus_si) ;