dojo.provide("dojo.enabu.main.widget.NonModalPopUp");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

dojo.declare("dojo.enabu.main.widget.NoteDetailsPopUp", dojo.enabu.main.widget.NonModalPopUp, {
    buttonModify : null,
    buttonRemove : null, 
    note : null, 
    
    htmlBaseId : '',
    contentContainer : '<div dojoAttachPoint="containerNode"></div>',
    toolbarButtons : '', 
    
    constructor : function(params, srcNodeRef, note){
        this.note = note;
    
        this.htmlBaseId = 'noteDetails_' + this.note.id;
        this.toolbarButtons = '<div class="toolbar" id="' + this.htmlBaseId + '_toolbar" style="text-align: center; padding-top:1px;"></div>';
        this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons + this.templateFooter;
        
    },
    
    templateHead :     '<div class="dijitDialog selection-note-details-popup" style="border: 0; background-color: transparent"><div dojoAttachPoint="mainDivNode"><table  cellpadding="0" cellspacing="0" border="0"><tr> ' +
					    '            <td><img src="/images/Main/layout/details/head-left.png" alt="left" /></td>' +
					    '            <td class="headline"><div style="float:right; padding-left:3px; padding-bottom:2px;" dojoAttachEvent="onclick: onCancel"><a href="" onClick="return false;"><img src="/images/Main/icons/10px-Crystal_Clear_action_button_cancel.png" /></a></div></td>' +
					    '            <td ><img src="/images/Main/layout/details/head-right.png" alt="left" /></td>' +
					    '        </tr>' +
					    '        <tr>' +
					    '            <td class="center-left">&nbsp;</td>' +
					    '            <td class="center">',
                        
    templateFooter :    '            </td><td class="center-right">&nbsp;</td>' +
					    '        </tr>' +
					    '        <tr>' +
					    '            <td class="bottom-left"><img src="/images/Main/layout/details/footer-left.png" alt="left" /></td>' +
					    '            <td class="bottom-center">&nbsp;</td>' +
					    '            <td class="bottom-right"><img src="/images/Main/layout/details/footer-right.png" alt="left" /></td>' +
					    '        </tr>' +
					    '    </table></div>' +
					    '</div>',
    
    attributeMap: dojo.mixin(dojo.clone(dijit._Widget.prototype.attributeMap), {}),
    onRemove: function(){},
    onEdit: function(){},
    onCancel : function() {
        this.hide();
    },
    
    _init : function() {
        //if(this.buttonModify == null && this.buttonRemove == null) {
            var toolbar = dojo.byId(this.htmlBaseId + '_toolbar');
            toolbar.innerHTML = '';
            var onEdit = dojo.hitch(this, this.onEdit);
            var onRemove = dojo.hitch(this, this.onRemove);
            this.buttonModify = new dijit.form.Button({label: "Modifica", onClick: onEdit, iconClass:"darwinNoteDetailsModifyButtonIcon" });
            this.buttonRemove = new dijit.form.Button({label: "Rimuovi", onClick: onRemove, iconClass:"darwinNoteDetailsRemoveButtonIcon" });
            toolbar.appendChild(this.buttonModify.domNode);
            toolbar.appendChild(this.buttonRemove.domNode);
        //}
    },
    
    /**
     * Richiama la show del parent e poi inizializza
     */
    show: function() {
    	this._init();
        this.inherited(arguments);
    }
});

dojo.declare("dojo.enabu.main.widget.RoundModalPopUp", dijit.Dialog, {
    constructor : function(params, srcNodeRef){
        this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons + this.templateFooter;
    },
    
    contentContainer : '<div dojoAttachPoint="containerNode"></div>',
    toolbarButtons : '',
    templateHead :    '<div class="dijitDialog pop-up-box" style="border: 0; background-color: transparent"><div dojoAttachPoint="mainDivNode"><table  cellpadding="0" cellspacing="0" border="0"><tr> ' +
                        '            <td><img src="/images/Main/layout/popup/head-left.png" alt="left" /></td>' +
                        '            <td class="headline"><div dojoAttachPoint="titleBar"><span dojoAttachPoint="titleNode" class="dijitDialogTitle" id="${id}_title"></span></div></td>' +
                        '            <td ><img src="/images/Main/layout/popup/head-right.png" alt="left" /></td>' +
                        '        </tr>' +
                        '        <tr>' +
                        '            <td class="center-left">&nbsp;</td>' +
                        '            <td class="center">' +
                        '            ',
    templateFooter :    '            </div></td><td class="center-right">&nbsp;</td>' +
                        '        </tr>' +
                        '        <tr>' +
                        '            <td class="bottom-left"><img src="/images/Main/layout/popup/footer-left.png" alt="left" /></td>' +
                        '            <td class="bottom-center">&nbsp;</td>' +
                        '            <td class="bottom-right"><img src="/images/Main/layout/popup/footer-right.png" alt="left" /></td>' +
                        '        </tr>' +
                        '    </table></div>' +
                        '</div>',
                        
    attributeMap: dojo.mixin(dojo.clone(dijit.Dialog.prototype.attributeMap), {})
    
});
    
