/*
Sky.DOMUtil 
copyright by MySign AG (http://www.mysign.ch), Marcus Fihlon
depencies: Prototype 1.6
*/

// creating namespace
var Sky;
if ( !Sky ) Sky = {};

// specifying class
Sky.DOMUtil = Class.create();

Sky.DOMUtil._REQUIRED_PROTOTYPE = '1.6.0.2';

Sky.DOMUtil.prototype =
{
	// initialization of this class
	// options:
	//     startelement - the element where to start in the DOM
	//                    (defaults to the body container)
	initialize: function ( opts )
	{
		// checking Sky.PrototypeExtensions
		if( Object.isUndefined( Sky.PrototypeExtensions ) )
		{
			throw( "Sky.DOMUtil requires the Sky.PrototypeExtensions JavaScript framework" );
		}
		// checking Prototype
		if( !Sky.PrototypeExtensions.isPrototypeLoaded( Sky.DOMUtil._REQUIRED_PROTOTYPE ) )
		{
			throw( "Sky.DOMUtil requires the Prototype JavaScript framework >= " + Sky.DOMUtil._REQUIRED_PROTOTYPE );
		}
	
		// default options
		this.options = 
		{
			'startelement' : $( 'bodyContainer' )
		}
		Object.extend( this.options, opts || {} );
	},

	// modify the DOM
	// options:
	//     tag - the name of the tags to be modified
	//     attribute - the attribute of the tags to be modified
	//     value - the new value of the attribute
	//     startelement - the element where to start in the DOM
	//                    (defaults to the body container)
	modify: function ( opts )
	{
		// extend the options
		Object.extend( this.options, opts || {} );

		// walk through the elements...
		var elements = this.options.startelement.select( this.options.tag );
		elements.each(

			// ...and modify them
			function( element )
			{
				element.setAttribute( this.options.attribute, this.options.value );
			}.bind( this )
		);
	},

	// callback for every element in the DOM
	// options:
	//     tag - the name of the tags
	//     function - the function to be called for each found element
	//                (will get the found element as parameter)
	//     startelement - the element where to start in the DOM
	//                    (defaults to the body container)
	callback: function ( opts )
	{
		// extend the options
		Object.extend( this.options, opts || {} );
		
		// walk through the elements and call the specified function
		var elements = this.options.startelement.select( this.options.tag );
		elements.each( this.options.callbackfunc );
	}
};

// For compatibility reasons
var My;
if( !My ) My = {};
My.DOMUtil = Sky.DOMUtil;
