// read cookie
function WM_readCookie(name) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf("bmk=");
  	if(pos == -1){
	   return setCookie();
       }
}
// set cookie
function setCookie(){
        today = new Date();
        mytime = Math.round((today.getTime()));
        var the_cookie = "bmk=" + mytime;
        var the_date = new Date("December 30, 2010");
        var the_cookie_date = the_date.toGMTString();
        the_cookie = the_cookie + " ;expires=" + the_cookie_date;
        the_cookie = the_cookie + " ;path=" + "/";
        the_cookie = the_cookie + " ;domain=" + ".bm.com";
        document.cookie = the_cookie;
}

// read cookie
WM_readCookie("bmk");
allBookmarks = new Array(); 
activeBookmarks = new Array(); 
currentBookmark = 0;

// load cookie to bookmarks ( activeBookmarks ) 
function loadBookmarks(){
	bookmarksString = null;
	tempArray = document.cookie.split(";");
	x = -1;
	for (tA = 0; tA < tempArray.length; tA++)
	{
		if (tempArray[tA].indexOf('bookmarks=') > -1)
		{
			tPos = tempArray[tA].indexOf("=")+2;
			bookmarksString = tempArray[tA].substring(tPos,tempArray[tA].length);
		}
	}
		
	if (bookmarksString != null)
	{		
		    tempArray = bookmarksString.split("^");
			if (tempArray.length > 1)
			{
			    x=0;
			    for (i=0; i < tempArray.length/3; i++)
			    {
				    activeBookmarks[i] = new Bookmark(tempArray[x],tempArray[x+1],tempArray[x+2])
				    x=x+3;
			    }
		    }
	 }
}

// save bookmarks
function saveBookmarks(){
	tempCookie = "bookmarks=";
	for(i=0; i < activeBookmarks.length; i++){
		tempCookie=tempCookie+"^"+(activeBookmarks[i].id)+"^"+(activeBookmarks[i].headline)+"^"+(activeBookmarks[i].read);
	}
 	var expire = new Date ();
	expire.setTime (expire.getTime() + (6 * 24 * 3600000)); //expires in 6 days from users clock
	expire = expire.toGMTString();
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;
}

// check cookies
function eventCheckForCookies(){
	tempCookie = "bookmarks=";
	for(i=0; i < activeBookmarks.length; i++){
		tempCookie=tempCookie+"^"+(activeBookmarks[i].id)+"^"+(activeBookmarks[i].headline)+"^"+(activeBookmarks[i].read);
	}
	var expire = new Date ();
	expire.setTime (expire.getTime() + (6 * 24 * 3600000)); //expires in 6 days from users clock
	expire = expire.toGMTString();
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;
  	checkForCookie = document.cookie.split(";");
	for (x=0; x < checkForCookie.length; x++){
		if (checkForCookie[x].indexOf("bookmarks") >= 0) {cookiesOn = true;}
	}
	if (cookiesOn == false) alert("You will need to enable\ncookies to use IHT Bookmark's.");
}

// bookmark class
function Bookmark(id,headline,read){
	this.id = id;
	this.headline = headline;
	this.read = read;
	this.clicked = markRead;
}

// mark read state
function markRead(id){
	if (!id) id = this.id.substring(5,this.id.length);
	for (i=0; i < activeBookmarks.length; i++){
		if (activeBookmarks[i].id == id) activeBookmarks[i].read = "yes";
	}
}	

// clear read state bookmark
function eventClearReadBookmarks(){
	tempBookmarks = new Array();
	x = 0;
	for (i=0; i < activeBookmarks.length; i++){
		if (activeBookmarks[i].read != "yes"){tempBookmarks[x] = activeBookmarks[i]; x++;}
	}
	activeBookmarks = tempBookmarks;
	drawBookmarks();
	setBookmarksVisibility();	
	saveBookmarks();
}

// clear all bookmarks
function eventClearAllBookmarks(){
	activeBookmarks = new Array();
	drawBookmarks();
	setBookmarksVisibility();	
	saveBookmarks();
}

// draw bookmarks
function drawBookmarks(){
	newHTML = "";

	if (activeBookmarks.length < 1){
		newHTML = "<span class='bookmarkItem'>To add this page to your Bookmarks menu, click the link \"Bookmark me\" below:";
	}

	for (i=0; i < activeBookmarks.length; i++){
		
		bookmark = activeBookmarks[i]
		if (bookmark.read == "yes") tClass = "bookmarkItemRead";
		else tClass = "bookmarkItem";
		newHTML += '<A href="'+bookmark.id+'" onmousedown="markRead(\''+bookmark.id+'\')" class="'+tClass+'" id="cLink'+bookmark.id+'">';
		newHTML += bookmark.headline+"</a>";
	}

	obj = document.getElementById("bookmarksWrapper");
	
    obj.innerHTML = newHTML;

    bookmarksSetWrapperHeight();

}

// set height
function bookmarksSetWrapperHeight(){
	obj = document.getElementById("bookmarksWrapper");
	obj.style.height= Math.round(bmScreen.visHeight()/4)+"px";
}

// check if same
function checkForDuplicates(){
	for (i=0; i < activeBookmarks.length; i++){
		if (newBookmark == activeBookmarks[i].id) {i = allBookmarks.length; duplicate = true;}
	}
}

// div of bmk obj
var bookmarksDivArray = new Array();

