if ( !nvi ) nvi = {};

nvi.onInitCallbacks = [];

// JavaScript Document
void nvi.eventManager.addListener( 'all' , nvi.eventManager.events.__pageInitialized , null , init ) ;

function init (){
	for ( var iCallback in nvi.onInitCallbacks ) nvi.onInitCallbacks[ iCallback ]();
}

/* Helpers 
-------------------------------------------------------------------------------------------- */
function nvl ( object , key , value ){
    if ( !pln.isDefined( object[ key ] ) ) object[ key ] = value;
    return object[ key ];
};

function create ( tag , properties , content , parent ){
    var element = pln.node.create( tag );
    
    if ( pln.isObject( properties ) ){
        try{
            for ( var i in properties ) void pln.node.setProperty( element , i , properties[ i ] );
        }catch( error ){}
    }
    
    if ( pln.isString( content ) )              void pln.node.setProperty( element , 'innerHTML' , content );
    else if ( pln.isHtmlElement( content ) )    void pln.node.add( content , element );
    
    if ( pln.isHtmlElement( parent ) ) void pln.node.add( element , parent );
    
    return element;
};

/* Class:Slider
-------------------------------------------------------------------------------------------- */
var Slider = function ( slidesId , options ){
    void nvi.onInitCallbacks.push( function (){
        // Options 
        if ( !pln.isObject( options ) ) options = {};
        void nvl( options , 'previousId' , null );
        void nvl( options , 'nextId' , null );
        void nvl( options , 'count' , 1 );
        void nvl( options , 'slideClassName' , 'slide' );
        void nvl( options , 'delay' , null );
        
        // if slider or navigation doesn't exists
        if ( 
            !pln.isHtmlElement( pln.node.getById( slidesId ) ) ||
            !pln.isHtmlElement( pln.node.getById( options.previousId ) ) ||
            !pln.isHtmlElement( pln.node.getById( options.nextId ) )
        ) return;
        
		
        var container    = pln.node.getById( slidesId );
        var children     = pln.node.getByAttributeValue( 'class' , options.slideClassName , container , true );
        
        // add <table> structure
        var table     = create( 'table' , { position : 'relative' } , null , container );
        var tbody     = create( 'tbody' , null , null , table );
        var row        = create( 'tr' , null , null , tbody );
        
        for ( var child in children ){
            var cell = create( 'td' , null , children[ child ] , row );
        }
        
        // needded CSS
        void pln.node.setProperty( container , 'overflow' , 'hidden' ); 
        void pln.node.setProperty( container , 'position' , 'relative' ); 
    
        // initialize
        var slides = new NVIBasicScroll( slidesId );
        
        if ( options.delay > 0 ) void slides.setDelay( options.delay * 1000 );
        
        void slides.registerPreviousButton( options.previousId , options.count );
        void slides.registerNextButton( options.nextId , options.count );
        void slides.initialize();
    } );
};