dojo.declare("dojo.enabu.main.widget.YesNoPopUp", dojo.enabu.main.widget.RoundModalPopUp, {
    
    toolbarButtons : '<div class="toolbar" id="yesNoPopupId" style="text-align: center; padding-top:10px;"></div>',
                     
    _init : function() {
        var toolbar = dojo.byId('yesNoPopupId');
        var onYes = dojo.hitch(this, this.onYes);
        var onCancel = dojo.hitch(this, this.onCancel);
        var button1 = new dijit.form.Button({label: "Ok", onClick: onYes, iconClass:"darwinApplyButtonIcon" });
        var button2 = new dijit.form.Button({label: "Annulla", onClick: onCancel, iconClass:"darwinCancelButtonIcon" });
        toolbar.appendChild(button1.domNode);
        toolbar.appendChild(button2.domNode);
    },
    
    /**
     * Richiama la show del parent e poi inizializza
     */
    show: function() {
        this.inherited(arguments);
        this._init();
    },
    
    onYes : function(){}
});


dojo.declare("dojo.enabu.main.widget.CommentEditPopUp", dojo.enabu.main.widget.RoundModalPopUp, {
    
    contentContainer : '<div dojoAttachPoint="containerNode"></div>',
    toolbarButtons : '<div class="toolbar" id="commentEditPopupId" style="text-align: center; padding-top:10px;"></div>',
    
    constructor : function(params, parent, selectionManager, isNew) {
        this.selectionManager = selectionManager;
        this.isNew = isNew;
        this.contentContainer = '<div dojoAttachPoint="containerNode"><div class="title"  style="font-size:12px; font-family:verdana; padding-bottom: 6px;" dojoAttachPoint="labelNode"></div>' +
                                '<div class="description" style="width: 400px"><textarea rows="5" id="' + this.selectionManager.EDIT_TEXT_AREA_ID + '" style="width: 98%" height></textarea></div></div>';
        this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons +  this.templateFooter;
        
        
    },
                     
    isNew : true,

    getValue : function() {
        return document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value;  
    },
    
    setComment : function(comment) {
        this.isNew = false;
        this.comment = comment;
        document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value = comment.content;
        
        
    },
    
    _init : function() {
    	var toolbar = dojo.byId('commentEditPopupId');
    	var onEdit = dojo.hitch(this, this.onEdit);
        var onCancel = dojo.hitch(this, this.onCancel);
    	var button1 = new dijit.form.Button({label: "Ok", onClick: onEdit, iconClass:"darwinApplyButtonIcon" });
    	var button2 = new dijit.form.Button({label: "Annulla", onClick: onCancel, iconClass:"darwinCancelButtonIcon" });
    	toolbar.appendChild(button1.domNode);
    	toolbar.appendChild(button2.domNode);
    },
    
    onCancel: function(){
        this.selectionManager.editNoteCancel();
    },
    
    onEdit: function(){
        if(this.isNew) {
            this.selectionManager.addCommentOk();
        } else {
            this.selectionManager.editNoteOk(this.comment, this.getValue());
        }
    },

    /**
     * Richiama la show del parent e poi inizializza
     */
    show: function() {
        this.inherited(arguments);
        this._init();
    },
    
    attributeMap: dojo.mixin(dojo.clone(dojo.enabu.main.widget.RoundModalPopUp.prototype.attributeMap),  
            {label: [{node: "labelNode", type: "innerHTML"}]})
    
});

