/*
Animate the zoom or opacity of images for a photo page.

Required tag elements in the webpage

id = bigPicture : As you would expect 
id = status : required even if working but will not run without it

*/

/*
First the google anaylitcs
*/

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-17083308-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


/*
Now the dc script
*/


var cnt = 1 ;
var txt = "" ;
var t ;
var x = 0;
var y = 0;
var elem ;
var width = 200;
var lastLoadedImage = 0;
var totalWidth = 0 ;
var lastImg = "false" ;
var init = false ;
var goodImages = 0;


if(window.captureEvents){
	window.captureEvents(Event.MOUSEMOVE) ;
	window.onmousemove = moving ;
}else{
		document.onmousemove = getMousePos;
	try{
		document.captureEvents(Event.MOUSEMOVE) ;
		document.onmousemove = moving ;}
	catch(err){fired = err.toString()}
}

function getMousePos(){
      x = event.clientX + document.body.scrollLeft;
      y = event.clientY + document.body.scrollTop;
}

function moving(e){
	x = e.pageX ;
	y = e.pageY ;
	fired = "Yes" ;	
}

function start(){
	t = setTimeout("run()", 10) ;
	if(lastLoadedImage == 0){
		loadFilm() ;	
		//alert("loading") ;
	}
	if(!init){
		goodImages = numberOfUsableImages() ;
	}
	
	init = true ;
}

function numberOfUsableImages(){
	/*
		Search through all images
		on this page. 
		If an image is OK to use (i.e. not part of the design)
		then include this in the count.
	*/
	
	var totalImages ;
	var usableImages = 0;
	
	totalImages = document.images.length ;
	
	for(c = 0; c < totalImages; c++){
		if(checkImage(document.images[c].src)){
			usableImages ++ ;
		}
	}
	
	return usableImages ;
	
}

function run(){
	elem = document.elementFromPoint(x, y) ;
	document.getElementById("status").innerHTML = txt ;
	cnt++ ;
	drawBigPicture() ;
	start() ;
}

function loadFilm(){
	
	var imageSource ;
	
	//Load the images into the film. If left or right clicked adjust the images accordingly
	
	//Test code just to load 
	
	//Figure out where to start
	
	//Load until full (at first just load 5 later by calculting how many will fit)
		//Check images are OK to laod (i.e. not part of page design elements.
	clearFilm() ;
	
	for(c=0; c < 5; c++){
		imageSource = document.images[lastLoadedImage].src ;
		if(checkImage(imageSource) && lastLoadedImage < goodImages){
			//This is a good image AND we are still below the number of goodImages
			totalWidth += (120 / document.images[lastLoadedImage].height) * document.images[lastLoadedImage].width ;
			if(totalWidth < 815){
				//If it fits then draw it. Otherwise skip it. Don't up lastLoadedImage as this will be needed next time.
				document.getElementById("film").innerHTML +=  "<img src=\"" + imageSource + "\" />" ;
				lastLoadedImage += 1 ;
			}
		}else{
			//Either the image is no good or we are beyond the last goodImage
			if(lastLoadedImage >= goodImages){
				//If we are past the last good image then set the lastLoadedImage to number of goodImages
				lastLoadedImage = goodImages ;
			}
			else{
				//Or if we are not past this must be a bad 'un
				//So move along (skip it. Reduce c so we continue until we have tried at least five times with a good image
				lastloadedImage += 1 ;
				c-- ;
			}
		}
	}
}

function rightClicked(){
		loadFilm() ;		
}

function leftClicked(){
	if(lastLoadedImage < 10){
		lastLoadedImage = 0 ;
		loadFilm() ;
	}else{
		lastLoadedImage -= 10 ;
		loadFilm() ;
	}
}

function clearFilm(){
	document.getElementById("film").innerHTML = "" ;
	//lastLoadedImage = 0 ;
	totalWidth = 0 ;
}

function drawBigPicture(){
	var html ;
	var startHTML ;
	var endHTML ;	
	var imgSrc = elem.src ;
	//Check src is a defined....
	if(typeof imgSrc != 'undefined' && checkImage(imgSrc)){
		lastImg = imgSrc ;
		startHTML = "<img src='" ;
		endHTML = "' width='" + width +  "px'/>" ;
		html = startHTML + imgSrc + endHTML ;
		document.getElementById("bigPicture").innerHTML = html ;
		if(width < 600){
			width += 300 ;
		}
	}else{
		if(lastImg == "false"){
			document.getElementById("bigPicture").innerHTML = "<br><br><br><p>Please hover the mouse over an image to zoom in</p>" ;
		}else{
			//Just leave the image in place.
		}
		width = 0 ;
	}
}

function checkImage(src){
		var fileStart ;
		var file = new String(src) ;
		var fileName ;
		var notFoundAlready = true ;
		var fileOK = true ;
	//Array of objects not to be fussed about...
		var ignoreFiles = new Array() ;
		ignoreFiles[0] = "Background.png" ;
		ignoreFiles[1] = "background.png" ;
		ignoreFiles[2] = "Ulley%20Logo%20&%20Banner.png" ;
		ignoreFiles[3] = "rightArrow.png" ;
		ignoreFiles[4] = "leftArrow.png" ;
		ignoreFiles[5] = "menuleft.png" ;
		ignoreFiles[6] = "menuright.png" ;
		ignoreFiles[7] = "Ulley Logo &amp; Banner - longer.jpg" ;
		ignoreFiles[8] = "Ulley%20Logo%20&%20Banner%20-%20longer.jpg" ;
		ignoreFiles[9] = "Background%20Wavey.jpg" ;
		
	//Check src is OK.
		//Make sure the file is not a page image
		//Logo, background
		
		//Find last '/' file name is to end.
		fileStart = file.lastIndexOf("/") ;
		fileName = file.substr(fileStart + 1, (file.length - fileStart)) ;
		//Now search through ignoreFiles to see if it is to be ignored.
		for(var c = 0; c < ignoreFiles.length; c++){
			if(fileName == ignoreFiles[c] && notFoundAlready){
					fileOK = false ;
					notFoundAlready = false ;
			}
		}
		
		return fileOK ;
}
