//////////////////////////////////////////////////////////////////////////
// BookmarkDefs.js
// Copyright 2000, Be Incorporated, All Rights Reserved
//
// This is the public interface to the BeIA bookmark-browsing frame.
// All public members and methods are properties of the 'bookmarks'
// object, and they can be modified or reassigned as needed.
//
// * OBJECT TYPES
//
//   BookmarkItem
//     name:    title of the bookmarked page
//     url:     bookmarked location
//
//   BookmarkFolder
//     name:    displayed folder title
//     id:      a unique descriptor used by the CGI component
//     items:   an array of items and/or folders
//     opened:  flag indicates whether to show an 'opened' or 'closed' folder icon
//
// * OBJECTS
//
//   bookmarks.mainFrame
//     Name of the topmost browsing frame.
//
//   bookmarks.scriptURL
//     URL to the CGI component
//
//   bookmarks.topFolder
//     Topmost bookmark folder
//
//   bookmarks.styleSheet
//     CSS data to be included in the generated document.
//     See below for details of the CSS classes.
//
// * METHODS
//
//   bookmarks.Build():
//     Generate the document.
//
//   bookmarks.RenderFolder(folder):
//     Return a markup string for the given folder's name.
//
//   bookmarks.RenderBookmark(bookmark):
//     Return a markup string for the given bookmark's name.
//
// * STYLE CLASSES
//
//   SPAN.folderText
//   SPAN.bookmarkText
//   SPAN.imageFrame
//   IMG.folderIcon
//   IMG.bookmarkIcon
//   DIV.bookmarkFrame
//   DIV.folderHeading
//   DIV.openFolderHeading
//   DIV.openFolderBG
//   DIV.openFolderContent
//   DIV.itemContent
//
//////////////////////////////////////////////////////////////////////////

var bookmarks = new Object()
document.bookmarks = bookmarks
_nextID = 0

function _addToFolder(folder, item) {
	if(folder != null) {
		folder.items[folder.items.length] = item
	}
}

function BookmarkItem(name, url, parentFolder) {
	this.name = name
	this.url = url
	this._id = _nextID++
	_addToFolder(parentFolder, this)
}

function BookmarkFolder(name, id, parentFolder, opened) {
	this.name = name
	this.id = id
	this.items = new Array()
	this._id = _nextID++
	this.opened = opened
	_addToFolder(parentFolder, this)
}

bookmarks.RenderAddBookmark = _defRenderAddBookmark
bookmarks.RenderEditBookmarks = _defRenderEditBookmarks
bookmarks.RenderFolder = _defRenderFolder
bookmarks.RenderBookmark = _defRenderBookmark
bookmarks.AddBookmark = _addBookmark
bookmarks.Build = _build

bookmarks.mainFrame = '_be:content'
bookmarks.scriptURL = "file://$SCRIPTS/bookmarks"
bookmarks.backgroundURL = "file://$RESOURCES/Bookmarks/listbg.gif"

bookmarks.imgWidth =            16
bookmarks.imgHeight =           16
bookmarks.imgDims =             " width="+bookmarks.imgWidth+" height="+bookmarks.imgHeight+" "
bookmarks.imgBookmark =         "file://$RESOURCES/Bookmarks/bookmark.png"
bookmarks.imgBookmarkOv =       "file://$RESOURCES/Bookmarks/bookmark_over.png"
bookmarks.imgFolderOpened =     "file://$RESOURCES/Bookmarks/folder.png"
bookmarks.imgFolderClosed =     "file://$RESOURCES/Bookmarks/folder_closed.png"
bookmarks.imgFolderOpenedOv =    "file://$RESOURCES/Bookmarks/folder_over.png"
bookmarks.imgFolderClosedOv =    "file://$RESOURCES/Bookmarks/folder_closed_over.png"

bookmarks.styleSheet =
"<STYLE TYPE='text/css'>\n" +
"BODY                    {background-color: black}\n" +
"A                       {text-decoration: none;border:0; margin:0}\n" +
"A                       {text-decoration: none;color:white}\n" +
"A                       {text-decoration: none;color:white}\n" +
"A                       {text-decoration: none;color:white}\n" +
"DIV.bookmarkFrame       {font: 12pt sans-serif; width:246px; position:relative; left:2px}\n" +
"DIV.folderHeading       {position: relative; border:0; margin:0; height:18px}\n" +
"DIV.openFolderHeading   {position: relative; margin:0; height:18px; background: #204060}\n" +
"DIV.openFolderBG        {position: relative; background: #204060}\n" +
"DIV.openFolderContent   {position: relative; }\n" +
"DIV.itemContent         {position: relative; border-top:solid #103050 2px; border-bottom:solid #6080a0 2px; background: #406080;margin:0; height:18px}\n" +
"IMG.folderIcon          {border:0; margin-left: 3px; margin-top: 2px}\n" +
"IMG.bookmarkIcon        {border:0; margin-left: 3px; margin-top: 2px}\n" +
"SPAN.imageFrame         {position: relative; border:0; margin:0}\n" +
"SPAN.folderText         {position: relative; border:0; margin:0; left: 4px; top: -4px; font-weight: bold; color:white}\n" +
"SPAN.bookmarkText       {position: relative; border:0; margin:0; left: 4px; top: -4px; text-decoration: underline; color:white}\n" +
"</STYLE>\n"

//////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION
//////////////////////////////////////////////////////////////////////////

function _messageReceived(msg) {
	if(msg.what == 'BOOK') {
		document.location.reload(true)
	}
}
beos.messageReceived = _messageReceived

