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

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

  $('.frame-vid-container:visible').html('').hide(); // get rid of video frame
  $('.frame-vid').css('background', 'transparent');
  $('.frame-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 () {
        $('.frame-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 startTimer(){
  if(!stopClicked) frameTimer = setTimeout("playFrames()",6000);
}
function stopTimer(){
  if (typeof frameTimer!="undefined") clearTimeout(frameTimer);
}
function playFrames(){
  showFrame('next');
  startTimer();
}
function stopFrames(){
  stopClicked = true;
  stopTimer();
}
function pauseFrames(){
  stopTimer();
}
function resumeFrames(){
  if(!stopClicked) startTimer();
}
function loadVidFrame(n){
  // stop animation
  stopFrames();

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

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

  // hide play button and show target
  $(target).parent().css('background', 'black');
  $(target).prev().hide();
  $(target).show().html(src);

}
function showAndHide(elementid) {
    var liArray = document.getElementById('tablis1').childNodes;
    var i=0,item;
    while (item = liArray[i++]) {
	    if (item.nodeType === 1) {
	     // item.className = item.className.replace(/(^| )current( |$)/g, '');
	     	item.className=item.className.replace(/(^| )active( |$)/g, '');
	    }
  	}
    document.getElementById('tlink'+elementid).className.replace(/(^| )shadowed( |$)/g, '');
    
    
    document.getElementById('tlink'+elementid).className+=" active";
    
    if (elementid!='4') {
    	document.getElementById('tlink'+elementid).classname+=" shadowed";
    }
	for (i=1;i<5;i++) {
		var element=document.getElementById('tab' + i);
		element.style.display="none";
	}
	document.getElementById('tab' + elementid).style.display="block"; 
	
}

