var errmsg	=	false;
var showmsg	=	false;


function myAlert(txt)
{
	if (showmsg || errmsg)
	{
		alert(txt);
	}
}

function myError(txt)
{
	errmsg	=	true;
	myAlert(txt);
	errmsg	=	false;
}


var curstill	=	0;		// the currently loaded still
var curtour		=	0;		// the currently loaded tour
var curnum		=	0;		// the current number used to change the border color
var curtype		=	false;	// contain the current type of image in the viewer (tour || img)
var cursym		=	false;	// contins the current symbol (i || t)
var curtitle	=	false;	// title of the currently selected tour or image
var grey		=	'#333';
var white		=	'#fff';
var prevstill	=	-1;
var prevtour	=	-1;

var types		=	Array(); // contains the types
types['i']		=	'img';
types['t']		=	'tour';

var imgs		=	Array();// contains the icons used next to title depending on type
imgs['i']		=	'<img class="icon" src="images/camera.png" alt="" />';
imgs['t']		=	'<img class="icon" src="images/cam.png" alt="" />';

var titles		=	Array();// contains arrays of titles
titles['i']		=	gtitles;
titles['t']		=	ttitles;


// function to change the viewing area
function showPane()
{
	myAlert('showPane() called');
	switch (cursym)
	{
		case 'i':
		document.getElementById('imageview').style.visibility	= 'visible';
		document.getElementById('tourview').style.visibility	= 'hidden';
		break;
		
		case 't':
		document.getElementById('imageview').style.visibility	= 'hidden';
		document.getElementById('tourview').style.visibility	= 'visible';
		break;
	}
}

// function to change the border highlight color of thumbs
function changeBorder(color)
{
	myAlert("changeBorder function called ("+cursym+", "+curtype+", "+curnum+")");
	if (!curtype)
	{
		curtype	=	types['i'];
		cursym	=	'i';
		color	=	white;
		setTitle();
	}
	document.getElementById(curtype+curnum).style.borderColor = color;
	return true;
}



function setTitle()
{
	myAlert("setTitle("+cursym+") function called");
	
	img			=	imgs[cursym];
	curtitle	=	titles[cursym][curnum];
	
	document.getElementById('title').innerHTML = img+' '+curtitle;
}



function setView()
{
	myAlert("setView("+cursym+") function called");
	
	switch (cursym)
	{
		case 'i':
		myAlert('Changing loaded still, curtour = '+curtour+'!');
		
		if (prevstill != curstill)
		{
			document.getElementById('viewer').style.backgroundImage = 'url('+gallery[curstill]+')';
		}
		break;
		
		case 't':
		myAlert('Changing loaded tour!');
		document.ptviewer.newPanoFromList(curtour);
		break;
	}
	
    setTitle();
}



function nextImg()
{ 
	myAlert('nextImg() called');
	changeBorder(grey);
	
	if (cursym	!=	'i')
	{
		cursym	=	'i';
		curtype	=	types[cursym];
	}
	
	prevstill	=	curstill;
	curstill	=	(curstill < (gallery.length-1)) ? curstill+1 : 0;
	curnum		=	curstill;
	changeBorder(white);
	setView();
	
}


function prevImg()
{ 
	myAlert('nextImg() called');
	changeBorder(grey);
	
	if (cursym	!=	'i')
	{
		cursym	=	'i';
		curtype	=	types[cursym];
	}
	
	prevstill	=	curstill;
	curstill	=	(curstill > 0) ? curstill-1 : gallery.length-1;
	curnum		=	curstill;
	changeBorder(white);
	setView();
}


function nextTour()
{ 
	myAlert('nextTour() called');
	changeBorder(grey);
	
	if (cursym	!=	't')
	{
		cursym	=	't';
		curtype	=	types[cursym];
	}
	
	prevtour	=	curtour;
	curtour		=	(curtour < (numtours-1)) ? curtour+1 : 0;
	curnum		=	curtour;
	changeBorder(white);
	setView();
}


function prevTour()
{ 
	myAlert('prevTour() called');
	changeBorder(grey);
	
	if (cursym	!=	't')
	{
		cursym	=	't';
		curtype	=	types[cursym];
	}
	
	prevtour	=	curtour;
	curtour		=	(curtour > 0) ? curtour-1 : numtours-1;
	curnum		=	curtour;
	changeBorder(white);
	setView();
}


function next()
{
	switch (curtype)
	{
		case 'tour':
		nextTour();
		break;
		
		case 'img':
		nextImg();
		break;
	}
}


function prev()
{
	switch (curtype)
	{
		case 'tour':
		prevTour();
		break;
		
		case 'img':
		prevImg();
		break;
	}
}



function clickThumb(x, swtype)
{ 
    myAlert('clickThumb('+x+', '+swtype+') called');
    changeBorder(grey);
    
    curnum	=	x;
    
    if (cursym != swtype)
    {
    	cursym	=	swtype;
    	curtype	=	types[cursym];
    }
    
    showPane();
    
    switch (cursym)
    {
    	case 'i':
    	prevstill	=	curstill;
    	curstill	=	x;
    	break;
    	
    	case 't':
    	prevtour	=	curtour;
    	curtour		=	x;
    	break;
    }
	
	changeBorder(white);
	
	setView();
}