//var _imgFolderOpened = new Image(bookmarks.imgWidth, bookmarks.imgHeight);
//_imgFolderOpened.src = bookmarks.imgFolderOpened
//var _imgFolderOpenedOv = new Image(bookmarks.imgWidth, bookmarks.imgHeight);
//_imgFolderOpenedOv.src = bookmarks.imgFolderOpenedOv
//
//var _imgFolderClosed = new Image(bookmarks.imgWidth, bookmarks.imgHeight);
//_imgFolderClosed.src = bookmarks.imgFolderClosed
//var _imgFolderClosedOv = new Image(bookmarks.imgWidth, bookmarks.imgHeight);
//_imgFolderClosedOv.src = bookmarks.imgFolderClosedOv
//
//var _imgBookmark = new Image(bookmarks.imgWidth, bookmarks.imgHeight);
//_imgBookmark.src = bookmarks.imgBookmark
//var _imgBookmarkOv = new Image(bookmarks.imgWidth, bookmarks.imgHeight);
//_imgBookmarkOv.src = bookmarks.imgBookmarkOv
//
//function _setImage(name, target) {
//	document.images[name].src = target.src
//}

bookmarks.topFolder = new BookmarkFolder("Bookmarks")

function _defRenderAddBookmark(attributes) {
	return "<A "+attributes+">Add Bookmark</A>";
}

function _defRenderEditBookmarks(attributes) {
	return "<A HREF='dummy' onClick='top.be_goto_url(\"file://$RESOURCES/Bookmarks/edit.html\"); return false;'>Edit Bookmarks</A>";
}

function _buildFolderContent(folder) {
	var out = ""
	if(folder != bookmarks.topFolder) {
		out +=
		"<SPAN CLASS='imageFrame'>" +
		"<IMG CLASS='folderIcon'"+bookmarks.imgDims+
		"SRC='";
		if(folder.opened == 1) {
			out += bookmarks.imgFolderOpened;
		} else {
			out += bookmarks.imgFolderClosed;
		}
		out += "'>"+
		"</SPAN>";
	}
	out += 
	"<SPAN CLASS='folderText'>" +
	bookmarks.RenderFolder(folder) +
	"</SPAN>"
	return out
}

function _defRenderFolder(folder) {
	return folder.name
}

function _buildBookmarkContent(item, index) {
	var imgName = "icon"+item._id
	var divName = "bdiv"+item._id
	return "<DIV CLASS='itemContent' onClick='top.be_content().location.href=\""+item.url+"\";' NAME='"+divName+"'>" +
	_pad(index) +
	"<SPAN CLASS='imageFrame'>"+
	"<IMG CLASS='bookmarkIcon'"+bookmarks.imgDims+
	"SRC='"+bookmarks.imgBookmark+"' NAME='"+imgName+"' "+
	"</SPAN><SPAN CLASS='bookmarkText'>"+
	bookmarks.RenderBookmark(item)+
	"</SPAN></DIV>"
}

function _defRenderBookmark(item) {
	if(item.name.length < 32) {
		return item.name
	} else {
		return item.name.substr(0, 32) + " ..."
	}
}

function _build() {
	var out = "<HTML>"
	out += _buildStylesheet()
	out += "<HEAD><TITLE>title goes here</TITLE></HEAD><BODY background='"+bookmarks.backgroundURL+"'><DIV CLASS='bookmarkFrame'>"
	out += _buildFolder(bookmarks.topFolder, 0)
	out += "<FORM ID='form0' ACTION='"+bookmarks.scriptURL+"' METHOD=GET>"
	out += "<INPUT TYPE=hidden NAME='add_url'>"
	out += "<INPUT TYPE=hidden NAME='add_title'>"
	out += "<INPUT TYPE=hidden NAME='level'>"
	out += "</FORM>"
	out += "</DIV></BODY></HTML>"
	document.write(out)
}

function _pad(index) {
	var out = ""
	while(index > 0) {
		--index;
		out += "&nbsp;&nbsp;&nbsp;"
	}
	return out
}

function _buildFolder(folder, index) {
	if(folder == null) {
		return
	}
	var out = ""
	if(index > 0) {
		if(folder.opened) {
			out += "<DIV CLASS='openFolderHeading'"
		}
		else {
			out += "<DIV CLASS='folderHeading'"
		}

		var toggleLink = bookmarks.scriptURL + "?level=" + folder.id;
		out += " onClick='document.location.href=\""+toggleLink+"\";'>"
		out += _pad(index-1);
		out += _buildFolderContent(folder, folder.opened)
		out += "</DIV>"
	}
	var closeMarkup = ""
	if(folder.items.length > 0) {
		if(index == 0) {
			out += "<DIV CLASS='bookmarkFrame'>"
			closeMarkup = "</DIV>"
		}
		else {
			out += "<DIV CLASS='openFolderBG'><DIV CLASS='openFolderContent'>"
			closeMarkup = "</DIV></DIV>"
		}
	}
	for(var n = 0; n < folder.items.length; n++) {
		if(folder.items[n].items != null) {
			out += _buildFolder(folder.items[n], index+1)
		}
		else {
			out += _buildBookmarkContent(folder.items[n], index)
		}
	}
	out += closeMarkup
	return out
}

function _buildStylesheet() {
	return bookmarks.styleSheet;
}

function _addBookmark(form) {
	var frame = top.be_content();
	form.add_url.value = frame.location.href;
	form.add_title.value = frame.document.title;
	form.level.value = bookmarks.curLevel;
	form.submit()
	return false
}

function _getScriptURL() {
	var URL = document.URL
	if(URL == null) {
		return URL
	}
	var argStart = URL.indexOf('?')
	if(argStart > 0) {
		return URL.substring(0, argStart)
	}
	else {
		return URL
	}
}

// END -- BookmarkDefs.js --