
//------------------------------------------------------------------------
// Set up Array to hold the 'href' links for each of the menu options
var menuLinks = new Array;
menuLinks[menuLinks.length] = {item:"home", linkTo:"index.html", message:" click here to return to the Home Page "};
menuLinks[menuLinks.length] = {item:"books", linkTo:"books.html", message:" click here for details of my Novels and Creative Writing Handbooks "};
menuLinks[menuLinks.length] = {item:"teach", linkTo:"teaching.html", message:" click here for details of Creative Writing Courses and other Support Services "};
menuLinks[menuLinks.length] = {item:"contact", linkTo:"contact.html", message:" click here for details of how to contact me "};
menuLinks[menuLinks.length] = {item:"link", linkTo:"link.html", message:" click here for some useful creative writing links "};



// set up Hash table to link menuLinks[item] to menuLinks[index]

for (var i = 0; i < menuLinks.length; i++){
	menuLinks[menuLinks[i].item] = menuLinks[i];
}

//------------------------------------------------------------------------
// Array to hold links to appropriate book html page

var bookLinks = new Array;
bookLinks[bookLinks.length] = {item:"wind", linkTo:"./books/wind.html"};
bookLinks[bookLinks.length] = {item:"canopy", linkTo:"./books/canopy.html"};
bookLinks[bookLinks.length] = {item:"dream", linkTo:"./books/dream.html"};
bookLinks[bookLinks.length] = {item:"fragment", linkTo:"./books/fragment.html"};
bookLinks[bookLinks.length] = {item:"future", linkTo:"./books/future.html"};
bookLinks[bookLinks.length] = {item:"heart", linkTo:"./books/heart.html"};
bookLinks[bookLinks.length] = {item:"legacy", linkTo:"./books/legacy.html"};
bookLinks[bookLinks.length] = {item:"night", linkTo:"./books/night.html"};
bookLinks[bookLinks.length] = {item:"peace", linkTo:"./books/peace.html"};
bookLinks[bookLinks.length] = {item:"promise", linkTo:"./books/promise.html"};
bookLinks[bookLinks.length] = {item:"purple", linkTo:"./books/purple.html"};
bookLinks[bookLinks.length] = {item:"roses", linkTo:"./books/roses.html"};
bookLinks[bookLinks.length] = {item:"dreamweaver", linkTo:"./books/dreamweaver.html"};
bookLinks[bookLinks.length] = {item:"springboard", linkTo:"./books/springboard.html"};


// set up Hash table to link bookLinks[item] to bookLinks[index]

for (var i = 0; i < bookLinks.length; i++){
	bookLinks[bookLinks[i].item] = bookLinks[i];
}
//--------------------------------------------------------------------------------------
//	Array to hold images and references for home page book show
var showImages = new Array;
showImages[showImages.length] = {divId:"bk0", name:"canopy", imgSrc:"./images/canopy.jpg"};
showImages[showImages.length] = {divId:"bk1", name:"dream", imgSrc:"./images/dream.jpg"};
showImages[showImages.length] = {divId:"bk2", name:"fragment", imgSrc:"./images/fragment.jpg"};
showImages[showImages.length] = {divId:"bk3", name:"future", imgSrc:"./images/future.jpg"};
showImages[showImages.length] = {divId:"bk4", name:"heart", imgSrc:"./images/heart.jpg"};
showImages[showImages.length] = {divId:"bk5", name:"legacy", imgSrc:"./images/legacy.jpg"};
showImages[showImages.length] = {divId:"bk6", name:"night", imgSrc:"./images/night.jpg"};
showImages[showImages.length] = {divId:"bk7", name:"peace", imgSrc:"./images/peace.jpg"};
showImages[showImages.length] = {divId:"bk8", name:"promise", imgSrc:"./images/promise.jpg"};
showImages[showImages.length] = {divId:"bk9", name:"purple", imgSrc:"./images/purple.jpg"};
showImages[showImages.length] = {divId:"bk10", name:"roses", imgSrc:"./images/roses.jpg"};
showImages[showImages.length] = {divId:"bk11", name:"wind", imgSrc:"./images/wind.jpg"};
showImages[showImages.length] = {divId:"bk12", name:"dreamweaver", imgSrc:"./images/dreamweaver.jpg"};
showImages[showImages.length] = {divId:"bk13", name:"springboard", imgSrc:"./images/springboard.jpg"};

//-----------------------------------------------------------------------
//	Array to hold specific email addresses

var mail = new Array();
var action = "mailto:";
mail[mail.length] = {item:"enquiries", address:"queries@margaret-graham.com"};

for (var i = 0; i < mail.length; i++){
	mail[mail[i].item] = mail[i];
}

//------------------------------------------------------------------------

