/**
* AJAX Tabs
* (c) 2008 Dan Ladds <dan@danladds.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/

var Ajax_Tabset = Class.create (
{
	midChange : function () {},

    // Load a given URL in the tabs
    goto : function (url, method, params)
    {
        // Defaults
        if (!method) method = 'get';
        if (!params) params = '';
        
        var tabs = this;
        
        new Ajax.Request
	    (
	        url,
	        {
	            method: method,
	            parameters: params,
	            evalScripts: true,
	            onSuccess: function (transport)
	            {
	            	// Fade old content out
					Effect.Fade 
					(
						tabs.id,
						{
							from : 1,
							to : 0.1,
							duration: 0.7,
							queue : 'end',
							afterFinish : function ()
							{
								$(tabs.id).update (transport.responseText);
							
								// Fade new content in
        						Effect.Appear (tabs.id, { queue : 'end', from : 0.1, to : 1, duration: 1 });
							}
						}
					);
	            
	            	tabs.midChange ();
	            	tabs.midChange = function () {};
	            }
	        }
	    );
    },
    
    
    // Submit a for via ajax
    submitform : function (form_element)
    {
        this.goto 
        (
            $(form_element).action, 
            $(form_element).method, 
            $(form_element).serialize ()
        );
        
        return false;
    },
    
    
    // Change to a tab
    changeto : function (id)
    {
        // Remove active class
        $$('a[rel=' + this.id + ']').each
        (
            function (item, index)
            {
                item.removeClassName ('onlink');
            }
        );
        
        // Add active class
        $(id).addClassName ('onlink');
    
    	// Get name and URL
    	var parts	= $(id).rev.split (' ');
    	var url 	= parts[0];
    	var name	= parts[1];
    
    	// Change bg before fading back in
        this.midChange  = function ()
        {
    		$$('.bgarea')[0].id = name + '-background';
    	}
    
        // Load content
        this.goto (url);

        return false;
    },
    
    
    // Constructor
    initialize : function (id)
    {
        this.id = id;
        
        // Keep object scope
        var s = this;
    
        // Get all tabs
        $$('a[rel=' + id + ']').each
        (
            function (item, index)
            {
                var a = function ()
                {
                    s.changeto (item);
                }
            
                item.observe ('click', a);
                item.observe ('tab:changeto', a);
                item.onclick = function () 
                {
                    return false;
                }
            }
        );
    }
});
