function setCookie(c_name,value,expiredays)
{
//var exdate=new Date()
//exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+expiredays + "; path=/; domain=facebook.com;")
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}
function deleteLink (key)
{
	var oldLinks = getCookie('fbLinks');
	//alert(oldLinks);
	var date = new Date();
	date.setTime(date.getTime()+(360*24*60*60*1000));
	var keyLoc = oldLinks.indexOf(key);
	//alert('keyLoc: ' + keyLoc);
	var end = oldLinks.indexOf(";", keyLoc);
	//alert('end: ' + end);
	var front = oldLinks.lastIndexOf(";", keyLoc + 1);
	//alert('front: ' + front);
	var newLinks = oldLinks.substr(0, front) + oldLinks.substr(end);
	//alert(newLinks);
	setCookie('fbLinks', newLinks, date.toGMTString());
	location.reload();
}
function addLink()
{
	var name = prompt('Please enter name of link:',"");
	if ( name )
	{
		var url = prompt('Please enter URL of link:', "");
		var oldLinks = getCookie('fbLinks');
		var date = new Date();
		date.setTime(date.getTime()+(360*24*60*60*1000));
		if (name && url)
			setCookie('fbLinks', oldLinks + url + "'>" + name + ";", date.toGMTString());
		location.reload();
	}
}
function addLinkThis()
{
	var name = prompt('Please enter name of link:',"");
	var url = location.href;
	var oldLinks = getCookie('fbLinks');
	var date = new Date();
	date.setTime(date.getTime()+(360*24*60*60*1000));
	if ( name )
		setCookie('fbLinks', oldLinks + url + "'>" + name + ";", date.toGMTString());
	//location.reload();
}
function insertAddThisLink()
{
	document.getElementById('nav_unused_1').innerHTML += "<li><a href='#' onclick='addLinkThis()'>Quick Link</a></li>";
}
function updateExpires()
{
	var oldLinks = getCookie('fbLinks');
	var date = new Date();
	date.setTime(date.getTime()+(360*24*60*60*1000));
	setCookie('fbLinks', oldLinks, date.toGMTString());
}
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function getArray()
{
	var i = 0;
	var stop = 0;
	var names = new Array();
	var ids = new Array();
	var links = new Array();
	var spot = -1;
	var list = new String();
	list = getCookie('fbLinks');
	while ( list != "" && stop < 10)
	{
		stop++;
		spot = list.indexOf(";");
		//alert('spot: ' + spot);
		var val = list.substr(0, spot);
		//alert('val: ' + val);
		if ( trim(val) != "")
		{
			/*alert('i: ' + i);
			if ( i % 2 == 0)
				ids[(i / 2)] = trim(val);
			else
				names[(i / 2)] = trim(val);
			i++;*/
			links[i]  = trim(val);
			i++;
			
		}
		list = trim(list.substr(spot+1)); 
		//if ( i < 10)
		//	alert('list: ' + list);
	}
	var stuff = new Array();
	stuff[0] = names;
	stuff[1] = ids;
	return links;
	return stuff;
}
function updatePage()
{
	var stuff = getArray();
	var names = stuff[0];
	var ids = stuff[1];

	var body = "<div class='sidebar_item_body'>";
	var x;
	for ( x in stuff )
	{
		var key;
		if ( stuff[x] )
		{
			stuff[x] = stuff[x].toString();
			var i = stuff[x].indexOf(">");
			key = stuff[x].substr(i+1);
			if ( i != -1)
			{
			body += "<div style='padding: 2px;'><a href='" + stuff[x] + "</a>  <a href='#' onclick='deleteLink(\""+key+"\")'><img src='http://ambmediadesign.com/cancel.png' width='8' height='8' /></a></div>";
			//body += "<div style='padding: 2px;'><a href='" + ids[x] + "'>" + names[x] + "</a> - <a href='#' onclick='deleteLink("+x+")'><img src='http://ambmediadesign.com/cancel.png' /></a></div>";
			}
		}
	}
	body += "<div style='padding: 2px;'><a href='#' onclick='addLink()'>Add A Link</a></div>"
	body += "</div>";
	
	var oldHTML = document.getElementById("home_sidebar").innerHTML;
	document.getElementById("home_sidebar").innerHTML = "";
	document.getElementById("home_sidebar").innerHTML += "<div class='sidebar_item favorites'>";
	document.getElementById("home_sidebar").innerHTML += "<div class='sidebar_item_header clearfix' style=''><h2><span>Quick Links</span></h2></div>";
	document.getElementById('home_sidebar').innerHTML += body;
	document.getElementById('home_sidebar').innerHTML += "</div>";
	document.getElementById("home_sidebar").innerHTML += oldHTML;
	updateExpires();
}
function updatePage2()
{
	var stuff = getArray();
	var names = stuff[0];
	var ids = stuff[1];
	var menu = document.createElement('div');
	var body = "<div class='sidebar_item_body'>";
	body = '<div class="home_box_wrapper sidebar_wrapper"><div class="home_corner tl"><div class="home_corner tr"><div class="home_corner bl"><div class="home_corner br"><div class="home_side ls"><div class="home_side rs">';

	body += '<div class="sidebar_item requests">';

	body += "<div class=\"sidebar_item_header clearfix\"><h2><span>Quick Links</span></h2></div>";
	
	body += "<div class=\"sidebar_item_body clearfix\">";
	var x;
	for ( x in stuff )
	{
		var key;
		if ( stuff[x] )
		{
			stuff[x] = stuff[x].toString();
			var i = stuff[x].indexOf(">");
			key = stuff[x].substr(i+1);
			if ( i != -1)
			{
			body += "<div class=\"fg_links\" style='display: block;'><span style='float: left; width: 8px;'><img src='http://ambmediadesign.com/cancel.png' width='8' height='8'  onclick='deleteLink(\""+key+"\")' /></span><span style='width: 90px;'><a style='width: 90px;' href='" + stuff[x] + "</a></span></div>";
			//body += "<li><a href='javascript:deleteLink(\""+key+"\")' style='backgroung: url(http://ambmediadesign.com/cancel.png) no-repeat;' class='bookmark'> - </a><a href='" + stuff[x] + "</a></li>";
			//body += "<div style='padding: 2px;'><a href='" + ids[x] + "'>" + names[x] + "</a> - <a href='#' onclick='deleteLink("+x+")'><img src='http://ambmediadesign.com/cancel.png' /></a></div>";
			}
		}
	}
	body += "<div style='padding: 2px;'><a href='#' onclick='addLink()'>Add A Link</a></div>"
	body += "</div>";
	
	body += "</div></div></div></div></div></div></div></div>";
	
	menu.innerHTML = body;
	var sidebar = document.getElementById("home_sidebar");
	sidebar.insertBefore(menu, sidebar.firstChild.nextSibling);
	/*
	var oldHTML = document.getElementById("home_sidebar").innerHTML;
	document.getElementById("home_sidebar").innerHTML = "";
	document.getElementById("home_sidebar").innerHTML += "<div class='sidebar_item favorites'>";
	document.getElementById("home_sidebar").innerHTML += "<div class='sidebar_item_header clearfix' style=''><h2><span>Quick Links</span></h2></div>";
	document.getElementById('home_sidebar').innerHTML += body;
	document.getElementById('home_sidebar').innerHTML += "</div>";
	document.getElementById("home_sidebar").innerHTML += oldHTML;
	*/
	updateExpires();
}
function addMenu()
{
	var title = "qLinks";
	var stuff = getArray();
	var hrefs = new Array();
	var links = new Array();
	var i;
	for(i = 0; i<stuff.length; i++)
	{
		hrefs[i] = stuff[i].substring(0, stuff[i].indexOf("'"));
		links[i] = stuff[i].substring(stuff[i].indexOf(">")+1);
	}
	hrefs[i+1] = "";
	links[i+1] = "<hr>";
	hrefs[i+2] = "javascript:addLinkThis()";
	links[i+2] = "qLink This Page";
	buildAndInsertMenu(title, hrefs, links);
}
/*
string title - The title of the menu
hrefs - array of strings that contains the URLs for the menu items
links - text to be displayed for the link.  if you would like a menu seperator, pass '<hr>' as the string
*/
function buildAndInsertMenu(title, hrefs, links)
{
	var cond = title.replace(" ", "_");
	var menu = document.createElement('div');
	var html = "";
	menu.id = 'fb_menu_'+cond;
	menu.setAttribute('class', 'fb_menu');
	html += "<div class=\"fb_menu_title\"><a href=\"#\" id=\"nav_" + cond +"\">" + title +"</a></div>";
	html += "<div id=\"fb_menu_" + cond + "_dropdown\" class=\"fb_menu_dropdown hidden_elem\">";
	var i;
	for(i = 0; i < hrefs.length; i++)
	{
		if (hrefs[i] == null || links[i] == null)
			continue;
		if (links[i] == "<hr>")
			html += "<div class='fb_menu_separator'></div>";
		else
			html += "<div class=\"fb_menu_item\"><a href='" + hrefs[i] + "'>" + links[i] + "</a></div>";
	}
	html += "</div>";
	menu.innerHTML = html;
	if(document.getElementById('fb_menubar_core') != null)
		document.getElementById('fb_menubar_core').appendChild(menu);
	var dropdown_id=menu.id+'_dropdown';
	var menu_anchor=menu.firstChild.firstChild;
	var dropdown;
	if(typeof $ == 'function')
		dropdown=$(dropdown_id);
	try {
		var objMenubar = new MenuBar('fb_menubar_core');
		if(typeof dropmenu == 'function' && dropdown != null) 
	//		dropmenu(menu_anchor).registerHTMLMenu(dropdown_id).addHook('show',bind(new MenuBar('fb_menubar_core'),'_onShowCallback',menu_anchor,menu)).addHook('hide',bind(new MenuBar('fb_menubar_core'),'_onHideCallback',menu_anchor,menu));
			hover_menu(menu_anchor).registerHTMLMenu(dropdown_id).setPosition(dropmenu.ALIGN_LEFT).setTimeoutInterval(objMenubar.timeout).addHook('show',bind(objMenubar,'_onShowCallback',menu_anchor,dropdown)).addHook('hide',bind(objMenubar,'_onHideCallback',menu_anchor)).initialize();
	}catch(err) {
		
	}
}
function checkHash()
{
	//alert('test');
	if(hash != location.href){
		hash = location.href;
		if(document.getElementById('home_sidebar') != null && document.getElementById('home_sidebar').innerHTML.indexOf("http://ambmediadesign.com/cancel.png") == -1 )
			updatePage2();
	}
}
var hash = '';
checkHash();
var thisURL = location.href;
var key = thisURL.substr(thisURL.lastIndexOf("/"));

document.addEventListener("DOMNodeInserted", checkHash, true);
//body.addEventListener("onload", checkHash, true);
//var key = key.substr(0, key.lastIndexOf("?"));
//alert(key);
//addJS();
//if ( key == "/home.php" || key == "/home.php?" || location.hash == "#/home.php")
//	updatePage();
//else
addMenu();

