<?

error_reporting (E_ALL ^ E_NOTICE);


	if(!isset($_SERVER['REQUEST_URI'])) { 
		if(isset($_SERVER['QUERY_STRING'])) { 
			$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']; 
		}
		else {
			$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
		}
	}

	$QS = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

	// remove any .html from the end of the url
	//$QS = str_replace('.html', '', $QS);

	// break the query string off from the rest of the address
	list ($LocationPath, $QSData) = explode('?', $QS, 2);

	// if there's something in the query string
	if ($QSData != '') {

		$QSPieces = explode('/', $QSData);		

	}
	
	
	if (substr($QSData,strlen($QSData)-3,3) == '/bw' || substr($QSData,strlen($QSData)-4,4) == '/bw2') {	
		$CurrentFilename = $QSPieces[count($QSPieces)-2];
	}
	else {
		$CurrentFilename = $QSPieces[count($QSPieces)-1];
	}
	
	//print $CurrentFilename;


	$Directory = './images/uploaded/';

	if (is_dir($Directory)) {
		if ($dh = opendir($Directory)) {
   			while (($file = readdir($dh)) !== false) {
				if ($file != '.' && $file != '..' && $file != 'index.html' && $file != '.htaccess') {
					if (filetype($Directory . $file) == 'dir') {
						//$DirectoriesArray[] = $dir.$file;
						//ReadDirectory($dir . $file.'/');
					}
					else {
						$ImageInfo = @getimagesize($Directory . $file);
						
						if ($ImageInfo[2] == 1 || $ImageInfo[2] == 2 || $ImageInfo[2] == 3) {
							$FilesArray[] = $file;
							$FileSizesArray[] = filesize($Directory . $file);						
						
							$ImageWidths[] = $ImageInfo[0];
							$ImageHeights[] = $ImageInfo[1];
							$FileTypes[] = $ImageInfo[2];
						}
						
					}
				}
			}
			closedir($dh);
		}
	}
	
	
	for ($i = 0; $i < count($FilesArray); $i++) {
		for ($j = 0; $j < count($FilesArray)-1; $j++) {

			if ($FilesArray[$j] > $FilesArray[$j+1]) {
									
				$Swap1 = $FilesArray[$j+1];
				$Swap2 = $FileSizesArray[$j+1];
				$Swap3 = $ImageWidths[$j+1];
				$Swap4 = $ImageHeights[$j+1];
				$Swap5 = $FileTypes[$j+1];
			
				$FilesArray[$j+1] = $FilesArray[$j];
				$FileSizesArray[$j+1] = $FileSizesArray[$j];
				$ImageWidths[$j+1] = $ImageWidths[$j];
				$ImageHeights[$j+1] = $ImageHeights[$j];
				$FileTypes[$j+1] = $FileTypes[$j];
			
				$FilesArray[$j] = $Swap1;
				$FileSizesArray[$j] = $Swap2;
				$ImageWidths[$j] = $Swap3;
				$ImageHeights[$j] = $Swap4;
				$FileTypes[$j] = $Swap5;
									
			}
								
		}

	}
	
	
	print '<html><head>
<title>Existing Images</title>
<meta name="Description" content="">
<meta name="Keywords" content="">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
</head>
<body bgcolor="#eeeeee" text="#000000" link="#000000" vlink="#000000" alink="#000000" leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" rightmargin="0" style="margin:0" onLoad="UpdatePreviewImage();">

	<table width="100%" border="0" cellpadding="5" cellspacing="0"><tr><form name="ExistingImagesForm" onSubmit="return false;">
<td width="100%" valign="middle">

	<div align="center"><table width="210" height="210" border="1" cellpadding="0" cellspacing="0">
    <tr>
      <td align="center" valign="center" bgcolor="#ffffff">';
      
      
      	
	if ($FilesArray[0] != '') {
		print '<img name="ImagePreview" id="ImagePreview" src="./images/uploaded/'.$FilesArray[0].'" width="200" height="200" alt="Image Preview" border="0">';
	}
	else {
		print '<img name="ImagePreview" id="ImagePreview" src="spacer.gif" width="200" height="200" alt="Image Preview" border="0">';
	}
	
	print '</td>
    </tr>
