/********************************************
*  iJax Bookmark Extension by Innotive Inc. *
********************************************/


/*
To use the extension, create an instance of BookmarkExt class and add it to the iJax browser using AddExtension method.
e.g. call following after creating ibrowser instance. 5 is the number of bookmarks to use.
     ibrowser.AddExtension(new BookmarkExt(5));
*/
function BookmarkExt(bookmarkNum)
{
	var BOOKMARK_COLOR = [ "#CEE5F9", "#E4C7F6", "#DEF8EA", "#F9F1DD", "#22C67D", "#9C5DAF", "#5094F0", "#F4CF8D" ];
	var BOOKMARK_HCOLOR = [ "#DEF5F9", "#F4D7F6", "#EEF8FA", "#FFFAED", "#32D68D", "#AC6DBF", "#60A4FF", "#FFDF9D" ];

	var BOOKMARK_WIDTH = 20;
	var BOOKMARK_SPACING = 10;

	var self = this;

	var layer = null;
	var browser = null;

	var bookmarks = null;

	var pageWidth, pageHeight;

	// Reposition all the bookmarks. Modify this function to customize positions and sizes of bookmarks.
	var positionBookmarks = function()
	{
		if (bookmarks == null) return;
		var curPage = browser.GetBrowserProperty("currentpage");
		var level = browser.GetCurrentLevel();
		var ratio = browser.GetMapWidth(level) / browser.GetMapWidth(browser.GetLevelCount());
		var bottom = -browser.GetViewportY();
		var pw = ratio * pageWidth;
		var left = Math.round((browser.GetViewportWidth() - pw) / 2);
		var right = left + pw
		for (var i = 0; i < bookmarks.length; ++i)
		{
			var page = bookmarks[i].page;
			if (page < 0)
			{
				// positions tempty bookmarks
				bookmarks[i].style.left = (right - (bookmarks.length - i) * (BOOKMARK_SPACING + BOOKMARK_WIDTH));
				bookmarks[i].style.top = bottom - 15;
				bookmarks[i].style.height = 10;
				bookmarks[i].title = "Click to set the bookmark";
			}
			else if (page == curPage)
			{
				// positions a bookmark for the current page.
				bookmarks[i].style.left = (right - (bookmarks.length - i) * (BOOKMARK_SPACING + BOOKMARK_WIDTH));
				bookmarks[i].style.top = bottom - 20;
				bookmarks[i].style.height = 40;
				bookmarks[i].title = "Click to unset the bookmark";
			}
			else if (page < curPage)
			{
				// positions bookmarks set for the left pages.
				bookmarks[i].style.left = (left + (bookmarks.length - i - 1) * (BOOKMARK_SPACING + BOOKMARK_WIDTH) + BOOKMARK_SPACING);
				bookmarks[i].style.top = bottom - 20;
				bookmarks[i].style.height = 20;
				bookmarks[i].title = "Page " + (page*2-1);
			}
			else 
			{
				// positions bookmars set for the right pages.
				bookmarks[i].style.left = (right - (bookmarks.length - i) * (BOOKMARK_SPACING + BOOKMARK_WIDTH));
				bookmarks[i].style.top = bottom - 20;
				bookmarks[i].style.height = 20;
				bookmarks[i].title = "Page " + (page*2-1);
			}
		}
	}

	// This method is called by iJax when the extension is added.
	this.initialize = function(parent)
	{
		browser = parent;
	}

	// This method is called when a content is loaded. It's also called if the browser already has 
	// a content loaded when the extension is added. Do your content-specific initialiation here.
	this.onLoad = function()
	{
		if (layer != null) try { layer.parentNode.removeChild(layer); } catch (e) {}

		if (browser.GetBrowserProperty("type").toLowerCase() != "news") return;
		pageWidth = parseInt(browser.GetBrowserProperty("pagewidth"));
		pageHeight = parseInt(browser.GetBrowserProperty("pageheight"));

		layer = document.createElement("div");
		layer.id = "_bookmark";

		layer.style.left = layer.style.top = 0;
		layer.style.position = "absolute";
		browser.GetLayer().appendChild(layer);

		layer.style.visibility = "hidden";

		bookmarks = new Array();

		for (var i = 0; i < bookmarkNum; ++i)
		{
			bookmarks[i] = document.createElement("div");
			bookmarks[i].id = "bm" + i;
			bookmarks[i].no = i;
			bookmarks[i].page = -1;
			bookmarks[i].style.position = "absolute";
			bookmarks[i].style.width = BOOKMARK_WIDTH;
			bookmarks[i].style.backgroundColor = BOOKMARK_COLOR[i];
			bookmarks[i].style.border = "1px solid #999999";
			bookmarks[i].style.filter = 'alpha(opacity=70);';
			bookmarks[i].style.overflow = "hidden";
			bookmarks[i].style.opacity = 0.7;
			bookmarks[i].onmouseover = function() { this.style.backgroundColor = BOOKMARK_HCOLOR[this.no]; }
			bookmarks[i].onmouseout = function() { this.style.backgroundColor = BOOKMARK_COLOR[this.no]; }
			bookmarks[i].onclick = function()
			{
				var curPage = browser.GetBrowserProperty("currentpage");
				if (this.page < 0)
				{
					this.page = curPage;
					positionBookmarks();
					// fire setbookmark event.
					browser.ProcessEvent("event", "setbookmark", this.no+1, curPage);
				}
				else if (this.page == curPage)
				{
					this.page = -1;
					positionBookmarks();
					// fire unsetbookmark event.
					browser.ProcessEvent("event", "unsetbookmark", this.no+1, null);
				}
				else 
				{
					browser.Execute("gotopage", this.page);
				}
			}
			layer.appendChild(bookmarks[i]);
		}

		var mainLevel = browser.GetBrowserProperty("mainlevel");
		if (mainLevel == browser.GetCurrentLevel())
		{
			layer.style.visibility = "";
			positionBookmarks();
		}
	}

	// This method is called when the content is unloaded.
	this.onUnload = function()
	{
		try
		{
			if (layer != null)
			{
				layer.parentNode.removeChild(layer);
				layer = null;
			}
			bookmarks = null;
		} 
		catch (e) { }
	}

	// Use this method to catch any browser event. If this returns true, the event won't be 
	// forwarded to the next extensions or browser's main event handler. So make sure this
	// returns false unless you want to override any event.
	this.onBrowserEvent = function(ev, param1, param2)
	{
		if (layer)
		{
			var mainLevel = browser.GetBrowserProperty("mainlevel");

			if ((ev == "pagechanged") || (ev == "resize"))
			{
				positionBookmarks();
			}
			else if (ev == "levelchanging")
			{
				if (param2 != mainLevel) layer.style.display = "none";
			}
			else if (ev == "levelchanged")
			{
				if (param2 == mainLevel)
				{
					layer.style.display = "";
					positionBookmarks();
				}
			}
		}

		return false;
	}

	// Use this method to add or override any Execute action. This method is called before
	// iJax browser processes the actions, and if this return true, iJax browser or other
	// extensions will not process the action. So if you want to override the action, it 
	// should return true. If you just want to do something in addition to default behaiour,
	// it should return false. Be careful not to return true by default!
	this.Execute = function(action, param)
	{
		if (bookmarks == null) return false;
		switch(action)
		{
			case "setbookmark":
				var p = param.split(",");
				if (p.length >= 2)
				{
					bookmarks[parseInt(p[0]) - 1].page = parseInt(p[1]);
					positionBookmarks();
				}
				return true;
			case "unsetbookmark":
				bookmarks[parseInt(param) - 1].page = -1;
				positionBookmarks();
				return true;
		}
		return false;
	}

	return this;
}

