/*******************************************************************************
 * Licensed Materials - Property of IBM
 * Mobile Browser Simulator
 * © Copyright IBM Corporation 2011. All Rights Reserved.
 *
 * U.S. Government Users Restricted Rights - Use, duplication or 
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *******************************************************************************/

/**
 * Defines a widget used to calculate the PPI of the users monitor
 */
define("ibm_mobile/PPICalculator",[
	"dojo",
	"dijit",
	"dijit/_Widget",
	"dijit/_TemplatedMixin",
	"dijit/_WidgetsInTemplateMixin",
	"dojo/text!ibm_mobile/templates/PPICalculator.html",
	"dijit/form/Button",
	"dojo/html",
	"dojo/i18n"
], function(dojo, dijit, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin, ppiCalculatorTemplate) {
	
	var PPICalculator = dojo.declare("ibm_mobile.PPICalculator", [dijit._Widget, dijit._TemplatedMixin, dijit._WidgetsInTemplateMixin ], {
	
		// PUBLIC PARAMS
		screenPPI : 97,
		
		// PUBLIC EVENTS
		onScreenPPISet : function(newScreenPPI) {},
		
		// WIDGET PARAMS
		templateString : ppiCalculatorTemplate,
	
	
		// PRIVATE CONSTANTS
		/** ISO/IEC 7810 ID-1 width in inches (standard credit card) */
		CREDIT_CARD_WIDTH_IN : 3.37,
		
		/** ISO/IEC 7810 ID-1 height in inches (standard credit card) */
		CREDIT_CARD_HEIGHT_IN : 2.125,
		
		// PRIVATE
		_upArrowTimers : null,
		_downArrowTimers : null,
		
		// DOJO ATTATCH POINTS
		_descriptionDIV : null,
		_stepsOL : null,
		_ppiDragButton: null,
		_bottomCalibrationBar: null,
		_creditCard: null,
		
		/**
		 * <p>Widget lifecycle function.</p>
		 * 
		 * @param params
		 */
		constructor : function() {
			this._upArrowTimers = [];
			this._downArrowTimers = [];
		},
		
		/**
		 * <p>Widget lifecycle function.</p>
		 */
		postCreate : function() {
			//add in translated strings
			var nls = dojo.i18n.getLocalization("ibm_mobile", "mobile");
			dojo.html.set(this._descriptionDIV, nls.PPICalculator_desc);
			var steps = [nls.PPICalculator_step_1, nls.PPICalculator_step_2];
			dojo.forEach(steps, function(step) {
				dojo.create("li", {
					innerHTML : step
				}, this._stepsOL, "last");
			}, this);
			
			
			//hook up the drag events
			dojo.connect(this._ppiDragButton, 'onMouseDown', dojo.hitch(this, this._onMouseDown));
			dojo.connect(this._bottomCalibrationBar, 'onmousedown', dojo.hitch(this, this._onMouseDown));
			
			// wire UP and DOWN keys to calibration
			dojo.connect(this, 'onKeyDown', dojo.hitch(this, this._onKeyDown));
			dojo.connect(this, 'onKeyUp', dojo.hitch(this, this._onKeyUp));
		},
		
		getScreenPPI : function() {
			return this.screenPPI;
		},
		
		setScreenPPI : function(newScreenPPI) {
			this.screenPPI = newScreenPPI;
			
			dojo.style(this._creditCard, {
				'width' : (this.CREDIT_CARD_WIDTH_IN * this.screenPPI) + 'px',
				'height' : (this.CREDIT_CARD_HEIGHT_IN * this.screenPPI) + 'px'
			});
			
			this.onScreenPPISet(this.screenPPI);
		},
		
		_onMouseDown : function(downEvent) {
			var startY = downEvent.clientY;
			var startHeight = dojo.style(this._creditCard, 'height');
		
			//start tracking mouse movements
			var mouseMoveConnection = dojo.connect(dojo.doc, 'onmousemove', dojo.hitch(this, function(moveEvent) {
				var newHeight = startHeight + moveEvent.clientY - startY;
			
				if(newHeight < 1) {
					newHeight = 1;
				}
				
				this.setScreenPPI(newHeight / this.CREDIT_CARD_HEIGHT_IN);
			}));
			
			//stop tracking drag on mouse up
			var onMouseUpConnection = dojo.connect(dojo.doc, 'onmouseup', dojo.hitch(this, function() {
				dojo.disconnect(onMouseUpConnection);
				dojo.disconnect(mouseMoveConnection);
			}));
			
			//stop the event from propagating
			dojo.stopEvent(downEvent);
		},
		
		_onKeyDown : function(keyEvent) {
			var resizeTimer = new dojox.timing.Timer(200);
			
			if(keyEvent.keyCode == KeyEvent.DOM_VK_UP) {
				this._upArrowTimers.push(resizeTimer);
				resizeTimer.onTick = dojo.hitch(this, function() {
					this.setScreenPPI(this.screenPPI-1);
				});
			} else if(keyEvent.keyCode == KeyEvent.DOM_VK_DOWN) {
				this._downArrowTimers.push(resizeTimer);
				resizeTimer.onTick = dojo.hitch(this, function() {
					this.setScreenPPI(this.screenPPI+1);
				});
			}
			
			//call at least once
			resizeTimer.onTick();
			
			//start timer for key held
			resizeTimer.start();
		},
		
		_onKeyUp : function(keyEvent) {
			if(keyEvent.keyCode == KeyEvent.DOM_VK_UP) {
				dojo.forEach(this._upArrowTimers, function(timer) {
					timer.stop();
				});
			} else if (keyEvent.keyCode == KeyEvent.DOM_VK_DOWN) {
				dojo.forEach(this._downArrowTimers, function(timer) {
					timer.stop();
				});
			}
		}
	}); //end dojo.declair
	
	return PPICalculator;
});