Create code.js

var Queue = Queue || {};

Queue = (function() {
	var 
	_cache = {
		customer: $('.customer'),
		customerRow: $('.customer-title'),
		cartTxt: $('.cart-status'),
		queueScreen: $('#queue-container'),
		verifyScreen: $('#verify-container'),
		isCustomerExpanded: 'false'
	},

	_init = function() {
		_showDetails();
		_addIscrolls();
		_toggleButtons();
	},

	/**
	 * show details when customer row is clicked
	 */
	_showDetails = function() {

		_cache.customerRow.on('click', function(e) {

			var _this = $(this);

			// if you are clicking on one that isn't currently open
			if (!_this.parent().hasClass('open')) {
				
				if (_this.parent().siblings().hasClass('open')) {
					// remove all other customers that are open and add open class to current item
					$('.customer.open')
						.removeClass('open')
						.find('.customer-details')
							.slideToggle('fast');

					// trigger false on close of customer details
					_setParams('false');
				}
				
			} else {

				// trigger false on close of customer details
				_setParams('false');
			}
			
			// show customer-details of clicked element
			_this
				.next('.customer-details')
					.slideToggle('fast', _refreshScroller(Queue.MainScroller))
				.parent()
					.toggleClass('open');
		});
	},

	/**
	 * add iscrolls
	 */
	_addIscrolls = function() {

		// main iScroll
		Queue.MainScroller = new IScroll('#main-scroller-wrapper', {
			mouseWheel: true,
			click: true
		});
	},

	/**
	 * refresh scroller after more details are shown
	 */
	_refreshScroller = function(scroller) {
		setTimeout( function(){
			scroller.refresh()
		}, 200);
	},

	/**
	 * logic for when red action button is clicked
	 */
	_toggleButtons = function(e) {
		$('.red-action').on('click', function(){

			var _this = $(this);

			// if it's a store item
			if (_this.hasClass('store-button')) {
				_storeButtonClicked(_this);
			} else if (_this.hasClass('assist-button')) {
				_setParams('true');
			}
			
			// add selected class (red bg color and white text)
			_this.toggleClass('selected');
		});
	},

	/**
	 * set params for app to listen to
	 */
	_setParams = function(param) {
		_cache.isCustomerExpanded = param;

		if (_cache.isCustomerExpanded === 'true') {
			_cache.queueScreen.toggle();
			_cache.verifyScreen.toggle();
		
			_setVerifyProtocol();
		}

       	document.location = 'http://AKVZ_MTI_APP:' + 'isCustomerExpanded:' + _cache.isCustomerExpanded;
	},

	/**
	 * once verify screen shows, add a protocol for whenever you click on it
	 */
	_setVerifyProtocol = function() {

		_cache.verifyScreen.on('touchend', function() {
			document.location = 'http://AKVZ_MTI_APP:verifyWasTapped';
		});
	},

	/**
	 * changes text and addes item to cart when button in store sidebar is clicked
	 */
	_storeButtonClicked = function(element) {
		
		// change button text
		if (element.text() === 'Get It') {
			element.text('Got It');
			
			// if customer open, add to their cart
			if (_cache.customer.hasClass('open')) {
				$('.customer.open').addClass('has-item').find('.cart-status').show().append('1 Item');
			}

		} else {
			element.text('Get It');
			$('.customer.has-item').find('.cart-status').hide().html('Cart: ');
		}	
	};

	return {
		init: _init
	};

}());

$(Queue.init);