/*
 * Global variables used later
 */
  // Duration of the effects - Speech & Compliments
	var effect_duration_in_seconds = 0.2;
  // When a popup layer is used we hide the title copy - should we?
	var hide_alt_and_title_copy_for_popup_layers = true;
  // Number of seconds to display the "Loading" icon when going remote to get the voice-over
	var display_speech_loading_icon_for_x_seconds = 45;


/*
 * Function used for excluding certain browsers from the more 'dynamic' functions
 */
function return_browser_excluded() {
  // We do not want to run the clever stuff if it's Internet Explorer on a Mac
	if(!is_windows  &&  is_ie) {
		return true;
	}

  // All is OK
	return false;
}


/*
 * Function used to convert an 'em' value into pixels
 * - To modify the value returned supply a second buffer value
 */
function convert_em_to_pixels(css_value, pixel_buffer, check_for_default_font_size) {
  // Are we supposed to check for the default font-size?
	if(check_for_default_font_size  &&  css_value == "62.5%") {
	  // Backup for Internet Explorer (1em / 62.5% = 10px)
		return "10px";
	}

  // Is the value supplied in 'em'?
	if(css_value.indexOf("em") == -1) {
	  // This is not an 'em' value
		return css_value;
	}

  // Remove the 'em' from the value
	css_value = css_value.replace("em", "");
  // Convert the resulting value to a float
	css_value = parseFloat(css_value);
  // Make sure we still have a number
	if(isNaN(css_value)) {
		css_value = 10;
	}

  // Create the pixel value
	css_value = css_value * 10;
  // Make sure we now have a whole number
	css_value = parseInt(css_value);
  // Add in room for error?
	if(pixel_buffer  &&  !isNaN(parseInt(pixel_buffer))) {
		css_value += parseInt(pixel_buffer);
	}

  // Return our new pixel value
	return css_value.toString() + "px";
}


/*
 * Speaking function(s)
 * - Green background needs to be preloaded otherwise white text on a white background really doesn't work!
 */
var popup_corners = new Array();
var popup_corners_preloaded = new Array();
popup_corners[0]  = "/img/popup-m.gif";
popup_corners[1] = "/img/popup-bl.gif";
popup_corners[2] = "/img/popup-br.gif";
popup_corners[3] = "/img/popup-tl.gif";
popup_corners[4] = "/img/popup-tr.gif";
function preload_popup_images() {
	for(var i=0; i<popup_corners.length; i++) {
		popup_corners_preloaded[i] = new Image();
		popup_corners_preloaded[i].src = popup_corners[i];
	}
}
preload_popup_images();


/*
 * Speaking function(s)
 */
