modalAlert = Class.create({
  initialize: function(message, link_element, url, params, redirect, options) {

    this.message  = message;
    this.link_element = link_element;
    this.url = url;
	this.params = $H(params);
	this.modal;
	this.redirect = redirect;
	
	this.options = {
	    type: 'confirm'
     };

        // this line extends the options to overide the default options.
        Object.extend(this.options, options || {});

        // load up the modal window object
        this.modal = new Control.Modal($(this.link_element),{  
            overlayOpacity: 0.1,
            className: 'modal',  
            //fade: true,
            //height: 100,
            //width: 200,	
            closeOnClick: false
         }); 

        this.modal.container.insert(this.getMessage()); 
        
        // observe the close
        Event.observe($(this.link_element+'_closeModal'), 'click', this.closeModal.bind(this));
        
        // observe the OK and call the url with the params
        if($(this.link_element+'_okModal'))
        {
            Event.observe($(this.link_element+'_okModal'), 'click', this.okModal.bind(this));
        }
    },

    getMessage: function() {
       // build the message to return to the box
       /*var message = this.message;
       
       var elementHtml = Builder.node('div', {class: 'action-modal'}, [
               Builder.node('h4', this.message),
               Builder.node('br'),
               Builder.node('div', {id: this.link_element+'_okModal', class: 'greenBtn'}, [
                   Builder.node('a', 'OK')]),
               Builder.node('div', {id: this.link_element+'_closeModal', class: 'greenBtn'}, [
                   Builder.node('a', 'Cancel')]),
       ]);*/
       
       if(this.options.type == 'confirm')
       {
           var elementHtml = '<div class="action-modal"><h4 id="message-text">'+this.message+'</h4><div id="'+this.link_element+'_okModal" class="greenBtn"><a>OK</a></div><div id="'+this.link_element+'_closeModal" class="redBtn"><a>Cancel</a></div></div>';
       }
       else if(this.options.type == 'alert')
       {
           var elementHtml = '<div class="action-modal"><h4 id="message-text">'+this.message+'</h4><div id="'+this.link_element+'_closeModal" class="greenBtn"><a>OK</a></div></div>';      
       }
       else if(this.options.type == 'flag')
       {
           var elementHtml = '<div class="action-modal"><h2 id="message-text">Flag message here!</h2><p><textarea name="reason" id="reason"></textarea></p><div id="'+this.link_element+'_okModal" class="greenBtn"><a>Send</a></div><div id="'+this.link_element+'_closeModal" class="redBtn"><a>Cancel</a></div></div>';          
       }
       else if(this.options.type == 'pm')
       {
           var elementHtml = '<div class="action-modal"><h2>Please enter your message.</h2><p style="display:none" id="error-text-'+this.link_element+'" class="error"></p><p><b>To:</b> '+this.message+'</p><p><b><b>Subject:</b> </b> <input name="subject" id="subject" type="text" /></p><p><textarea name="messge" id="message"></textarea></p><div id="'+this.link_element+'_okModal" class="greenBtn"><a>Send</a></div><div id="'+this.link_element+'_closeModal" class="redBtn"><a>Cancel</a></div></div>';                   
       }
       else if(this.options.type == 'login')
       {
           var elementHtml = '<div class="action-modal login-modal" id="login-fbconnect"><h1 id="fb-title">Please login using one of the following options</h1><ul id="login-tabs"><li class="tab"><a href="#one" id="fb-link">Facebook login</a></li><li class="tab"><a href="#two"  id="op-link">Original Poetry login</a></li></ul><div id="one" class="content"><p><strong>Use your Facebook account to login to Original Poetry</strong><br />If you currently have a Facebook account, login could not be easier. <br />Follow a few prompts to quickly integrate your Facebook account with Original Poetry.</p><p><fb:login-button length="long" onlogin="facebook_onlogin();" background="light" size="medium"></fb:login-button></p></div><div id="two" class="content"><p style="display:none" id="error-text-login" class="error"></p><p><b>Username:</b> <input name="username" id="username" type="text" /></p><p><b><b>Password:</b> </b> <input name="password" id="password" type="password" /></p><div id="'+this.link_element+'_okModal" class="greenBtn"><a>Login</a></div><p><a href="http://www.originalpoetry.com/login/forgot-password/">Forgot your password?</a></p></div></div><div id="cancel-footer"><div id="'+this.link_element+'_closeModal" class="redBtn"><a>Cancel</a></div>Not registered with Original Poetry or Facebook? <a href="http://www.originalpoetry.com/register/">Register now.</a></div><script>FB.XFBML.Host.parseDomTree(); </script><script>new Control.Tabs(\'login-tabs\');</script>';                         
       }
       else if(this.options.type == 'login-fb')
       {
           var elementHtml = '<div class="action-modal" id="fb-login"><h3>Please login to Original Poetry to begin <br />merging your account with Facebook</h3><p style="display:none" id="error-text-login" class="error"></p><p><b>Username:</b> <input name="username" id="username2" type="text" /></p><p><b><b>Password:</b> </b> <input name="password" id="password" type="password" /></p><div id="'+this.link_element+'_okModal" class="greenBtn"><a>Login</a></div><p><a href="http://www.originalpoetry.com/login/forgot-password/">Forgot your password?</a></p></div></div><div id="cancel-footer"><div id="'+this.link_element+'_closeModal" class="redBtn"><a>Cancel</a></div>Not registered with Original Poetry? <a href="http://www.originalpoetry.com/register/">Register now.</a></div>';                         
       }
       
       return elementHtml;
    },
    


    closeModal: function(){
        this.modal.close();
    },
    
    afterComplete: function(transport){
        
        // get the responce text
        var res = transport.responseJSON;
        
        // check for fial messges
        if(res.status == 'fail')
        {
            if(this.options.type == 'pm')
            {
                // get the error string and trplace the | with break likes 
                var errorString = res.msg.gsub('|', '<br>');
                $('error-text-'+this.link_element).show();
                $('error-text-'+this.link_element).update(errorString);
            }
            else if((this.options.type == 'login-fb') || (this.options.type == 'login'))
            {
                // get the error messages and show them
                $('error-text-login').show();
                $('error-text-login').update(res['errorMessages']);
            }
            else
            {
            
                // display the fail message
                $('message-text').update(res.msg);
            
                if($(this.link_element+'_okModal'))
                {
                    // revove the ok button
                    $(this.link_element+'_okModal').remove();
                }
                
            }
        }
        else
        {
            // check to see if there is a redirect after the complete if not just close the window
            if(this.redirect == 'reload')
            {
                window.location.reload();
            }
            else if(this.redirect != false)
            {
                window.location = this.redirect;
            }
            
            this.modal.close();       
        }
        
    
    },
    
    okModal: function(){
        // set other params
        if(this.options.type == 'flag')
        {
            this.params.set('reason', $F('reason'));
        }
        else if(this.options.type == 'pm')
        {
            this.params.set('subject', $F('subject'));
            this.params.set('message', $F('message'));
        }
        else if(this.options.type == 'login-fb')
        {
            this.params.set('username', $('username2').value);
            this.params.set('password', $('password').value);     
        }
        else if(this.options.type == 'login')
        {
            this.params.set('username', $('username').value);
            this.params.set('password', $('password').value);     
        }

          new Ajax.Request(this.url, {
        method: 'post',
        parameters: this.params,
        onComplete: this.afterComplete.bind(this)
    });
    }
});