
var iAdvImgShuffle = function ( images, folderUri )
{
    var ImgDuration_FADEOUT =  250;
    var ImgDuration_HOLD    = 3500;
    var ImgDuration_FADEIN  =  500;

    var imgCount  = images.length;
    var imgOrder  = new Array(imgCount);
    for (var i = 0; i < imgCount; i++) {
      imgOrder[i]   = i;
    }
    imgOrder.sort(function(){return 0.5 - Math.random()});
    var imgCurrentIndex = 0;

    var uriForImage = function ( imgIndex )
                      {
                        return folderUri + '/' + images[imgIndex].img;
                      };
    var imgNext     = new Image()
    imgNext.src     = uriForImage(imgOrder[imgCurrentIndex]);

    var jQframe     = jQuery('#portfolio-sample-frame');

    var jQcaption   =
        { box       : jQuery('#portfolio-caption')
        , type      : jQuery('#portfolio-project-type')
        , desc      : jQuery('#portfolio-project-description')
        , client    : jQuery('#portfolio-project-client')
        };
    var clearCaptions = function ( )
                      {
                        jQcaption.type.empty();
                        jQcaption.desc.empty();
                        jQcaption.client.empty();
                      }
    var setCaptions   = function ( )
                      {
                        var imgCurrent = images[imgOrder[imgCurrentIndex]];
                        jQcaption.type.text(imgCurrent.type);
                        jQcaption.desc.text(imgCurrent.desc);
                        jQcaption.client.text(imgCurrent.client);

                        var txt = imgCurrent.type + ' • ' +
                                  imgCurrent.desc + ' • ' +
                                  imgCurrent.client;
                        jQimg.attr('alt', txt).attr('title', txt);
                      }

    var jQimg       = jQuery('<img class="item" src="" alt="" title="" />');
    jQframe.children('a').empty().append(jQimg);
    jQimg.hide();

    var advFadeIn   = function ( )
                      {
                        jQcaption.box.hide();
                        clearCaptions();
                        jQimg.attr('src', imgNext.src);
                        jQimg.fadeIn(ImgDuration_FADEIN, advHold);
                      };
    var advHold     = function ( )
                      {
                        setCaptions();
                        jQcaption.box.show();
                        setTimeout(advFadeOut, ImgDuration_HOLD);
                        if (++imgCurrentIndex >= imgCount) {
                          imgCurrentIndex = 0;
                        }
                        imgNext.src = uriForImage(imgOrder[imgCurrentIndex]);
                      };
    var advFadeOut  = function ( )
                      {
                        jQimg.fadeOut(ImgDuration_FADEOUT, advFadeIn);
                      };

    advFadeIn();

    iAdvTestimonialShuffle();
};


var iAdvTestimonialShuffle = function ( )
{
    var TextDuration_FADEOUT =  750;
    var TextDuration_HOLD    = 19000;
    var TextDuration_FADEIN  =  250;

    var jQcontainer       = jQuery('#testimonials');
    var jQitems           = jQcontainer.children('li').hide();
    var itemCount         = jQitems.length;
    var itemCurrentIndex  = itemCount;

    var txtFadeIn   = function ( )
                      {
                        jQitems.eq(itemCurrentIndex).hide();
                        if (++itemCurrentIndex >= itemCount) {
                          itemCurrentIndex = 0;
                        }
                        jQitems.eq(itemCurrentIndex).show();
                        jQcontainer.fadeIn(TextDuration_FADEIN, txtHold);
                      };
    var txtHold     = function ( )
                      {
                        setTimeout(txtFadeOut, TextDuration_HOLD);
                      };
    var txtFadeOut  = function ( )
                      {
                        jQcontainer.fadeOut(TextDuration_FADEOUT, txtFadeIn);
                      };

    txtFadeIn();
};

var iAdvVideoCaption_trigger = (function()
{
  var _jQc = null;

  var _init = function ( )
  {
    var jQ = jQuery('#iAdvVideoCaptions');
    if (jQ.length > 0) {
      _jQc =
      { divs      : {}
      , items     : {}
      };
      var jQdivs = jQ.children('div');
      for (var i = 0; i < jQdivs.length; i++) {
        var jQdiv = jQdivs.eq(i);
        var name  = jQdiv.attr('class');
        if (name) {
          _jQc.divs[name] = jQdiv;
          var jQli  = jQdiv.find('li');
          if (jQli.length > 0) {
            _jQc.items[name] = jQli;
          }
        }
      }
    }
  };

  var _trigger = function ( cueName, cueItem ) {

    if (!_jQc) {
      _init();
    }

    var div = _jQc.divs[cueName]
      , li  = _jQc.items[cueName]
      , c, i;

    for (c in _jQc.divs) {
      if (_jQc.divs[c] !== div) {
        _jQc.divs[c].hide(500);
      }
    }
    if (div) {
      if (li) {
        for (i = (cueItem ? cueItem - 1 : 0); i < li.length; i++) {
          li.eq(i).hide();
        }
        div.show((cueItem > 0) ? 0 : 500);
        for (i = 0; i < cueItem; i++) {
          li.eq(i).show(i === (cueItem-1) ? 500 : 0);
        }
      }
      else {
        div.show(500);
      }
    }
  };

  return _trigger;

})();

