

var Comments = function(){
    var dialog, postLink, viewLink, txtComment;
    var tabs, commentsList, postBtn, renderer;
    var wait, error, errorMsg;
    var posting = false;
    
    return {
        init : function(){
             // cache some elements for quick access
             txtComment = Ext.get('comment');
             postLink = Ext.get('post-comment');
             wait = Ext.get('comment-post-wait');
             error = Ext.get('comment-post-error');
             errorMsg = Ext.get('comment-post-error-msg');
             
             this.createDialog();
             
             postLink.on('click', function(e){
                 e.stopEvent();
                 tabs.activate('post-tab');
                 dialog.show(postLink);
             });
             
        },
        
        // submit the comment to the server
        submitComment : function(){
            postBtn.disable();
            wait.radioClass('active-msg');
            
            var commentSuccess = function(o){
                postBtn.enable();
                var div = document.getElementById('user_reviews');
                div.innerHTML = o.responseText;
				dialog.hide();
            };
            
            var commentFailure = function(o){
                postBtn.enable();
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
            

            
            var formRequest = function(form, uri, cb, data, isUpload, sslUri){
	            Ext.lib.Ajax.request('POST', uri, cb, Ext.Ajax.serializeForm(form));
			};
			
            formRequest('comment-form', 'ajax.php',
                    {success: commentSuccess, failure: commentFailure});          
        },
        
        createDialog : function(){
            dialog = new Ext.BasicDialog("comments-dlg", { 
                    autoTabs:true,
                    width:500,
                    height:400,
                    shadow:true,
                    minWidth:300,
                    minHeight:300
            });
            dialog.addKeyListener(27, dialog.hide, dialog);
            postBtn = dialog.addButton('Post Review', this.submitComment, this);
            dialog.addButton('Cancel', dialog.hide, dialog);

            
            // clear any messages and indicators when the dialog is closed
            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
                txtComment.dom.value = '';
            });
            
            // stoe a refeence to the tabs
            tabs = dialog.getTabs();
            
            // auto fit the comment box to the dialog size
            var sizeTextBox = function(){
                txtComment.setSize(dialog.size.width-44, dialog.size.height-264);
            };
            sizeTextBox();
            dialog.on('resize', sizeTextBox);
            
            // hide the post button if not on Post tab
            tabs.on('tabchange', function(panel, tab){
                postBtn.setVisible(tab.id == 'post-tab');
            });            
        }
    };
}();


Ext.EventManager.onDocumentReady(Comments.init, Comments, true);


var EmailRecipe = function(){
    var dialog, postLink, viewLink, txtComment;
    var tabs, commentsList, postBtn, renderer;
    var wait, error, errorMsg;
    var posting = false;
    
    return {
        init : function(){
             // cache some elements for quick access
             txtComment = Ext.get('emailrecipe');
             postLink = Ext.get('post-emailrecipe');
             wait = Ext.get('emailrecipe-post-wait');
             error = Ext.get('emailrecipe-post-error');
             errorMsg = Ext.get('emailrecipe-post-error-msg');
             
             this.createDialog();
             
             postLink.on('click', function(e){
                 e.stopEvent();
                 tabs.activate('emailrecipe-tab');
                 dialog.show(postLink);
             });
             
        },
        
        // submit the comment to the server
        submitComment : function(){
            postBtn.disable();
            wait.radioClass('active-msg');
            
            var commentSuccess = function(o){
                postBtn.enable();
                //var div = document.getElementById('user_reviews');
                //div.innerHTML = o.responseText;
                error.radioClass('active-msg');
                errorMsg.update(o.responseText);
            	setTimeout(function(){dialog.hide();},2000);
                
				
            };
            
            var commentFailure = function(o){
                postBtn.enable();
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
            

            
            var formRequest = function(form, uri, cb, data, isUpload, sslUri){
	            Ext.lib.Ajax.request('POST', uri, cb, Ext.Ajax.serializeForm(form));
			};
			
            formRequest('emailrecipe-form', 'ajax.php',
                    {success: commentSuccess, failure: commentFailure});          
        },
        
        createDialog : function(){
            dialog = new Ext.BasicDialog("emailrecipe-dlg", { 
                    autoTabs:true,
                    width:500,
                    height:400,
                    shadow:true,
                    minWidth:300,
                    minHeight:300
            });
            dialog.addKeyListener(27, dialog.hide, dialog);
            postBtn = dialog.addButton('Send Email', this.submitComment, this);
            dialog.addButton('Cancel', dialog.hide, dialog);

            
            // clear any messages and indicators when the dialog is closed
            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
                txtComment.dom.value = '';
            });
            
            // stoe a refeence to the tabs
            tabs = dialog.getTabs();
            
            // auto fit the comment box to the dialog size
            var sizeTextBox = function(){
                txtComment.setSize(dialog.size.width-44, dialog.size.height-264);
            };
            sizeTextBox();
            dialog.on('resize', sizeTextBox);
            
            // hide the post button if not on Post tab
            tabs.on('tabchange', function(panel, tab){
                postBtn.setVisible(tab.id == 'post-tab');
            });            
        }
    };
}();


