// TODO:
// 1: documentation (header, function descriptions etc.)
// 2: mailCreate and mailReset
// 3: revise galleryOpen to use image object?

// constants
var PREFIX_THUMB = 'tn_';
var PREFIX_IMAGE = 'img_';


function sj_galleryOpen(pID)
{
	// clear existing
	parent.gImages = new Array();

	// declare
	var vImage = new Image();
	var vImageSrc = new String();
	
	// scan the doc for thumbnails
	for(n = 0; n < document.images.length; n++)
	{
		// get cur image & source
		vImage = document.images[n]; 
		vImageSrc = vImage.src;
		
		// if id is the same as the caller set index so we know which to display
		if(vImage.id == pID)
		{
			parent.gIndex = n;
		}	
		
		// if src includes thumbnail prefix add it to the array, otherwise ignore
		if(vImageSrc.search(PREFIX_THUMB) > -1)
		{		
			parent.gImages.push(vImageSrc.replace(PREFIX_THUMB, PREFIX_IMAGE));	
		}				
	}
	
	// if rez is > 800x600 we can use the normal version, else the low rez version
	var vGallery = (screen.height > 600) ? 'sj_gallery.htm' : 'sj_gallery_low.htm';

	// open gallery in new window
	// TODO: make width, height and status variable for 800x600
	// TODO: ascertain how to turn of firefox status bar 
	// TODO: make the low rez gallery a few pixels smaller
	window.open(vGallery,'gallery','width=738, height=614, status=no');
}


function sj_galleryInit()
{
	// copy down data
	gImages	= opener.parent.gImages;
	gIndex = opener.parent.gIndex;

	// display current image
	sj_galleryDisplay();
}


function sj_galleryDisplay()
{
	document.getElementById('oDisplay').innerHTML = '<img src="' + gImages[gIndex] + '">';
}


function sj_galleryClose()
{
	window.close();
}


function sj_galleryNext()
{
	// increment
	gIndex++;

	// check upper bound
	if(gIndex == gImages.length)
	{
		gIndex = 0; 
	}

	// display
	sj_galleryDisplay();
}


function sj_galleryPrev()
{
	// check lower bound
	if(gIndex == 0)
	{
		gIndex = gImages.length;
	}
	
	// decrement
	gIndex--;

	// display
	sj_galleryDisplay();
}


function sj_mailOpen(pSource)
{
	// add domain, page title/topic and move cursor to body
	pSource.href += "@sunjammer.co.uk";
	pSource.href += "?subject=[" + document.title + "] " + pSource.innerHTML;
	pSource.href += "&body=";
}

function sj_mailUndo(pSource)
{
	// discard everything from the @
	var i = pSource.href.search('@');
	
	if(i > -1)
		pSource.href = pSource.href.slice(0 , i)
}

function sj_normalInit()
{
	// update title
	// BUG: this only works for "www.kasatka.co.uk/sunjammer/"
	parent.document.title = document.title

	// attach the "last updated" date
	document.getElementById('oDate').innerHTML = 'Updated: ' + document.lastModified;

	document.getElementById('oSection').visility='hidden';
	sj_buildBreadcrumbs()
	sj_buildSectionLinks()
}


function sj_printOpen()
{
	window.open('sj_print.htm','print','width=738, height=614');
}	


function sj_printInit() 
{
	// set caption
	document.title = opener.parent.oMain.document.title;

	// get contents of oContents in document in main frame
	document.getElementById('oContent').innerHTML = opener.parent.oMain.document.getElementById('oContent').innerHTML;
	
	var vImage = new Image();
	var vImageSrc = new String();
	
	// scan the doc for thumbnails
	for(n = 0; n < document.images.length; n++)
	{
		// "remove" thumbnails
		var img = document.images[n]; 
		if(img.src.search(PREFIX_THUMB) > -1)
		{
			img.height = 0;
			img.width = 0;
		}		
	}	
	
	// print resultant document
	print(document); 
	window.close();	
}

function sj_buildBreadcrumbs()
{
	var vHome = '<a href="http://www.sunjammer.co.uk/" target="_top">Home</a>';
	var vCrumbs = '';
	
	try	
	{ 
		// walk up the menu structure to build the breadcrumbs
		vCrumbs = '|' + sj_menuToUrl(top.gMenu);
		var vMenu = oCMenu.m[top.gMenu.parent];
		while(vMenu.name.length > 0)
		{
			vCrumbs = '|' + sj_menuToUrl(vMenu) + vCrumbs;
			vMenu = oCMenu.m[vMenu.parent];
		}
	} 
	catch(e) 
	{
		// the menu undefined parent 
	}

	vCrumbs = (vCrumbs.length == 0) ? vHome : vHome + vCrumbs;
	document.getElementById('oCrumbs').innerHTML = vCrumbs	
}


function sj_buildSectionLinks()
{
	var vTopics = '';

  // default to hidden to handle issues with refresh
	document.getElementById('oSection').visility='hidden';
	
	// if the parent menu exists, get it and iterate through its submenus 
	// converting each in turn into a link and concatenate them into a list
	try	
	{ 
		if(top.gMenu.parent)
		{
			// NOTE: to avoid the refresh issues the table is hidden by default
			//  so we must show it
			document.getElementById('oSection').visility='visible';
			
			vTopics = 'In this section:<ul>';
			
			var vParent = oCMenu.m[top.gMenu.parent];
			for(i = 0; i < vParent.subs.length; i++)
			{
				vTopics += '<li>' + sj_menuToUrl(oCMenu.m[vParent.subs[i]]);
			}
			vTopics += '</ul>'
		}	
	} 
	catch(e) 
	{
		// the menu has undefined parents
	}

	document.getElementById('oTopics').innerHTML = vTopics	
}


function sj_menuToUrl(pMenu)
{
	/* 
	// retain this in case we want to go fully linked
	//
	if(pMenu.img1)
	{
		// menus with icons have a tag containing the breadcrumb info
		var vSplit[] = pMenu.tag.split('|')
		return '<a href="../' + pMenu.tag.split('|')[1] + '">' + pMenu.tag.split('|')[0] + '</a>';
	}

	return '<a href="../' + pMenu.lnk + '">' + pMenu.txt + '</a>';
	*/
	if(pMenu.lnk)
	{
		// active link
		//return '<a href="../' + pMenu.lnk + '">' + pMenu.txt + '</a>';
		return '<a href="#" onClick="' + oCMenu.name + '.onclck(\'' + pMenu.name + '\'); return false">'  + pMenu.txt + '</a>';
	}
	else if(pMenu.txt && !pMenu.img1)
	{
		// text but no link
		// NOTE: the img1 path is copied in to txt causing a false positive so must be excepted
		return pMenu.txt;
	}
	else
	{
		// no txt and no link
		return pMenu.tag 
	}
}

