var slideshow = null;
//var curr_vr = null;

function showWriteUp()
{
	obj = document.getElementById('listWriteUp');
	obj.style.display = 'block';
	
	writeupElem = document.getElementById('divWriteupContent');
	writeupElem.scrollTop = 0;
	document.getElementById('aWriteupArrowUp').style.display = 'none';
	if (writeupElem.scrollHeight > writeupElem.offsetHeight)
		document.getElementById('aWriteupArrowDown').style.display = 'block';
}

function hideWriteUp()
{
	obj = document.getElementById('listWriteUp');
	obj.style.display = 'none';

	if (scrollTimer != null)
		clearInterval(scrollTimer);
}

function showSpecs()
{
	obj = document.getElementById('listSpecs');
	obj.style.display = 'block';
}

function hideSpecs()
{
	obj = document.getElementById('listSpecs');
	obj.style.display = 'none';
}

function showMaps()
{
	if (typeof map_exists == 'undefined')
		return;

	slideshow.stop();

	obj = document.getElementById('listMaps');
	obj.style.display = 'block';
	map_load();
}

function hideMaps()
{
	obj = document.getElementById('listMaps');
	obj.style.display = 'none';

	slideshow.start();
}

function showFloorplans()
{
	slideshow.stop();

	obj = document.getElementById('listFloorplans');
	obj.style.display = 'block';
}

function hideFloorplans()
{
	obj = document.getElementById('listFloorplans');
	obj.style.display = 'none';
	slideshow.start();
}

function showFloorplanPhoto(photo_id)
{
	slideshow.showPhotoByID(photo_id);
}

var curr_floorplan = 0;

function showNextFloorplan()
{
	if (num_floorplans == 0)
		return;

	var fp = document.getElementById('fp_' + curr_floorplan);
	if (fp != null)
		fp.style.display = 'none';

	curr_floorplan++;
	if (curr_floorplan >= num_floorplans)
		curr_floorplan = 0;

	var fp = document.getElementById('fp_' + curr_floorplan);
	if (fp != null)
		fp.style.display = 'block';
}

function showPrevFloorplan()
{
	if (num_floorplans == 0)
		return;

	var fp = document.getElementById('fp_' + curr_floorplan);
	if (fp != null)
		fp.style.display = 'none';

	curr_floorplan--;
	if (curr_floorplan < 0)
		curr_floorplan = num_floorplans - 1;

	var fp = document.getElementById('fp_' + curr_floorplan);
	if (fp != null)
		fp.style.display = 'block';
}

map_visible = true;

MapAerialPrev = MapAerialNext = function()
{
	if (document.getElementById('aerial') == undefined)
		return;

	if (map_visible) {
		document.getElementById('map').style.display = 'none';
		document.getElementById('aerial').style.display = 'block';
		map_visible = false;
	} else {
		document.getElementById('aerial').style.display = 'none';
		document.getElementById('map').style.display = 'block';
		map_visible = true;
	}
}

var scrollTimer = null;
var writeupElem = null;

function scrollTo(direction, endPoint)
{
	if (
		(direction > 0 && (writeupElem.scrollTop >= endPoint || writeupElem.scrollTop >= writeupElem.scrollHeight - writeupElem.offsetHeight)) ||
		(direction < 0 && (writeupElem.scrollTop <= endPoint || writeupElem.scrollTop <= 0))
	)
		
	{
		clearInterval(scrollTimer);
		scrollTimer = null;

		if (writeupElem.scrollTop <= 0) {
			document.getElementById('aWriteupArrowUp').style.display = 'none';
		} else {
			document.getElementById('aWriteupArrowUp').style.display = 'block';
		}
		
		if (writeupElem.scrollTop >= writeupElem.scrollHeight - 300) {
			document.getElementById('aWriteupArrowDown').style.display = 'none';
		} else {
			document.getElementById('aWriteupArrowDown').style.display = 'block';
		}

		return;
	}

	writeupElem.scrollTop += (direction * 3); 
}

function writeupScroll(direction)
{
	if (scrollTimer != null)
		clearInterval(scrollTimer);

	var endPoint = document.getElementById('divWriteupContent').scrollTop + (direction * 100);
	if (endPoint < 0)
		endpoint = 0;

	scrollTimer = setInterval('scrollTo(' + direction + ', ' + endPoint + ')', 20);
}

function hideVRTour()
{
	//if (curr_vr != null)
	//	curr_vr.style.display = 'none';
	//
	//curr_vr = null;

	document.getElementById('vr_tour').style.visibility = 'hidden';
	document.ptviewer.stopAutoPan();

	slideshow.start();
}

function underline(obj, set)
{
	if (set)
		obj.style.textDecoration = 'underline';
	else
		obj.style.textDecoration = 'none';
}




