// Utility functions for effects etc



// global to indicate animation is in progress (stop users multiple clicking on buttons etc)

var bumbleAnimationInProgress = 0;



// ******** GENERAL / UTILITY ******** 



// find X coordinate of object

function bumbleGetX(objId) {

  obj = document.getElementById(objId);

  var curleft = 0;

  if (obj.offsetParent)

    while(1) {

      curleft += obj.offsetLeft;

      if (!obj.offsetParent)

      break;

      obj = obj.offsetParent;

    }

  else if (obj.x) curleft += obj.x;

  return curleft;

}

// find Y coordinate of object

function bumbleGetY(objId) {

  obj = document.getElementById(objId);

  var curtop = 0;

  if (obj.offsetParent)

    while(1) {

      curtop += obj.offsetTop;

      if (!obj.offsetParent)

      break;

      obj = obj.offsetParent;

    }

  else if(obj.y) curtop += obj.y;

  return curtop;

}

// get object width

function bumbleGetWidth(objId) {

  obj = document.getElementById(objId);

  if (typeof obj.clip !== "undefined") {

    return obj.clip.width;

  } 

  else {

    if (obj.style.pixelWidth) { 

      return obj.style.pixelWidth;

    } 

    else {

      return obj.offsetWidth;

    }

  }

}

// get object height

function bumbleGetHeight(objId) {

  obj = document.getElementById(objId);

  if (typeof obj.clip !== "undefined") {

    return obj.clip.height;

  } 

  else {

    if (obj.style.pixelHeight) {

      return obj.style.pixelHeight;

    } 

    else {

      return obj.offsetHeight;

    }

  }

}



// ******** FADE IN / OUT  ********



// Fade object in

function bumbleFadeIn(objId, timeMs) { 

  // default duration for transition

  if (!timeMs || timeMs < 1) timeMs = 200;

  obj = document.getElementById(objId);

  // set object to invisible

  obj.style.visibility == 'hidden'

  bumbleFadeChangeOpacity(0, objId);

  // fade in

  bumbleTransitionObj(objId, 'opacity', 0, 100, '', timeMs);

}



// Fade object out

function bumbleFadeOut(objId, timeMs) {

  // default duration for transition

  if (!timeMs || timeMs < 1) timeMs = 200;

  obj = document.getElementById(objId);

  // set object to visible

  obj.style.visibility == 'visible'

  bumbleFadeChangeOpacity(100, objId);

  // fade out

  bumbleTransitionObj(objId, 'opacity', 100, 0, '', timeMs);

}





// Toggle fade in/out for an object

function bumbleFadeToggle(objId, timeMs) {

  // default duration for transition

  if (timeMs < 1) timeMs == 1500;

  obj = document.getElementById(objId);

  if (obj.style.visibility == 'hidden') {

    bumbleFadeIn(objId, timeMs);

  }

  else {

    bumbleFadeOut(objId, timeMs);

  }

}





// ******** MOVEMENT  ********



// slide target object by fixed amount

function bumbleSlideObj(objId, direction, pixels, timeMs, maxPixels) {

  targetObj = document.getElementById(objId);

  pixels = parseInt(pixels);

  // get current position of target

  targetX = bumbleGetX(objId);

  targetY = bumbleGetY(objId);

    

  switch(direction) {

    case "left": 

      var targetLeft = targetObj.style.left ? targetObj.style.left : 0;

      targetLeft = parseInt(targetLeft); 

      var newPos = targetLeft - pixels; 

      targetObj.style.position = 'relative'; 

      bumbleTransitionObj(objId, 'left', targetLeft, newPos, 'px', timeMs, maxPixels)

      break;

    case "right": 

      var targetLeft = targetObj.style.left ? targetObj.style.left : 0;

      targetLeft = parseInt(targetLeft); 

      var newPos = targetLeft + pixels; 

      targetObj.style.position = 'relative'; 

      bumbleTransitionObj(objId, 'left', targetLeft, newPos, 'px', timeMs, maxPixels)

      break;

  }

}



// perform animation by transitioning between two style values

