var Ajax;
if ( Ajax && (Ajax != null) )
{
    Ajax.Responders.register( {
        onCreate: function()
        {
            if ( $( 'spinner' ) && Ajax.activeRequestCount > 0 )
                Effect.Appear( 'spinner', {duration:0.5,queue:'end'} );
        },
        onComplete: function()
        {
            if ( $( 'spinner' ) && Ajax.activeRequestCount == 0 )
                Effect.Fade( 'spinner', {duration:0.5,queue:'end'} );
        }
    } );
}

function showEnlargedImage(urlImg){
    showHtmlModal( '<img src="'+urlImg+'" alt="Sorry No Enlarged Image Available for this item."/>' );
}


function showHtmlModal( html )
{
    var videoId = '';
    $.modal( '<div style="background-color:white;padding:5px; min-width: 600px; min-height: 600px;">' + html + '</div>', {
        onOpen: function ( dialog )
        {
            if ( $('#mainAudio').html() == 'audio on' ){
                $.dbj_sound.stop( "#media1" );
            }
            dialog.overlay.fadeIn( 'slow', function ()
            {
                dialog.data.show( );
                dialog.container.slideDown( 'slow' );
            } );
        },
        onClose: function ( dialog )
        {
            if ( $('#mainAudio').html() == 'audio on' ){
                $.dbj_sound.loop( "#media1" );                
            }
            dialog.container.slideUp( 'normal', function ()
            {
                dialog.overlay.fadeOut( 'normal', function ()
                {
                    $.modal.close( );
                } );
            } );
        },
        autoResize : true,
//        containerId: 'publicmodal-container',
        containerId: 'public-video-container',
        overlayId: 'publicmodal-overlay',
        closeClass: 'publicmodal-close',
        overlayClose: true,
        opacity: 80
//        position: Array('55px')
        
    } )
}

function showVideoModal( videoUrl )
{
    if( videoUrl.lastIndexOf( "=" ) != -1 )
    {
        var clipId = videoUrl.substring( videoUrl.lastIndexOf( "=" ) + 1 );
        showHtmlModal( '<object id="flashObj" width="486" height="472" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0"><param name="movie" value="http://c.brightcove.com/services/viewer/federated_f9/71385328001?isVid=1&publisherID=70921357001" /><param name="bgcolor" value="#FFFFFF" /><param name="autoplay" value="true"><param name="flashVars" value="videoId=' + clipId + '&playerID=71385328001&domain=embed&" /><param name="base" value="http://admin.brightcove.com" /><param name="seamlesstabbing" value="false" /><param name="allowFullScreen" value="true" /><param name="swLiveConnect" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://c.brightcove.com/services/viewer/federated_f9/71385328001?isVid=1&publisherID=70921357001" bgcolor="#FFFFFF" flashVars="videoId=' + clipId + '&playerID=71385328001&domain=embed&" base="http://admin.brightcove.com" name="flashObj" width="486" height="412" seamlesstabbing="false" type="application/x-shockwave-flash" allowFullScreen="true" swLiveConnect="true" allowScriptAccess="always" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>' )
    }
}

var Global = {
    /**
     * Writes a debug statement to the Firefox console if it is available. Make sure to use this method instead of
     * console.debug() since that method causes a Javascript error if the console is not available.
     * @param text Debug content.
     */
    debug: function( text )
    {
        if ( typeof(console) != "undefined" && this.enableDebug )
        {
            console.debug( text );
        }
    },

    /**
     * Reloads the current page in the browser.
     */
    reloadPage: function()
    {
        location.reload( true );
    },

    /**
     * Navigates the browser to the given url.
     * @param url
     */
    nav: function( url )
    {
        document.location.href = url;
    },

    /**
     * Builds a modal dialog.
     * @param id Id of the dialog in the dom (optional).
     * @param title Title of the dialog (optional).
     * @param body Body of the dialog (optional)
     */
    buildDialog: function( id, title, body )
    {
        if ( !id )
        {
            id = "dialog_" + (new Date( )).getTime( );
        }
        var dialog = $( '<div id="' + id + '" style="display:none;"></div>' ).appendTo( "body" );

        dialog.dialog( {
            autoOpen: false,
            bgiframe: true,
            resizable: false,
            width: 400,
            modal: true
        } );

        if ( title )
        {
            dialog.dialog( 'option', 'title', title );
        }
        if ( body )
        {
            dialog.html( body );
        }

        return dialog;
    },

    /**
     * Opens the url in a new window.
     * @param url Url.
     * @param name Name of the window.
     */
    openWindow: function( url, name )
    {
        window.open( url, name ).focus( );
    },

    /**
     * Replaces all instances in the string of items in the replacements array.
     * @param string String to update.
     * @param replacements Array of replacements.
     */
    replace: function( string, replacements )
    {
        if ( replacements )
        {
            $.each( replacements, function( index, value )
            {
                string = string.replace( index, value );
            } );
        }
        return string;
    },

    /**
     * Opens the "model2" div and loads the url into the ifram.
     * @param url Url to load into the iframe.
     * @param callBack Function to call after the modal is closed.
     */
    openModel2: function( url, callBack )
    {
        $( "#modal2_iframe" ).attr( "src", url );
        
        $( "#modal2" ).modal( {
            onOpen: function ( dialog )
            {
                dialog.overlay.fadeIn( 'slow', function ()
                {
                    dialog.data.show( );
                    dialog.container.slideDown( 'slow' );
                } );
            },
            onClose: function ( dialog )
            {
                dialog.container.slideUp( 'normal', function ()
                {
                    dialog.overlay.fadeOut( 'normal', function ()
                    {
                        $.modal.close();
                        if ( callBack )
                        {
                            callBack( );
                        }
                    } );
                } );
            },
            containerId: 'adminmodal-container',
            overlayId: 'adminmodal-overlay',
            closeClass: 'adminmodal-close',
            overlayClose: true,
            opacity: 80
        } );
    },

    /**
     * Opens the "model2" div and loads the url into the ifram.
     * @param url Url to load into the iframe.
     * @param callBack Function to call after the modal is closed.
     */
    openPublicModel1: function( url, callBack )
    {
        $( "#public-modal_iframe" ).attr( "src", url );

        $( "#public-modal" ).modal( {
            onOpen: function ( dialog )
            {
                dialog.overlay.fadeIn( 'slow', function ()
                {
                    dialog.data.show( );
                    dialog.container.slideDown( 'slow' );
                } );
            },
            onClose: function ( dialog )
            {
                dialog.container.slideUp( 'normal', function ()
                {
                    dialog.overlay.fadeOut( 'normal', function ()
                    {
                        $.modal.close();
                        if ( callBack )
                        {
                            callBack( );
                        }
                    } );
                } );
            },
            containerId: 'publicmodal-container',
            overlayId: 'publicmodal-overlay',
            closeClass: 'publicmodal-close',
            overlayClose: true,
            opacity: 80
            //position: Array('55px')
        } );
    }

};
Global.enableDebug = true;

