// Variables to control various bits

// Sub Nav
subNavOpacity = 0;

// Slideshow
numSlideShowImages = 7;
currSlide = 1;
currXPos = 0;
newSlide = 1;
newXPos = 0;
speed = 10; // the higher the slower!
autoPlayInterval = 3000; // in ms, to start, increases to 3000ms

// Gallery
galleryXPos = 0;
maxXPos = 500; // it's actually -500, just positive so it's easier to work out whats going on
slideSpeed = 10;
lastImageNum = 40;
overImage = false;
galleryImageOpacity = 0;
	
function doSubNav(elem,vis) {
	if (vis == "start") {
		elem.style.visibility = "visible";
		doingSubNav = true;
	}
	if (vis == "start" || vis == "over") {
		if ("undefined" != typeof invisibleSubNav) {clearTimeout(invisibleSubNav);};
		if ("undefined" != typeof clearSubNav) {clearTimeout(clearSubNav);};
		if ("undefined" != typeof fadeInOut) {clearInterval(fadeInOut);};

		if (!Modernizr.csstransitions && subNavOpacity < 100) {
			fadeInOut = setInterval('fadeSubNavInOut(\'in\')',20);
		} else {
			elem.style.opacity = "1";
		}
	} else {
		invisibleSubNav=setTimeout('document.getElementById(\'subNavContainer\').style.visibility = "hidden";',200);
		doingSubNav = false;
		if (!Modernizr.csstransitions && subNavOpacity > 0) {
			fadeInOut = setInterval('fadeSubNavInOut(\'out\')',20);
		} else {
			elem.style.opacity = "0";
		}
	}
}

function fadeSubNavInOut(dir) {
	elem = document.getElementById('subNavContainer');
	if (dir == "in") {subNavOpacity+=5} else {subNavOpacity-=5};
	if (dir == "in" && subNavOpacity>=100) {subNavOpacity=100;window.clearInterval(fadeInOut)};
	if (dir == "out" && subNavOpacity<=0) {subNavOpacity=0;window.clearInterval(fadeInOut)};
	elem.style.filter = "alpha(opacity="+subNavOpacity+")";
}

function initSlideshow() {
	//slideshow(Math.floor(Math.random()*(numSlideShowImages-1)+1)); // - previously chose a random start
	slideshow(1);
	document.getElementById('slideshow').style.width = (numSlideShowImages*750) + "px";
}


function startSlideshow() {
	if (autoPlayInterval==1000) {autoPlayInterval=3000};
	nextSlide = currSlide+1;
	if (nextSlide>numSlideShowImages) {nextSlide=1};
	autoSlide = setTimeout('slideshow(nextSlide)',1000);
}

function startStopAutoSlideshow() {
	if ("undefined" != typeof autoSlideShow) {clearInterval(autoSlideShow)};
	if ("undefined" != typeof autoSlide) {clearTimeout(autoSlide)};
	autoPlayInterval = 3000; // in ms, to start, increases to 3000ms
	startAgain = setTimeout('autoSlideShow = setInterval(\'startSlideshow()\',autoPlayInterval)',5000);
}

function slideshow(goImage) {
	newSlide = goImage;
	newXPos = -((goImage-1)*750);
	//if (Modernizr.csstransitions) {
	//	document.getElementById('slideshow').style.left = newXPos +'px';
	//} else {
		if("undefined" != typeof doSlide) {clearInterval(doSlide)};
		doSlide = setInterval('slideStep()',20);
	//}
	for (i=1;i<=numSlideShowImages;i++) {
		document.getElementById('slideshowNav'+i).className = "navBlock whiteBlock";
	}
	document.getElementById('slideshowNav'+goImage).className = "navBlock orangeBlock";
}

function slideStep() {
	if ((newSlide>currSlide&&currXPos<=(newXPos+1))||(newSlide<currSlide&&currXPos>=(newXPos-1))||newSlide==currSlide) {
		clearInterval(doSlide);
		document.getElementById('slideshow').style.left = newXPos +'px';
		currSlide=newSlide;
	} else {
		if (newSlide>currSlide) {
			currXPos+= (newXPos-currXPos)/speed;
		} else {
			currXPos+= (newXPos-currXPos)/speed;
		}
		document.getElementById('slideshow').style.left = parseInt(currXPos) +'px';
	}
}

function galleryHover(elemID,vis) {
	elem = document.getElementById(elemID);
	vis=="show" ? elem.style.opacity = "1" : elem.style.opacity = "0";
	vis=="show" ? elem.style.filter = "alpha(opacity=100)" : elem.style.filter = "alpha(opacity=0)";
}

