/* ModalOverlay - By Sergio Ruiz Aragón (AKA Raferner)
 * 
 * Base on ContactPop 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
*/
GeneralInfoPop = {
	/************ CONFIG **************/
    // make sure to keep the trailing comma after each of these variable definitions
	
    replaceHref : '', // can be array or string of hrefs or nothing if you want to use jQuery selectors (below)
    formPhpLocation : 'register-overlay/pop-informacion-general.php', // relative path to the backend contact form
    pathToGeneralInfoPop : '', // relative path to the GeneralInfo-Pop directory
	
    infoHeadline : ' Vuelosaunclick ',
    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 : '.info_general', // set this to use jQuery selectors in addition to the hrefs - string (ex: '.contact, #contact-link' o '#boton_registro, #boton_registro_inicio, #boton_registro_fin')
    closeButtonSelector : '.close-overlay', // this works with any jQuery selector - string (ex: '#close-button, .close')
    
    resetFormEachTime : 1, // 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
        GeneralInfoPop.obj.overlay = jQuery('<div id="info-pop-overlay"></div>').appendTo( jQuery('BODY') );
        
        GeneralInfoPop.obj.panelWrapper = jQuery('<div id="info-pop-panel-wrapper"></div>').appendTo( GeneralInfoPop.obj.overlay );
        
        GeneralInfoPop.obj.panel = jQuery('<div id="info-pop-panel"></div>').appendTo( GeneralInfoPop.obj.panelWrapper );
        
        // append panel headline
        GeneralInfoPop.obj.panelHeadline = $( '<h2 id="info-pop-header">' + GeneralInfoPop.infoHeadline + '</h2>') . appendTo( GeneralInfoPop.obj.panel );
        
        // set panel headline background color
        if ( GeneralInfoPop.headerBgColor != '#777777' ) GeneralInfoPop.obj.panelHeadline.css( 'backgroundColor', GeneralInfoPop.headerBgColor );
        
        // append panel header close button
        GeneralInfoPop.obj.panelHeadline.append( '<a href="#" class="close-overlay" title="Cerrar" >X</a>' );
        
        // append form
		GeneralInfoPop.obj.form = jQuery('<form action="' + GeneralInfoPop.formPhpLocation + '" method="post" id="info-pop-form" ></form>').appendTo( GeneralInfoPop.obj.panel );
        
        // append loading graphic
        GeneralInfoPop.obj.loading = jQuery('<div id="info-pop-loading-gif-wrapper"></div>').appendTo( GeneralInfoPop.obj.panel );
        
        GeneralInfoPop.obj.loading.append('<img src="' + GeneralInfoPop.pathToGeneralInfoPop + 'images/register_overlay/ajax-loader.gif" alt="" id="info-pop-loading-gif" />');
    },

    getFormContent : function() {
        // grab form html using jQuery's AJAX API
        jQuery.get( GeneralInfoPop.formPhpLocation, { 'ajaxForm' : 1, 'params': info_selected },  function(html) {                
            if ( html ) {
                GeneralInfoPop.obj.loading.fadeOut(200);
                GeneralInfoPop.obj.form.html( html );
                GeneralInfoPop.attachFormEvents();
            }
        });
    },
    
    attachFormEvents : function() {
        // close buttons
        jQuery( GeneralInfoPop.closeButtonSelector, GeneralInfoPop.obj.panel).click( function(ev) {
            ev.preventDefault();
            GeneralInfoPop.hideOverlay();
        });
        
        // attach submit event each time for IE
        if ( jQuery.browser.msie ) {
            jQuery('input.submit', GeneralInfoPop.obj.form).click( function(ev) {
                ev.preventDefault();                
                GeneralInfoPop.submitForm();
            });
        }
        // only attach submit event once for other browsers
        else if ( !GeneralInfoPop.submitEvent ) {            
            GeneralInfoPop.obj.form.submit( function(ev) {
                ev.preventDefault();                
                GeneralInfoPop.submitForm();
            });
            
            GeneralInfoPop.submitEvent = 1;
        }
    },
    
    checkOverlayFade : function() {
        if ( $.browser.msie && !GeneralInfoPop.fadeOverlayIE && !( $.browser.version < 7 && GeneralInfoPop.fadeOverlayIE6 ) ) return false;
        else return true;
    },
    
    showOverlay : function() {
        // if first time append the overlay and get the form content
        if ( typeof(GeneralInfoPop.obj.overlay) == 'undefined' ) {         
            GeneralInfoPop.appendOverlay();
            GeneralInfoPop.getFormContent();
        }
        else if ( GeneralInfoPop.resetFormEachTime ) GeneralInfoPop.getFormContent();
        
        if ( GeneralInfoPop.overlayFade ) GeneralInfoPop.obj.overlay.fadeIn( GeneralInfoPop.overlayFadeOut, GeneralInfoPop.overlayEasing );
        else GeneralInfoPop.obj.overlay.show();
    },
    
    hideOverlay : function() {
        if ( GeneralInfoPop.overlayFade ) GeneralInfoPop.obj.overlay.fadeOut( GeneralInfoPop.overlayFadeIn, GeneralInfoPop.overlayEasing );
        else GeneralInfoPop.obj.overlay.hide();
    },
    
    submitForm : function() {
        // add form fields to array
         jQuery('input, select, textarea', GeneralInfoPop.obj.form).each( function() {
            GeneralInfoPop.addFormField( jQuery(this) );
         });
         
         // set the ajaxForm post value
         GeneralInfoPop.formFields['ajaxForm'] = 1;
         
         // fade in the loading graphic
         GeneralInfoPop.obj.form.fadeOut(200);
         GeneralInfoPop.obj.loading.fadeIn(200);
         
         // post the form with jQuery's AJAX API
        jQuery.post( GeneralInfoPop.formPhpLocation, GeneralInfoPop.formFields, function(html) {                
            if ( html ) {
                GeneralInfoPop.obj.form.html( html );
                GeneralInfoPop.obj.loading.fadeOut(200);
                GeneralInfoPop.obj.form.fadeIn(200);
                
                GeneralInfoPop.attachFormEvents();
            }
          });
    },
    
    addFormField : function( $field ) {
        var fieldName = $field.attr('name');
        if ( fieldName ) GeneralInfoPop.formFields[ fieldName ] = $field.val();
    },
    
    init : function() {
        var anchorSelector = '';
        // force array
        if ( typeof( GeneralInfoPop.replaceHref ) != 'object' ) GeneralInfoPop.replaceHref = [ GeneralInfoPop.replaceHref ];
        
        // add anchor selectors
        for ( var i = 0; i < GeneralInfoPop.replaceHref.length; i++ ) {
            if ( GeneralInfoPop.replaceHref[i] ) anchorSelector += 'a[href=' + GeneralInfoPop.replaceHref[i] + '], ';
        }
        
        // add  additional jQuery selectors
        if ( GeneralInfoPop.openButtonSelector ) anchorSelector += GeneralInfoPop.openButtonSelector;
        else anchorSelector = anchorSelector.substr(0, anchorSelector.length - 2);

        // define ctas and click event
        GeneralInfoPop.obj.ctas = jQuery(anchorSelector);
        
        GeneralInfoPop.obj.ctas.click( function(ev) {
            ev.preventDefault();
            GeneralInfoPop.showOverlay();
        });
        
        // determine if fading overlay or just hide/showing
        GeneralInfoPop.overlayFade = GeneralInfoPop.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 = GeneralInfoPop.pathToGeneralInfoPop + '../images/register_overlay/overlay-ie6.png';
        
        else overlayImg.src = GeneralInfoPop.pathToGeneralInfoPop + 'images/register_overlay/overlay.png';
    }
};

