var controller = (function($) {
	
	var attributes, 
		ticketHistory,
		entryTemplate = '<canvas id="canvas"></canvas>',
		wwClient = qmatic.webwidget.client,
		attributeKeys = {
			BACKGROUND_COLOR   			: 'background.color',
			TEXT_COLOR      			: 'text.color',
			TEXT_FONT		            : 'text.font',
			TEXT						: 'text',
			DIRECTION					: 'direction',
			SHOW_DURATION				: 'show.duration',
			TRANSITION_DURATION	 		: 'transition.duration',
			INTERPOLATOR				: 'interpolator'
		},
		attributeDefaults = {},
		scrolltext;

	attributeDefaults[attributeKeys.BACKGROUND_COLOR] = 'transparent';
	attributeDefaults[attributeKeys.TEXT_COLOR] = '#E5EDFF';
	attributeDefaults[attributeKeys.TEXT_FONT] = 'Helvetica;28pt;normal;normal';
	attributeDefaults[attributeKeys.TEXT] = 'Edit me...';
	attributeDefaults[attributeKeys.DIRECTION] = 'ltr';
	attributeDefaults[attributeKeys.SHOW_DURATION] = '2000';
	attributeDefaults[attributeKeys.TRANSITION_DURATION] = '500';
	attributeDefaults[attributeKeys.INTERPOLATOR] = 'decelerate';
	
	
	// Public contents of controller
	return {
		/**
		 * Called when widget client has finished loading. This is where you initialise your widget
		 */
		onLoaded : function(configuration) {
			$('canvas').remove();
									
			var attr = configuration.attributes;
			
			var attribParser = new qmatic.webwidget.AttributeParser(attr || {}),
				defaultsParser = new qmatic.webwidget.AttributeParser(attributeDefaults),
				ak = attributeKeys,
				
				backgroundColor = 	attribParser.getString(ak.BACKGROUND_COLOR, attributeDefaults[ak.BACKGROUND_COLOR]),
				
				textColor = 		attribParser.getString(ak.TEXT_COLOR, attributeDefaults[ak.TEXT_COLOR]);
				
				var textFontSize = 		attribParser.getFontSize(ak.TEXT_FONT, defaultsParser.getFontSize(ak.TEXT_FONT));
				var textFontFamily = 	attribParser.getFontFamily(ak.TEXT_FONT, defaultsParser.getFontFamily(ak.TEXT_FONT));
				var textFontStyle = 	attribParser.getFontStyle(ak.TEXT_FONT, defaultsParser.getFontStyle(ak.TEXT_FONT));
				
				var text = 				attribParser.getString(ak.TEXT, attributeDefaults[ak.TEXT]);
				
				var direction = 		attribParser.getString(ak.DIRECTION, attributeDefaults[ak.DIRECTION]);
				var showDuration = 			attribParser.getString(ak.SHOW_DURATION, attributeDefaults[ak.SHOW_DURATION]);
				var transitionDuration = 			attribParser.getString(ak.TRANSITION_DURATION, attributeDefaults[ak.TRANSITION_DURATION]);
				var interpolatorName = 					attribParser.getString(ak.INTERPOLATOR, attributeDefaults[ak.INTERPOLATOR]);
			
			attributes = attr;
			
			var number = new Date().toString(); 
			
			$('#scrolltext').html('<canvas id="canvas_' + number + '"></canvas>');
			if(backgroundColor)
				$('#canvas').css('background-color', backgroundColor);
			
			scrolltext = new ScrollingText();
			scrolltext.initScrollingText(text, textFontSize, textColor, textFontFamily, textFontStyle, direction, configuration.width.replace('px', ''), configuration.height.replace('px', ''), showDuration, transitionDuration, interpolatorName, number);
		},
		
		onLoadError : function(message) {
			$('body').append('<p>Widget load error: ' + message + '</p>');
		},
		
		onCleanUp : function() {
			// Perform cleanup of widget if necessary. Host allows 3 seconds worth of cleanup time before forcefully removing widget.
			if (window.console && window.console.log) {
				console.log('Scrolling text cleanup');
			}
		}
	};
	
	
})(jQuery);