/* -------------------------------------------------------------------------- */
/* -------------------------------- Pad ------------------------------------- */
/* -------------------------------------------------------------------------- */

Pad = function() {
	Pad.superClass.apply( this, arguments );

	this.Id = 'Pad';
	this.element = this.createNode( 'div', { 'class': 'pad-wrap hidden' }, document.body );
	this.table = this.createNode( 'table', { 'class': 'pad' }, this.element );
	this.inner = this.createNode( 'tbody', false, this.table );
	this.rows = new Array();
	this.data = false;
	this.current = false;
	this.showed = false;
	this.target = false;

	this.addHandler( window, 'resize', this.setPosition );
}
Pad.inheritsFrom( HTMLGlif );

/* ----------------------------- Construct ---------------------------------- */

Pad.prototype.construct = function( data ) {
	this.data = data || false;
	
	for( var i = this.rows.length; i; i-- ) {
		this.rows[ i - 1 ].remove();
		this.rows.splice( i - 1, 1 );
	}
	
	if( !this.data || this.data.length == 0 ) {
		this.data = false;
		this.show( false );
		return false;
	}

	for( var r in this.data ) {
		var rD = this.data[r];
		var newRow = this.createRow();
		for( var c in rD['HTML'] ) {
			var cHTML = rD['HTML'][c];
			var newCell = this.createNode( 'td', false, newRow.element, cHTML );
		}
	}
	
	this.setClassName( 'overflow', this.rows.length > 7 );

	if( this.rows[0] ) this.toggleHover( null, this.rows[0] );
}

/* ---------------------------- Create Row ---------------------------------- */

Pad.prototype.createRow = function() {
	var newRow = new HTMLGlif( 'tr', false, this.inner );
	newRow.idx = this.rows.length;
	this.rows[ this.rows.length ] = newRow;
	newRow.attachObserver( 'Hover', this.toggleHover, this );
	newRow.attachObserver( 'RowChosen', this.rowChosen, this );
	newRow.addHandler( newRow.element, 'mouseover', newRow.notify, 'Hover' );
	newRow.addHandler( newRow.element, 'click', newRow.notify, 'RowChosen' );
	return newRow;
}

/* ---------------------------- Toggle Hover -------------------------------- */

Pad.prototype.toggleHover = function( params, obj ) {
	if( this.current >= 0 && this.rows[ this.current ] ) {
		this.rows[ this.current ].setClassName( 'hover', false );
	}

	obj.setClassName( 'hover', true );
	this.current = obj.idx;
}

/* ----------------------------- Row Chosen --------------------------------- */

Pad.prototype.rowChosen = function( params, obj ) {
	var data = this.data[ obj.idx ];
	this.notify( 'DataChosen', data );
}

/* ---------------------------- Set Target ---------------------------------- */

Pad.prototype.setTarget = function( target ) {
	this.target = target;
}

/* ---------------------------- Show / Hide --------------------------------- */

Pad.prototype.show = function( flag ) {
	this.setClassName( 'hidden', !flag );
	this.showed = flag;

	var so = this.offset();
	var po = this.offset( this.target.element );
	if( !po ) return false;
	//this.setSize( po.width );
}

/* --------------------------- Set Position --------------------------------- */

Pad.prototype.getData = function() {
	return this.data;
}

Pad.prototype.setPosition = function( beacon ) {
	beacon = beacon || this.target.element;
	
	var so = this.offset();
	var po = this.offset( beacon );
	if( !po ) return false;
	var x = po.left;
	var y = po.top + po.height;
	if( this.element.firstChild.offsetWidth <= po.width ) {
		this.setSize( po.width );
	} else {
		this.setStyle({ width: 'auto' });
	}
	this.constructor.superClass.prototype.setPosition.call( this, x, y );
}

Pad.prototype.specialKeyPressed = function( code ) {
	if( !this.showed ) return;
	if ( code == 38 && this.current > 0 )  {											//up
		this.rows[ this.current - 1 ].notify( 'Hover' );
	}
	
	if ( code == 40 && this.current < ( this.rows.length - 1 ) ) {					// down
		this.rows[ this.current + 1 ].notify( 'Hover' );
	}
	
	if ( code == 13 && this.current >= 0 && this.rows[ this.current ] ) {				// enter
		this.rows[ this.current ].notify( 'RowChosen' );
	}
}