dojo.declare("dojo.enabu.main.widget.BookmarkEditPopUp", dojo.enabu.main.widget.RoundModalPopUp, {
	
	contentContainer : '<div dojoAttachPoint="containerNode"></div>',
	toolbarButtons : '<div class="toolbar" id="bookmarkEditPopupId" style="text-align: center; padding-top:10px;"></div>',
	
	constructor : function(params, parent, selectionManager, isNew) {
		this.selectionManager = selectionManager;
		this.isNew = isNew;
		this.contentContainer = '<div dojoAttachPoint="containerNode"><div class="title"  style="font-size:12px; font-family:verdana; padding-bottom: 6px;" dojoAttachPoint="labelNode"></div>' +
		'<div class="description" style="width: 400px"><input type="text" id="' + this.selectionManager.EDIT_TEXT_AREA_ID + '" style="width: 98%" size="50" maxlength="50" /></div></div>';
		this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons +  this.templateFooter;
	},
	
	isNew : true,
	
	getValue : function() {
		return document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value;  
	},
	
	setBookmark : function(bookmark) {
		this.isNew = false;
		this.bookmark = bookmark;
		document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value = bookmark.content;
	},
	
	_init : function() {
		var toolbar = dojo.byId('bookmarkEditPopupId');
		var onEdit = dojo.hitch(this, this.onEdit);
		var onCancel = dojo.hitch(this, this.onCancel);
		var button1 = new dijit.form.Button({label: "Ok", onClick: onEdit, iconClass:"darwinApplyButtonIcon" });
		var button2 = new dijit.form.Button({label: "Annulla", onClick: onCancel, iconClass:"darwinCancelButtonIcon" });
		toolbar.appendChild(button1.domNode);
		toolbar.appendChild(button2.domNode);
	},
	
	onCancel: function(){
		this.selectionManager.editNoteCancel();
	},
	
	onEdit: function(){
		if(this.isNew) {
			this.selectionManager.addBookmarkOk();
		} else {
			this.selectionManager.editNoteOk(this.bookmark, this.getValue());
		}
	},
	
	/**
	 * Richiama la show del parent e poi inizializza
	 */
	show: function() {
		this.inherited(arguments);
		this._init();
	},
	
	attributeMap: dojo.mixin(dojo.clone(dojo.enabu.main.widget.RoundModalPopUp.prototype.attributeMap),  
			{label: [{node: "labelNode", type: "innerHTML"}]})
		
});

dojo.declare("dojo.enabu.main.widget.ProfileBookmarkEditPopUp", dojo.enabu.main.widget.RoundModalPopUp, {
	
	contentContainer : '<div dojoAttachPoint="containerNode"></div>',
	toolbarButtons : '<div class="toolbar" id="bookmarkEditPopupId" style="text-align: center; padding-top:10px;"></div>',
	
	constructor : function(params, parent, selectionManager, isNew) {
		this.selectionManager = selectionManager;
		this.isNew = isNew;
		this.contentContainer = '<div dojoAttachPoint="containerNode"><div class="title"  style="font-size:12px; font-family:verdana; padding-bottom: 6px;" dojoAttachPoint="labelNode"></div>' +
		'<div class="description" style="width: 400px"><input type="text" id="' + this.selectionManager.EDIT_TEXT_AREA_ID + '" style="width: 98%" size="50" maxlength="50" /></div></div>';
		this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons +  this.templateFooter;
	},
	
	isNew : false,
	
	getValue : function() {
		return document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value;  
	},
	
	setIdBookmark : function(idBookmark) {
		this.idBookmark = idBookmark;
	},
	
	setBookmarkContent : function(content) {
		document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value = content;
	},
	
	_init : function() {
		var toolbar = dojo.byId('bookmarkEditPopupId');
		var onEdit = dojo.hitch(this, this.onEdit);
		var onCancel = dojo.hitch(this, this.onCancel);
		var button1 = new dijit.form.Button({label: "Ok", onClick: onEdit, iconClass:"darwinApplyButtonIcon" });
		var button2 = new dijit.form.Button({label: "Annulla", onClick: onCancel, iconClass:"darwinCancelButtonIcon" });
		toolbar.appendChild(button1.domNode);
		toolbar.appendChild(button2.domNode);
	},
	
	onCancel: function(){
	},
	
	onEdit: function(){
	},
	
	/**
	 * Richiama la show del parent e poi inizializza
	 */
	show: function() {
		this.inherited(arguments);
		this._init();
	},
	
	attributeMap: dojo.mixin(dojo.clone(dojo.enabu.main.widget.RoundModalPopUp.prototype.attributeMap),  
			{label: [{node: "labelNode", type: "innerHTML"}]})
		
});

