/** ----------------------------------------------------------------------------

    extension:  REZET
    author:     INTERNETHIC

    summary:    =DOCUMENT READY
    			=OPEN IN NEW WINDOW
    			=PRINT BTN
    			=POPIN
    			=INPUT FOCUS

----------------------------------------------------------------------------- */

/**
 * =DOCUMENT READY
----------------------------------------------------------------------------- */
jQuery(document).ready(function() {
    openInNewWindow();
    initPrintBtn();
    initInputFocus();

    /*
    jQuery("a.myPopin").popin({
    	loaderImg : '/extension/rezet/design/rezet/images/loader.gif'
    });
    */

    //if(!(jQuery.browser.msie && parseFloat(jQuery.browser.version) < 7)) REZET_PopinBehaviors.initPopin(jQuery(document));

    REZET_PopinBehaviors.initPopin(jQuery(document));

});


/**
 *  =OPEN IN NEW WINDOW
 *  Fonction remplaçant l'utilisation obsolète de target="_blank"
----------------------------------------------------------------------------- */
var openInNewWindow = function(){
    jQuery('a.newWindow').live( 'click', function(){
        window.open(this.href);
        return false;
    });
}


/**
 *  =PRINT BTN
 *  Bouton d'impression
----------------------------------------------------------------------------- */
var initPrintBtn  = function(){
    jQuery('.print').live( 'click', function(){
        window.print();
        return false;
    });
}


/**
 *  =INPUT FOCUS
 *  On place le contenu du title dans les input vident (au focus on vide, au blur, on replace le titre si rien n'est saisi)
----------------------------------------------------------------------------- */
var initInputFocus  = function( forceClearInput ){
    jQuery( 'input[title]' ).each( function(){
        var $input = jQuery( this );
        var $title = $input.attr( 'title' );
        var $form = jQuery( this ).parents( 'form:first' );
        var $win = jQuery(window);
        var $excludeChangeInputFormID = [ 'formRechercheFilter' ];

        if( $title != 'Date' ){ $input.val(''); }

        if( forceClearInput ){
            $form.submit( function(){ remove(); });

            if( jQuery.inArray( $form.attr( 'id' ), $excludeChangeInputFormID  ) == -1 ){ $form.find( 'select' ).change( function(){ remove(); }); }
        } else {

            if( $input.value === '' ){ $input.val( $title ).addClass( 'focused' ); }

            $input.blur( function(){ if( this.value === '' ){ $input.val( $title ).addClass( 'focused' ); } }).focus(remove).blur();

            $form.submit( function(){ remove(); });

            if( jQuery.inArray( $form.attr( 'id' ), $excludeChangeInputFormID  ) == -1 ){ $form.find( 'select' ).change( function(){ remove(); }); }

            $win.unload( function(){ remove(); });
       }

       function remove() {
            if( $input.val() === $title && $input.hasClass( 'focused' ) ){
                $input.val('').removeClass( 'focused' );
            }
        }
    });
}