Global.Ajax = {
    /**
     * Handles an error thrown during an AJAX request.
     * @param request Request data.
     * @param textStatus Status of the response (ex: 404)
     * @param errorThrown The error that was thrown.
     */
    handleError: function( request, textStatus, errorThrown )
    {
        alert( "An Error has occured" );
    },

    /**
     * Sends a delete request to the application. If the delete method returns an empty string then it is treated as a
     * successful delete, if any text is returned then it is displayed to the user.
     * @param url Url to the delete method, can contain {id} as a placeholder for the Persistable Id.
     * @param callBack Function to call if the delete is successful.
     */
    doDelete: function( url, callBack )
    {
        Global.debug( "Global.Ajax.doDelete(): url=" + url );

        $.ajax( {
            type: "POST",
            url: url,
            data: { _method: "DELETE"},
            success: function( data )
            {
                if ( data != '' )
                {
                    alert( data );
                    //var dialog = Global.buildDialog( null, "The delete could not be performed", data );
                    //dialog.dialog( 'open' );
                }

                else if ( callBack )
                {
                    callBack( );
                }
            },
            error: function( request, textStatus, errorThrown )
            {
                Global.Ajax.handleError( request, textStatus, errorThrown );
            }
        } );
    },

    /**
     * Uses GET to load the given url. Used for requests that do not return content such as "live" calls. If the returned
     * data is an empty string it is considered a successful call, otherwise the data is displayed to the user.
     * @param url Url to call, can contain an optional {id} placeholder.
     * @param callBack Function to call if the request is successful.
     */
    doGet: function( url, callBack )
    {
        Global.debug( "Global.Ajax.doGet(): url=" + url );

        Global.Ajax.doGetData( url, function( data )
        {
            if ( data != '' )
            {
                alert( data );
                //var dialog = Global.buildDialog( null, null, data );
                //dialog.dialog( 'open' );
            }

            else if ( callBack )
            {
                callBack( );
            }
        } );
    },

    /**
     * Calls the given url to get data.
     * @param url Url to call, can contain an option {id} placeholder.
     * @param callBack Function to call if the request is successful.
     */
    doGetData: function( url, callBack )
    {
        Global.debug( "Global.Ajax.doGetData(): url=" + url );

        $.ajax( {
            type: "GET",
            url: url,
            success: function( data )
            {
                callBack( data );
            },
            error: function( request, textStatus, errorThrown )
            {
                Global.Ajax.handleError( request );
            }
        } );
    },

    /**
     * Uses POST to submit the given form. Used for requests that do not return content. If the returned data is not
     * an empty string it is displayed to the user.
     * @param form Html form
     * @param callBack Function to call if the request is successful.
     */
    doPostForm: function( form, callBack )
    {
        Global.debug( "Global.Ajax.doPostForm(): url=" + form.attr( 'action' ) );

        Global.Ajax.doPostFormData( form, function( data )
        {
            if ( data != '' )
            {
                alert( data );
                //var dialog = Global.buildDialog( null, null, data );
                //dialog.dialog( 'open' );
            }

            else if ( callBack )
            {
                callBack( );
            }
        } );
    },

    /**
     * Uses POST to submit the given form to get data.
     * @param form Html form
     * @param callBack Function to call if the request is successful.
     */
    doPostFormData: function( form, callBack )
    {
        Global.debug( "Global.Ajax.doPostFormData(): url=" + form.attr( 'action' ) );

        $.ajax( {
            type: "POST",
            url: form.attr( 'action' ),
            data: form.serialize( ),
            success: function( data )
            {
                callBack( data );
            },
            error: function( request, textStatus, errorThrown )
            {
                Global.Ajax.handleError( request );
            }
        } );
    }
};
