var player = {
  div:   false,
  obj:   false,
  drag:  false,
  x:     false,
  init:  function(){

    //if( typeof( DetectFlashVer ) != 'undefined' &&
    //    !DetectFlashVer( '8', '0', '22' ) ) return;

    this.div  = $div();
    Element.addClassName( this.div, 'overlay' );
    this.drag = $div();
    Element.addClassName( this.drag, 'drag' );
    this.drag.appendChild( $h4( 'Media Player' ) );
    this.x    = document.createElement( 'img' );
    this.x.setAttribute( 'alt', 'close' );
    this.x.setAttribute( 'src', '/images/layout/x.gif' );

    trace( 'mp3player.init() ran' );

    // set the events to observe
    this.x.onclick = this.hide.bindAsEventListener( this );

    // make it draggable
    if( typeof( Drag ) != 'undefined' ){
      Drag.init( this.drag, this.div );
    }

    // get it on the page
    this.div.appendChild( this.drag );
    this.div.appendChild( this.x );
    document.getElementsByTagName( 'body' )[0].appendChild( this.div );

    trace( 'appended stuff to the body' );

    // create the links
    var links = $$( 'a.audio' );
    links.each( function( item ){
      trace( 'in the loop' );
      Event.observe( item, 'click', this.show.bind( this ), false );
      // item.firstChild.nodeValue = 'Listen to the .mp3';
    }.bind( this ) );

    var links = $$( 'a.video' );
    links.each( function( item ){
      trace( 'in the loop' );
      Event.observe( item, 'click', this.show.bind( this ), false );
      // item.firstChild.nodeValue = 'Listen to the .mp3';
    }.bind( this ) );

    trace( 'mp3player.init() finished' );

  },
  show:  function( e ){
    if( player.div.style.visibility != 'visible' ){

      // get the element
      var el = Event.element( e );
      var file = el.href;

      // get the class, width & height
      var width, height, swf, p, fObj;
      if( Element.hasClassName( el, 'audio' ) ){
        this.curr_class = 'mp3';
        p = $p( 'Please click the play button to listen to the audio stream.' );
        // make a new flash object
        fObj = new SWFObject( '/players/dewplayer.swf',
                              'player', '290', '24', '8.0.22', '#ffffff', true);
        fObj.altTxt = 'You need Flash to listen';
        fObj.addParam( 'wmode', 'transparent' );
        fObj.addVariable( 'playerID', '4008' );
        fObj.addVariable( 'bg', '0xF8F8F8' );
        fObj.addVariable( 'leftbg', '0xEEEEEE' );
        fObj.addVariable( 'text', '0x666666' );
        fObj.addVariable( 'lefticon', '0x666666' );
        fObj.addVariable( 'rightbg', '0xCCCCCC' );
        fObj.addVariable( 'rightbghover', '0x999999' );
        fObj.addVariable( 'righticon', '0x666666' );
        fObj.addVariable( 'righticonhover', '0xFFFFFF' );
        fObj.addVariable( 'slider', '0x666666' );
        fObj.addVariable( 'track', '0xFFFFFF' );
        fObj.addVariable( 'loader', '0x9FFFB8' );
        fObj.addVariable( 'border', '0x666666' );
        fObj.addVariable( 'son', file.substring( 0, file.length - 1 ) + '.mp3' );

        trace( 'made fObj' );

        // append the paragraph
        this.drag.appendChild( p );

        if( this.obj == false ) this.obj = $div();
        this.obj.setAttribute( 'id', 'the-audio' );
        this.div.appendChild( this.obj );
        fObj.write( 'the-audio' );
        trace( 'appended the movie' );

        // display the player
        Element.addClassName( player.div, player.curr_class );
        player.div.style.top = ( Element.hasClassName( el, 'mp3' ) ) ? ( Event.pointerY( e ) - 50 ) + 'px'
                                                                       : ( Event.pointerY( e ) - 70 ) + 'px';
        player.div.style.left = ( Event.pointerX( e ) - 30 ) + 'px';
        player.div.style.visibility = 'visible';
      }else if( Element.hasClassName( el, 'video' ) ){
        player.curr_class = 'mov';
        var arr = file.split( '/' );
        file = arr[ arr.length - 2 ];
        p = $p( 'Please click the play button to watch the video.' );
        // make the new movie object
        fObj = new SWFObject( 'http://blip.tv/scripts/flash/showplayer.swf',
                              'player', '320', '278', '8.0.22', '#959595', true);
        //fObj.addParam( 'scale', 'aspect' );
        fObj.altTxt = 'You need Flash to watch';
        fObj.addParam( 'wmode', 'transparent' );
        fObj.addParam( 'quality', 'high' );
        fObj.addParam( 'play', 'true' );
        fObj.addParam( 'loop', 'false' );
        fObj.addParam( 'scale', 'showall' );
        fObj.addParam( 'wmode', 'window' );
        fObj.addParam( 'devicefont', 'false' );
        fObj.addParam( 'bgcolor', '#959595' );
        fObj.addParam( 'menu', 'false' );
        fObj.addParam( 'allowScriptAccess', 'sameDomain' );
        fObj.addParam( 'salign', '' );
        fObj.addVariable( 'autoplay', 'true' );
        fObj.addVariable( 'file', 'http://blip.tv/file/get/' + file + '.flv' );
        trace( 'made fObj' );

        // append the paragraph
        this.drag.appendChild( p );

        // make the container
        if( this.obj == false ) this.obj = $div();
        this.obj.setAttribute( 'id', 'the-video' );
        this.div.appendChild( this.obj );
        fObj.write( 'the-video' );
        trace( 'appended the movie' );

        // display the player
        Element.addClassName( player.div, player.curr_class );
        player.div.style.top = ( Element.hasClassName( el, 'mp3' ) ) ? ( Event.pointerY( e ) - 50 ) + 'px'
                                                                       : ( Event.pointerY( e ) - 200 ) + 'px';
        player.div.style.left = ( Event.pointerX( e ) - 30 ) + 'px';
        player.div.style.visibility = 'visible';
      }
    }
    Event.stop( e );

  },
  hide:  function(){

    trace( 'hiding' );

    // remove the current object
    this.obj.innerHTML = '';
    this.obj.parentNode.removeChild( this.obj );

    // hide the player
    player.div.style.visibility = '';
    Element.removeClassName( player.div, player.curr_class );
    player.drag.removeChild( player.drag.lastChild );

  }
};

// add to the onload
Event.observe( window, 'load', function(){ player.init(); }, false );
