/* 
   Copyright 2008 Stanziq, Inc.

*/


Peeq.Application.prototype = {

    run: function(username,password) {
	if(!this.connected) 
	{

	    //if username and password are not specified log in anonymously
	    if(!username && !password)
	    {
		username = Peeq.XMPP_DOMAIN;
		password = "password";
		this.anonymous = true;
	    }
	    var connection_callback = app._connectCallback;
	    if (app.connection.service === Peeq.PREBIND_URL)
	    {
		
		connection_callback = app._prebindCallback;
		//make ajax call to prebind
		var prebindreq = "<body rid='"+app.connection.rid+"' to='"+Peeq.XMPP_DOMAIN+"' wait='"+Peeq.DEFAULT_WAIT+"' hold='"+Peeq.DEFAULT_HOLD+"' />";
		jQuery.ajax({type:"POST",
				      processData:false,
				      url:Peeq.PREBIND_URL,
				      data:prebindreq,
	     error:function(a,b,c) { 
		console.error("Error with pre-bind.");
		app.connection.service = Peeq.BOSH_URL;
		app.run(username,password);
		},
				      success:connection_callback});
	    }
	    else
	    {
		this.connection.connect(username,
					password,
					connection_callback,
					Peeq.DEFAULT_WAIT,
					Peeq.DEFAULT_HOLD,
					Peeq.DEFAULT_WINDOW,
					Peeq.DEFAULT_ROUTE);
	    }	    
	}
	//add message handler
	this.connection.addHandler(app._handleMessage,
				   null,
				   'message',
				   null,
				   null,
				   null);
	
    },
    reconnect: function() {
	if(!app.connected)
	{
	    app.run();
	    //generate reconnect event and run application
	    jQuery(document).trigger('peeq_reconnect',[]);

	}
    },
    disconnect: function(cond) {
	if(this.connected)
	{
	    this._disconnected = true;
	    this.connected = false;
	    app.connection.disconnect();
	    //generate reconnect event and run application
	    jQuery(document).trigger('peeq_disconnect',[]);

	}
    },    
    authFail: function(cond) {
	this._authFail = true;
	this.disconnect();
	jQuery(document).trigger('peeq_authfail',[]);

    },
    connecting: function(cond) {
      //do nothing
    },
    setConnected: function(cond) {
	this.connected = true;
	this._disconnected = false;

	var presence = $pres({from:app.connection.jid}).tree();
	
	app.connection.send(presence);

	//generate reconnect event and run application
	jQuery(document).trigger('peeq_connected',[cond]);

    },
    disconnected: function(cond) {

	this._disconnected = true;
	this.connected = false;
	jQuery(document).trigger('peeq_disconnected',[cond]);
    },
    disconnecting: function(cond) {

	if(!app.connected)
	{
	    jQuery(document).trigger('peeq_disconnecting',[cond]);
	}
    },
    connFail: function(cond) {
	var xmpp_error_message = "";

	if (cond == "remote-connection-failed" && !this.connected)
	{
	    xmpp_error_message = "Error: Remote connection failed. Please check the domain portion of your username and try again.";
	}
	else if (cond == "remote-connection-failed" && this.connected)
	{
	    xmpp_error_message = "Error: Remote connection failed.";
	}
	else if (cond == "conflict")
	{
	    xmpp_error_message = "Error: You have been logged out due to a second login attempt from the same JID and resource.";
	}
	else if (cond == "bad-service")
	{
	    xmpp_error_message = "Error: Unable to contact web server."
	}
	else
	{
	    if(!app._disconnected)
	    {
		xmpp_error_message = "Error: An unknown error occurred. Please try again, and if this problem persists, contact support.";
	    }
	}
	
	if(xmpp_error_message.length != 0)
	{

	    jQuery(document).trigger('peeq_connection_fail',[xmpp_error_message]);

	}	
    },
    _prebindCallback: function(status) {
	var jid = $(status).find("jid").text();
	var sid = $(status).find("body").attr("sid");
	var rid = parseInt($(status).find("body").attr("rid"))+1;
	app.connection.service = Peeq.BOSH_URL;
	
	app.connection.attach(jid,sid,rid,app._connectCallback);
	app.setConnected();
    },
    _connectCallback: function(status,cond) {

	var _statusMap = {
	    0: app.connFail,
	    //1: app.connecting,   //Strophe.Status.CONNECTING
	    2: app.connFail,     //Strophe.Status.CONNFAIL
	    3: app.connecting,
	    4: app.authFail,     //Strophe.Status.AUTHFAIL
	    5: app.setConnected, //Strophe.Status.CONNECTED
	    6: app.disconnected, //Strophe.Status.DISCONNECTED
	    7: app.disconnecting //Strophe.Status.DISCONNECTING
	};

        var callme = _statusMap[status];
	if(callme)
	{
	    callme.apply(app,[cond]);
	}
    },

    _handleMessage: function(stanza) {

	if(app.subscribing === false)
	{
	    var data = [stanza];
	    jQuery(document).trigger('peeq_new_message',data);
	}
	return true;
    }
};