var speakme_alt = "", speakme_title = "";
var css_reset_speakme = "Element.setStyle('speakmeexplain', {width:'#width#', height:'auto', left:'#left#', top:'#top#', fontSize:'#font-size#'});";
var speech_loading = new Image();
speech_loading.src = "/img/btn-thinking.gif";
var speech_timer;
function speakme(speakurl) {
  // Update image being used for "Speech" to the loading version and then revert back in 'x' seconds
	swap_speakme_image("on");

  // Fall back code, incase we end up at a remote URL to get the voice-over
	speech_timer = setTimeout('swap_speakme_image("off");', (display_speech_loading_icon_for_x_seconds * 1000));

  // Setup values
	var hostname = document.location.host;
	var speech_url = "http://asp.readspeaker.net/cgi-bin/endemorsone?customerid=1003300&url=http://" + hostname + speakurl;

  // Are we running on a local URL?
	if(hostname.indexOf(".local") != -1) {

		var view_speaking_version = window.confirm("To either download the current live speech (or used a cached version if available) simply click 'OK', otherwise 'Cancel'.");
		if(view_speaking_version) {
		  // New version to "grab" the WAV files
			document.getElementById("speech").src = "/grab_speech_file.html?url=" + speakurl;
			return false;
		}
		var view_speaking_version_manually_read = window.confirm("Would you prefer to simply view the basic copy online instead?");
		if(view_speaking_version_manually_read) {
			top.location.href = speakurl;
			return false;
		}
	  // Revert back to the original image - clearing and overwriting the timer
		clearTimeout(speech_timer);
		swap_speakme_image("off");

	} else {
	  // New version to "grab" the WAV files
		document.getElementById("speech").src = "/grab_speech_file.html?url=" + speakurl;
	}
	return false;
}
function swap_speakme_image(on_or_off) {
	if(on_or_off == "on") {
	  // Show the loading image
		$("speakmebubble").src = speech_loading.src;
	} else {
	  // Revert back to the original image
		$("speakmebubble").src = "/img/btn-speech.gif";
	}
}
function prepare_css_reset_speakme() {
	var css_values = new Array(4);

  // Make sure this function has not already been called
	if(css_reset_speakme == "") {
		return false;
	}
	if(css_reset_speakme.indexOf("#") == -1) {
		return true;
	}

  // Check to see if the values returned are 'em' or pixels
  // - If they are 'em' we need to convert them to pixels
	css_values["width"] = Element.getStyle("speakmeexplain", "width");
	css_values["width"] = convert_em_to_pixels(css_values["width"], 5);
	css_values["left"] = Element.getStyle("speakmeexplain", "left");
	css_values["left"] = convert_em_to_pixels(css_values["left"], 24);
	css_values["top"] = Element.getStyle("speakmeexplain", "top");
	css_values["top"] = convert_em_to_pixels(css_values["top"], 7);
	css_values["font-size"] = Element.getStyle("speakmeexplain", "font-size");
	css_values["font-size"] = convert_em_to_pixels(css_values["font-size"]);

  // Validate values retrieved
	if(css_values["width"].indexOf("px") == -1  ||  css_values["left"].indexOf("px") == -1  ||  css_values["top"].indexOf("px") == -1  ||  css_values["font-size"].indexOf("px") == -1) {
	  // We have a problem!
		css_reset_speakme = "";
		return false;
	}

  // Update the reset function string
  // - This would usually be done in a loop but Internet Explorer 6 does not like my loop!
	css_reset_speakme = css_reset_speakme.replace("#width#", css_values["width"]);
	css_reset_speakme = css_reset_speakme.replace("#left#", css_values["left"]);
	css_reset_speakme = css_reset_speakme.replace("#top#", css_values["top"]);
	css_reset_speakme = css_reset_speakme.replace("#font-size#", css_values["font-size"]);

  // Indicate success
	return true;
}
function speakme_explain(linkobj, over) {
  // Are we supposed to run this function for this browser?
	if(return_browser_excluded()) {
		return;
	}

  // Make sure we did not have a problem with an earlier call to this function
	if(css_reset_speakme == ""  ||  !linkobj) {
		return;
	}

  // Set the reset values manually using pixels instead of 'em' (the pixel values only need to be done once)
  // - This block of code will be run the first time this function is called
	if(over  &&  css_reset_speakme.indexOf("#") != -1) {
		if(!prepare_css_reset_speakme()) {
		  // We have a problem!
			return;
		}
	}

  // Make sure the CSS attributes have been reset correctly before applying an effect
  // - Sometimes these can go wrong if the user mouses over & out very quickly a number of times
	eval(css_reset_speakme);

  // Mouseover
	if(over) {
	  return true;
	  // Shall we remove the alt & title text from the Speech Bubble link
		if(hide_alt_and_title_copy_for_popup_layers) {
			speakme_alt = $("speakmebubble").alt;
			$("speakmebubble").alt = "";
			speakme_title = linkobj.title;
			linkobj.title = "";
		}

	  // Update the layer with the copy
		Element.update("speakmeexplain_copy", "To have this page read out to you please click the speech bubble, wait a second or two, and turn up your speakers.");

	  // Before we show the popup layer the sub-navigation needs to be pushed back for IE so the layer appears above it
		if(is_ie) {
			Element.setStyle("subnavigation2", {zIndex:-10});
		}

	  // 'Grow' the explanation copy
		new Effect.BlindDown(
			"speakmeexplain",
			{
				scaleX : true,
				scaleY : true,
				scaleContent : false,
				duration : effect_duration_in_seconds
			}
		);
		return;
	}

  // Mouseout
	new Effect.BlindUp(
		"speakmeexplain",
		{
			scaleX : true,
			scaleY : true,
			scaleContent : false,
			duration : effect_duration_in_seconds,
			afterFinish : function(effect) {
			  // Remove the copy from the layer
				Element.update("speakmeexplain_copy", "");
			}
		}
	);

  // Make sure we make the z-index a positive number so the 'read more' links work again!
  // - The timer is so the minimise effect has time to run before this is shown
	if(is_ie) {
		setTimeout("Element.setStyle('subnavigation2', {zIndex:10});", (effect_duration_in_seconds * 1000));
	}

  // Add the alt & title text back into the Speech Bubble link (only if it was actually hidden)
	if(hide_alt_and_title_copy_for_popup_layers) {
		$("speakmebubble").alt = speakme_alt;
		linkobj.title = speakme_title;
	}
}


