( function( $ ){
  
  var globals = {
    swfObj: false
  }

  var methods = {
    init: function( options )
    {
      //push options into config
      var defaults = {
      
      };
      var options = $.extend( defaults, options );

      swfObj = ( $.isFunction( swfobject.embedSWF )) ? true : false;

      return this.each( function( i, el ){
        $( el ).css( 'display', 'none' );
      });
    },

    load: function( options )
    {
      var defaults = {
        width: '100%',
        height: '100%',
        version: '9.0.0',
        id: 'ntrstlSwf',
        swf: '',
        flashVars: {},
        params: { 
          bgcolor: "transparent",
          menu: "false",
          scale: "noscale",
          wmode: "transparent" 
        },
        attr: {},
        altContent: '<div id="ntrstlSwf"></div>',
        onLoad: 'animateIn',
        dim: true,
        dimColor: '#000'
      };
      var options = $.extend( defaults, options );

      var $this = this;

      //Append content to be replaced
      $this.append( options.altContent );

      if( swfObj ){
        swfobject.embedSWF( options.swf, options.id, options.width, options.height, options.version, '', options.flashVars, options.params, options.attr, embedCallback );        
      }

      function embedCallback(e)
      {
        if( e.success ){
          $this.css({
            'display': 'block',
            'height': '100%',
            'width': '100%'
          });

          if( options.dim ){
            $( '<div class="dim"></div>' ).prependTo( $this )
              .css({
                'width': '100%',
                'height': '100%',
                'opacity': 0.8,
                'background-color': options.dimColor,
                'display': 'none'
              })
              .fadeIn();
            if(options.dimage){
              $this.css({
                'background-image': 'url(' + options.dimage + ')',
                'background-color': 'transparent'
              });
            }
          }
          $('#'+options.id).css({
            'position': 'absolute',
            'bottom': 0
          });
        }else{
          $.error( options.swf + ' could not be embedded' );
        }
      }

      return this;
    },

    unload: function( options )
    {
      document.getElementById('main').forceSceneBack();
      this.empty();
      this.css( 'display', 'none');

      return this;
    }

  };

  $.fn.ntrstl = function( method )
  {
    //method calling logic
    if ( methods[ method ] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jquery.interstitial' );
    }
  };

})( jQuery );

