var iQmultimedia = {
	init: function() {
		$(document.body).removeClass('js-disabled');
		iQmultimedia.replaceButtons();
		iQmultimedia.overText();
		iQmultimedia.formHelp.init();
		if($$('video').length > 0) {
			iQmultimedia.checkVideo();
		}
	},

	overText: function() {
		new OverText($('searchfield'));
	},

	replaceButtons: function() {
		var formsOnPage = [];
		
		// Find any inputs who have the class "button" and replace them with a link
		$$('input.button[type=submit]').each(function(item, index) {
			var replacementButton = new Element('a', {
				'class': item.getProperty('class') + ' btnsubmit',
				'name': item.getProperty('name'),
				'events': {
					'click': function() {
						var parentForm = this.getParent('form');
						parentForm.formSubmit = parentForm.submit;
						var inputName = this.getProperty('name');
						var hiddenField = new Element('input', {
							'type': 'hidden',
							'name': inputName,
							'value': this.get('text')
						});
						hiddenField.injectInside(parentForm);
						parentForm.formSubmit();
					}
				}
			});
			
			var parentForm = item.getParent('form');
			if(!formsOnPage.contains(parentForm)) {
				formsOnPage.push(parentForm);
			}

			replacementButton.set('html', item.value);
			replacementButton.replaces(item);
		});
		
		formsOnPage.each(function(formItem, formIndex) {
			formItem.addEvent('submit', function(evt) {
				var childSubmits = this.getElements('a.btnsubmit');
				var hiddenField = new Element('input', {
					'type': 'hidden',
					'name': childSubmits[0].getProperty('name'),
					'value': childSubmits[0].get('text')
				});
				hiddenField.injectInside(this);
			});
		});
	},
	
	formHelp: {
		init: function() {
			$$('a.formhelp').addEvent('click', iQmultimedia.formHelp.showHelp);
		},
		
		showHelp: function(evt) {
			evt.stop();

			var containerElement = this.getParent('fieldset');
			var helpBox = containerElement.getElement('div.help');
			if(helpBox) { // If there is already a help box, hide it
				helpBox.toggle();
			} else { // Otherwise, build a help box and fill it with content from the path
				helpBox = new Element('div', {
					'class': 'help'
				}).inject(containerElement.getElement('legend'), 'after');
				
				helpBox.set('html', '<p class="loading">Loading&hellip;</p>');
				helpBox.load(this.href);
			}
		}
	},

	checkVideo: function() {
		var videoElement = document.getElement('video');
		var supportsVideo = !!(videoElement.canPlayType);

		if(!supportsVideo && (Browser.Plugins.Flash.version < 9 || (Browser.Plugins.Flash.version == 9 && Browser.Plugins.Flash.build < 60))) {
			var warning = new Element('div', {
				'class': 'warning widget',
				'html': '<p>Please <a href="http://www.adobe.com/go/getflash">update Flash</a> or <a href="http://www.apple.com/quicktime/download/">QuickTime</a> if you can&rsquo;t see the video on the right</p>'
			});

			warning.inject(document.getElement('.pageheader .text'), 'bottom');
		} else if(supportsVideo && !videoElement.canPlayType('video/mp4')) {
			// Browser can't play MP4 in the video tag
			var fallback = videoElement.get('html');
			videoElement.getParent().set('html', fallback);
		}
	}
};

window.addEvent('domready', iQmultimedia.init);