</table><br><select name="ImagesList" onChange="UpdatePreviewImage();">';
	
	for ($i = 0; $i < count($FilesArray); $i++) {
	
		if ($FileSizesArray[$i] > 1024) {
			$FileSizesArray[$i] /= 1024;
			
			if ($FileSizesArray[$i] > 1024) {
				$FileSizesArray[$i] /= 1024;
				
				$FileSizesArray[$i] = number_format($FileSizesArray[$i],1) . ' MB';
			}
			else {
				$FileSizesArray[$i] = number_format($FileSizesArray[$i],1) . ' KB';
			}
		}
		else {
			$FileSizesArray[$i] = $FileSizesArray[$i] . ' bytes';
		}
		
		$ImageWidthsList .= 'ImageWidths[\''.$FilesArray[$i].'\'] = '.$ImageWidths[$i].';';
		$ImageHeightsList .= 'ImageHeights[\''.$FilesArray[$i].'\'] = '.$ImageHeights[$i].';';
	
		if ($FilesArray[$i] == $CurrentFilename) {
			print '<option value="'.$FilesArray[$i].'" selected> '.$FilesArray[$i].' ('.$ImageWidths[$i].'x'.$ImageHeights[$i].', '.$FileSizesArray[$i].')</option>';
		}
		else {
			print '<option value="'.$FilesArray[$i].'"> '.$FilesArray[$i].' ('.$ImageWidths[$i].'x'.$ImageHeights[$i].', '.$FileSizesArray[$i].')</option>';
		}
		
	}
	
	
	if (substr($QSData,strlen($QSData)-3,3) == '/bw') {
		$BWPart = 'BW';
	}
	if (substr($QSData,strlen($QSData)-4,4) == '/bw2') {
		$BWPart = 'BW2';
	}
	
	
	print '</select></div>
	
	
	<script>
	
	var ImageWidths = new Array();
	var ImageHeights = new Array();
	
	'.$ImageWidthsList.'
	'.$ImageHeightsList.'
	
	function ContinueClicked() {
	
		window.opener.ExistingImageSelected'.$BWPart.'(document.forms[\'ExistingImagesForm\'].elements[\'ImagesList\'].value, ImageWidths[document.forms[\'ExistingImagesForm\'].elements[\'ImagesList\'].value], ImageHeights[document.forms[\'ExistingImagesForm\'].elements[\'ImagesList\'].value]);
	
	}
	
	function UpdatePreviewImage() {
	
		document.getElementById(\'ImagePreview\').src = \'./images/uploaded/\'+document.forms[\'ExistingImagesForm\'].elements[\'ImagesList\'].value;
		
		var ImageWidth = ImageWidths[document.forms[\'ExistingImagesForm\'].elements[\'ImagesList\'].value];
		var ImageHeight = ImageHeights[document.forms[\'ExistingImagesForm\'].elements[\'ImagesList\'].value];
		
		if (ImageWidth > 200 || ImageHeight > 200) {

			var WidthRatio = (ImageWidth / 200);
			var HeightRatio = (ImageHeight / 200);
					
			if (WidthRatio >= HeightRatio) {
						
				var NewWidth = Math.round(ImageWidth / WidthRatio);
				var NewHeight = Math.round(ImageHeight / WidthRatio);
						
			}
			else {
						
				var NewWidth = Math.round(ImageWidth / HeightRatio);
				var NewHeight = Math.round(ImageHeight / HeightRatio);
					
			}
			
			document.getElementById(\'ImagePreview\').width = NewWidth;
			document.getElementById(\'ImagePreview\').height = NewHeight;
			
		}
		else {
			document.getElementById(\'ImagePreview\').width = ImageWidth;
			document.getElementById(\'ImagePreview\').height = ImageHeight;
		}
	
	}
	
	</script>
	
	</td>
</tr></table>
<img src="spacer.gif" width="1" height="5" alt="" border="0"><br>
<div align="center"><input type="button" id="ContinueButton" value="Continue" onClick="ContinueClicked();"></div>
</form>

</body></html>';
	

?>
