
var F2 = {

   EventManager : {

      /* Fast2 client events
       * PageHeightChanged = Whenever something modifies the height of the page
       *    objects this event should be published, this to ensure that elements
       *    that require javascript to be displeyed corectly will not crack.
       *
       */

      subscribe: function subscribe ( event , fn ) {
         $( window ).bind( event , fn );
      },

      publish: function ( event ) {
         $( window ).trigger( event );
      }

   },

   replaceAreaSrc : function () {
     $( 'iframe.ReplaceUrl' ).attr( 'src' , $( 'iframe.ReplaceUrl' ).attr( 'target' ) );
   },

   areaTooltips : function () {
      $( '.Bostadsfakta a' ).mouseover( function ( e ) {
         var target = '#' + $( this ).attr( 'class' );
         $( target ).css( {
            left: e.pageX - ( $( target).width() + 50  + $( '.StageArea' ).offset().left ),
            top: e.pageY - ( parseInt( $( target ).height() / 2 , 10 ) + $( '.StageArea' ).offset().top )
        } ).fadeIn( 200 );
      } ).mouseout( function () {
         var target = '#' + $( this ).attr( 'class' );
         $( target ).fadeOut( 200 , function () {$( target ).css( 'display' , 'none' )} );
      } ).click( function ( e ) {
         e.preventDefault();
         e.stopPropagation();
         return false;
      } );
   },

   statisticsTooltip : function () {
      $( '.StatisticsContainer' ).mouseover( function ( e ) {
        // Paint the graph
        f2_statistics_show_graph();

        $( '#Statistics' ).css( {
            left: e.pageX - ( $( '#Statistics' ).width() + 50  + $('.StageArea').offset().left ),
            top: e.pageY - ( parseInt( $( '#Statistics' ).height() / 2 , 10 ) + $('.StageArea').offset().top )
        } ).fadeIn( 200 );
      } ).mouseout( function () {
         $( '#Statistics' ).fadeOut( 200 , function () {$( '#Statistics' ).css( "display" , "none" )} );
      } ).click( function ( e ) {
         e.preventDefault();
         e.stopPropagation();
         return false;
      } );
   },

   objectAreaLinks : function () {
      if ( $( 'html' ).hasClass( 'OmradeObjektIntegration' ) ) {
         $( 'a , form' ).not( '.ImageGalleryNext, .ImageGalleryPrevius' ).attr( 'target' , '_top' );
         
         $( 'a.ImageGalleryNext , a.ImageGalleryPrevius' ).each( function () {
             var endPosition = window.location.href.indexOf( '#' ) >= 0 ? window.location.href.indexOf( '#' ) : window.location.href.length;
             var pageHref= window.location.href.substring( 0 , endPosition );
             var $this = $( this );
             var linkHref = $this.attr( 'href' ).substring( $this.attr( 'href' ).indexOf( '&img' ) , $this.attr( 'href' ).length);
             $this.attr( 'href' , pageHref + linkHref );
         } );
      }
   },

   fixMessagesUrl : function () {
      $( '.MinaMeddelanden form' ).each( function () {
         var $this = $( this );
         var newAction = $this.attr( 'action' ).replace(/=(.*?)=/ , "=min_brevlada&qmin_brevladaunnamed1=" );
         $this.attr( 'action' , newAction );
      } );
   },

   siteMap : function () {
      $( '#Find' ).mouseover( function ( e ) {
         $( '#SiteMap' ).fadeIn( );
      } );
      $( '#SiteMap' ).mouseleave( function ( e ) {
         $( this ).fadeOut( );
      } );
   },
   
   fixFormUrls : function () {
      $( 'form[target]' ).each( function () {
         var $this = $( this );
         // The action follow the convention ?page=PAGEALIAS&qPAGEALIASunamed1=
         var newAction = $this.attr( 'action' ).replace( /=(.*?)&q(\1)unnamed1/ , '=' + $this.attr( 'target' ) + '&q' + $this.attr( 'target' ) + 'unnamed1' );
         $this.attr( 'target' , '' ).attr( 'action' , newAction );
      } );
   }

};

$( document ).ready( function () {

   F2.replaceAreaSrc();
   F2.areaTooltips();
   F2.statisticsTooltip();
   F2.objectAreaLinks();
   F2.fixMessagesUrl();
   F2.siteMap();
   F2.fixFormUrls();

});

