/**
 * modalFeedback.js
 * @author M Chudy
 * @copyright 2011 DCG
 */
document.addEvent('domready', function(e){
    var CONTACT_PATH = 'contact.php';
    $$('a').each(function(link){
        if($(link).href.indexOf(CONTACT_PATH)>-1){
            $(link).addEvent('click', function(e){
                e.stop(); // stop default hyperlink action
                buildFeedbackBox();
            });
        }
    });

    function buildFeedbackBox(){
        var loaderImage = new Asset.image('staticFiles/images/bar_loader.gif');
        destroyFeedbackBoxes();
        var outerFeedbackBox = new Element('div', {
            'class':'modalFeedback_outerFeedbackBox',
            'styles':{
                'width':'500px',
                'top':((window.getScroll().y + 100) + 'px'),
                'left':(Math.round((window.getSize().x - 500)/2) + 'px')
            }
        });

        var formHouse = new Element('div', {
            'class':'modalFeedback_formHouse'
        });
        formHouse.appendChild(new Element('h2', {
            'html':'Contact DCG',
            'id':'contactFormHouseHeader',
            'styles':{
                'cursor':'move'
            }
        }));
        formHouse.appendChild(new Element('p', {
            
        'html':'We would love to hear from you.  Please feel free to contact us using the form below.'
        
        }));

        var closer = new Element('div', {
            'html':'[x]',
            'class':'modalFeedback_closeTrigger'
        });
        outerFeedbackBox.appendChild(closer);
        outerFeedbackBox.appendChild(formHouse);
        document.body.appendChild(outerFeedbackBox);

        new Request({
            method: 'get',
            url: 'getContactForm.php',
            data: {
                'rand_d' : Math.random()
            },
            onRequest: function() { 
                var loaderHouse = new Element('div', {
                    'id':'loaderBarHouse',
                    'styles':{
                        'text-align':'center'
                    }
                });
                loaderHouse.appendChild($(loaderImage).clone());
                formHouse.appendChild(loaderHouse);
            },
            onComplete: function(response) {
                if($('loaderBarHouse')!= null){
                    $('loaderBarHouse').dispose();
                }
                formHouse.appendChild(new Element('div', {
                    'html':response,
                    'id':'formContent'
                }));
                if($('contactReferrer') != null){
                    $('contactReferrer').value = window.location;
                }
                validateContactForm();
                $$('.feedback_field').each(function(item){
                    $(item).addEvent('keyup', function(e){
                        validateContactForm();
                    });
                });
                var closerFunction = function(){
                    destroyFeedbackBoxes()
                }
                $$('.contactFormSubmitter').each(function(item){
                    $(item).addEvent('click', function(e){
                        e.stop();
                        if(validateContactForm()){
                            $('contactForm').set('action','postContactForm.php');
                            new Form.Request($('contactForm'), $('formContent'), {
                                onSend:function(){
                                    //formHouse.innerHTML = '';
                                    var loaderHouse = new Element('div', {
                                        'id':'loaderBarHouse',
                                        'styles':{
                                            'text-align':'center'
                                        },
                                        'html':'Processing your Feedback...<br />'
                                    });
                                    loaderHouse.appendChild($(loaderImage).clone());
                                    $('formContent').appendChild(loaderHouse);
                                },
                                onSuccess:function(){
                                    if($('loaderBarHouse')!= null){
                                        $('loaderBarHouse').dispose();
                                    }
                                    setTimeout(closerFunction, 3000);
                                }
                            }).send();
                        }
                    });
                });
            }
        }).send();
        
        $(closer).addEvent('click', function(e){
            destroyFeedbackBoxes();
        });
        new Drag($(outerFeedbackBox),{
            'handle':'contactFormHouseHeader'
        } );
    }
    
    function destroyFeedbackBoxes(){
        $$('div.modalFeedback_outerFeedbackBox').each(function(item){
            $(item).fade('out');
        });
    }
});