function menuItemHover(evt){
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt){
		var elem = (evt.srcElement) ? evt.srcElement : evt.target;
		elem.className = "menuHover";
	}
}
function menuItemNormal(evt){
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt){
		var elem = (evt.srcElement) ? evt.srcElement : evt.target;
		elem.className = "mg_menu_item";
	}
}
function bookLinkHover(evt){
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt){
		var elem = (evt.srcElement) ? evt.srcElement : evt.target;
		elem.className = "booklistHover";
	}
}
function bookLinkNormal(evt){
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt){
		var elem = (evt.srcElement) ? evt.srcElement : evt.target;
		elem.className = "booklist_item";
	}
}
// function to manage the links to other pages
function linkTo(id){
	location.href = menuLinks[id].linkTo;
}
// function to manage the links to books
function linkToBook(id){
	location.href = bookLinks[id].linkTo;
}

//------------------------------------------------------------------------

// declare variable for the interval timer

var coverInterval;
var pause = -1;

// set up the array to hold details of each of the book covers included in the page

var allBooks;

// initialise the book counter
var currBook = -1;
// set the name of the div holding the book cover images
//var coverDivImg = "book_list";
//var showImg = document.getElementById("bookImg");
// function to initialise the allBooks array


function initBookShow(elem){
	var imgDiv;
	var img;
	var bookShow = document.getElementById(elem);
	for(i = 0; i < showImages.length; i++){
		imgDiv = document.createElement("div");
		imgDiv.style.position = "absolute";
		imgDiv.style.top = 0 + "px";
		imgDiv.style.left = 0 + "px";
		imgDiv.id = showImages[i].divId;
		imgDiv.title = "click image for more details";
		if(i == 0){
			imgDiv.style.visibility = "visible";
		}else{
			imgDiv.style.visibility = "hidden";
		}
		img = document.createElement("img");
		img.src = showImages[i].imgSrc;
		img.name = showImages[i].name;
		imgDiv.appendChild(img);
		bookShow.appendChild(imgDiv);
	}
	var allChildren = bookShow.childNodes;
	var coverElems = new Array();
	for (var i = 0; i < allChildren.length; i++){
		if (allChildren[i].nodeType == 1){
			coverElems[coverElems.length] = allChildren[i];
		}
	}
	allBooks = coverElems;
	currBook = 0;
	coverInterval = setInterval("next()", 3000);
}
//----------------------------------------------------------------------------------
function bookDetail(){
	var bookCoverId = allBooks[currBook].id;
	var bk_id = document.getElementById(bookCoverId).childNodes[0];
	var bk_link = bookLinks[bk_id.name].linkTo;
	location.href = bk_link;
}

// function to cycle through each of the book covers included in the page 

function next(){
	if (pause == 1){ return }
	if (currBook < 0){
		allBooks[++currBook].style.visibility = "visible";
	}else if (currBook < allBooks.length - 1){
		allBooks[currBook].style.visibility = "hidden";
		allBooks[++currBook].style.visibility = "visible";
	}else if (currBook == allBooks.length - 1){
		allBooks[currBook].style.visibility = "hidden";
		currBook = -1;
		next();
	}
}

function pauseBooks(elem){ 
	pause = 1; 
	//var obj = document.getElementById(elem);
	//obj.style.visibility = "visible";
}
function resumeBooks(elem){
	pause = -1 
	//var obj = document.getElementById(elem);
	//obj.style.visibility = "hidden";
}

//----------------------------------------------------------------------------------

function menuItemOn(elem){
	var e = document.getElementById(elem);
	e.style.backgroundColor="#F4F3DA";
	e.style.color ="#808000";
	defaultStatus = menuLinks[elem].message;
}

function menuItemOff(elem){
	var e = document.getElementById(elem);
	e.style.backgroundColor="#D8D9F6";
	e.style.color ="#6466A4";
	defaultStatus = "";
}

function mailMessage(elem){
	var addressString = action + mail[elem].address;
	window.location.href = addressString;
}

//function to open a new window for the 'results' pages
var detailWin;
function detailsWindow(){
	var w = 900;
	var h = 580;
	var ref = "teaching_details.html";
	var title = "";
	var availW = (window.screen.availWidth)/2 - (w/2);
	var availH = (window.screen.availHeight)/2 - (h/2);
	var left = parseInt((screen.availWidth/2) - (w/2));
	var top = parseInt((screen.availHeight/2) - (h/2));
	var winFeatures = "width=" + w + ",height=" + h + ",status,scrollbars,resizable,left=" + 
					left + ",top=" + top + ",screenX=" + left + ",screenY=" + top;
	//----------------------------------------------------------------------------------
	//alert(detailWin);
	if (!detailWin || detailWin.closed) {
		detailWin = window.open(ref, title, winFeatures);
	// handle Navigator 2 which doesn't have an opener property
		if (!detailWin.opener) {
			detailWin.opener = window;
		}
		detailWin.focus();
	} else if (detailsWindow.focus) {
	// window is already open and focusable, so bring it to the front
		detailWin.focus();
	}
	//----------------------------------------------------------------------------------
	//detailsWindow = window.open(ref, title, winFeatures);
}


function getPosition(elem){
	var offsetTrail = document.getElementById(elem);
	var offsetLeft = 0;
	var offsetTop = 0;
	while(offsetTrail){
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined"){
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return{left:offsetLeft, top:offsetTop};
}
