var gallery_selection_CurrentPage = 0;
var gallery_selection_PageCount = 0;
var gallery_selection_PageLoaded = Array();
var gallery_selection_Headers = Array();

var gallery_content_CurrentPage = 0;
var gallery_content_PageCount = 0;
var gallery_content_PageLoaded = Array();
var gallery_content_Headers = Array();

var gallery_photo_CurrentPage = 0;
var gallery_photo_CurrentPhoto = '';
var gallery_photo_PageCount = 0;
var gallery_photo_PageNumbers = Array();
var gallery_photo_PageLoaded = Array();
var gallery_photo_Headers = Array();

var currentGallery = '';
var currentMode = '';

var ajax_enabled = browserSupportsAjax();


// Detects if the browser supports Ajax 
function browserSupportsAjax()
{
	if (typeof XMLHttpRequest == "undefined" && typeof ActiveXObject == "undefined" && window.createRequest == "undefined")
		return false;
	return true;
}
    
    
function generate_gallerypages(type, page, pagecount, include_currentpage)
{	
	eval('gallery_'+type+'_PageCount = '+pagecount);

	var html = '';
	for(var ctr=1; ctr<=pagecount; ctr++)
	{
		if ((ctr==page) && !include_currentpage) continue;
		var pos = (ctr<page) ? -710 : (ctr > page) ? 710 : 0;
		html += "<DIV ID='gallery"+type+"page"+ctr+"' CLASS='gallery"+type+"page' STYLE='left:"+pos+"px;'></DIV>";
	};		
	return html;
};

function gotoPage(mode, page_or_photo)
{
	if(!ajax_enabled) return true;
	return gotoPageEx(mode, page_or_photo, true);
}

function gotoPageEx(mode, page_or_photo, add_to_history)
{
	var pageNumber = (mode == 'photo') ? gallery_photo_PageNumbers[page_or_photo] : page_or_photo;
	var pageLoaded;
	eval('pageLoaded = gallery_'+mode+'_PageLoaded['+pageNumber+'];');

	if (!pageLoaded)
	{
		xajax_doLoadPage(mode, currentGallery, page_or_photo, add_to_history);
		return false;
	};
	
	var instantTransition = mode != currentMode;
	var oldMode = currentMode;
	currentMode = mode;

	eval('gallery_'+mode+'_CurrentPage = '+pageNumber);
	if(mode=='photo') gallery_photo_CurrentPhoto = page_or_photo;

	var afterScroll = 'applyHeader();';
	if (mode == 'photo')
	{
		toggleOverflows('hidden');
		afterScroll += "toggleOverflows('auto');";
	};

	if (add_to_history) appendHistory();
	
	animateScroll(mode, instantTransition, afterScroll);

	if (instantTransition)
	{
		document.getElementById('gallery'+oldMode).style.display = 'none';
		document.getElementById('gallery'+currentMode).style.display = 'block';
	};

	return false;
};


function applyHeader()
{
	var pageNumber;
	eval('pageNumber = gallery_'+currentMode+'_CurrentPage;');

	var header;
	eval('header = gallery_'+currentMode+'_Headers['+pageNumber+'];');

	document.getElementById('galleryheader').innerHTML = header;
}


function openGallery(gallery)
{
	if(!ajax_enabled) return true;

	if (gallery==currentGallery) gotoPage('content', 1);
	else xajax_doLoadGallery(gallery, 'content', 1, true);

	return false;
};


function initGallery(gallery, content_pagecount, photo_pagecount, first_photo, goto_mode, goto_page, add_to_history)
{
	gallery_content_CurrentPage = 1;
	gallery_content_PageCount = content_pagecount;
	gallery_content_PageLoaded = Array();
	gallery_content_Headers = Array();

	gallery_photo_CurrentPage = 1;
	gallery_photo_CurrentPhoto = first_photo;
	gallery_photo_PageCount = photo_pagecount;
	gallery_photo_PageNumbers = Array();
	gallery_photo_PageLoaded = Array();
	gallery_photo_Headers = Array();

	document.getElementById('gallerycontent').innerHTML = generate_gallerypages('content', 1, content_pagecount, true);
	document.getElementById('galleryphoto').innerHTML = generate_gallerypages('photo', 1, photo_pagecount, true);

	currentGallery = gallery;
	gotoPageEx(goto_mode, goto_page, add_to_history);
};


function extractpos(pos)
{
	if(pos == '') return 0;
	return parseInt(pos.split('p')[0]);
};


function animateScroll(mode, instant, oncomplete)
{
	var currentPage;
	eval('currentPage = gallery_'+mode+'_CurrentPage;');

	var pageCount;
	eval('pageCount = gallery_'+mode+'_PageCount;');

	if (currentPage == 0) return false;
	
	
	var repeatScroll = false;
	for(var ctr=1; ctr<=pageCount; ctr++)
	{
		var goal = 0;
		if (ctr < currentPage) goal = -710;
		else if (ctr > currentPage) goal = 710;

		var div = document.getElementById('gallery'+mode+'page'+ctr);
		var pos = extractpos(div.style.left);

		if (pos == goal) continue;

		var move = goal-pos;
		if (!instant)
		{
			if (move > 0) move = Math.min(move, 40);
			if (move < 0) move = Math.max(move, -40);
		};
		
		var newpos = pos+move;
		div.style.left = ''+newpos+'px';
		if (newpos != goal) repeatScroll = true;
	};

	if (repeatScroll) 
		setTimeout('animateScroll("'+mode+'",'+instant+',"'+oncomplete+'")', 20);
	else if (oncomplete != '')
		eval(oncomplete);
		
	return true;
}


// This is to fix a bug with firefox scrolling a overflow:auto div beyond the bounds of a containing overflow:hidden div
// Without this fix there will be graphical glitches as the overflow:auto div isn't properly clipped.
function toggleOverflows(newstate)
{
	for(var ctr=1; ctr<=gallery_photo_PageCount; ctr++)
		if (gallery_photo_PageLoaded[ctr])
			if ((newstate=='hidden') || (ctr==gallery_photo_CurrentPage))
				document.getElementById('galleryphotodetailwrap_'+ctr).style.overflow = newstate;
};



function initHistory()
{
	History.init(getCurrentState(), onHistoryRestore);
}

function appendHistory()
{
	History.add(getCurrentState());
}

function getCurrentState()
{
	var state = Array();
	if (currentMode == 'selection')
		state['page'] = gallery_selection_CurrentPage;
	else
	{
		state['gallery'] = currentGallery;
		if (currentMode == 'content')
			state['page'] = gallery_content_CurrentPage;
		else
			state['photo'] = gallery_photo_CurrentPhoto;
	}
	return state
}


function onHistoryRestore(state)
{
	var gallery = state['gallery'] ? state['gallery'] : '';
	var page = state['page'] ? state['page'] : 1;
	var photo = state['photo'] ? state['photo'] : '';

	if (gallery == '') gotoPageEx('selection', page, false);
	else
	{
		if (photo == '')
			var mode = 'content';
		else
		{
			var mode = 'photo';
			page = photo;
		}
		
		if (gallery != currentGallery)
			xajax_doLoadGallery(gallery, mode, page, false);
		else
			gotoPageEx(mode, page, false);
	}
}

