/*
 *               ____
 *              /\   \
 *             ___\   \___
 *            /\          \
 *            \ \___    ___\
 *        ____ \/__/\   \__/
 *       /\   \    \ \___\    klof  |  innovative web technology
 *      ___\   \___ \/___/
 *     /\          \          keen/style.js
 *     \ \___    ___\         HTML Element style manipulation
 *      \/__/\   \__/
 *          \ \___\           Copyright 2003-2007, klof
 *           \/___/           http://keen.klof.net/
 *
 */


/* Create the master Keen object if it doesn't exist */
if ( typeof Keen != "object" )
	Keen = new ( function(){ this._child = 0 } )();



/**
 *  K.Lib3 Style Object
 *  @name    Keen.style
 *  @type    constructor
 *  @access  public
 *  @param   string  template path [optional]
 *  @param   string  compilation path [optional]
 *  @returns object
 *  @syntax  object = &new Keen.style()
 */
Keen.style = function()
{
	this._version = "1.0.1";
};
/**
 *  Get the value of a CSS property
 *  @name    get
 *  @type    method
 *  @access  public
 *  @param   object html element
 *  @param   string style property
 *  @returns string
 *  @syntax  object.get( oElement, "background-color" );
 *  @note    style property can be either the CSS or JavaScript style property syntax
 */
Keen.style.prototype.get = function( oElement, sProperty )
{
	if ( typeof document.defaultView == "object" && typeof document.defaultView.getComputedStyle != "undefined" )
	{
		var oCS = document.defaultView.getComputedStyle( oElement, "" );
		if ( oCS && oCS.getPropertyValue )
			return oCS.getPropertyValue( this.cssProperty( sProperty ) );
	}
	else if ( oElement.currentStyle )
	{
		return oElement.currentStyle[ this.scriptProperty( sProperty ) ];
	}
	return false;
};
/**
 *  Set the style property to a given value
 *  @name    set
 *  @type    method
 *  @access  public
 *  @param   object html element
 *  @param   string style property
 *  @param   mixed  property value
 *  @returns boolean
 *  @syntax  object.set( oElement, "background-color", "#f00" );
 */
Keen.style.prototype.set = function( oElement, sProperty, mValue )
{
	return oElement.style[ this.scriptProperty( sProperty ) ] = mValue;
};
/**
 *  Convert a style property in CSS format to that same property in script format
 *  @name    scriptProperty
 *  @type    method
 *  @access  public
 *  @param   string style property
 *  @returns string
 *  @syntax  object.scriptProperty( "background-color" ); // returns "backgroundColor"
 */
Keen.style.prototype.scriptProperty = function( sProperty )
{
	var n = 0;
	while( ( n = sProperty.indexOf( "-", n ) ) >= 0 )
		sProperty = sProperty.substr( 0, n ) + sProperty.charAt( ++n ).toUpperCase() + sProperty.substring( n + 1 );
	return sProperty;

//  didn't work in older Safari's/IE's (besides.. having a anonymous function for every hyphen in all properties handled is kinda brute)
//	return sProperty.replace( /(-)([a-z])/g, function( a, b, c ){ return c.toUpperCase(); } );
};
/**
 *  Convert a style property in script format to that same property in CSS format
 *  @name    cssProperty
 *  @type    method
 *  @access  public
 *  @param   string style property
 *  @returns string
 *  @syntax  object.cssProperty( "backgroundColor" ); // return background-color
 */
Keen.style.prototype.cssProperty = function( sProperty )
{
	return sProperty.replace( /([A-Z])/g, "-$1" ).toLowerCase();
};
/**
 *  Verifies whether a specific style property exists in the provided HTML element
 *  @name    propertyExists
 *  @type    method
 *  @access  public
 *  @param   object HTML element
 *  @param   string style property
 *  @returns boolean
 *  @syntax  object.propertyExists( oElement, "background-color" );
 */
Keen.style.prototype.propertyExists = function( oElement, sProperty )
{
	return ( typeof oElement.style[ sProperty ] != "undefined" );
};

// construct the Keen.style Object onto itself so we have access to it's members
Keen.style = new Keen.style;

