var videoManager =
{
  currentId: 0,
  currentData: null,

  showVideo: function ( id )
  {
    if ( !$('flashVideo') ) return false;
    if ( !SWFObject ) return false;
    id = ( id ) ? parseInt( id ) : 'new';
    $('flashVideo').empty();
    var so = new SWFObject( "/modules/video/swf/StreetPioneersPlayer.swf", "streetPlayer", "360", "320", "8", "#ffffff" );
    so.addVariable( "config", "/index.php/video/show/"+id+".xml");
    so.write( "flashVideo" );
    this.loadQueuepoints( id );
  },

  loadQueuepoints: function ( id )
  {
    if ( !id ) id = 'new';
    if ( id == this.currentId ) return;
    this.currentId = id;
    var jSonRequest = new Json.Remote(
      "/index.php/video/jsonLoadUnlocks/" + id,
      { onComplete:this.onQueuepointsLoaded.bind( this ) } ).send();
  },

  onQueuepointsLoaded: function ( data )
  {
    $('unlocks').setStyle( 'display', ( data.length == 0 ) ? 'none' : 'block' );
    $('unlocks').empty();
    data.each( function ( item ) { this.drawUnlock( item ) }, this );
    this.currentData = data;
  },

  drawUnlock: function ( data )
  {
    var unlock = new Element( 'a', {href: '#', id: 'unlock_' + data.unlock_id, title: data.unlock_description } );
    unlock.addEvent( 'click', this.onUnlockClicked.bind( this ) );
    unlock.setStyle( 'background-image', 'url(/modules/video/img/unlocks/' + data.unlock_icon_off + ')' );
    $('unlocks').appendChild( unlock );
  },

  onUnlockClicked: function ( e )
  {
    var e = new Event(e);
    e.preventDefault();
    e.target.blur();
    var id = parseInt(e.target.id.replace( /unlock_/, ''));
    if ( !e.target.hasClass( 'active' ) ) return false;
    this.currentData.each( function ( item ) {
      if ( item.unlock_id == id ) {
        if ( [3,5,7,9,12,13,15,16,17,18,19,21,22,23].contains( parseInt( item.unlock_type_id ) ) ) {
          var email = prompt( item.unlock_description );
          this.saveEmailRegistration( email, item.unlock_mailinglist_id );
        } else {
          this.openWindow( item.unlock_value, id );
        }
        return;
      }
    }, this );
    return false;
  },

  saveEmailRegistration: function ( email, mailinglist_id )
  {
    var objRE = /^[0-9a-zA-Z]([-_.]*[0-9a-zA-Z])*@[0-9a-zA-Z]([-.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,6}$/;
    if ( !email ) return;
    if ( !objRE.test( email ) ) {
      var new_email = prompt( "Dat is geen geldig email adres, probeer het opnieuw" );
      this.saveEmailRegistration( new_email, mailinglist_id );
      return;
    }
    alert( 'Bedankt! Je email adres is opgeslagen!' );
    var myXHR = new XHR({method: 'post'}).send('/index.php/newsletter.html', 'xhr=1&adress_email_adress=' + email + '&mailinglist_id=' + mailinglist_id );
  },

  queuepointReached: function ( timecode, unlock_id )
  {
    this.currentData.each( function ( item ) {
      if ( item.unlock_id == unlock_id ) {
        var div_id = 'unlock_' + unlock_id;
        $(div_id).setStyle( 'background-image', 'url(/modules/video/img/unlocks/' + item.unlock_icon_on + ')' );
        $(div_id).addClass( 'active' );
        return;
      }
    }, this );
  },

  openWindow: function ( url, id )
  {
    window.open( url, 'unlockWindow' + id ); //, 'width=900,height=600,scrollbars=yes,toolbar=no,location=no');
  }
}

// Knoop em aan de player ;)
var queuepointReached = videoManager.queuepointReached.bind( videoManager );