function getMousePos(obj,e) {
	// Find x&y of current object
	var currLeft = currTop = 0;
	if (obj.offsetParent) {
		do {
		currLeft += obj.offsetLeft;
		currTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	// Then find mouse position according to that object
	if (document.layers||document.getElementById&&!document.all) {
		// NS
		xPos =  (e.pageX-currLeft);
		yPos =  (e.pageY-currTop);
	} else if (document.all) { 
		// IE
		xPos =  (event.clientX-currLeft);
		yPos =  (event.clientY-currTop);
	}
}

function slideGallery() {
	elem = document.getElementById('gallery');
	if (xPos < 200||xPos>550) {
		if (xPos<200 && galleryXPos<0) {
			if (galleryXPos>-1) {galleryXPos = 0}
			galleryXPos += ((0-galleryXPos)/slideSpeed)*((0-xPos+200)/200);
		}
		if (xPos>550 && galleryXPos>-maxXPos) {
			if (galleryXPos<-(maxXPos-1)) {galleryXPos = -maxXPos}
			galleryXPos -= ((maxXPos+galleryXPos)/slideSpeed)*((xPos-750+200)/200);
		}
	}
	elem.style.left = galleryXPos + "px";

	// Set hover/out images
	imageNum = parseInt(((xPos-galleryXPos)/125)+1);
	if (yPos>96) {imageNum +=10};
	if (yPos>192) {imageNum +=10};
	if (yPos>288) {imageNum +=10};
	if (imageNum != lastImageNum) {
		galleryHover('pic'+lastImageNum,'hide');
		galleryHover('pic'+imageNum,'show');
		lastImageNum = imageNum;
	}
}

function showGalleryImage(imgNum) {
	if (!imgNum) {
		imageNum = parseInt(((xPos-galleryXPos)/125)+1);
	} else {
		imageNum = imgNum;
		if (imageNum!=10&&imageNum!=20&&imageNum!=30&&imageNum!=40) {
			imageNum = parseInt(imgNum-parseInt(parseInt(imgNum/10)*10))
		} else {
			imageNum = 10;
		};
	}
	// Now imageNum is a no between 1-10
	galleryImage = imageNum;
	picType = "plants";
	if (yPos>96||imgNum>10) {picType = "indoor-plants"; galleryImage +=10};
	if (yPos>192||imgNum>20) {picType = "outdoor-plants"; galleryImage +=10};
	if (yPos>288||imgNum>30) {picType = "replica-plants"; galleryImage +=10};
	largePic = picType+"-"+imageNum+".jpg";	
	document.getElementById('floatingContainer').style.display = "table";
	document.getElementById('popupBox').style.display = "inline";
	document.getElementById('popupBox').innerHTML = '<img src="gallery/large/'+largePic+'" class="galleryImage" id="galleryImg"><div class="popupControls" id="pControls"><img src="images/gallery-prev.png" class="prev" id="galleryPrev" onClick="galleryDoPrev()" onMouseOver="overImage=true" onMouseOut="overImage=false"><img src="images/gallery-next.png" class="next" id="galleryNext" onClick="galleryDoNext()" onMouseOver="overImage=true" onMouseOut="overImage=false"></div><img src="images/close-button.png" id="closeButt" class="closeButton" onClick="hideGalleryImage()">';
	document.getElementById('galleryImg').style.opacity = "0";
	document.getElementById('galleryImg').filter = "alpha(opacity=0)";
	document.getElementById('galleryPrev').style.visibility = document.getElementById('galleryNext').style.visibility = document.getElementById('closeButt').style.visibility = "hidden";
	document.getElementById('galleryImg').onload = function() {
		imgW = document.getElementById('galleryImg').width;
		document.getElementById('pControls').style.width = (imgW+1) + "px";
		document.getElementById('pControls').style.marginLeft =  -Math.ceil(imgW/2) + "px";
		if (galleryImage>1) {document.getElementById('galleryPrev').style.visibility = "visible"}
		if (galleryImage<40) {document.getElementById('galleryNext').style.visibility = "visible"}
		document.getElementById('closeButt').style.visibility = "visible";
		galleryImageFadeIn = setInterval('imageFadeIn()',20);
	};
}

function hideGalleryImage() {
	document.getElementById('floatingContainer').style.display = "none";
	document.getElementById('popupBox').style.display = "none";
}

function galleryDoPrev() {
	if (galleryImage>1) {
		galleryImage--;
		showGalleryImage(galleryImage);
	}
}

function galleryDoNext() {
	if (galleryImage<40) {
		galleryImage++;
		showGalleryImage(galleryImage);
	}
}

function imageFadeIn() {
	if (galleryImageOpacity<100) {
		galleryImageOpacity += 10;
		document.getElementById('galleryImg').style.opacity = galleryImageOpacity/100;
		document.getElementById('galleryImg').style.filter = "alpha(opacity="+galleryImageOpacity+")";
	} else {
		galleryImageOpacity = 0;
		clearInterval(galleryImageFadeIn);
	}
}

function inputHiLo(elem,defaultValue,action) {
	if (action=="focus" && elem.value==defaultValue) {elem.value='';elem.style.color='#000000'}
	if (action=="blur" && elem.value=="") {elem.value=defaultValue;elem.style.color='#888888'}
}

function validateContactForm() {
	if (isNaN(document.getElementById('userTelephone').value.replace(/ /g,""))) {
		alert('Sorry, that doesn\'t appear to be a valid telephone no.\n\nPlease use only digits and try again.');
		return false;	
	} else if (document.getElementById('userTelephone').value.replace(/ /g,"").length<6) {
		alert('Sorry, your phone number appears to be less than 6 digits.\n\nPlease try again.');
		return false;	
	} else {
		return true;
	};
}
