<?php
	$title = "smokinggun | SOURCE CODE";
	
	$nav_link = "/demos/index.php";
	$nav_image = "title_source.png";
	
# this should be stripped out when put on the live site
	require("../path_to_lib.php");

#
# tool for managaing catalog categories
# 

	require($path_to_lib."sg_main.php"); 		# main library

# include the html snippets for drawing the interface
	include($path_to_lib."html/public_html.php");

echo $public_header;

echo <<<HTML


<script type="text/javascript">

/*

Copyright (C) 2001 John Weir www.smokinggun.com

This code is free for use.  If possible include the copyright.

*/
	//mouse cordinate positions
	mX1 = mX2 = mY1 = mY2 = 0;
	firstClick = true;
	seconClick = false;
	
	function mouseClick(e)
		{
		if (e) event = e;
		mX = event.clientX;
		mY = event.clientY;

		if (firstClick)
			{
			mX1 = mX;
			mY1 = mY;
			secondClick = true;
			firstClick = false;
			crossHair.style.left = (mX1-10)+"px";
			crossHair.style.top = (mY1-10)+"px";
			crossHair.style.visibility = "visible";
			}
		else if (secondClick)
			{
			mX2 = mX;
			mY2 = mY;
			secondClick = false;
			firstClick = true;
			crossHair.style.visibility = "hidden";
			createImage();
			}
		}
	
	function createImage()
		{
		newImage = document.createElement("img");
		newImage.src = "line.php?x1="+mX1+"&x2="+mX2+"&y1="+mY1+"&y2="+mY2;
		
		m = (mX1-mX2)/(mY1-mY2);
		
		if (mX2 > mX1) newImage.style.left = mX1+"px";
			else newImage.style.left = mX2+"px";
			
		if (mY2 < mY1) newImage.style.top = mY2+"px";
			else newImage.style.top = mY1+"px";
			
		newImage.style.position = "absolute";
		document.getElementById("bodyNode").appendChild(newImage);
		}
	
	
	
	function init()
		{
		// mac ie has a problem with onclick
		document.onmouseup = mouseClick;
		crossHair = document.getElementById("cHair");
		
		// move the crosshair object to be a child of the document
		document.getElementById("bodyNode").appendChild(crossHair);
		windowOnload();
		}
	windowOnload = window.onload;
	window.onload = init;
	
	</script>
	<style type="text/css">
	#cHair {position:absolute; visibility:hidden}
	</style>

<p>
Click two points to draw a line.
</p>
<a href="#" onclick="toggleObjectDisplay('info')">Info</a> 
&nbsp;|&nbsp;
<a href="#" onclick="toggleObjectDisplay('source')">Source</a>
<img id="cHair" src="images/arrow_down.png">
<fieldset  id="info">
<div class="legend">Info</div>
A demonstration of using PHP to generate a graphic based upon a user's mouse clicks.
<br>
<br>
<b>How it works:</b>
<ul>
	<li>
	Capture mouse position for first two clicks.
	</li>
	<li>
	Add position data to a URL which points to a PHP script.
	</li>
	<li>
	PHP Script generates a GIF based upon the position data. This is returned to the browser.
	</li>
	<li>
	Javascript is used to draw the GIF as a new image in the document body.
	</li>
</ul>
<b>Notes</b>
<ul>
	<li>
	Scrolling offsets are not taken into account.
	</li>
</ul>
</fieldset>
<fieldset id="source">
<div class="legend">PHP Source</div>
Below is the PHP source to generate the gif line.  View the HTML source for the javascript code.<br>
<samp style="background:#FFFFFF">

HTML;

show_source("line.php");

echo <<<HTML
</fieldset>

HTML;

echo $public_footer;

?>
