//global vars
var bookmarkXSLLoaded	  = false; //see if the bookmark xsl has been loaded yet
var bookmarkXsltProcessor = null; //stylesheet processor
var bookmarkTimerId		  = 0; //timer id to keep menu open or close 
var asyncType 			  = (($.browser.opera) || ($.browser.safari)) ? false : true;

//bookmarks a character
function ajaxBookmarkChar()
{
	//change "bookmark this character" img
	if(asyncType == true)
		$(".bmcLink").replaceWith("<div class=\"bmcEnabled\"></div>");
		
	$.ajax({
		async: asyncType,
		type: "GET",
		dataType: "xml",
		url: "/vault/bookmarks.xml?" + charUrl +"&action=1",
		success: function(msg){			
			if(($.browser.opera) || ($.browser.safari))
				window.location.reload(); //refresh page
			else
				buildBookmarkMenu(msg); //rebuild the bookmark list				
		},
		error: function(msg){
			//TO DO: Go to error page!
			window.location.reload(); //refresh page
		}
	});
}

//removes a bookmarked character from the drop down list
function ajaxRemoveChar(removedLink, clickedItem)
{
	
	//hide tooltip
	$(theGlobalToolTip).hide();
	
	$.ajax({
		type: "GET",
		async: asyncType,
		dataType: "xml",
		url: "/vault/bookmarks.xml?" + removedLink +"&action=2",
		success: function(msg){	
			if(($.browser.opera) || ($.browser.safari))
				window.location.reload(); //refresh page
			else
				buildBookmarkMenu(msg); //rebuild the bookmark list			
		},
		error: function(msg){
			//TO DO: Go to error page!
			window.location.reload(); //refresh page	
		}
	});
}

//keeps bookmark menu dropdown open
function persistBookMarkMenu()
{
	if(bookmarkTimerId != null)
		clearTimeout(bookmarkTimerId);
		
	bookmarkTimerId = setTimeout('$("#userSelect").hide(); $("#userSelect").css("z-index","-1");', 500);	
}

//initialize the bookmark dropdown menu
function bindBookmarkMenu()
{	
	//bind timers for bookmark menu
	setBookmarkPersistTimers();
	
	//bind function for removing a bookmark
	$(".bookmark-remove").click(function(){
			
		//get the link that tells us what character we want to remove	
		var removeBookmarkLink = $(this).prev().attr("href");		
				
		//remove the "/character-sheet.xml?" portion
		removeBookmarkLink = removeBookmarkLink.substr(removeBookmarkLink.indexOf(".xml?")+5);	
		
		//theCharUrl doesnt always exist, so wrap in "try/catch" to avoid errors
		try{
			//remove potential spaces in realm names
			theCharUrl			= theCharUrl.replace(/ /g,"+");
			theCharUrl		    = theCharUrl.replace(/\%20/g,"+");
			removeBookmarkLink  = removeBookmarkLink.replace(/ /g,"+");
			removeBookmarkLink  = removeBookmarkLink.replace(/\%20/g,"+");	
			
			//means we're on this page, so change the green check to "add bookmark"
			if(theCharUrl == removeBookmarkLink){
				$(".bmcEnabled").replaceWith("<a class=\"bmcLink\" href=\"javascript:ajaxBookmarkChar()\"><span>"+bookmarkThisChar+"</span><em/></a>");			
			}
		}catch(e){ }
		
		//call ajax function to remove the item
		ajaxRemoveChar(removeBookmarkLink,this);	
	});
	
	//rebind tooltips
	bindToolTips();
}

//builds bookmark menu 
function buildBookmarkMenu(bookmarkXml)
{	
	//load XSL stylesheet if we haven't yet	
	if(!($.browser.opera) && !($.browser.safari)){
		if(bookmarkXSLLoaded == false)
		{
			//get the stylesheet			
			var xslDoc = Sarissa.getDomDocument();
			xslDoc.async = false;
			xslDoc.load("/layout/bookmarks.xsl");
			
			bookmarkXsltProcessor = new XSLTProcessor();
			bookmarkXsltProcessor.importStylesheet(xslDoc);			
			bookmarkXSLLoaded = true;
		}
	}
	
	$(".bmlist").remove();
	$(".nobookmark").remove();				
		
	//transform the fragment
	var tempDiv = document.createElement("div");
	tempDiv.innerHTML = "";
	tempDiv.appendChild(bookmarkXsltProcessor.transformToFragment(bookmarkXml,window.document));		
		
	//add new menu to dom
	$(".myBooks").after(tempDiv.innerHTML);
				
	//reinitiliaze bookmarks
	bindBookmarkMenu();	
}


//binds certain elements to show/persist the bookmark menu
function setBookmarkPersistTimers()
{
	//show bookmark menu and keep it open
	$(".bookmark-user").mouseover(function() {
		persistBookMarkMenu();	
		$("#userSelect").show();
		$("#userSelect").css("z-index","10");
		clearTimeout(bookmarkTimerId);
	});
	
	//when leaving bookmark icon, start timer again to prevent menu
	//from not closing if they dont mouse over the menu portion
	$(".bookmark-user").mouseout(function() {
		persistBookMarkMenu();		   
	});
		
	//keep bookmarkmenu open (stop timer)	
	$(".user-line-item").mouseover(function(){
		clearTimeout(bookmarkTimerId);
	});	
	
	$(".user-menu-section").mouseover(function(){
		clearTimeout(bookmarkTimerId);
	});	
	
	//when mousing over parchment, hide the menu
	$(".parchment-top").mouseover(function(){
		$("#userSelect").hide()						   
	});
	
	//when leaving the menu, start timer again
	$(".user-menu-contents").mouseout(function(){
		persistBookMarkMenu();								   
	});	
}