/*
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 *
 * Copyright 1996-2008 by Sven Homburg
 *
 * 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
 */

var TabSet = Class.create();
TabSet.prototype = {

    lastActivatedPanelId: null,

    initialize: function()
    {
    	// tabSetId, panelArray, activeId, actionLink, eventName
		this.options = Object.extend({
      		eventName:"click"   
    		}, arguments[0] || {});
        
        this.options.panelArray = this.options.panelArray.split(",");
        
        this.options.panelArray.each(function(element)
        {
        	
        	$('panel_' + element).observe(this.options.eventName, this.activate.bindAsEventListener(this));
            //$('panel_' + element).onclick = this.activate.bindAsEventListener(this);
            if (this.options.activeId != element)
                $(element).hide();
            else
            {
                $(element).show();
                this.lastActivatedPanelId = 'panel_' + element;
            }

        }.bind(this));
    },
    activate: function(evt)
    {
        var clickedPanel = Event.element(evt).id;
        var clickedPanelContent = clickedPanel.split("_")[1];
        this.deactivate(this.lastActivatedPanelId);
		
        $(clickedPanelContent).show();
        $(clickedPanel).addClassName("activated");
        this.lastActivatedPanelId = clickedPanel;
		/* Disabled by ningdh
        new Ajax.Request(this.options.actionLink + "/" + clickedPanelContent, {
            method: 'post',
            onFailure: function(t)
            {
                alert(t.responseText);
            },
            onException: function(t, exception)
            {
                alert(exception);
            }
        });*/
    },
    deactivate: function(panelId)
    {
        var panelContent = panelId.split("_")[1];
        $(panelContent).hide();
        $(panelId).removeClassName("activated");
    }
}