$(document).ready(function() {

	$('html').click(function(){
		closeContext();
	});

	var menuLeaveTimer = "";
	
	$('.standard_link').live("contextmenu",function(e) {
		positionContextMenu(e);
		return false;
	});
	
	$('.favlist_link').live("contextmenu",function(e) {
		positionContextMenu(e);
		return false;
	});
	
	$('#context_menu_container').mouseleave(function() {
		// start a timer here and close the menu after 
		// the user has moved off the menu and stayed off
		// for longer than 3 seconds.
		menuLeaveTimer = setTimeout(closeContext,2000);
	});
	$('#context_menu_container').mouseover(function() {
		if (menuLeaveTimer) {
			clearTimeout(menuLeaveTimer);
		}
	});

	$('#menu_close').click(function() {
		$('#context_menu_container').fadeOut('fast',clearContextMenu);
	});
	
	$('#item_edit').click(function(e){
		$('#bookmark_edit_box').css('left', (e.clientX-200)+"px");
		$('#bookmark_edit_box').css('top', (e.clientY-100)+"px");
	});
	
	$('#item_sharels').click(function(e){
		$('#sharelink_box').css('left', (e.clientX-200)+"px");
		$('#sharelink_box').css('top', (e.clientY-100)+"px");
	});
	
	$('#bookmark_edit_box').draggable();
	$('#sharelink_box').draggable();

});

function positionContextMenu(e) {
	var scrollTop = document.body.scrollTop ? document.body.scrollTop :
		document.documentElement.scrollTop;
	var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft :
		document.documentElement.scrollLeft;
	$('#context_menu_container').css('left', e.clientX + scrollLeft + 'px');
	$('#context_menu_container').css('top', e.clientY+scrollTop + 'px');
}

function clearContextMenu() {
	// clear out menu items to avoid any lingering items showing up
	// in error on the next appearance.
	$('#context_menu_title').html('--');
    $('#item_view').html('--');
    $('#item_viewnew').html('--');
	$('#item_delete').html('--');
	$('#item_edit').html('--');
	$('#item_icon').html('--');
	$('#item_sharels').html('--');
	$('#item_sharefb').html('--');
	$('#item_sharetw').html('--');
	$('#item_sharegp').html('--');
}

function closeContext() {
	$('#context_menu_container').fadeOut('fast',clearContextMenu);
}

// this gets called to prepare the contents of the menu items
function setMenuItems(id, type, e) {
	if (!e) var e = window.event;
	if (e.which != 3) {
		return;
	}
	$.getJSON("rpc.php?action=getbookmark&thecode=" + id + "&thename='nil'",
		function(msg) {
			renderMenuItems(msg['id'], msg['url'], msg['url_title'], type);
		}
	);
}

function renderMenuItems(id, url, title, menutype) {
	// retrieve title and url here
	// set to url and title vars
	// do we need to decode if we get it directly from the db? (lines 124-128)
	if (menutype == "linkstream") {
		$('#item_delete').css('display','none');
		$('#item_delete').html = '';
		$('#item_edit').css('display','block');
		$('#item_icon').css('display','block');
	}	
	if (menutype == "trash") {
		$('#item_delete').css('display','block');
		$('#item_delete').html('<a href="javascript:showDeleteBookmarkConfirm(\''+id+'\');"><img src="images/delete.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Delete this bookmark</a>');
		$('#item_edit').css('display','block');
		$('#item_icon').css('display','block');
	} 
	if (menutype == "normal") {
		$('#item_delete').css('display','block');
		$('#item_delete').html('<a href="javascript:showTrashBookmarkConfirm(\''+id+'\');"><img src="images/trash_cm.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Move to trash</a>');
		$('#item_edit').css('display','block');
		$('#item_icon').css('display','block');
	}
	if (menutype == "favorites") {
		$('#item_delete').css('display','none');
		$('#item_delete').html('');
		$('#item_edit').css('display','none');
		$('#item_edit').html('');
		$('#item_icon').css('display','none');
		$('#item_icon').html('');
	}

	url = encodeURI(url);
	enTitle = encodeURI(title);

	$('#context_menu_title').html(title);
    $('#item_view').html('<a href="'+url+'"><img src="images/open_site.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;View site</a></li>');
    $('#item_viewnew').html('<a href="'+url+'" target="_blank"><img src="images/open_newwin.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;View site in new window</a></li>');
	$('#item_edit').html('<a href="#" onclick="closeContext();editLink(\''+id+'\')"><img src="images/edit_cm.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Edit this bookmark</a>');
	$('#item_icon').html('<a href="#" onclick="closeContext();editIcon(\''+id+'\');"><img src="images/edit_icon.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Change icon</a>');
	$('#item_sharels').html('<a href="#" onclick="closeContext();shareLink(\''+id+'\')"><img src="images/ryecon16.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Share on public linkstream</a>');
	$('#item_sharefb').html('<a href="http://www.facebook.com/share.php?u='+url+'&t='+enTitle+'" target="_blank"><img src="images/facebook_15x15.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Share on Facebook</a>');
	$('#item_sharetw').html('<a href="http://twitter.com/home?status='+enTitle+'%20'+url+'" target="_blank"><img src="images/twitter_15x15.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Share on Twitter</a>');
	$('#item_sharegp').html('<a href="https://m.google.com/app/plus/x/?v=compose&content='+enTitle+'%20'+url+'" target="_blank"><img src="images/googleplus_15x15.png" border="0" style="vertical-align:middle;padding-bottom:4px;">&nbsp;Share on Google+</a>');

	if ( ($('#context_menu_container').css('display') == "none") ) {
		$('#context_menu_container').fadeIn('fast');
	} else {
		$('#context_menu_container').fadeOut('fast',clearContextMenu);
	}

}