function bumbleTransitionObj(objId, style, startVal, endVal, unit, timeMs, maxPixels, callBack) {

  // set animation start time

  var startTime = new Date().getTime();   

  if (bumbleAnimationInProgress == 0)

    bumbleTransitionObjDo(objId, style, startVal, endVal, unit, timeMs, startTime, maxPixels, callBack);  

  bumbleAnimationInProgress = 1;

}

function bumbleTransitionObjDo(objId, style, startVal, endVal, unit, timeMs, startTime, maxPixels, callBack) {

  var elapsed = new Date().getTime() - startTime;

  var distancePercent = (1 - Math.cos((elapsed/timeMs) * Math.PI)) / 2;

  // if time run is less than specified length

  if (elapsed < timeMs) { 

    if (startVal < endVal) {       

      var currentVal = startVal + ((endVal - startVal) * distancePercent);

    }

    else {

      var currentVal = startVal - ((startVal - endVal) * distancePercent);

    }     

    bumbleChangeStyle(style, currentVal, objId, unit);

    if (callBack && callBack != undefined) callBack = addslashes(callBack);

    window.setTimeout('bumbleTransitionObjDo(\'' + objId + '\', \'' + style + '\', ' + startVal + ', ' + endVal + ', \'' + unit + '\', \'' + timeMs + '\', \'' + startTime + '\', \'' + maxPixels + '\', \'' + callBack + '\')', 10);

  }

  // if animation has finished

  else {

    bumbleChangeStyle(style, endVal, objId, unit);

    var distance = 0 - endVal;

    // if maximum pixel value is reached (relative to zero) reset to starting point - used to reset slideshow positioning

    if ((maxPixels && maxPixels !== "undefined")) {

      if (distance >= maxPixels)

        bumbleChangeStyle(style, 0, objId, unit);

      else if (distance < 0) {

        var distanceDiff = endVal - startVal;

        var moveToEnd = 10 - (parseInt(document.getElementById(objId).style.width) - (distanceDiff * 3)); 

        bumbleChangeStyle(style, moveToEnd, objId, unit);

      }

    }

    bumbleAnimationInProgress = 0; 

    if (callBack && callBack != '') 

      eval(callBack);

  }

}



// change a style value for object

function bumbleChangeStyle(style, styleVal, objId, unit) { 

    var objStyle = document.getElementById(objId).style;

    if (style == 'opacity') {

      if (styleVal == 0) objStyle.visibility = 'hidden';

      else objStyle.visibility = 'visible';

      objStyle.opacity = (styleVal / 100);

      objStyle.MozOpacity = (styleVal / 100);

      objStyle.KhtmlOpacity = (styleVal / 100);

      objStyle.filter = "alpha(opacity=" + styleVal + ")";

    }

    else {

      eval('document.getElementById(\'' + objId + '\').style.' + style + ' = \'' + styleVal + unit + '\';');

    }

} 



// ********  FADING SLIDESHOW OF DIVS  ********



function bumbleFadeDivs(containerDiv, timeMs, pagethruDiv) {

  // timeMs = 'manual' for manual pagethrough (links within pagethruDiv)

  var parentElement = document.getElementById(containerDiv);

  if (parentElement.firstChild) { 

    var oChild = parentElement.firstChild;

    var firstObj = '';

    if (pagethruDiv) var pageThru = document.getElementById(pagethruDiv);

    var p = 1;

    var pagethruClass = '';

    while(oChild) { 

      if (oChild.nodeName=='div' || oChild.nodeName=='DIV') {

        if (oChild.id != '') {

          if (firstObj == '') 

            firstObj = oChild.id;

          else oChild.style.display = 'none';

          if (pagethruDiv && timeMs == 'manual') { 

            pagethruClass = p == 1 ? ' class="active"' : "";

            pageThru.innerHTML += '<a href="#" onclick="bumbleFadeDivsProceed(\'\', \'' + timeMs + '\', \'' + p + '\', \'' + containerDiv + '\', \'' + pagethruDiv + '\'); return false;"' + pagethruClass + '>' + p + '</a>';

          }  

          p++;

        }        

      }

      oChild = oChild.nextSibling;

    }

    if (pagethruDiv && timeMs == 'manual') { 

      pageThru.innerHTML += '<a href="#" onclick="bumbleFadeDivsProceed(\'\', \'' + timeMs + '\', \'2\', \'' + containerDiv + '\', \'' + pagethruDiv + '\'); return false;" class="next">Next</a>';

    }

    if (firstObj != '' && timeMs != 'manual')

      bumbleFadeDivsProceed(firstObj, timeMs);

  }

}