dojo.declare("dojo.enabu.main.widget.CitePopUp", dojo.enabu.main.widget.RoundModalPopUp, {
    
    contentContainer : '<div dojoAttachPoint="containerNode"></div>',
    toolbarButtons : '<div class="toolbar" id="commentEditPopupId" style="text-align: center; padding-top:10px;"></div>',
    
    constructor : function(params, parent, selectionManager) {
        this.selectionManager = selectionManager;
        this.contentContainer = '<div dojoAttachPoint="containerNode" class="howToCitePopUp"><div dojoAttachPoint="selectedTextNode" class="selected-text"></div>' +
                                '<div dojoAttachPoint="citeContentNode" class="cite-content"></div>' +
                                '<div dojoAttachPoint="chapterStringNode" class="chapter"></div>' +
                                '<div dojoAttachPoint="noteNode" class="cite-note" style="width: 400px"></div></div>';
                                
            
        this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons +  this.templateFooter;
    },
                     
    setComment : function(comment) {
        this.isNew = false;
        this.comment = comment;
        document.getElementById(this.selectionManager.EDIT_TEXT_AREA_ID).value = comment.content;
    },
    
    _init : function() {
        var toolbar = dojo.byId('commentEditPopupId');
        var onEdit = dojo.hitch(this, this.onEdit);
        var onCancel = dojo.hitch(this, this.onCancel);
        var button = new dijit.form.Button({label: "Ok", onClick: onCancel, iconClass:"darwinApplyButtonIcon" });
        toolbar.appendChild(button.domNode);
    },
    
    onCancel: function(){
        this.destroy();
    },
    
    /**
     * Richiama la show del parent e poi inizializza
     */
    show: function() {
        this.inherited(arguments);
        this._init();
    },
    
    attributeMap: dojo.mixin(dojo.clone(dojo.enabu.main.widget.RoundModalPopUp.prototype.attributeMap),  
            {note: [{node: "noteNode", type: "innerHTML"}]}, 
            {chapterString: [{node: "chapterStringNode", type: "innerHTML"}]},
            {citeContent: [{node: "citeContentNode", type: "innerHTML"}]},
            {selectedText: [{node: "selectedTextNode", type: "innerHTML"}]}
            )
    
});


dojo.declare("dojo.enabu.main.widget.LogToCommentPopUp", dojo.enabu.main.widget.RoundModalPopUp, {
    
    contentContainer : '<div dojoAttachPoint="containerNode"></div>',
    toolbarButtons : '<div class="toolbar" id="commentEditPopupId" style="text-align: center; padding-top:10px;"></div>',
    
    constructor : function(params, parent, selectionManager) {
        this.selectionManager = selectionManager;
        this.contentContainer = '<div dojoAttachPoint="containerNode"><form id="logToCommentPopUpFrmLogin" method="get" action="/main/login"></form><form id="logToCommentPopUpFrmRegister" target="_new" method="get" action="/main/register"></form>' +
                                '<div class="">Solo gli utenti registrati possono inserire commenti al testo e aggiungere bookmark. Nel caso tu abbia gi&agrave; un account personale su Darwinbooks premi il bottone "Login" altrimenti, il bottone "Registrati" per creare un proprio profilo all\'interno del sistema.</div>' +
                                '<div class=""></div>' +
                                '</div>';
                                
            
        this.templateString = this.templateHead + this.contentContainer + this.toolbarButtons +  this.templateFooter;
    },
                     
    _init : function() {
        var toolbar = dojo.byId('commentEditPopupId');
        var onEdit = dojo.hitch(this, this.onEdit);
        var onLogin = dojo.hitch(this, this.onLogin);
        var button = new dijit.form.Button({label: "Login", onClick: onLogin, iconClass:"darwinLoginButtonIcon" });
        toolbar.appendChild(button.domNode);
        var onRegister = dojo.hitch(this, this.onRegister);
        var button = new dijit.form.Button({label: "Registrati", onClick: onRegister, iconClass:"darwinRegisterButtonIcon" });
        toolbar.appendChild(button.domNode);
        var onCancel = dojo.hitch(this, this.onCancel);
        var button = new dijit.form.Button({label: "Annulla", onClick: onCancel, iconClass:"darwinCancelButtonIcon" });
        toolbar.appendChild(button.domNode);
    },
    
    onLogin: function(){
        document.frmUserLogin.submit(); 
        return false;
    },

    onCancel: function(){
        this.destroy();
    },
    
    onRegister: function(){
        var form = dojo.byId('logToCommentPopUpFrmRegister');
        form.submit();
        this.destroy();
    },
    
    /**
     * Richiama la show del parent e poi inizializza
     */
    show: function() {
        this.inherited(arguments);
        this._init();
    }
});