;(function(){

    /**
     * @class Invitation     
     */
    function Invitation( toast, data ) {
    
        if ( Invitation.current ){
        
            $.error( "Only one instance of an Invitation object may exist" );
        
        }

        Invitation.current = this;
        
        this.element = $(toast);
        
        this.data = $.extend({},data);

        this.init();

		var self = this;

    };

    Invitation.prototype = {

        init: function() {

            // Catch the click event for all anchor tags in a toast.
            this.element.delegate('a', 'click', {

                invitation:this

            }, _onAction);

			this.render();

        },
        
        // Host: Send an invitation to a guest.
        invite: function() {
        
            this.post(_invite)
 
        },
        
        // Guest: Acceptance of a host's invitation.
        accept: function() {

            this.post(_accept);
        
        },

        // Guest: Refusal of a host's invitation.
        reject: function() {
        
            this.post(_reject);
        
        },
        
        // Host: Cancels the invitation.
        cancel: function() {
        
            this.post(_cancel);
        
        },
        
        // Both: Closing the handshake.
        close: function() {
        
            this.post(_close);
        
        },
                
        update: function( data ) {
        
            $.extend( this.data, data );
            
            this.render();
                
        },
        
        // Renders any content stored in this.data.content;
        render: function() {
        
            this.element.html( this.data.content );
			if($.fn.gamePortal){
				$('div.toast a.launch-portal').gamePortal();
			}

        },
        
        // Resync the invitation with the data on the server.
        sync: function(){
        
            this.data.action = '/handshaking/poll'; //ew hardcoded...
        
            this.post(_sync);
               
        },
                
        post: function( success ){

            // If for some reason an Invitation method is fired before another one finishes, we won't make another request.
            if ( !this.xhr ){
            
                this.xhr = $.ajax({
                
                    url: this.data.action,
                    
                    data: this.data.model,
                    
                    type: 'POST', 
                    
                    success: $.proxy(success, this),
					
                    complete: $.proxy(_complete, this)
                  
                });

            }
        
        }

    };
     
    function _onAction( e ){
        
        var element = $(this), invite = e.data.invitation, data = element.data();

        // Check for a data-method attribute
        if (data && data.method && data.action){
        
            // Set the current action url of the Invitation instance
            invite.update({ action: data.action });
        
            // Call the function with the same name as the attribute value on the Invitation instance.
            invite[data.method]();
            
            // If this attribute is present, block the browser default action.
            return false;
        }
        
    }
        
    // When the host sends an invitation.
    function _invite( response ){

        this.update({ content:response.ViewMarkup });

    };
    
    // When a guest accepts an invitation
    function _accept( response ){

        this.update({ content:response.ViewMarkup });
    
    };

    // When a guest rejects an invitation
    function _reject( response ){

        this.update({ content:response.ViewMarkup });

    };
	
    
    // When either user closes an invitation window.
    function _close(){
    			
        // Call the underlying toast objects close method.
        this.element.data('toast').close();
        
        Invitation.current = null;
    
    };
	
	// When either user cancels an invitation
	function _cancel( response ){
					
		this.update({ content:response.ViewMarkup });
		
	}
    
    function _sync( response ){

        this.update({ content: response.ViewMarkup });
    
    }
    	
    function _complete(){
    
        this.xhr = null;

	}

    Chemistry.include({ Invitation:Invitation });

})();

if(typeof logc !== 'function'){
	function logc(inp){
		if (window.console){
			console.log(inp);
		}
		return inp;
	}
}
