//Three navbars - toc-navbar, icon dock and vestigal icon dock (to be removed);
var TOC_NAVBAR_HIGHLIGHT="red";
var PHOTO_1 = "html/india_400px.html";
var PHOTO_2 = "html/peru_400px.html";
var PHOTO_3 = "html/thesis_400px.html";
var PHOTO_4 = "html/portraits_400px.html";
var BOOK_0 = "stories/when_we_all_have_brains.html";
var BOOK_1 = "stories/rebirth.html";
var BOOK_1_ELEMENT = "rebirth";  //used in loaded_two function call made in navbar
var HOME_PAGE = "home_page.php";

iconDockEventHandler = function(e) {
	// ******** NAVIGATION FOR ICON DOCK ***************//
	try{
		//NB Next step is to put meta data in database
		if ($(e).children("img").attr("id")=="dock-img1" || $(e).children("img").attr("id")=="dock2-img1")
		{
			drawSlideShow("india-400px","Brian MacMillan's Photographs from India, 2007",PHOTO_1,1)
		}
		if ($(e).children("img").attr("id")=="dock-img2" || $(e).children("img").attr("id")=="dock2-img2")
		{
			drawSlideShow("peru-400px","Brian MacMillan's Photographs from Peru, 2004 and 2009",PHOTO_2,2)
		}
		if ($(e).children("img").attr("id")=="dock-img3" || $(e).children("img").attr("id")=="dock2-img3")
		{
			drawSlideShow("thesis-400px","Stills from Brian MacMillan's Thesis Project",PHOTO_3,3)
		}
		if ($(e).children("img").attr("id")=="dock-img4" || $(e).children("img").attr("id")=="dock2-img4")
		{
			drawSlideShow("portraits-400px","Brian MacMillan's Portraits, 2001 - present",PHOTO_4,4)
		} 

		if ($(e).children("img").attr("id")=="dock-img0")
		{
			//NB Brian To do
			return;
			var sCT = "ct0"; //NB Brian - to be replaced with a content id - primary key for content in database
			$.colflow.title="Brian MacMillan - Home Page";
			document.title = $.colflow.title;
			if (!document.getElementById("home-page")){
				var _div = $(document.createElement('div')).attr("id",sCT); // NB Brian hard-wired ctn = ct[length]+1; is hardwired here.
				$('#content-text').append(_div);
				$("#"+sCT).load(HOME_PAGE,
					function (responseText, textStatus, XMLHttpRequest) {
					if (textStatus=="success") {
						$("#ct-display").hide();
						$("#content-header").hide();
						$("#tab").hide();
						$("#icon-dock").hide();
						$("#toc-navbar").hide();
					} else {
						alert("Error: failed to load 'Homepage "+textStatus);
						return;
					}
				});
			} else {}
		}
		/**** SWITCH TO REBIRTH OR WHEN WE ALL HAVE BRAINS *****/
		if ($(e).children("img").attr("id")=="dock-img6" || $(e).children("img").attr("id")=="dock-img7" || $(e).children("img").attr("id")=="dock2-img6" || $(e).children("img").attr("id")=="dock2-img7")
		{
			
			switchScheme("STORIES");
			if ($(e).children('img').attr("id")=="dock-img6"){
				//Switch to When We All Have Brains
				$.colflow.title="Brian MacMillan - When We All Have Brains and Other Stories";
				document.title = $.colflow.title;
				$.colflow.bookid = 0;
				drawPage();
				setTOCTitles();
			};
			if ($(e).children('img').attr("id")=="dock-img7"){
				$.colflow.title="Brian MacMillan - Rebirth and Other Stories";
				document.title = $.colflow.title;
				if (!document.getElementById("book-1")){
					//var _div = $(document.createElement('div')).addClass('base-column').attr("id","rebirth")
					var _div = $(document.createElement('div')).attr("id","ct1");
					$('#content-text').append(_div);
					//document.getElementById("book-1").style.zIndex=-1;
					$("#ct1").load(BOOK_1,
						function (responseText, textStatus, XMLHttpRequest) {
						if (textStatus=="success") {
							$.colflow.bookid = 1;
							$.colflow.newBook(1);
							document.getElementById("book-1").style.display="block";
							//wait until story is loaded before proceeding
							loaded_two(BOOK_1_ELEMENT,bookIsLoaded,1);
							setTOCTitles();
						} else {
							alert("Error: failed to load 'Rebirth and Other Stories'");
							return;
						}
					});
				} else {$.colflow.bookid = 1;drawPage();setTOCTitles();}
			};
		}
		if ($(e).children("img").attr("id")=="dock-img8")
		{
			var sCT="ct2"; // Content text node number. should be dynamically calculated from length
			
			window.open("http://brianmacmillan.net/bm_poetry.php")
			return;
			$.colflow.title="Brian MacMillan - Poetry";
			document.title = $.colflow.title;
			if (!document.getElementById("book-1")){
				//var _div = $(document.createElement('div')).addClass('base-column').attr("id","rebirth")
				var _div = $(document.createElement('div')).attr("id",sCT);
				$('#content-text').append(_div);
				//document.getElementById("book-1").style.zIndex=-1;
				$("#"+sCT).load(BOOK_1,
					function (responseText, textStatus, XMLHttpRequest) {
					if (textStatus=="success") {
						$.colflow.bookid = 1;
						$.colflow.newBook(1);
						document.getElementById("book-1").style.display="block";
						//wait until story is loaded before proceeding
						loaded_two(BOOK_1_ELEMENT,bookIsLoaded,1);
						setTOCTitles();
					} else {
						alert("Error: failed to load 'Rebirth and Other Stories'");
						return;
					}
				});
			} else {$.colflow.bookid = 1;drawPage();setTOCTitles();}
		}		//NB Need to fix
		//document.getElementById("username").focus();
	
	} catch(err)
	{
		errorHandler("iconDockEventHandler",err);
	}
}
function drawSlideShow(psContainer,psTitle,psFolder,piIndex)
{
	switchScheme("PHOTOS");
	$.colflow.valignPicture();
	//$.colflow.title="Brian MacMillan's Photographs from Peru 2004 and 2009";
	$.colflow.title=psTitle;
	document.title = $.colflow.title;
	/* append images as ul/li to slideshow inner - this creates work in terms of generifying image handling to handle n number of slideshows.*/
	if (!document.getElementById(psContainer)){
		var _ul = $(document.createElement('ul')).attr("id",psContainer).addClass("gallery gallery-items colflow");
		$('#slideshow-inner').append(_ul);
		$("#"+psContainer).load(psFolder,
			function (responseText, textStatus, XMLHttpRequest) {
			if (textStatus=="success") {
				$.colflow.newImageAlbum("#"+psContainer);
				document.getElementById(psContainer).style.display="none";
				loaded_two("last-"+psContainer+"-li",prepareImages,psContainer);
				$(".gallery li img").show();//{display:none}
			} else {
				alert("Error: failed to load '"+psContainer+"'");
				return;
			}
		});
		//No image is being displayed ...//
		//if ($(".colflow-wrapper").children("img").length==0){
		//	alert("in set default image");
		//};
	} else {$.colflow.albumid = "#"+psContainer;drawPageSlideShow();}
}
/*
setImage = function(psContainer){
	//NB To change. This will simply draw the first image, not remember the page number.
	$.colflow.setDefaultImage("#"+psContainer);
}
*/
function prepareImages(psContainer)
{
	//launched by loaded_two, which queries for the existence of a control then executes a function
	//NB Brian - frequent calling of this means that loaded_two failed. Error handling needed.
	$.colflow.createImageObjects('#'+psContainer);
	//$.colflow.setDefaultImage("#"+psContainer);
	$.colflow.activate($.colflow.image())
}

