dojo.provide("dojo.enabu");
dojo.provide("dojo.enabu.darwin");
dojo.provide("dojo.enabu.darwin.selection");


dojo.declare("dojo.enabu.darwin.selection.BookmarkManagerQuery", null, {
	bookRelease : null,
	
	constructor : function (bookRelease) {
		this.bookRelease = bookRelease;
	}
});

/**
 * La classe che gestisce le selezioni
 * 
 * actionUrl 		- La action per le operazioni AJAX
 * bookRelease		- L'identificativo della bookrelease
 * noteManager		- La classe che gestisce le note nella pagina
 * selectableArea	- La classe che identifica l'area selezionabile
 * pageDetails		- Una struttura che contiene i dettagli della pagin (inizio, fine, tipo di numerazione)
 * 
 */
dojo.declare("dojo.enabu.darwin.selection.BookmarkManager", null, {
    
	bookRelease : null,
	actionUrl : null,
	container : null,
	mainContainer : null,
	
	constructor : function(bookRelease, idContainer, actionUrl) {
		this.actionUrl = actionUrl;
		this.bookRelease = bookRelease;
		this.container = dojo.byId(idContainer);
		this.mainContainer = dojo.byId(idContainer + '_main');
	},
	
	init : function() {
		this.loadBookmarks();
	},
	
    reset : function () {
		this.container.innerHTML = '';
    },
   
    addLink : function(label, url) {
        var li = document.createElement("li");
        var div = document.createElement("div");
        var link = document.createElement("a");
        link.href = url;
        link.innerHTML = label;
        div.appendChild(link);
        li.appendChild(div);

    	this.container.appendChild(li);
    },
    
    loadBookmarks : function () {
        var params = 'params=' + encodeURIComponent(dojo.toJson(new dojo.enabu.darwin.selection.BookmarkManagerQuery(this.bookRelease)));
        dojo.xhrPost({
        	load : dojo.hitch(this, this.loadBookmarksHandleSuccess), 
        	error: dojo.hitch(this, this.loadBookmarksHandleFailure), 
        	handleAs : 'json', 
        	url : this.actionUrl + 'getBookmarks' + "?" + params
        	});
    },
    
    loadBookmarksHandleSuccess : function(o){
        if(o.Response != false) {
            var bookmarks = o.Response.bookmarks;
            try {
                this.reset();
                for(var i = 0; i < bookmarks.length; i++) {
                    var tmpObj = bookmarks[i];
                    this.addLink(tmpObj.label, tmpObj.link);
                }
                
                if(bookmarks.length > 0) {
                	this.mainContainer.style.display = 'block';
                } else {
                	this.mainContainer.style.display = 'none';
                }
                	
            } catch(e) {
                alert('Errore nella fase di caricamento dei bookmark');
            }
        } else {
            
        }
    },
    
    loadBookmarksHandleFailure : function(o){
        
    }
    
});