// 3D Photos - Dependent on transitions.js
var totalPhotos, product
var current3d = 1
var stopandgoButt
var active3d = false
var nextTime, hideTimer

function setup_photos3d(id,total) {
	if(total>1) {
		fader('loading-photos',0)
		loadingPhotos.style.top="570px"
		totalPhotos = total
		product = id
		stopandgoButt = document.getElementById('stopandgoIMG')
		stopandgoButt.onclick = start_photos3d
		document.getElementById('stopandgo3d').style.top = "580px";
	}
}
function start_photos3d() {
	fade('loading-photos',0,25,true)
	stopandgoButt.onclick = null
	active3d = true
	
	//fader('photo'+current3d,0)
	
	fader('buttons-3d',0)
	stopandgoIMG()
	fade('stopandgo3d',100,20,false)
	document.getElementById('buttons-3d').style.top = "545px"
	
	get_photo3d()
	return false;
}
function get_photo3d() {
	if(current3d<totalPhotos) {
		var httpObject=get_httpObject()
		if (httpObject==null) return

		var url=sunday+'/wp-content/themes/sundaybikesV3/includes/get_photo3d.php?productID='+product+'&current3d='+(current3d+1)
		
		httpObject.onreadystatechange= function() { photo3dURL(this) } 
		httpObject.open("GET",url,true)
		httpObject.send(null)
	} else {
		setup_3dButtons()
		window.setTimeout("nextPhoto3d()",1500)
	}
}
function photo3dURL(httpObject) { 
	if (httpObject.readyState==4 || httpObject.readyState=="complete") {
		current3d++
		var photo = new Image()
		photo.src = httpObject.responseText
		photo.id = "photo"+current3d
		photo.onload = function(){ photoLoaded(this) }
	}
}
function photoLoaded(img) {
	document.getElementById("photos-3d").appendChild(img)
	fade('photo'+current3d,0,20,true)
	fader('photo'+(current3d-1),0)
	if(current3d != 1) hideTimer = window.setTimeout("fader('photo"+(current3d-1)+"',0)",150)
	else hideTimer = window.setTimeout("fader('photo"+totalPhotos+"',0)",150)	
	get_photo3d()
}
function nextPhoto3d() {
	var previous3d = current3d
	if(current3d < totalPhotos) current3d++
	else current3d = 1

	document.getElementById("photo"+current3d).style.zIndex = 69
	document.getElementById("photo"+previous3d).style.zIndex = 68
	fader("photo"+previous3d,0)
	fade("photo"+current3d,0,20,true)
	hideTimer = window.setTimeout("hidePreviousPhoto('photo"+previous3d+"')",150)
	if(active3d) nextTimer = window.setTimeout("nextPhoto3d()",1500)
}
function hidePreviousPhoto(id) {
	document.getElementById(id).style.zIndex = 1
	fader(id,0)
}
function setup_3dButtons() {
	fade('buttons-3d',0,25,true)
	fade('stopandgo3d',0,25,true)
	with(document) {
		getElementById('next3d').onclick = next3dClick
		getElementById('previous3d').onclick = previous3dClick
		getElementById('stopandgo3d').onclick = stopandgo3dClick
	}
	fade('loading-photos',100,25,false)
}
function stopandgo3dClick() {
	if(active3d) {
		active3d = false
		clearTimeout(hideTimer)
		clearTimeout(nextTimer)
	} else {
		active3d = true
		nextPhoto3d()
	}
	stopandgoIMG()
	return false;
}
function stopandgoIMG() {
	if(active3d) stopandgoButt.src = sunday+"/wp-content/themes/sundaybikesV3/images/3d-stop.gif"
	else stopandgoButt.src = sunday+"/wp-content/themes/sundaybikesV3/images/3d-play.gif"
}
function next3dClick() {
	if(active3d) stopandgo3dClick()
	nextPhoto3d()
	return false;
}
function previous3dClick() {
	if(active3d) {
		stopandgo3dClick()
		var previous3d = current3d-1
		if(previous3d-1<1) previous3d=totalPhotos
		fade("photo"+current3d,100,25,false)
		fader("photo"+previous3d,100)
		
		current3d--
		return false;

	} else  {
		with(document) {
			var previous3d = current3d

			current3d--
			if(current3d<1)current3d=totalPhotos

			getElementById("photo"+previous3d).style.zIndex = 69
			getElementById("photo"+current3d).style.zIndex = 68
			fade("photo"+current3d,0,20,true)
			fader("photo"+previous3d,0)
	
			hideTimer = window.setTimeout("hidePreviousPhoto('photo"+previous3d+"')",150)
		}
		return false;
	}
}
