/*
Title: scroller.js - A Javascript file to handle the horizontal scrolling of images
Author: Chen Zihui (zihui@launchplus.sg)
*/

jQuery(document).ready(function(e) {
  // Event handling for clicking of the scroll arrows
  
    var arrItems=new Array();
    var arrIndex = 0;
    var maxLength = 0;
    var item_counter = 0;
    
    jQuery('#dyn-portfolio-images').find('li').each(function(index){
        maxLength = maxLength + jQuery(this).width();    
        arrItems[arrIndex] = jQuery(this).width();
        arrIndex++;
    });
    
    var maxItems = arrIndex;
     
     maxLength = (-1) * maxLength;
     maxLength = maxLength + (30*arrItems.length) + jQuery('#dyn-portfolio-images').find('li').width();
  
  jQuery('a.scroll-btn').click(function(e) {
    e.preventDefault();
	
    
	// Get current scroll value
	var currLeft = jQuery('#dyn-portfolio-images').css('left');
	
	currLeft = currLeft.substr(0, currLeft.indexOf('px'));
	

     
     
     var int_Left = parseInt(currLeft);
	// Check if left or right scroll selected
	if (jQuery(this).hasClass('right-scroll')) {
	  if ( int_Left < maxLength) {
	   return;
	  }
	  
	  var toScroll = parseInt(currLeft) - parseInt(arrItems[item_counter]) - 30;
      if ( item_counter < maxItems){
             item_counter++ ;

             };
             
	}
	else {
	  if (currLeft == 0) {
		return;
	  }
	  
	  var toScroll = parseInt(currLeft) + parseInt(arrItems[item_counter-1]) + 30;
	  if ( item_counter >= -1){
	          item_counter-- ;

              };
              
	}
	
	// Check if there is a scroll taking place
	if (jQuery('#dyn-portfolio-images').hasClass('scrolling')) { 
	  return false;
	}
	else {
	  jQuery('#dyn-portfolio-images').addClass('scrolling');
	}
	
	// Scroll image
	jQuery('#dyn-portfolio-images').animate({
	  left: toScroll + 'px'
	}, 700, function() { 
	  jQuery('#dyn-portfolio-images').removeClass('scrolling');
	});
  });  
});
