$(document).ready( function() 
    {
        CondorFrontend.InitializePopups();
        CondorFrontend.InitializeGAOutboundLinks();
    });



var CondorFrontend =
{
    FlashElements: null,
	
    GetApplicationUrl: function()
    {
        if ( CondorFrontend.ApplicationUrl != null )
            return CondorFrontend.ApplicationUrl;

        CondorFrontend.ApplicationUrl = document.getElementsByTagName( 'base' )[ 0 ].href;
        CondorFrontend.ApplicationUrl = CondorFrontend.ApplicationUrl.substring( 0, CondorFrontend.ApplicationUrl.length - 1 );
        return CondorFrontend.ApplicationUrl;
    },
	
    GetFlashElements: function()
    {
        if ( CondorFrontend.FlashElements == null )
            CondorFrontend.FlashElements = $( 'embed:visible, object:visible' );
        return CondorFrontend.FlashElements;
    },
	
    InitializePopups: function()
    {
        var aLinks, i;

        if ( document.location.href.indexOf( '_inPopup=true' ) > -1 )
        {
            // We're in a popup, append all querystrings on internal links
            aLinks = $( "a" ).get();
            var baseHref = CondorFrontend.GetApplicationUrl();
            for ( i=0; i<aLinks.length; i++ )
                if ( aLinks[ i ].href.indexOf( baseHref ) == 0 )
                    $( aLinks[ i ] ).attr( 'href', aLinks[ i ].href + ( ( aLinks[ i ].href.indexOf( '?' ) > -1 ) ? '&' : '?' ) + '_inPopup=true' );
        }
        else
        {
            if ( document.location.href.indexOf( '_inPopup=true' ) > -1 )
            {
                // Make sure the _inPopup=true is not within our URL, so we don't have 'inPopup' behaviour when we're not in a popup.
                var newLocation = document.location.href.replace( '&_inPopup=true&iframe', '' );
                newLocation = newLocation.replace( '?_inPopup=true&iframe', '' );
                newLocation = newLocation.replace( '&_inPopup=true', '' );
                newLocation = newLocation.replace( '?_inPopup=true', '' );

                // Redirect only if our URL has changed
                if ( newLocation != document.location.href )
                    document.location = newLocation;
            }

            // Convert links for FancyBox
            aLinks = $( "a.OpenInPopup" ).get();
            for ( i=0; i<aLinks.length; i++ )
                $( aLinks[ i ] ).attr( 'href', aLinks[ i ].href + ( ( aLinks[ i ].href.indexOf( '?' ) > -1 ) ? '&' : '?' ) + '_inPopup=true&iframe' );
			
            $( "a.OpenInPopup" ).fancybox(
            {
                'hideOnContentClick': false,
                'zoomSpeedIn': 300,
                'zoomSpeedOut': 300,
                'overlayShow': false,
                'frameWidth': 900,
                'frameHeight': 500,
                'callbackOnStart': function() {
                    CondorFrontend.GetFlashElements().css( 'visibility', 'hidden' );
                },
                'callbackOnClose': function() {
                    CondorFrontend.GetFlashElements().css( 'visibility', 'visible' );
                }
            });
        }
    },
	
    InitializeGAOutboundLinks: function()
    {
        if ( window.pageTracker == null )
            return;

        $( "a" ).click(function ( e )
        {
            var href = $( this ).attr( 'href' );

            if ( href.indexOf( CondorFrontend.GetApplicationUrl() ) > -1 )
                return;

            if ( href.indexOf( 'mailto:' ) < 0
                && href.indexOf( 'http:' ) < 0
                && href.indexOf( 'ftp:' ) < 0
                && href.indexOf( 'https:' ) < 0 )
                return;

            pageTracker._trackEvent( 'Outbound clicks', href, document.location.href );
        });
    }
}
