/*
	The preferences object is required for load and storing user preferences, such as
	whether the user last created an annotation using a keywords drop-down or a text
	entry field.  The current implementation is a static dummy.
	  
	Keywords initialization is needed for the keywords drop-down.  It is called now to
	fetch the list of keywords so they will be ready when needed.
*/

// Needed when creating annotations:
ANNOTATION_ACCESS_DEFAULT = 'private';	// default access

function myClickSaveSearch( id, maxno )
{
	if(maxno >= 15){
	        var doIt = confirm('Saving this recent search will remove the first saved search from history. Max allowed is 15. Do you want to continue?');
	        if(!doIt){
	                return false;
	        }
	}

	document.getElementById("saveSearchId").innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";
	return clickSaveSearch( id );
}

function myClickSaveSearchRSH(id)
{

	var rshval = document.getElementById("resCountRSH").value;
	var sshval = document.getElementById("resCountSSH").value;
	if(sshval >= 15){
	var doIt = confirm('Saving this recent search will remove the first saved search from history. Max allowed is 15. Do you want to continue?');
		if(!doIt){
			return false;
		}
	}

	 var rshid = "rsh_" + id;
        document.getElementById(rshid).innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";
	var event = this;
        clickSaveSearch(id);
	//document.getElementById(rshid).innerHTML="<img src='/oso/public/all_images/delete2.jpg' onclick=\"deleteMySearchHistory(this,'" + id + "')\" height='15px' width='15px' />";
        document.getElementById(rshid).innerHTML="SAVED";
	var oldval = document.getElementById("resCountRSH").value;
	var newval = oldval - 1;
	document.getElementById("resCountRSHTXT").innerHTML="" + newval + " recent searches : ";
	document.getElementById("resCountRSH").value = newval;
	return true;
}

	
function deleteMyAnnotation( event, id )
{
    var conf=confirm("Are you sure you wish to delete this annotation?");
    if (conf == true) {
        var anid = "an_" + id;
        document.getElementById(anid).innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";
        deleteAnnotationId( event, id );
        document.getElementById(id).innerHTML="";
	document.getElementById(id).style.display = "none";
	var oldval = document.getElementById("resCountA").value;
        var newval = oldval - 1;
        document.getElementById("resCountATXT").innerHTML="" + newval + " Annotations : ";
	document.getElementById("resCountA").value = newval;
        return true;
    }
}


function deleteMyBookmark( event, link, id )
{
	var bkid = "bk_" + id;
	document.getElementById(bkid).innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";
	deleteBookmark( event, link );
	document.getElementById(id).innerHTML="";
	document.getElementById(id).style.display = "none";
        var oldval = document.getElementById("resCountBK").value;
        var newval = oldval - 1;
        document.getElementById("resCountBKTXT").innerHTML="" + newval + " Bookmarks : ";
	document.getElementById("resCountBK").value = newval;
	return true;
}

function deleteMySearchHistory( event, id )
{
        var sshid = "ssh_" + id;
        document.getElementById(sshid).innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";
        deleteSearchHistory( event, id );
        //document.getElementById(id).innerHTML="";
        document.getElementById(id).style.display = "none";
	var oldval = document.getElementById("resCountSSH").value;
	var newval = oldval - 1;
	document.getElementById("resCountSSHTXT").innerHTML="" + newval + " saved searches : ";
	document.getElementById("resCountSSH").value = newval;
	return true;
}



function myClickAddToBookmark( event, urlId )
{
	var bookmarkid = urlId.substring( urlId.lastIndexOf( '#' ) + 1 );
	bookmarkid = bookmarkid + "_bk";
	document.getElementById(bookmarkid).innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";
	return clickAddToBookmark( event, urlId );
	
}

function myClickRemoveBookmark( event, urlId )
{
	var bookmarkid = urlId.substring( urlId.lastIndexOf( '#' ) + 1 );
        bookmarkid = bookmarkid + "_bk";
	document.getElementById(bookmarkid).innerHTML="<img src='/oso/public/all_images/loader_blue_segment.gif' width='20px' height='20px' border='0' />";

	return clickRemoveBookmark( event, urlId );
}

function myClickCreateAnnotation( event, id )
{
	if ( 'bungeni' == window.marginaliaUiStyle ){
		return clickCreateAnnotation( event, id, new SelectActionNoteEditor() );
	}
	else{
		return clickCreateAnnotation( event, id );
	}
}