// create div array
function createPageBookmarksArray(){
	d = document.getElementsByTagName("DIV")
	for (j=0; j < d.length; j++) 
	    if (d[j].id.indexOf("bmk") > -1)
	    {
		    bookmarksDivArray[bookmarksDivArray.length] = d[j];
	    }
}

// 
function bookmarkInstanceVisibility(id,state){
	t = document.getElementsByName(id);
	if(t.length > 0){
		for (j=0; j < t.length; j++)
		{
			t[j].style.visibility = state;
			 
			t[j].onclick = addBookmark;
		}
	}else{ 
	 	d = bookmarksDivArray;
  		{
			for (j=0; j < d.length; j++) if (d[j].id == id){
				d[j].style.visibility = state; 
				d[j].onclick = addBookmark;
			}
		}
	}		
}

// 
function setBookmarksVisibility(){
	for (i=0; i < allBookmarks.length; i++){
		
		vis = "visible";
		
		for (x=0; x <activeBookmarks.length; x++) 
		    if (allBookmarks[i].id == activeBookmarks[x].id) vis = "hidden";
		
		obj = "bmk"+allBookmarks[i].id;
		
		bookmarkInstanceVisibility(obj,vis)
	}
}

// add bookmark
function addBookmark(){
	
	newBookmark = this.id.substring(3,this.id.length)
	duplicate = false;

	for (i=0; i < allBookmarks.length; i++){
		if (newBookmark == allBookmarks[i].id) {pos = i; i = allBookmarks.length}
	}
	
	if (activeBookmarks.length > 0)	checkForDuplicates() 	

	if (!duplicate){	
		eventCheckForCookies();

		if (cookiesOn == true){
			activeBookmarks[activeBookmarks.length] = new Bookmark(allBookmarks[pos].id,allBookmarks[pos].headline);

	 		bookmarkInstanceVisibility(this.id,'hidden')
	 		obj = document.getElementById("navBookmarks")
//	 		debugger;
            try{
	 		    drawBookmarks();	

	 		    if(document.all)event.cancelBubble = true;
    			
			    saveBookmarks();

			}catch(e){debugger;}
	 	}
	 }
}
var cookiesOn = false;

// init bookmarks
function initBookmarks(){
	createPageBookmarksArray()
	loadBookmarks();
	drawBookmarks(); 
}	

// screen object
function screenObject(){
	this.bottom = function(){
		if (document.body.scrollHeight) return document.body.scrollHeight};
	
	this.height = function() {
		if (document.body.offsetHeight) return document.body.offsetHeight;}
	
	this.visHeight = function() {
		if (window.innerHeight) return window.innerHeight;
		if (document.body.clientHeight) return document.body.clientHeight;}
	
	this.width = function() {
		if (document.body.offsetWidth) return document.body.offsetWidth;}
	
	this.scrollTop = function() {
		if(document.body.scrollTop) return document.body.scrollTop
		if (window.pageYOffset) return window.pageYOffset;
		else return 0;};
}

function initialize(){
	bmScreen = new screenObject(); 
	userEventsInit(); 
	window.onresize = bookmarksSetWrapperHeight;
	initBookmarks(); 
}

function closePage(){
	saveBookmarks();
}

function displayFix(){
	//document.getElementById("bodyNode").style.display = "block"
}	

var SDF86707D__onload= document.body.onload;
if(typeof document.body.onload == 'function')
{
 document.body.onload = function()
 {
SDF86707D__onload();        
  displayFix();
 }
}
else
{
 document.body.onload = function()
 {
  eval(SDF86707D__onload);        
  displayFix();
 }
}



window.onunload = closePage;
if (document.all) classFix = "className";
else classFix = "class"

var activeMenu = null; 
function menuOver(){
	document.onclick = null;
	if (document.all) event.cancelBubble=true
}

function menuOut(){
	document.onclick = eventHideMenu;
	if (document.all) event.cancelBubble=true
}

function eventShowMenu(e){
	for (i=0; i < allBookmarks.length; i++){
		vis = "visible";
		for (x=0; x <activeBookmarks.length; x++) if (allBookmarks[i].id == activeBookmarks[x].id) vis = "hidden";
		obj = "bmk"+allBookmarks[i].id;
		 document.getElementById("menuBookmarks").style.display = "";
		bookmarkInstanceVisibility(obj,vis);
	}
	activeMenu = "menu"+this.id.substring(3,this.id.length)
	obj = document.getElementById(activeMenu)
	if (obj){ document.body.appendChild(obj);  obj.style.visibility = "visible"}
	if (document.all){ 
		document.onclick = eventHideMenu;
		event.cancelBubble=true;
	}
}

function eventHideMenu(){
	document.onclick = null;
	for (i=0; i < allBookmarks.length; i++){
		vis = "hidden";
		obj = "bmk"+allBookmarks[i].id;
		bookmarkInstanceVisibility(obj,vis)
	}
	if (activeMenu != null){
		obj = document.getElementById(activeMenu)
		obj.style.visibility = "hidden"
	}
}

menus = new Array("Bookmarks")
function userEventsInit(){
	for (i=0; i < menus.length; i++){
		obj = document.getElementById("nav"+menus[i])
		obj.onclick = eventShowMenu;
		obj = document.getElementById("menu"+menus[i])
		obj.onmouseover = menuOver;
		obj.onmouseout = menuOut;
	}
}

if (!document.all){
	document.writeln('<style type="text/css">');
	document.writeln('.c, .click {cursor:pointer}');
	document.writeln('.noborder {border:1px solid}');
	document.writeln('</style>');
}