ContentSlider = {
	debug:true,
	
	init : function() {
		// self=this;
		if($('.slider_bar').length > 0) {
			self.initContentSliders();
			// here for testing and to prevent breaking on resize
			/**/
			window.onresize = function(e) {self.initContentSliders(e);};
			
		}
	},
	
	keep_arrows_hidden:false,
	mask_width:0,
	initContentSliders : function(e) {
		// self=this;
		self.trace('initContentSliders()');
		if(e) {
			self.trace(' resize event');
			if($('.mask').width() < 640) {return;}
		}
		
		/* initialize the slider containers */http://www.cbc.ca/kidscbc2/the-feed/are-you-a-shark-dolphin-or-octopus
		$('.slider_bar').each(function(){
			self.trace('   initializing '+ $(this).attr('id'));
			var container = $(this).find('.slider_content');
			var count = $(container).find('.tile').length;
			var width = $(container).find('.tile').outerWidth();
			self.mask_width = $(this).find('.mask').width();
			
			/*
			self.trace(' -container: '+ container);http://www.cbc.ca/kidscbc2/the-feed/how-much-do-you-know-about-these-furry-desert-animals
			self.trace(' -count: '+ count);
			self.trace(' -width: '+ width);
			self.trace(' -self.mask_width: '+ self.mask_width);
			*/
			
			self.keep_arrows_hidden = false;
			if(self.mask_width >= 1050) {
				self.move_amount = 3;
				self.nudge_amount = (width/8)*5;
			}
			else if(self.mask_width >= 830) {
				self.move_amount = 2;
				self.nudge_amount = (width/8)*7;
			}
			else if(self.mask_width >= 640) {
				self.move_amount = 2;
				self.nudge_amount = (width/8)*6 + 10;
			}
			else {
				//self.move_amount = 2;
				self.keep_arrows_hidden = true;
				self.move_amount = 1;
			}
			
			$(container).attr('data-index', 0);
			$(container).css('width', (count * width)+'px');
			$(container).css('left', '0px');
		});
		
		/* initialize slider navigation */
		$('.slider_bar .slide_button.left_arrow').hide();
		if(!self.keep_arrows_hidden) {
			$('.slider_bar .slide_button.right_arrow').show();
			$('.mask').addClass('right_shadow');
			$('.mask').removeClass('left_shadow');
		}
		$('body').on('click', '.slider_bar .slide_button', self.slideHandler);
		
		$('body').on( 'swipeleft', '.slider_bar', function(e){e.direction="right";	self.slideHandler(e);});
		$('body').on( 'swiperight','.slider_bar', function(e){e.direction="left";	self.slideHandler(e);});
		
		/* size conditional to activate swipe (removed to always allow swipe)*/
		if(self.mask_width >= 648) {
			self.trace(' use arrow controls');
		} 
		else {
			self.trace(' use swipe controls');
		}
		
	},
	
	move_amount:0, 			// number of tiles to move each click (based on visible item count)
	nudge_amount:0,			// magic number that reveals a little of a tile when paging to the end
	is_moving:false, 		// used to prevent overclicking and breaking $.animate()
	slideHandler : function(e) {
		
		if(!e.handled && !self.is_moving) {
			self.trace('slideHandler()');
			self.trace('   target:'+ $(e.target).closest('.slider_bar').attr('id'));
			
			/*CHECK DIRECTION*/
			var direction;
			if(e.direction) {
				// self.trace('   direction:'+e.direction, 1);
				direction = e.direction;
			}
			else {
				if($(e.target).hasClass('left_arrow')) {
					direction = 'left';
				}
				else {
					direction = 'right';
				}
			}
			
			/* SET THE VARS */
			var parent = $(e.target).closest('.slider_bar');
			var mask = $(parent).find('.mask');
			var slider = $(parent).find('.slider_content');
			var current_position = parseInt($(slider).css("left"));
			var current_index = parseInt($(slider).attr('data-index')) * 1;
			var width = $(slider).find('.tile').outerWidth();
			var count = $(slider).find('.tile').length;
			
			var change; 	// new position
			var new_index;	// new page index
			if(direction == 'left') {
				new_index = current_index - self.move_amount;
				change = (current_position + (self.move_amount * width)); // move left
			}
			else {
				new_index = current_index + self.move_amount;
				change = (current_position - (self.move_amount * width)); // move right
			}
			
			/**/
			self.trace('   new_index: '+new_index);
			self.trace('   current_index: '+current_index);
			self.trace('   self.move_amount: '+self.move_amount);
			self.trace(' result: '+ (count - self.move_amount));
			
			
			self.trace('   arrow check');
			/* RUN A SANITY CHECK ON THE VARS */
			// normal movement
			if(!self.keep_arrows_hidden) {
				if(new_index < (self.move_amount - 1) && new_index < current_index) {
					self.trace('  case 1: (new_index < move_amount - 1) && (new_index < current_index)');
					new_index = 0;
					change = 0; 
					$(parent).find('.slide_button.left_arrow').hide();
					$(parent).find('.slide_button.right_arrow').show();
					$(mask).removeClass('left_shadow');
				}
				else if (new_index >= count - (self.move_amount + 1)) { // this magic number needs to be fixed with an a modulous check
					self.trace('  case 2: new_index >= count - (self.move_amount + 1)');
					new_index = count - self.move_amount;
					change = -((new_index * width) - self.nudge_amount);	
					$(parent).find('.slide_button.left_arrow').show();
					$(parent).find('.slide_button.right_arrow').hide();
					$(mask).removeClass('right_shadow');
				}
				else {
					self.trace('  case 2: else');
					//self.trace('>>>>>show dem shadows');
					$(parent).find('.slide_button.left_arrow').show();	
					$(parent).find('.slide_button.right_arrow').show();
					$(mask).addClass('right_shadow left_shadow');
				}
			}
			// swipe only movement
			else {
				if(new_index <= 0) {
					new_index = 0;
					change = 0;
				}
				else if(new_index > (count - 2)) {
					// self.trace(' - stopping here?');
					new_index = count - 1;
					change = -((new_index * width) + (width - self.mask_width));			
				}
			}
			
			/* APPLY VALUES, ANIMATE SLIDER */
			$(slider).attr('data-index', new_index);
			if(change != current_position) {
				self.is_moving = true;
				$(slider).animate({left:change+'px'},function(){self.is_moving = false});
			}
			e.handled = true;
		}
		
	},
	
	trace : function (msg, urgent) {
		// self=this;
		if(self.debug)	{
			console.log(msg);
			if(urgent) 	alert(msg);
		}
	}
	
}





