/**
 * Generates inline JavaScript for displaying a platform-specific screenshot
 * image
 *
 * A suitable noscript tag containing the default image tag (Vista) is also
 * returned.
 *
 * Also note, the utils.js script should be included in the document head for
 * the JavaScript generated by this function to work correctly.
 *
 * @param string  $filename  the original filename (Windows version).
 * @param string  $alt       the alt text.
 * @param integer $width     optional. The image width.
 * @param integer $height    optional. The image height.
 * @param string  $class     optional. CSS class to apply to the image.
 * @param array   $platforms optional. Array of extra platforms. Valid extra
 *                           platforms are: 'mac', 'linux' and 'xp'. By default,
 *                           the extra platform 'mac' is used. Vista is the
 *                           fallback platform for all images. If no extra
 *                           platforms are specified, a simple image tag is
 *                           displayed.
 *
 * @return string a piece of inline JavaScript that can be used to display a
 *                platform-specific screenshot for the given parameters.
 */
function buildPlatformImage($filename, $alt, $width = null, $height = null,
	$class = null, $platforms = array('mac'))
{
	$valid_platforms = array('mac', 'linux', 'xp');
	$platforms = array_intersect($valid_platforms, $platforms);

	// maps platforms to JavaScript boolean expressions
	$platform_conditional = array(
		'mac'     => 'gPlatform == PLATFORM_MACOSX',
		'linux'   => 'gPlatform == PLATFORM_LINUX',
		'xp'      => '!gPlatformVista',
	);

	$filename = minimizeEntities($filename);
	$filename = escapeForJavaScript($filename);

	$alt = minimizeEntities($alt);
	$alt = escapeForJavaScript($alt);
	$alt = str_replace('%', '%%', $alt);

	// build image tag template
	$image_template = '<img src="%s" alt="' . $alt .'"';
	if (is_int($width)) {
		$image_template.= ' width="' . $width . '"';
	}
	if (is_int($height)) {
		$image_template.= ' height="' . $height . '"';
	}
	if (is_string($class)) {
		$class = minimizeEntities($class);
		$class = escapeForJavaScript($class);
		$class = str_replace('%', '%%', $class);
		$image_template.= ' class="' . $class . '"';
	}
	$image_template.= ' />';

	// build platform-specific image tags
	$filename_exp = explode('.', $filename);
	$extension = array_pop($filename_exp);
	$base = implode('.', $filename_exp);
	foreach ($platforms as $platform) {
		$platform_filename = $base . '-' . $platform . '.' . $extension;
		$platform_image[$platform] =
			sprintf($image_template, $platform_filename);
	}

	$default_image = sprintf($image_template, $filename);

	ob_start();

	if (count($platforms) === 0) {
		echo $default_image;
	} else {
		echo '<script type="text/javascript">// <![CDATA[', "\n";

		foreach ($platforms as $index => $platform) {
			if ($index == 0) {
				printf("if (%s) {\n\tdocument.write('%s');\n",
					$platform_conditional[$platform],
					$platform_image[$platform]);
			} else {
				printf("} else if (%s) {\n\tdocument.write('%s');\n",
					$platform_conditional[$platform],
					$platform_image[$platform]);
			}
		}

		printf("} else {\n\tdocument.write('%s');\n}\n",
			$default_image);

		echo '// ]]></script><noscript><div style="display: inline;">',
			$default_image,
			"</div></noscript>";
	}

	return ob_get_clean();
}
 /**
  * Gets a JavaScript structure for localized Firefox builds. NOTE: ***This requires the escapeForJavaScript() function
  * but it isn't defined in this library!  Since this is a function for a specific part of mozilla.com I'm leaving
  * the function out of the library but if this becomes used elsewhere we should copy it in.  If you need the function
  * you can find it at moz.com/includes/functions.inc.php.***
  *
  * This stucture is used for JavaScript searching on the 'All Languages'
  * page and for JavaScript platform switching on the same page.
  *
  * Structured as:
  * [
  *  [
  *   $locale_id,
  *   $english,
  *   $native,
  *   { 'Windows': { 'filesize': '9.0' }, 'OS X': { ... etc ... } }
  *  ],
  *  ... etc ...
  * ]
  *
  * @param array options
  * @return string JavaScript structure.
  */
 function _getJavaScriptBuildArray($build_array, $options = array())
 {
     $options['latest_version'] = array_key_exists('latest_version', $options) ? $options['latest_version'] : LATEST_FIREFOX_VERSION;
     $_languages = array();
     if (array_key_exists('continent', $options)) {
         // if continent is specified, get build info for the specified continent only
         $_build_array = array();
         if (array_key_exists($options['continent'], $this->localeDetails->continents)) {
             foreach ($this->localeDetails->continents[$options['continent']] as $_location) {
                 if (array_key_exists($_location, $this->localeDetails->locations)) {
                     foreach ($this->localeDetails->locations[$_location]['primary'] as $_locale) {
                         if (array_key_exists($_locale, $build_array)) {
                             $_build_array[$_locale] = $build_array[$_locale];
                         }
                     }
                     foreach ($this->localeDetails->locations[$_location]['secondary'] as $_locale) {
                         if (array_key_exists($_locale, $build_array)) {
                             $_build_array[$_locale] = $build_array[$_locale];
                         }
                     }
                 }
             }
         }
     } else {
         // no continent specified, use all build info
         $_build_array = $build_array;
     }
     $_build_array = $this->_sortBuildArrayByEnglishName($_build_array);
     foreach ($_build_array as $_locale => $_build_info) {
         $_build_info = $_build_info[$options['latest_version']];
         $_locale_details = $this->localeDetails->languages[$_locale];
         // skip languages that don't have a FF3 release
         if (empty($_build_info)) {
             continue;
         }
         $_language = array();
         // [0] locale-id field
         $_language[] = "'" . escapeForJavaScript($_locale) . "'";
         // [1] English language version field
         $_language[] = "'" . escapeForJavaScript($_locale_details['English']) . "'";
         // [2] localized version field
         $_language[] = "'" . escapeForJavaScript($_locale_details['native']) . "'";
         $_builds = '{';
         foreach ($_build_info as $_system => $_build) {
             $_builds .= "'" . escapeForJavaScript($_system) . "': ";
             $_builds .= '{';
             foreach ($_build as $_name => $_value) {
                 $_builds .= "'" . escapeForJavaScript($_name) . "': ";
                 $_builds .= "'" . escapeForJavaScript($_value) . "', ";
             }
             $_builds = substr($_builds, 0, -2);
             $_builds .= '}, ';
         }
         $_builds = substr($_builds, 0, -2);
         $_builds .= '}';
         // [3] builds structure field
         $_language[] = $_builds;
         $_language = '[' . implode(', ', $_language) . ']';
         $_languages[] = $_language;
     }
     $_javascript = "[" . implode(",\n ", $_languages) . "]\n";
     return $_javascript;
 }