/* ContactPop_residential jQuery Plugin
 *
 * By Jon Raasch
 * http://jonraasch.com
 *
 * Copyright (c)2009 Jon Raasch. All rights reserved.
 * Released under FreeBSD License, see readme.txt
 * Do not remove the above copyright notice or text.
 *
 * For more information please visit: 
 * http://jonraasch.com/blog/contact-pop-jquery-plugin
*/
ContactPop_residential = {
    /************ config **************/
    
    // make sure to keep the trailing comma after each of these variable definitions
    
    replaceHref : 'contact_residential.php', // can be array or string of hrefs or nothing if you want to use jQuery selectors (below)
    
    formPhpLocation : 'post_req/contact_residential.php', // relative path to the backend contact form
    
    pathToContactPop : 'Contact-Pop', // relative path to the Contact-Pop directory
    
    contactHeadline : 'Post Residential Requirement',
    headerBgColor : '#777777', // background color of overlay panel header
    
    overlayFadeIn : 600, // overlay fade in speed (milliseconds)
    overlayFadeOut : 500, // overlay fade out speed (milliseconds)
    
    overlayEasing : '', // if you install the easing plugin (http://gsgd.co.uk/sandbox/jquery/easing/), the info goes here, example: 'easeInOutQuad'
    
    openButtonSelector : '', // set this to use jQuery selectors in addition to the hrefs - string (ex: '.contact, #contact-link')
    closeButtonSelector : '.close-overlay', // this works with any jQuery selector - string (ex: '#close-button, .close')
    
    resetFormEachTime : 0, // resets the form if the overlay is hidden and shown again
    
    fadeOverlayIE : 0, // default off - in IE 7/8 alpha transparency flashes black when ffaded
    fadeOverlayIE6 : 0, // default off - for performance
    
    
    
    /********** end config ************/
    
    obj : {},
    formFields : {},
    submitEvent : 0,
    overlayFade : 1,
    
    appendOverlay : function() {
        // append overlay and panel divs
        ContactPop_residential.obj.overlay = jQuery('<div id="contact-pop-overlay"></div>').appendTo( jQuery('BODY') );
        
        ContactPop_residential.obj.panelWrapper = jQuery('<div id="contact-pop-panel-wrapper"></div>').appendTo( ContactPop_residential.obj.overlay );
        
        ContactPop_residential.obj.panel = jQuery('<div id="contact-pop-panel"></div>').appendTo( ContactPop_residential.obj.panelWrapper );
        
        // append panel headline
        ContactPop_residential.obj.panelHeadline = $( '<h2 id="contact-pop-header">' + ContactPop_residential.contactHeadline + '</h2>') . appendTo( ContactPop_residential.obj.panel );
        
        // set panel headline background color
        if ( ContactPop_residential.headerBgColor != '#777777' ) ContactPop_residential.obj.panelHeadline.css( 'backgroundColor', ContactPop_residential.headerBgColor );
        
        // append panel header close button
        ContactPop_residential.obj.panelHeadline.append( '<a href="#" class="close-overlay">X</a></h2>' );
        
        // append form
        ContactPop_residential.obj.form = jQuery('<form action="' + ContactPop_residential.formPhpLocation + '" method="post" id="contact-pop-form"></div>').appendTo( ContactPop_residential.obj.panel );
        
        // append loading graphic
        ContactPop_residential.obj.loading = jQuery('<div id="contact-pop-loading-gif-wrapper"></div>').appendTo( ContactPop_residential.obj.panel );
        
        ContactPop_residential.obj.loading.append('<img src="' + ContactPop_residential.pathToContactPop + 'img/ajax-loader.gif" alt="" id="contact-pop-loading-gif" />');
    },

    getFormContent : function() {
        // grab form html using jQuery's AJAX API
        jQuery.get( ContactPop_residential.formPhpLocation, { 'ajaxForm' : 1 },  function(html) {                
            if ( html ) {
                ContactPop_residential.obj.loading.fadeOut(200);
                ContactPop_residential.obj.form.html( html );
                ContactPop_residential.attachFormEvents();
            }
        });
    },
    
    attachFormEvents : function() {
        // close buttons
        jQuery( ContactPop_residential.closeButtonSelector, ContactPop_residential.obj.panel).click( function(ev) {
            ev.preventDefault();
            ContactPop_residential.hideOverlay();
        });
        
        // attach submit event each time for IE
        if ( jQuery.browser.msie ) {
            jQuery('input.submit', ContactPop_residential.obj.form).click( function(ev) {
                ev.preventDefault();                
                ContactPop_residential.submitForm();
            });
        }
        // only attach submit event once for other browsers
        else if ( !ContactPop_residential.submitEvent ) {            
            ContactPop_residential.obj.form.submit( function(ev) {
                ev.preventDefault();                
                ContactPop_residential.submitForm();
            });
            
            ContactPop_residential.submitEvent = 1;
        }
    },
    
    checkOverlayFade : function() {
        if ( $.browser.msie && !ContactPop_residential.fadeOverlayIE && !( $.browser.version < 7 && ContactPop_residential.fadeOverlayIE6 ) ) return false;
        else return true;
    },
    
    showOverlay : function() {
        // if first time append the overlay and get the form content
        if ( typeof(ContactPop_residential.obj.overlay) == 'undefined' ) {         
            ContactPop_residential.appendOverlay();
            ContactPop_residential.getFormContent();
        }
        else if ( ContactPop_residential.resetFormEachTime ) ContactPop_residential.getFormContent();
        
        if ( ContactPop_residential.overlayFade ) ContactPop_residential.obj.overlay.fadeIn( ContactPop_residential.overlayFadeOut, ContactPop_residential.overlayEasing );
        else ContactPop_residential.obj.overlay.show();
    },
    
    hideOverlay : function() {
        if ( ContactPop_residential.overlayFade ) ContactPop_residential.obj.overlay.fadeOut( ContactPop_residential.overlayFadeIn, ContactPop_residential.overlayEasing );
        else ContactPop_residential.obj.overlay.hide();
    },
    
    submitForm : function() {
        // add form fields to array
         jQuery('input, select, textarea', ContactPop_residential.obj.form).each( function() {
            ContactPop_residential.addFormField( jQuery(this) );
         });
         
         // set the ajaxForm post value
         ContactPop_residential.formFields['ajaxForm'] = 1;
         
         // fade in the loading graphic
         ContactPop_residential.obj.form.fadeOut(200);
         ContactPop_residential.obj.loading.fadeIn(200);
         
         // post the form with jQuery's AJAX API
        jQuery.post( ContactPop_residential.formPhpLocation, ContactPop_residential.formFields, function(html) {                
            if ( html ) {
                ContactPop_residential.obj.form.html( html );
                ContactPop_residential.obj.loading.fadeOut(200);
                ContactPop_residential.obj.form.fadeIn(200);
                
                ContactPop_residential.attachFormEvents();
            }
          });
    },
    
    addFormField : function( $field ) {
        var fieldName = $field.attr('name');
        if ( fieldName ) ContactPop_residential.formFields[ fieldName ] = $field.val();
    },
    
    init : function() {
        var anchorSelector = '';
        // force array
        if ( typeof( ContactPop_residential.replaceHref ) != 'object' ) ContactPop_residential.replaceHref = [ ContactPop_residential.replaceHref ];
        
        // add anchor selectors
        for ( var i = 0; i < ContactPop_residential.replaceHref.length; i++ ) {
            if ( ContactPop_residential.replaceHref[i] ) anchorSelector += 'a[href=' + ContactPop_residential.replaceHref[i] + '], ';
        }
        
        // add  additional jQuery selectors
        if ( ContactPop_residential.openButtonSelector ) anchorSelector += ContactPop_residential.openButtonSelector;
        else anchorSelector = anchorSelector.substr(0, anchorSelector.length - 2);

        // define ctas and click event
        ContactPop_residential.obj.ctas = jQuery(anchorSelector);
        
        ContactPop_residential.obj.ctas.click( function(ev) {
            ev.preventDefault();
            ContactPop_residential.showOverlay();
        });
        
        // determine if fading overlay or just hide/showing
        ContactPop_residential.overlayFade = ContactPop_residential.checkOverlayFade();
        
        // preload overlay image - keep this in the init() function so the rest of the page loads first
        
        var overlayImg = new Image();
        if ( jQuery.browser.msie && jQuery.browser.version < 7 ) overlayImg.src = ContactPop_residential.pathToContactPop + '/img/overlay-ie6.png';
        
        else overlayImg.src = ContactPop_residential.pathToContactPop + '/img/overlay.png';
    }
};

jQuery(function() {
    // initiate ContactPop_residential once the page loads
    ContactPop_residential.init();
});