ContactPop = {
	/************ CONFIG **************/
    // make sure to keep the trailing comma after each of these variable definitions
	
    replaceHref : 'contact.php', // can be array or string of hrefs or nothing if you want to use jQuery selectors (below)
    formPhpLocation : 'register-overlay/contact-pop.php', // relative path to the backend contact form
    pathToContactPop : '', // relative path to the Contact-Pop directory
	
    contactHeadline : ' Recibe avisos de ofertas en tu mail ',
    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' o '#boton_registro, #boton_registro_inicio, #boton_registro_fin')
    closeButtonSelector : '.close-overlay', // this works with any jQuery selector - string (ex: '#close-button, .close')
    
    resetFormEachTime : 1, // 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.obj.overlay = jQuery('<div id="contact-pop-overlay"></div>').appendTo( jQuery('BODY') );
        
        ContactPop.obj.panelWrapper = jQuery('<div id="contact-pop-panel-wrapper" ></div>').appendTo( ContactPop.obj.overlay );
        
        ContactPop.obj.panel = jQuery('<div id="contact-pop-panel"></div>').appendTo( ContactPop.obj.panelWrapper );
        
        // append panel headline
        ContactPop.obj.panelHeadline = $( '<h2 id="contact-pop-header">' + ContactPop.contactHeadline + '</h2>') . appendTo( ContactPop.obj.panel );
        
        // set panel headline background color
        if ( ContactPop.headerBgColor != '#777777' ) ContactPop.obj.panelHeadline.css( 'backgroundColor', ContactPop.headerBgColor );
        
        // append panel header close button
        ContactPop.obj.panelHeadline.append( '<a href="#" class="close-overlay" title="Cerrar" >X</a></h2>' );
        
        // append form
        //ContactPop.obj.form = jQuery('<form action="' + ContactPop.formPhpLocation + '" method="post" id="contact-pop-form" onsubmit="if (!document.getElementById(\'acceptConditions\').checked) { alert(\'Debes aceptar las condiciones de uso, para poder registrate\'); } return false;"></div>').appendTo( ContactPop.obj.panel );
		ContactPop.obj.form = jQuery('<form action="' + ContactPop.formPhpLocation + '" method="post" id="contact-pop-form" ></form>').appendTo( ContactPop.obj.panel );
        
        // append loading graphic
        ContactPop.obj.loading = jQuery('<div id="contact-pop-loading-gif-wrapper"></div>').appendTo( ContactPop.obj.panel );
        
        ContactPop.obj.loading.append('<img src="' + ContactPop.pathToContactPop + 'images/register_overlay/ajax-loader.gif" alt="" id="contact-pop-loading-gif" />');
    },

    getFormContent : function() {
        // grab form html using jQuery's AJAX API
        jQuery.get( ContactPop.formPhpLocation, { 'ajaxForm' : 1, 'params': info_selected },  function(html) {
            if ( html ) {
                ContactPop.obj.loading.fadeOut(200);
                ContactPop.obj.form.html( html );
                ContactPop.attachFormEvents();
            }
        });
    },
    
    attachFormEvents : function() {
        // close buttons
        jQuery( ContactPop.closeButtonSelector, ContactPop.obj.panel).click( function(ev) {
            ev.preventDefault();
            ContactPop.hideOverlay();
        });
        
        // attach submit event each time for IE
        if ( jQuery.browser.msie ) {
            jQuery('input.submit', ContactPop.obj.form).click( function(ev) {
                ev.preventDefault();                
                ContactPop.submitForm();
            });
        }
        // only attach submit event once for other browsers
        else if ( !ContactPop.submitEvent ) {            
            ContactPop.obj.form.submit( function(ev) {
                ev.preventDefault();                
                ContactPop.submitForm();
            });
            
            ContactPop.submitEvent = 1;
        }
    },
    
    checkOverlayFade : function() {
        if ( $.browser.msie && !ContactPop.fadeOverlayIE && !( $.browser.version < 7 && ContactPop.fadeOverlayIE6 ) ) return false;
        else return true;
    },
    
    showOverlay : function() {
        // if first time append the overlay and get the form content
        if ( typeof(ContactPop.obj.overlay) == 'undefined' ) {         
            ContactPop.appendOverlay();
            ContactPop.getFormContent();
        }
        else if ( ContactPop.resetFormEachTime ) ContactPop.getFormContent();
        
        if ( ContactPop.overlayFade ) ContactPop.obj.overlay.fadeIn( ContactPop.overlayFadeOut, ContactPop.overlayEasing );
        else ContactPop.obj.overlay.show();
    },
    
    hideOverlay : function() {
        if ( ContactPop.overlayFade ) ContactPop.obj.overlay.fadeOut( ContactPop.overlayFadeIn, ContactPop.overlayEasing );
        else ContactPop.obj.overlay.hide();
    },
    
    submitForm : function() {
        // add form fields to array
         jQuery('input, select, textarea', ContactPop.obj.form).each( function() {
            ContactPop.addFormField( jQuery(this) );
         });
         
         // set the ajaxForm post value
         ContactPop.formFields['ajaxForm'] = 1;
         
         // fade in the loading graphic
         ContactPop.obj.form.fadeOut(200);
         ContactPop.obj.loading.fadeIn(200);
         
         // post the form with jQuery's AJAX API
        jQuery.post( ContactPop.formPhpLocation, ContactPop.formFields, function(html) {                
            if ( html ) {
                ContactPop.obj.form.html( html );
                ContactPop.obj.loading.fadeOut(200);
                ContactPop.obj.form.fadeIn(200);
                
                ContactPop.attachFormEvents();
            }
          });
    },
    
    addFormField : function( $field ) {
        var fieldName = $field.attr('name');
        if ( fieldName ) ContactPop.formFields[ fieldName ] = $field.val();
    },
    
    init : function() {
        var anchorSelector = '';
        // force array
        if ( typeof( ContactPop.replaceHref ) != 'object' ) ContactPop.replaceHref = [ ContactPop.replaceHref ];
        
        // add anchor selectors
        for ( var i = 0; i < ContactPop.replaceHref.length; i++ ) {
            if ( ContactPop.replaceHref[i] ) anchorSelector += 'a[href=' + ContactPop.replaceHref[i] + '], ';
        }
        
        // add  additional jQuery selectors
        if ( ContactPop.openButtonSelector ) anchorSelector += ContactPop.openButtonSelector;
        else anchorSelector = anchorSelector.substr(0, anchorSelector.length - 2);

        // define ctas and click event
        ContactPop.obj.ctas = jQuery(anchorSelector);
        
        ContactPop.obj.ctas.click( function(ev) {
            ev.preventDefault();
            ContactPop.showOverlay();
        });
        
        // determine if fading overlay or just hide/showing
        ContactPop.overlayFade = ContactPop.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.pathToContactPop + '../images/register_overlay/overlay-ie6.png';
        
        else overlayImg.src = ContactPop.pathToContactPop + 'images/register_overlay/overlay.png';
    }
};

jQuery(function() {
    // initiate ContactPop once the page loads
    ContactPop.init();
    // initiate GeneralInfoPop once the page loads
    GeneralInfoPop.init();
});