function bumbleFadeDivsProceed(objId, timeMs, gotoNum, parentId, pagethruDiv) {

  if (objId && objId != '') {

    var obj = document.getElementById(objId);

    obj.style.display = 'block';

    obj.style.visibility = 'visible';

    var nextSibling = '';

    var nextObj = obj.nextSibling;

    while(nextObj) { 

      if (nextObj.id != '' && (nextObj.nodeName=='div' || nextObj.nodeName=='DIV')) {

        nextSibling = nextObj.id;

        break;

      }

      nextObj = nextObj.nextSibling;

    }

  }

  else {

    if (pagethruDiv) var pageThru = document.getElementById(pagethruDiv);

    var pagethruClass = '';

    pageThru.innerHTML = '';

    var container = document.getElementById(parentId);

    var oChild = container.firstChild;

    var p = 1;

    var currentNum = 0;

    var firstObj = '';

    while(oChild) { 

      if ((oChild.nodeName=='div' || oChild.nodeName=='DIV') && oChild.id != '') {         

        if (oChild.style.display != 'none') {

          objId = oChild.id; 

          var obj = document.getElementById(objId);

          currentNum = p; 

        }        

        if (p == gotoNum) {

          nextSibling = oChild.id; 

        }

        if (pagethruDiv && timeMs == 'manual') { 

          pagethruClass = p == gotoNum ? ' class="active"' : "";

          pageThru.innerHTML += '<a href="#" onclick="bumbleFadeDivsProceed(\'\', \'' + timeMs + '\', \'' + p + '\', \'' + parentId + '\', \'' + pagethruDiv + '\'); return false;"' + pagethruClass + '>' + p + '</a>';

        }

        p++;

      }

      oChild = oChild.nextSibling;

    } 

    if (pagethruDiv && timeMs == 'manual') { 

      gotoNum = parseInt(gotoNum);

      if (p == (gotoNum + 1) || gotoNum === false || gotoNum === undefined) var nextNum = 1;

      else var nextNum = (gotoNum + 1 < p) ? gotoNum + 1 : 1; 

      pageThru.innerHTML += '<a href="#" onclick="bumbleFadeDivsProceed(\'\', \'' + timeMs + '\', \'' + nextNum + '\', \'' + parentId + '\', \'' + pagethruDiv + '\'); return false;" class="next">Next</a>';

    }

  } 

  if (nextSibling == '' || nextSibling === false || nextSibling === undefined) {

    var nextSiblingObj = obj.nextSibling;

    var iterations = 0;

    while(nextSiblingObj) { 

      if (nextSiblingObj.nodeName=='div' || nextSiblingObj.nodeName=='DIV') {

        if (nextSiblingObj.id != '') {

          nextSibling = nextSiblingObj.id;

          break;

        }

      }

      nextSiblingObj = nextSiblingObj.nextSibling;

      if ((nextSibling == '' || nextSibling === false || nextSibling === undefined) && iterations < 1) { 

        nextSiblingObj = obj.parentNode.firstChild;

        var iterations = 1;

      }

    }

  }

  if (nextSibling != '') { 

    var callBack = 'bumbleFadeDivsTransition(\'' + objId + '\', \'' + nextSibling + '\', \'' + timeMs + '\');';    

    if (timeMs != 'manual') {

      if (callBack && callBack != undefined) callBack = addslashes(callBack);

      window.setTimeout('bumbleTransitionObj(\'' + objId + '\', \'opacity\', \'100\', \'0\', \'\', 500, \'\', \'' + callBack + '\')', timeMs);

    }

    else if (timeMs == 'manual') {

      bumbleTransitionObj(objId, 'opacity', '100', '0', '', 200, '', callBack);

    }

  }

}



