/**
 * Embeds a Flash object in the page. The object will be embedded in the passed
 * element, any existing contents of that element will be replaced. By using this
 * function the Flash object will work properly in all browsers, inclusing IE.
 * @param div The object in which the embed code will be placed.
 * @param id The ID that will be given to the inserted object.
 * @param url The relative or absolute path to the SWF file.
 * @param width Width of the SWF, in pixels.
 * @param height Height of the SWF, in pixels.
 * @param flashvars The collection of FlashVars to pass to the object. This
 *                  parameter can either be a string or an object with key/value
 *                  pairs.
 */
function flash(div, id, url, width, height, flashvars) {

	var flashvarsString = '';
	if (flashvars.length) {
		flashvarsString = flashvars;
	} else {
		// Iterate everything in the object and turn it into a query string
		for (var key in flashvars) {
			if (flashvarsString.length > 0) {
				flashvarsString += '&';
			}
			flashvarsString += key + '=' + flashvars[key];
		}
	}
	
	// Generate the embed code, without the <embed> tag that Dreamweaver produces
	// because it's not valid XHTML.
	var embed = '';	
	embed += '<object type="application/x-shockwave-flash" id="' + id + 
	         '" data="' + url + '" width="' + width + '" height="' + height + '">';
	embed += '    <param name="movie" value="' + url + '" />';
	embed += '    <param name="allowScriptAccess" value="always" />';
	embed += '    <param name="allowFullscreen" value="true" />';
	embed += '    <param name="flashvars" value="' + flashvarsString + '" />'
	embed += '</object>';
	
	div.innerHTML = embed;	
}

/**
 * Changes the language to the specified value. The page will reload to show
 * the new language.
 */
function changeLanguage(language) {
	
	var currentURL = window.location.toString();
	if (currentURL.indexOf('language=') != -1) {
		currentURL = currentURL.substring(0, currentURL.indexOf('language') - 1);
	}
	
	if (currentURL.indexOf('?') == -1) {
		window.location = currentURL + '?language=' + language;
	} else {
		window.location = currentURL + '&language=' + language;
	}
}

/**
 * Inserts an URL into the textfield with the specified ID. The URL will be 
 * wrapped around any currently selected text, if no text is selected it will 
 * be placed at the current position of the caret. The produced URL will open 
 * in a new window.
 */
function assistURL(textfieldID) {

	var textarea = document.getElementById(textfieldID);
	var startIndex = textarea.selectionStart;
	var endIndex = textarea.selectionEnd;
	
	if ((startIndex < 0) || (endIndex < 0)) {
		startIndex = 0;
		endIndex = 0;
	}
	
	var url = prompt('', '');
	if ((url == null) || (url.length == 0)) {
		return;
	}
	
	var prefixText = textarea.value.substring(0, startIndex);
	var selectedText = textarea.value.substring(startIndex, endIndex);
	var suffixText = textarea.value.substring(endIndex, textarea.value.length);
	textarea.value = prefixText + '<a href="' + url + '" target="_blank">' + 
			selectedText + '</a>' + suffixText;
}
