var $j = jQuery.noConflict();

var Page =
{
    isIE7: ( jQuery.browser.msie && jQuery.browser.version == "7.0" ) ? true : false,
	init: function() {
		$j( '#addMoreFriendsBtn' ).click(function( e ) {
			$j( '#container' ).css( 'background', 'url(\'images/background2.png\')' );
			$j( '#container' ).css( 'height', '579px' );
            $j( '#container' ).css( 'background-repeat', 'no-repeat' );
			$j( '.hidden' ).removeClass( 'hidden' );
			$j( '.addFriendContainer' ).addClass( 'hidden' );
			$j( '.btnSendContainer' ).css( 'top', '460px' );
            $j( '.txtBottomPage' ).css( 'top', '478px' );
            $j( '.txtBottomPage' ).css( 'left', '7px' );
            if( Page.isIE7 )
            {
                $j( '.txtBottomPage' ).css( 'top', '485px' );
                $j( '.txtBottomPage' ).css( 'font-size', '7px' );
                $j( '.btnSendContainer' ).css( 'top', '466px' );
            }
			Page.stopDefault( e );
		}),
		$j( '.traped_firstname' ).focus( function( e ) {
			var self = $j( this ).attr( 'id' );
			message = 	'<div id="dialog-'+self+'">'+
							'<input type="hidden" name="inputId" id="inputId" value="'+self+'">'+
							'<div class="logoFirstname"><img src="images/prenom.png" /></div>'+
							'<div id="stripteasePrenoms" class="stripteasePrenoms">'+
								'<div class="loading"><p class="loadingMess">Chargement, merci de patienter ...</p></div>'+
							'</div>'+
							'<div class="miniForm hidden"><div><label for="newPrenom">PRENOM</label>&nbsp;<input type="text" name="newPrenom" id="newPrenom" /></div><div>ATTENTION, LE PRENOM SAISI DANS LE CHAMP NE SERA PAS CITE DANS L\'ANNONCE TELEPHONIQUE.</div></div>'+
							'<div class="divBtnValider hidden"><input onClick="Page.selectPrenom($j(\'#newPrenom\').attr(\'value\'), \'000\');" class="btnValider" name="send" type="image" value="test" src="images/valider.png" alt="send" /></div>'+
							'<div class="firstnameNotExist clear"><a href="#" onClick="$j(\'.miniForm\').removeClass(\'hidden\'); $j(\'.divBtnValider\').removeClass(\'hidden\'); $j(\'.firstnameNotExist\').addClass(\'hidden\');" class="blue">Le prénom ne figure pas dans la liste</a></div>'+
						'</div>';

			if( $j( '#dialog-'+self ).length == 0 )
				$j( message ).appendTo( 'body' );

			$j( '#dialog-'+self ).dialog({
				width: '614px',
				height: '450px',
				modal: true,
				resizable: false,
				draggable: false,
				close: function(){
					$j( this ).dialog( 'destroy' ).remove();
				}
			});
			$j.ajax({
				type: "get",
				url: "prenom.php",
				data: "letter=A",
				dataType: "html",
				success: function( response ) {
					$j( '#stripteasePrenoms' ).html( response );
				}
			});
		}),
		$j('#zoneVideoClick').click( function( e ){
            Page.openFlashDialog( e );
		}),
		$j( '.errorContainer' ).removeClass( 'hidden' );
		$j( '.errorContainer' ).dialog({
			width: '600px',
			height: '135px',
			modal: true,
			resizable: false,
			draggable: false,
			close: function(){
				$j( this ).dialog( 'destroy' ).remove();
			}
		}),
		$j( '#errorBtnOk' ).click( function(e){
			$j( '.errorContainer' ).dialog( 'destroy' ).remove();
			Page.stopDefault( e );
		})
	},
	stopDefault: function( event )
	{
		( event && event.preventDefault ) ? event.preventDefault() : window.event.returnValue = false;
	},
	changeLettre: function( lettre )
	{
		$j( '#stripteasePrenoms' ).html( '<div class="loading"><p class="loadingMess">Chargement, merci de patienter ...</p></div>' );
		$j.ajax({
			type: "get",
			url: "prenom.php",
			data: "letter=" + lettre,
			dataType: "html",
			success: function( response ) {
				$j( '#stripteasePrenoms' ).html( response );
			}
		});
	},
	selectPrenom: function( prenom, idPrenom )
	{
		var self = $j( '#inputId' ).attr( 'value' );
		$j( '#'+self ).attr( 'value', prenom );
		$j( '#'+self+'_id' ).attr( 'value', idPrenom );
		$j( '#dialog-'+self ).dialog( 'destroy' ).remove();
	},
    openFlashDialog: function( e )
    {
        var flashDivCurrent = $j('#clickZoneVideo').html();

        message ='<div id="swfdialog">'+
                                    '<div class="loading"><p class="loadingMess">Chargement, merci de patienter ...</p></div>'+
                                '</div>';
        if( $j( '#swfdialog' ).length == 0 )
            $j( message ).appendTo( 'body' );

        $j( '#swfdialog' ).dialog({
            width: '614px',
            height: '450px',
            modal: true,
            resizable: false,
            draggable: false,
            close: function(){
                $j('#clickZoneVideo').html( flashDivCurrent );
                $j( this ).dialog( 'destroy' ).remove();
                $j('#zoneVideoClick').click( function( e ){
                    Page.openFlashDialog( e );
                })
            }
        });

        $j.ajax({
            type: "get",
            url: "getFlash.php",
            dataType: "html",
            success: function( response ) {
                $j('#clickZoneVideo').html('');
                $j('#swfdialog').html( response );
                $j('#closeSwf').click(function()
                {
                    $j('#swfdialog').dialog("destroy").remove();
                    $j('#clickZoneVideo').html( flashDivCurrent );
                    $j('#zoneVideoClick').click( function( e ){
                        Page.openFlashDialog( e );
                    })
                });
            }
        });
        Page.stopDefault( e );
    }
}

$j( document ).ready( function(){ Page.init(); } );
