var loading = true;
var frameCount = 1;
var curPos = 0;
var frameLabels = new Array();
var stopClicked = false;
var frameTimer = null;

function lshowFrame(n){
  if(loading) return false;

  $('.lframe-vid-container:visible').html('').hide(); // get rid of video frame
  $('.lframe-play-btn:hidden').show();

  if(isNaN(curPos)) curPos = 0;
  curPos = curPos - 0; // make sure that curPos isn't a string of a number
  var newPos = 0;

  // handle 'next','prev' or frame #
  switch(n){
  case 'prev':
    newPos = curPos-1; break;
  case 'next':
    newPos = curPos+1; break;
  default:
    newPos = isNaN(n) ? 0 : n;
  }

  // wrap around
  if(newPos<0){
      newPos = frameCount-1;
  } else if(newPos>(frameCount-1)) {
      newPos = 0;
  }

//alert('from '+curPos+' to '+newPos);

  // switch frames if newPos != curPos
  if(newPos!=curPos){
    loading = true;

    // frame
    $("#frame_"+curPos).fadeOut(300, function () {
        $('.lframe-wrapper:visible').hide();
        $("#frame_"+newPos).fadeIn(300);
    });

    // pager links
    $("a.active-frame").removeClass('active-frame');
    $("#frame_link_"+newPos).addClass('active-frame');

    // buttons
    var idx = newPos == 0 ? frameCount-1 : newPos-1;
    $("#prev_btn").attr("title",frameLabels[idx]);

    idx = newPos == frameCount-1 ? 0 : newPos+1;
    $("#next_btn").attr("title",frameLabels[idx]);

    curPos = newPos;
    loading = false;
  }
}
function lstartTimer(){
  if(!stopClicked) frameTimer = setTimeout("lplayFrames()",6000);
}
function lstopTimer(){
  if (typeof frameTimer!="undefined") clearTimeout(frameTimer);
}
function lplayFrames(){
  lshowFrame('next');
  lstartTimer();
}
function lstopFrames(){
  stopClicked = true;
  lstopTimer();
}
function lpauseFrames(){
  lstopTimer();
}
function lresumeFrames(){
  if(!stopClicked) lstartTimer();
}
function lloadVidFrame(n){
  // stop animation
  lstopFrames();

  // get embed tag
  var src = $('#vid_src_'+n).val();

  // get target
  var target = $('#vid_container_'+n);

  // hide play button and show target
  $(target).prev().hide();
  $(target).show().html(src);

}