function bumbleFadeDivsTransition(prevDivId, nextDivId, timeMs) { 

  prevDiv = document.getElementById(prevDivId);

  prevDiv.style.display = 'none';

  nextDiv = document.getElementById(nextDivId);

  nextDiv.style.visibility = 'hidden';

  nextDiv.style.display = 'block';

  if (timeMs != 'manual') {

    var callBack = 'bumbleFadeDivsProceed(\'' + nextDivId + '\', \'' + timeMs + '\');';

    bumbleTransitionObj(nextDivId, 'opacity', '0', '100', '', 500, '', callBack);

  }

  else {

    //var callBack = 'document.getElementById(\'' + prevDivId + '\').style.display = \'none\';';

    bumbleTransitionObj(nextDivId, 'opacity', '0', '100', '', 200, '', '');

  }

}



// ******** SLIDESHOW / GALLERY  ********



function bumbleSlideShow(imagesDelim, imagePath, containerDivId, width, height, timeMs, startOffset) {

  var container = document.getElementById(containerDivId);

  var gallery = document.createElement("div");

  var galleryId = "slideShow_" + Math.floor(Math.random() * 1000000);

  var imagesArr = imagesDelim.split(',');

  var lastKey = imagesArr.length - 1;

  var firstImg = imagesArr[0];

  var lastImg = imagesArr[lastKey];

  // add last image to beginning of array

  imagesArr.unshift(lastImg);

  // add first image to end of array

  imagesArr.push(firstImg);

  var containerWidth = parseInt(imagesArr.length * width) + 10;

  

  container.style.width = width + 'px';

  container.style.height = height + 'px';


  container.style.position = 'relative';

  container.style.overflow = 'hidden';

  

  // empty container div

  container.innerHTML = '';

  

  gallery.setAttribute("class", "slideshow_inner_container");

  gallery.setAttribute("id", galleryId);

  gallery.style.height = height + 'px';

  gallery.style.marginLeft = '-' + width + 'px';

  

  // expand container to fit all images

  gallery.style.width = containerWidth + 'px';

  container.appendChild(gallery);

  

  var thisImg = '';

  // add image objects to gallery div

  for (var i = 0; i < imagesArr.length; i++) {

    thisImg = document.createElement("img");

    thisImg.style.width = width + 'px';

    thisImg.style.height = height + 'px';

    thisImg.src = imagePath + imagesArr[i];

    gallery.appendChild(thisImg); 

  }

  

  // move images by offset if set  

  if (startOffset !== false && startOffset > 0) {

    gallery.style.position = 'relative';

    gallery.style.left = '-' + (width * startOffset) + 'px';

  }

  

  // add left / right icons for browsing slides

  var leftClick = document.createElement("div");

  var rightClick = document.createElement("div");

  var clickTop = parseInt(Math.floor(height / 2) + 16);

  var clickLeft = parseInt(width - 32);



  leftClick.setAttribute("class", "slideshow_left_browse");

  leftClick.setAttribute("className", "slideshow_left_browse");

  leftClick.style.float = 'left';

  leftClick.style.clear = 'left';

  leftClick.style.width = '32px';

  leftClick.style.height = '32px';

  leftClick.style.position = 'relative';

  var leftAction = "clearInterval(bumbleSlideAuto); bumbleSlideObj('" + galleryId + "', 'right', '" + width + "', '" + timeMs + "', '" + (containerWidth - (2 * width) - 10) + "')";

  leftClick.onclick = function() { eval(leftAction) };

  leftClick.style.top = '-' + clickTop + 'px';

  container.appendChild(leftClick); 

  

  rightClick.setAttribute("class", "slideshow_right_browse");

  rightClick.setAttribute("className", "slideshow_right_browse");

  rightClick.style.float = 'left';

  rightClick.style.clear = 'left';

  rightClick.style.width = '32px';

  rightClick.style.height = '32px';

  rightClick.style.position = 'relative';

  var rightAction = "clearInterval(bumbleSlideAuto); bumbleSlideObj('" + galleryId + "', 'left', '" + width + "', '" + timeMs + "', '" + (containerWidth - (2 * width) - 10) + "')";

  rightClick.onclick = function() { eval(rightAction) };

  rightClick.style.top = '-' + (clickTop + 32) + 'px';

  rightClick.style.left = clickLeft + 'px';

  container.appendChild(rightClick); 

  

  bumbleSlideAuto = window.setInterval("bumbleSlideObj('" + galleryId + "', 'left', '" + width + "', '" + timeMs + "', '" + (containerWidth - (2 * width) - 10) + "')", 6000);

}