function doOnLoad( userid, queryUrl, serviceRoot, uiStyle )
{
	initLogging( );
	window.marginaliaUiStyle = uiStyle;
	
	// Uncomment RestAnnotationService if you have set up a database, or
	if(document.location.port!=""){
		var annotationService = new RestAnnotationService( serviceRoot + ':'+ document.location.port + '/annotate.php', { } );
	}else{
		var annotationService = new RestAnnotationService( serviceRoot + '/annotate.php', { } );
	}
	
	//var bookmarkService = new RestBookmarkService( serviceRoot + '/bookmark.php', { } );
	
	//var keywordService = new RestKeywordService( serviceRoot + '/keywords.txt');
	//keywordService.init( );
	var preferences = new Preferences( new StaticPreferenceService( ) );
	
	marginaliaArgs = {
		preferences: preferences,
		keywordService: false,
		baseUrl:  null,
		showAccess:  false,
		showBlockMarkers:  true,
		onkeyCreate:  true,
		warnDelete: false,
		showCaret: false,
		userInRequest: false
	};
	
	if ( 'bungeni' == uiStyle )
	{
		marginaliaArgs.showActions = false;
		marginaliaArgs.displayNote = bungeni.displayNote;
		marginaliaArgs.editors = {
			'default':  Marginalia.newEditorFunc( BungeniNoteEditor ),
			freeform:  Marginalia.newEditorFunc( BungeniNoteEditor )
		};
	}		
	
	window.marginalia = new Marginalia( annotationService, userid, userid, marginaliaArgs );
	//Jeet : BookMark Service. 
	//window.bookmarker = new BookMarker( bookmarkService, userid, userid );
	
	if ( 'bungeni' == uiStyle )
		trackchanges.addEditShortcuts( );
	
	var marginaliaDirect = new MarginaliaDirect( annotationService );
	marginaliaDirect.init( );
	window.marginaliaQueryUrl = queryUrl;
	if(userid == '' || userid == null){
		window.marginalia.hideAnnotations( );
		getElementsByClassName('notes',null);
	}
	else
	{
		window.marginalia.showAnnotations( queryUrl );

		var theElements = document.getElementsByClassName("commentText");
		for(var i=0;i<theElements.length;i++)
		{
		 	if (theElements[i].innerHTML.length > 30) {
		 		theElements[i].innerHTML = theElements[i].innerHTML.substring(0,25) + "...";
			}
		}		

	}

//	window.marginalia.showAnnotations( queryUrl );
	
}



function getElementsByClassName(classname, node)  
{
if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	if(re.test(els[i].className)){
		//els[i].Attributes.Add("style", "display:none");
		els[i].style.display="none";
	}
	// return a;
}

function initLogging( )
{
	var log = window.log = new ErrorLogger( false, true );

	// Set these to true to view certain kinds of events
	// Most of these are only useful for debugging specific areas of code.
	// annotation-service, however, is particularly useful for most debugging
	log.setTrace( 'annotation-service', true );	// XMLHttp calls to the annotation service
	log.setTrace( 'word-range', false );			// Word Range calculations (e.g. converting from Text Range)
	log.setTrace( 'xpath-range', false );			// Trace XPath ranges
	log.setTrace( 'find-quote', true );			// Check if quote matches current state of document
	log.setTrace( 'node-walk', false );			// Used for going through nodes in document order
	log.setTrace( 'show-highlight', true );		// Text highlighting calculations
	log.setTrace( 'align-notes', false );			// Aligning margin notes with highlighting
	log.setTrace( 'range-compare', false );		// Compare range positions
	log.setTrace( 'range-string', false );			// Show conversions of word ranges to/from string
	log.setTrace( 'list-annotations-xml', true );// Show the full Atom XML coming back from listAnnotations
	log.setTrace( 'WordPointWalker', false );		// Show return values from WordPointWalker
	log.setTrace( 'prefs', false );				// List fetched preferences
	log.setTrace( 'keywords', false );				// List fetched keywords
	log.setTrace( 'BlockPoint.compare', false );	// Compare two BlockPoints
	log.setTrace( 'range-timing', false );			// Calculate the speed of range calculations
	log.setTrace( 'highlight-timing', false );	// Calculate the speed of highlight display
	log.setTrace( 'actions', true );				// Insertion of action text
	log.setTrace( 'behavior', true );				// Behavior mappings
}