/*
 * Display the full compliment when the 'read more' link is moused-over
 */
var compliment_title = "";
var css_reset_compliment = "Element.setStyle('#full_id#', {width:'#width#', height:'auto', fontSize:'#font-size#'});";
function prepare_css_reset_compliment(full_layer_id) {
	var css_values = new Array(4);

  // Make sure this function has not already been called
	if(css_reset_compliment == "") {
		return false;
	}
	if(css_reset_compliment.indexOf("#") == -1) {
		return true;
	}

  // Check to see if the values returned are 'em' or pixels
  // - If they are 'em' we need to convert them to pixels
	css_values["width"] = Element.getStyle(full_layer_id, "width");
	css_values["width"] = convert_em_to_pixels(css_values["width"], 5, true);
	css_values["font-size"] = Element.getStyle(full_layer_id, "font-size");
	css_values["font-size"] = convert_em_to_pixels(css_values["font-size"]);

  // Validate values retrieved
	if(css_values["width"].indexOf("px") == -1  ||  css_values["font-size"].indexOf("px") == -1) {
	  // We have a problem!
		css_reset_compliment = "";
		return false;
	}

  // Update the reset function string
  // - This would usually be done in a loop but Internet Explorer 6 does not like my loop!
	css_reset_compliment = css_reset_compliment.replace("#width#", css_values["width"]);
	css_reset_compliment = css_reset_compliment.replace("#font-size#", css_values["font-size"]);

  // Indicate success
	return true;
}
function popup_compliment(linkobj, compliment_id, over) {
  // Are we supposed to run this function for this browser?
	if(return_browser_excluded()  ||  is_opera) {
		return;
	}

  // Do we have a required variable?
	if(css_reset_compliment == ""  ||  !linkobj  ||  isNaN(parseInt(compliment_id))) {
		return;
	}

  // ID of layer to use
	var layer_id = "full_compliment" + compliment_id.toString();

  // Set the reset values manually using pixels instead of 'em' (the pixel values only need to be done once)
  // - This block of code will be run the first time this function is called and the values set are the same for all compliments
	if(over  &&  css_reset_compliment.indexOf("#") != -1) {
		if(!prepare_css_reset_compliment(layer_id)) {
		  // We have a problem!
			return;
		}
	}

  // Make sure the CSS attributes have been reset correctly before applying an effect
  // - Sometimes these can go wrong if the user mouses over & out very quickly a number of times
	eval(css_reset_compliment.replace("#full_id#", layer_id));

  // Mouseover
	if(over) {
	  // Shall we remove the alt & title text from the Speech Bubble link
		if(hide_alt_and_title_copy_for_popup_layers) {
			compliment_title = linkobj.title;
			linkobj.title = "";
		}

	  // Fetch the copy required for this compliment and only try to display it if the call is successful
		new Ajax.Updater(
			layer_id + "_copy",
			"/ajax/return_compliment.php",
			{
				parameters : "id=" + parseInt(compliment_id),
				onComplete : function() {
					new Effect.BlindDown(
						layer_id,
						{
							scaleX : true,
							scaleY : true,
							scaleContent : false,
							duration : effect_duration_in_seconds
						}
					);
				}
			}
		);
	  // We have finished
		return;
	}
  // Mouseout
	new Effect.BlindUp(
		layer_id,
		{
			scaleX : true,
			scaleY : true,
			scaleContent : false,
			duration : effect_duration_in_seconds
		}
	);

  // Add the title text back into the link (only if it was actually hidden)
	if(hide_alt_and_title_copy_for_popup_layers) {
		linkobj.title = compliment_title;
	}
}
