/*

        Copyright 2006-2008 OpenAjax Alliance

        Licensed under the Apache License, Version 2.0 (the "License"); 
        you may not use this file except in compliance with the License. 
        You may obtain a copy of the License at
        
                http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing, software 
        distributed under the License is distributed on an "AS IS" BASIS, 
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
        See the License for the specific language governing permissions and 
        limitations under the License.
*/
var inline = {};

(function() {

inline.getName = function()
{
  return "http://providers.openajax.org/inline";
}

inline.getVersion = function()
{
  return "1.0";
}

inline.configure = function( config )
{
  // TODO
}

inline.connect = function( params )
{
  if ( OpenAjax.hubspi.registerClient( this, params.clientName ) ) {
    params.callback( true, new InlineConnHandle( params.clientName ) );
    return;
  }
  // failure
  params.callback( false, null );
}

inline.isConnected = function( clientName )
{
  // TODO
}

inline.sendEvent = function( clientName, topic, data, matchingSubs )
{
  for ( var i = 0; i < matchingSubs.length; i++ ) {
    var s = _subs[ matchingSubs[i] ];
    s.cb( s.h, topic, data );
  }
}

var _subs = [];
var _subIndex = 0;

function InlineConnHandle( clientName )
{
  var id = clientName;
  
  this.isConnected = function()
  {
    // TODO
    return true;
  }
  
  this.equals = function( object )
  {
    // TODO
  }
  
  this.getClientName = function()
  {
    return id;
  }

  this.getManagerDomain = function() 
  { // return own domain since this is the manager
    return document.domain;
  }
  
  this.subscribe = function( topic, callback, eventCallback )
  {
    var subId = _subIndex++;
    _subs[ subId ] = { cb: eventCallback };
    if ( OpenAjax.hubspi.subscribeReq( id, topic, subId ) ) {
      _subs[ subId ].h = new SubscriptionHandle( this, topic, subId );
      callback( true, _subs[ subId ].h );
      return;
    }
    // failure
    callback( false, null );
  }
  
  this.publish = function( topic, data )
  {
    if ( !this.isConnected() ) {
      return false;
    }
    OpenAjax.hubspi.publishEvent( id, topic, data );
    return true;
  }
  
  this.disconnect = function( callback )
  {
    // TODO
  }
}

// handle which represents the subscription of a given client and topic
function SubscriptionHandle( connHandle, topic, handle )
{
  var _connHandle = connHandle;
  var _topic = topic;
  var _subId = handle;

  this.unsubscribe = function( callback )
  {
    OpenAjax.hubspi.unsubscribeReq( _connHandle.getClientName(), _subId );
    delete _subs[ _subId ];
  }

  this.isSubscribed = function()
  {
    // TODO
  }

  this.getConnHandle = function()
  {
    return _connHandle;
  }

  this.getTopic = function()
  {
    return _topic;
  }

  this.equals = function( object )
  {
    // TODO
  }
}

//-----------------------------------------------------------------------------------------------
// now register oneself
if ( typeof this["OpenAjax"] != "undefined" ) {
	if ( typeof this["OpenAjax"]["hubspi"] != "undefined" ) {
		if ( ! OpenAjax.hubspi.register( inline ) ) {
		  // XXX handle error
		  alert( "failed to register inline provider" );
		}
	}
}

})();