function tocNavbarInit()
{
	try
	{	
		/*
		 $('#toc-navbar div').children('div').each(function(i) {
			ileftPercent = TOC_NAVBAR_GUTTER*i;
			sLeftPercent = ileftPercent.toString()+"%";
			if (i<3)
			{
				//alert("sLeftPercent "+sLeftPercent+" "+ileftPercent);
			}
			this.style.left = sLeftPercent;
		});
		*/
		highlightCurrentChapterImage($.colflow.chapterid);
	} catch(err)
	{
		errorHandler("tocNavbarInit",err);
	}

}
function tocNavMouseOver(e)
{
	try
	{
		setBorder(e,"1px", "solid", TOC_NAVBAR_HIGHLIGHT);		
				
	} catch(err)
	{
		errorHandler("Error in tocNavMouseOver",err);
	}
}
function tocNavMouseOut(e)
{
	try
	{
		//NB awkward retro fit - i'm stripping the index 1 from id "toc-nav1", for example.
		//intermediate fix while i make everything more object based, less index based.
		//alert("toc nav mouse out index "+e.id.substr(7,1)
		if (e.id.substr(7,1) != $.colflow.chapterid)
		{
			setBorder(e,"1px", "solid", "#aaa");
		} else
		{
			setBorder(e,"1px", "solid",$.bm_tab.BOX_COLOR_DARK);
		}			
	} catch(err)
	{
		errorHandler("Error in tocNavMouseUut",err);
	}
}
function highlightCurrentChapterImage(piChapter)
{
	// unhighlight existing borders
	// and highlight the border for piChapter
	try
	{	
		//alert("in highlightCurrentChapterImage");
		var iCounter=0;
		 $('#toc-navbar div').children('div').each(function(i) {
			if (i==piChapter)
			{
				//do nothing
				//set in mouse out event. awkward coding but mouse events do that to you sometimes.
			} else
			{
				$(this).children('img').css('border','1px dotted gray');
			};
		});
	} catch(err)
	{
		errorHandler("highlightCurrentChapterImage",err);
	}
}



