// control vars

$(document).ready(function () {
 
  // set up scrollbars where required
  $('.scroll-pane-wide').jScrollPane({showArrows:true});
  	
  // pagination and scrolling variables
  var currentPage = 0;
  var viewHeight = $('#viewport').height();
  var contentHeight;
  setHeights();
  var maxScrollUp = contentHeight - viewHeight;
  
  var nextPage;
  var count = $('.pageItem').size();//set total count of paginated items
  var dir;
  //$('.pageItem').hide();//hide all paginate items
  //$('#sectionHeading').hide();	
  $('#viewport').hide();//hide all paginate items
  $('#controlBar').hide();//hide all paginate items
  
  $('.pageItem:first').show();//fade in page 1 of content
  $('#pageCtl .pgBtn:eq('+currentPage+')').toggleClass('red');//set class on pag btns - buggy on links
  $('#pageCtl .pgBtn').bind('click',numberedContent); //event listener for click on page numbers
  $('#pageCtl .cycleBtn').bind('click',cycleContent); //event listener for click on next/prev
  $('#scrollCtl a').bind('click',scrollController);
  //showScrollControl(); //call function
  $('.pageItem:first').css('visibility','visible');
  //$('#sectionHeading').fadeIn(1000);
  $('#viewport').fadeIn(1500);
  $('#controlBar').css('visibility','visible');
  $('#controlBar').fadeIn('slow');

// reset scroll to 0
function setHeights()
{
  contentHeight = $('.pageItem:eq('+currentPage+')').height();
  $('.pageItem:eq('+currentPage+')').css('top','0');
  maxScrollUp = contentHeight - viewHeight;
  if(maxScrollUp < 0) { $('#scrollCtl').hide(); } else { $('#scrollCtl').show(); }
  //console.log(contentHeight, maxScrollUp);
}


// pagination control function
// paginaation controls shown/hidden in eskript based on # of content fields with content
// deal with numbered page buttons
function numberedContent(event) {
	nextPage = ($(event.target).text())-1;
	swapContent(nextPage);
	return false;
}

// deal with next/prev btn
function cycleContent(event)
{
	var dir = $(event.target).text();
	if (dir == 'next') {
		if(currentPage == count-1) {
			nextPage = 0;
			swapContent(nextPage);
		} else {
			nextPage = currentPage+1;
			swapContent(nextPage);
		}
	} else if (dir == 'previous'){
		if(currentPage == "0") {
			nextPage = count-1;
			swapContent(nextPage);
		} else {
			nextPage = currentPage-1;
			swapContent(nextPage);
		}
	}
	return false;
}

// change content function
function swapContent(nextPage)
{
  $('.pageItem:eq('+currentPage+')').fadeOut('slow', fadeInNext);
  return false;
}
function fadeInNext() {
  $('.pageItem:eq('+nextPage+')').hide();
  $('.pageItem:eq('+nextPage+')').css('visibility','visible');
  $('.pageItem:eq('+nextPage+')').fadeIn('slow');
  $('.pgBtn:eq('+currentPage+')').toggleClass('red');
  $('.pgBtn:eq('+nextPage+')').toggleClass('red');
  currentPage = nextPage;
  setHeights();
}
// scrolling control functions
//start scroll event handler
  function scrollController(event)
  {
    
    var direction = 1;
    if($(this).attr('id') == 'up') {
      direction = -1;
    }
    //console.log('DIrection:'+direction);
    var amount = "-="+(100*direction);
    var curTop = $('.pageItem:eq('+currentPage+')').css('top').substring(0,$('.pageItem:eq('+currentPage+')').css('top').length-2);
    //console.log(amount,curTop);
    if((direction == 1 && curTop > -maxScrollUp) || (direction == -1 && curTop < 0)) {
      $('.pageItem:eq('+currentPage+')').animate({top: amount }, 'slow')			
    }
   return false;
  }
   
});