Ext.EventManager.onDocumentReady(EmailRecipe.init, EmailRecipe, true);


var RecipeBox = function(){
    var dialog, postLink, viewLink, txtComment;
    var tabs, commentsList, postBtn, renderer;
    var wait, error, errorMsg;
    var posting = false;
    
    return {
        init : function(){
             // cache some elements for quick access
             txtComment = Ext.get('recipe-note');
             postLink = Ext.get('save-recipe');
             wait = Ext.get('recipebox-post-wait');
             error = Ext.get('recipebox-post-error');
             errorMsg = Ext.get('recipebox-post-error-msg');
             
             this.createDialog();
             
             postLink.on('click', function(e){
                 e.stopEvent();
                 tabs.activate('recipebox-tab');
                 dialog.show(postLink);
             });
             
        },
        
        // submit the comment to the server
        submitComment : function(){
            postBtn.disable();
            wait.radioClass('active-msg');
            
            var commentSuccess = function(o){
                postBtn.enable();
                //var div = document.getElementById('user_reviews');
                //div.innerHTML = o.responseText;
                error.radioClass('active-msg');
                errorMsg.update(o.responseText);
            	setTimeout(function(){dialog.hide();},2000);
				
            };
            
            var commentFailure = function(o){
                postBtn.enable();
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
            

            
            var formRequest = function(form, uri, cb, data, isUpload, sslUri){
	            Ext.lib.Ajax.request('POST', uri, cb, Ext.Ajax.serializeForm(form));
			};
			
            formRequest('recipebox-form', 'ajax.php',
                    {success: commentSuccess, failure: commentFailure});          
        },
        
        createDialog : function(){
            dialog = new Ext.BasicDialog("recipebox-dlg", { 
                    autoTabs:true,
                    width:500,
                    height:400,
                    shadow:true,
                    minWidth:300,
                    minHeight:300
            });
            dialog.addKeyListener(27, dialog.hide, dialog);
            postBtn = dialog.addButton('Save To Recipebox', this.submitComment, this);
            dialog.addButton('Cancel', dialog.hide, dialog);

            
            // clear any messages and indicators when the dialog is closed
            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
                txtComment.dom.value = '';
            });
            
            // stoe a refeence to the tabs
            tabs = dialog.getTabs();
            
            // auto fit the comment box to the dialog size
            var sizeTextBox = function(){
                txtComment.setSize(dialog.size.width-44, dialog.size.height-264);
            };
            sizeTextBox();
            dialog.on('resize', sizeTextBox);
            
            // hide the post button if not on Post tab
            tabs.on('tabchange', function(panel, tab){
                postBtn.setVisible(tab.id == 'post-tab');
            });            
        }
    };
}();


Ext.EventManager.onDocumentReady(RecipeBox.init, RecipeBox, true);
