var Flash = (function($) {
	
	var MUTE = 'mute',
		UNMUTE = 'unmute',
		LQ = 'lq',
		HQ = 'hq';
	
	var Flash = {
		call: function(action) {
			// find all Flash objects.
			var o = $('object, embed');
			var ids = [];
	
			// Call their actions by their IDs.
			for (var i = 0; i < o.size(); i++) {
				swfobject.getObjectById(o.get(i).id)[action]();
			}
		}
	}
	
	Links = new LBi.LinkRelations();

	// handle flash interaction.
	Links.subscribe(/flash/, function(e) {
	
		e.preventDefault();
	
		// toggle the state.
		$(e.target).closest('li').toggleClass('toggle');
	
		// get the action to call on all Flash objects.
		var action = e.target.rel.replace('flash-', '');
		
		// Update cookies.
		switch (action) {
			case MUTE :
				LBi.setCookie('sound', action);
				break;
			case UNMUTE :
				LBi.setCookie('sound', action);
				break;
			case LQ : 
				LBi.setCookie('bandwidth', action);
				break;
			case HQ :
				LBi.setCookie('bandwidth', action);
				break;
			default : 
				break;
		}
		
		Flash.call(action);
	
	});
	
	$(function() {

		/*
		 * Set toggle class based on cookies.
		 *
		 */

		var sound = LBi.getCookie('sound');
		if (sound === MUTE) {
			$('.sound').addClass('toggle');
		}

		var bandwidth = LBi.getCookie('bandwidth');
		if (bandwidth === LQ) {
			$('.bandwidth').addClass('toggle');
		}
		
		/*
		 * Initialise tooltips for bandwidth and sound.
		 *
		 */
		
		var Tooltip = LBi.Class.extend(
			LBi.Tooltip,
			function() {}, 
			{
				render: function(e) {

					// the root element of the definition.
					var root = $(e.target);
					
					// set a data object.
					var data = {};
					data.tip = root.attr('name');

					// parse the template.
					var html = this.settings.template.parse(data);

					// insert the generated HTML
					$('#canvas').append(html);

					var tooltip = $(this.settings.tooltipSelector);

					// set the position of the tooltip.
					tooltip.css({
						top: root.offset().top + root.height() + 10,
						left: root.offset().left + (root.outerWidth() / 2) - tooltip.outerWidth() + 28
					});
					
				}
			}
		);
		
		new Tooltip('.bandwidth a, .sound a', {
			template: new LBi.Template('<div class="options-tooltip"><p>$tip</p><div class="tip_arrow"></div></div>'),
			tooltipSelector: '.options-tooltip'
		});
		
	});
	
	return Flash;

})(jQuery);
