/*--------------------------------------------------
	
	gallery.js - highways agency gallery script
	
	created: 20/08/04 robbie.scourou@tso.co.uk
	
	Version History
	
	1.1			RS			21/07/05 -  Modified to fix bug in IE5 - will now only show
							"Gallery" fucntionality to IE6+ and Firefox
	1.0			RS			Created
	
--------------------------------------------------*/

var currentImage = 0;
var picCount = 0;
var backButton, nextButton;

var valid = 0;

if (document.compatMode) // IE6+ and Firefox only
{
	valid = 1;
}

function postGallery ()
{
	if (valid)
	{
		document.writeln("</div>");
	}
}

function preGallery ()
{
	if (valid)
	{
		document.writeln("<div id=\"loading\">Please wait while the gallery loads...</div>");
		document.writeln("<div id=\"masterGalleryDiv\">");
		document.getElementById("masterGalleryDiv").style.display = "none";
	}
}

function initScript ()
{

	if (valid)
	{
		// get count of images on page based on imageDiv_ <div> tags //
		var imageExp = new RegExp("imageDiv_", "g");
		var divContents = document.getElementById("masterGalleryDiv").innerHTML;
		while(imageExp.exec(divContents))
		{
			picCount++;
		}
	
	       // this bit makes all the divs disappear on load except the first one // 
		
		
	    for (var i=1; i<picCount; i++)
		{
	               document.getElementById("imageDiv_" + i).style.display = "none";
		}
		
		// get rid of loading text and make gallery appear //
		document.getElementById("loading").style.display = "none";
		document.getElementById("masterGalleryDiv").style.display = "";
		// generate the back and forward links for all divs! //
	       insertButtons();
	}
}

function changeImage(imageNo)
{
	// swap divs around
	document.getElementById("imageDiv_" + currentImage).style.display = "none";
	document.getElementById("imageDiv_" + imageNo).style.display = "";
	// update currentImage to reflect new image
	currentImage = imageNo;
}

function insertButtons ()
{
    for (var i = 0; i < picCount; i++)
        {
			// defines regex for back and next buttons //
	        var imageNumber = new RegExp("image_no");
	
			// generic html for buttons //
			backButton = "<a href=\"javascript:changeImage(image_no)\"><img src=\"/images/gallery_prev.gif\" align=\"top\" width=\"120\" height=\"22\" alt=\"previous\" border=\"0\" vspace=\"5\"></a>";
			nextButton = "<a href=\"javascript:changeImage(image_no)\"><img src=\"/images/gallery_next.gif\" align=\"top\" width=\"120\" height=\"22\" alt=\"next\" border=\"0\" vspace=\"5\"></a>";

			// doc nav style buttons // 
			//backButton = "<a href=\"javascript:changeImage(image_no)\"><img src=\"/images/docnav_previous.gif\" width=\"89\" height=\"23\" alt=\"previous\" border=\"0\"></a>";
			//nextButton = "<a href=\"javascript:changeImage(image_no)\"><img src=\"/images/docnav_next.gif\" width=\"69\" height=\"23\" alt=\"next\" border=\"0\"></a>";
			
			// get the correct image numbers //
            if (i == 0)
			{
                backButton = backButton.replace(imageNumber,(picCount - 1));
                nextButton = nextButton.replace(imageNumber,(i+1));			
			}
		
            else if (i > 0 && i < (picCount -1))
            {
                backButton = backButton.replace(imageNumber,(i-1));
                nextButton = nextButton.replace(imageNumber,(i+1));
            }
            else
            {
                backButton = backButton.replace(imageNumber,(i-1));
                nextButton = nextButton.replace(imageNumber,((picCount - i) - 1));
            }
			
			// generate the buttons //
            document.getElementById("prev_" + i).innerHTML = (backButton);
            document.getElementById("next_" + i).innerHTML = (nextButton);				
        }
}