function Slideshow(ssImages, imgId0, imgId1, vrImgId)
{
	this.images = ssImages;
	this.timer = null;
	this.fadeTimer = null;
	this.pauseTimer = null;
	this.currImg = 0;
	this.imgContainers = [document.getElementById(imgId0), document.getElementById(imgId1)];

	this.currOpacity = 100;
	this.vrImg = document.getElementById(vrImgId);
}

Slideshow.prototype.fnChangeOpac = function(opacity, obj)
{
	obj.style.opacity = (opacity / 100);
	obj.style.MozOpacity = (opacity / 100);
	obj.style.KhtmlOpacity = (opacity / 100);
	obj.style.filter = "alpha(opacity=" + opacity + ")";
}

Slideshow.prototype.fade1 = function(obj)
{
	this.currOpacity -= 5;
	this.fnChangeOpac(this.currOpacity, this.imgContainers[0]);
}

Slideshow.prototype.fadeOut = function(obj)
{
	var selfObj = this;

	if (this.fadeTimer != null)	{
		clearInterval(this.fadeTimer);
		this.fadeTimer = null;
	}

	this.fadeTimer = setInterval(function() {
		if (selfObj.currOpacity <= 0) {
			selfObj.initOpac();
			return;
		}
		selfObj.fade1(obj);
		window.status = selfObj.currOpacity;
	}, 30);
}

Slideshow.prototype.initOpac = function()
{
	if (this.fadeTimer != null) {
		clearInterval(this.fadeTimer);
		this.fadeTimer = null;
	}
	this.fnChangeOpac(100, this.imgContainers[0]);
	this.imgContainers[0].src = this.imgContainers[1].src;
	this.currOpacity = 100;
}

Slideshow.prototype.pause = function()
{
	this.stop();

	var selfObj = this;

	if (this.pauseTimer != null) {
		clearTimeout(this.pauseTimer);
		this.pauseTimer = null;
	}

	this.pauseTimer = setTimeout(function() { selfObj.start(); }, 8000);
}

Slideshow.prototype.clickNext = function()
{
	this.pause();
	this.next();
}

Slideshow.prototype.clickPrev = function()
{
	this.pause();
	this.prev();
}

Slideshow.prototype.changePhoto = function()
{
	//if (curr_vr != null)
	//	curr_vr.style.display = 'none';
	//
	//curr_vr = null;

	this.imgContainers[1].src = this.images[this.currImg][1];
	this.fadeOut(this.imgContainers[0]);

	if (this.vrImg != null) {
		if (images[this.currImg][2] != "") {
			var selfObj = this;
			this.vrImg.onclick = function() {
				//document.getElementById('vrTour_' + selfObj.images[selfObj.currImg][2]).style.display = 'block';
				//curr_vr = document.getElementById('vrTour_' + selfObj.images[selfObj.currImg][2]);

				document.ptviewer.stopAutoPan();
				document.getElementById('vr_tour').style.visibility = 'visible';
				document.ptviewer.newPanoFromList(selfObj.images[selfObj.currImg][2]);
				selfObj.stop();
			}
			this.vrImg.style.display = 'block';
		} else {
			this.vrImg.style.display = 'none';
		}
	}
}

Slideshow.prototype.prev = function()
{
	this.currImg--;
	if (this.currImg < 0)
		this.currImg = this.images.length - 1;

	this.changePhoto();
}

Slideshow.prototype.next = function()
{
	this.currImg++;
	if (this.currImg >= this.images.length)
		this.currImg = 0;

	this.changePhoto();
}

Slideshow.prototype.start = function()
{
	var selfObj = this;

	this.imgContainers[0].src = this.imgContainers[1].src = images[this.currImg][1];

	if (this.timer != null) {
		clearInterval(this.timer);
		this.timer = null;
	}

	this.timer = setInterval(function() { selfObj.next() }, 3000);
}

Slideshow.prototype.stop = function()
{
	clearInterval(this.timer);
	this.timer = null;
	clearTimeout(this.pauseTimer);
	this.pauseTimer = null;
	this.initOpac();
}

Slideshow.prototype.showPhotoByID = function(photoID)
{
	slideshow.stop();
	
	for (var i = 0; i < this.images.length; i++) {
		if (this.images[i][0] == photoID) {
			imgIdx = i;
			break;
		}
	}

	if (i == this.images.length)
		return;

	this.currImg = i;
	slideshow.changePhoto();
}

function init()
{
	slideshow = new Slideshow(images, 'imgMainPhoto', 'imgTmpPhoto', 'divVRImageLrg');
	slideshow.start();
}

function toggleStreetview(show)
{
	if (show) {
		document.getElementById('listMaps').style.width = '870px';
		document.getElementById('pano').style.display = 'block';
	} else {
		document.getElementById('pano').style.display = 'none';
		document.getElementById('listMaps').style.width = '370px';
	}
}