/*	CLASSE do PANEL
-------------------------------------------*/
function PanelWidget( containerDomElement, abaIndex, boolSelect )
{
	if( abaIndex )
	{
		this.abaIndex = abaIndex;
	}
	else
	{
		this.abaIndex = 0;
	}
	this.container = $(containerDomElement);
	
	if( boolSelect )
	{
		this.combo = new Combo( $( "select.comboBox", this.container ) );
	}
	else
	{
		this.abas = new AbaGroup( $( "ul.widgetGroupAbas", this.container ) );
	}
	
	this.panel = new AjaxPanel( $( "div.widgetAjaxPanel", this.container ) );
	this.init();
}

PanelWidget.prototype.init = function()
{
	var that = this;
	
	if( this.abas )
	{
		this.abas.domElement.bind( "ABA_SELECTED", function( evt, data ) {
			that.handleAbaSelected( evt, data );
		} );
		
		this.abas.openAbaByIndex( this.abaIndex );
	}
	else if( this.combo )
	{
		this.combo.domElement.bind( "ITEM_SELECTED", function( evt, data ) {
			that.handleItemSelected( evt, data );
		} );
		
		this.combo.openItemByIndex( this.abaIndex );
	}
};

PanelWidget.prototype.handleAbaSelected = function( evt, data )
{
	this.abas.unselectAll();
	this.abas.updateLayout();
	data.select();
	this.panel.load( data.link );
};

PanelWidget.prototype.handleItemSelected = function( evt, data )
{
	this.panel.load( data );
};


/* CLASSE da COMBOBOX
------------------------------*/
function Combo( comboElement )
{
	that = this;
	this.domElement = comboElement;
	comboElement.change( function( evt )
		{
			that.handleItemSelected( evt, this.value );
		}
	);
}

Combo.prototype.handleItemSelected = function( evt, value )
{
	this.domElement.trigger( "ITEM_SELECTED", value );
}

Combo.prototype.openItemByIndex = function( selectedIndex )
{
	elm = $( "option", this.domElement )[ selectedIndex ];
	
	elm.selected = true;
	
	this.domElement.trigger( "ITEM_SELECTED", elm.value );
}


/*	CLASSE do CONTAINER DAS ABAS
-------------------------------------------*/
function AbaGroup( groupAbas )
{
	this.abasArray = [];
	this.domElement = $(groupAbas);//$( "<ul class='widgetGroupAbas'></ul>" );
	
	var that = this;
	
	$( "li", groupAbas ).each(function() {
		var aba = new Aba(this);	
		
		aba.domElement.bind( "SELECTED", function(evt, data){
			that.handleAbaSelected( evt, data );
		} );
		
		that.abasArray.push( aba );
		that.domElement.append(aba.domElement);
	});
}

AbaGroup.prototype.handleAbaSelected = function( evt, data )
{
	this.domElement.trigger( "ABA_SELECTED", data );	
};

AbaGroup.prototype.unselectAll = function()
{
	$(this.abasArray).each(function(){
		this.unselect();
	});	
};

AbaGroup.prototype.updateLayout = function()
{
	var length = this.abasArray.length;
	var zIndex = length - 1;
	for ( i = 0; i < length; i++ )
	{
		var element = this.abasArray[i];
		element.domElement.css( {"z-index":zIndex} );
		zIndex--;
	}
}

AbaGroup.prototype.openAbaByIndex = function( indexValue )
{
	this.abasArray[indexValue].open();
};








/*	CLASSE da ABA
-------------------------------------------*/
function Aba( abaElement )
{
	var that = this;
	this.link =$( "a", abaElement ).attr("href");
	this.value = $( "a", abaElement ).html();
	this.domElement = $(abaElement);
	this.linkElement = $( "a", abaElement );
	
	this.linkElement.bind( "click", function(evt) {
		that.handleClick( evt );								   
	} );
}

Aba.prototype.handleClick = function( evt )
{
	evt.preventDefault();
	evt.stopPropagation();
	
	this.open();
};

Aba.prototype.open = function()
{
	this.domElement.trigger( "SELECTED", [this] );
};

Aba.prototype.select = function()
{
	this.linkElement.addClass( "widgetAbaSelected" );	
	this.domElement.addClass( "moveUp" );
};

Aba.prototype.unselect = function()
{
	this.linkElement.removeClass( "widgetAbaSelected" );	
	this.domElement.removeClass( "moveUp" );
};






/*	CLASSE do PANEL AJAX
-------------------------------------------*/
function AjaxPanel( ajaxPanel )
{
	this.domElement = ajaxPanel;
}

AjaxPanel.prototype.load = function( contentURL )
{
	var that = this;
	this.domElement.load( contentURL, {}, function(){ that.domElement.trigger( "CONTENT_LOADED" ) } );
};

