/*
Script: kap_cssFunctions.js
    KickApps CSS Functions (for styling)

General Information:
    Company - <http://www.kickapps.com>
    Created - Wednesday, April 04, 2007 | 5:39 PM

Author:
    Oliver Nassar - onassar@kickapps.com

Editing:
    onassar - Wednesday, April 04, 2007 | 5:52 PM
    onassar - Wednesday, April 05, 2007 | 8:57 AM
    onassar - Thursday, May 31, 2007 	| 12:23 PM

Notes:
    - Responsible for most css style changes which are manipulated via JavaScript
    - kap_mootools.js *independent*
*/

/*
Function: kap_translateColor
    Translates a color from one version to another

Arguments:
    color - the color (hex, rgb or natural name) which should be translated
    type - what the color should be translated to (e.g. hex, rgb, natural)

Notes:
    - This function returns the translated color and does *not* depend on any other functions of libraries

Returns:
    The new translated color
*/

    // function to convert a given color to another color
    function kap_translateColor(color,type)
    {
        // if it's an rgb
        if(color.indexOf('rgb')>=0)
        {
            // if it should be converted to a hex code
            if(type=='hex')
            {
                // if it's null
                if(color==null)
                {
                    // return black
                    return '#000000';
                }
                // access the red color
                redColor        =    color.split('rgb(');
                redColor        =    redColor[1].split(',');
                redColor        =    redColor[0];
                // access the green color
                greenColor      =    color.split('rgb(');
                greenColor      =    greenColor[1].split(',');
                greenColor      =    greenColor[1];
                // access the blue color
                blueColor       =    color.split('rgb(');
                blueColor       =    blueColor[1].split(',');
                blueColor       =    blueColor[2].split(')');
                blueColor       =    blueColor[0];

                // convert the colors
                redColor      =    Math.max(0,redColor);
                redColor      =    Math.min(redColor,255);
                redColor      =    Math.round(redColor);
                color         =    "0123456789ABCDEF".charAt((redColor-redColor%16)/16) + "0123456789ABCDEF".charAt(redColor%16);
                greenColor    =    Math.max(0,greenColor);
                greenColor    =    Math.min(greenColor,255);
                greenColor    =    Math.round(greenColor);
                color        +=    "0123456789ABCDEF".charAt((greenColor-greenColor%16)/16) + "0123456789ABCDEF".charAt(greenColor%16);
                redColor      =    Math.max(0,blueColor);
                redColor      =    Math.min(blueColor,255);
                redColor      =    Math.round(blueColor);
                color        +=    "0123456789ABCDEF".charAt((blueColor-blueColor%16)/16) + "0123456789ABCDEF".charAt(blueColor%16);
                // return it
                return '#'+color;
            }
            // otherwise just return the original color
            return color;
        }
        // otherwise if it's a hex code
        else if(color.indexOf('#')>=0)
        {
            // if it should be converted to a rgb code
            if(type=='rgb')
            {
                redCode       =       parseInt(color.substring(1,7).substring(0,2),16);
                greenCode     =       parseInt(color.substring(1,7).substring(2,4),16);
                blueCode      =       parseInt(color.substring(1,7).substring(4,6),16);
                // return it
                return 'rgb('+redCode+','+greenCode+','+blueCode+')';
            }
            // otherwise just return the original color
            return color;
        }
        // otherwise it is a color name
        else
        {
            // array for generic conversions
            colorCodes               =     new Array();
            colorCodes['aqua']       =     '#00FFFF';
            colorCodes['black']      =     '#000000';
            colorCodes['blue']       =     '#0000FF';
            colorCodes['fuchsia']    =     '#FF00FF';
            colorCodes['gray']       =     '#808080';
            colorCodes['green']      =     '#008000';
            colorCodes['lime']       =     '#00FF00';
            colorCodes['maroon']     =     '#800000';
            colorCodes['navy']       =     '#000080';
            colorCodes['olive']      =     '#808000';
            colorCodes['purple']     =     '#800080';
            colorCodes['red']        =     '#FF0000';
            colorCodes['silver']     =     '#C0C0C0';
            colorCodes['teal']       =     '#008080';
            colorCodes['white']      =     '#FFFFFF';
            colorCodes['yellow']     =     '#FFFF00';

            // convert it to a hex code right away
            hexCode                  =     colorCodes[color];

            // if the hex code ought to be returned
            if(type=='hex')
            {
                // return it
                return hexCode;
            }
            // if it should be converted to an rgb code
            else if(type=='rgb')
            {
                // return the hex code translated into the rgb code
                return kap_translateColor(hexCode,'hex');
            }
            // otherwise just return the original color
            return color;
        }
    }

/*
Function: kap_getCSSValue
    Determines a specific CSS value for a desired field

Arguments:
    element - the element which ought to have a property determined
    property - the CSS attribute/property which ought to be returned

Notes:
    - This function is different from retrieving an elements style via the [element].style.[property] or mootools equivalent [element].getStyle('[property]') functions;
    namely, it gets the *computed* value, which the browser holds after *all* css has and page processes have been _loaded_

Returns:
    The value of the property for that element
*/

    // function to access a css property's value
    function kap_getCSSValue(element,property)
    {
        // access the element
        element              =    document.getElementById(element);

        // if it's IE
        if(element.currentStyle)
        {
            // if the property has a hyphen
            if(property.split('-').length>1)
            {
	            IEProperty       =    property.split('-');
	            IEProperty       =    IEProperty[0]+IEProperty[1].charAt(0).toUpperCase()+IEProperty[1].substring(1,IEProperty[1].length);
            }
            // otherwise no hyphen
            else
            {
            	// set the IEProperty value
            	IEProperty		 =	  property;
            }
            // get the property value
            propertyValue    =    element.currentStyle[IEProperty];
        }
        // otherwise if it's firefox
        else if(window.getComputedStyle)
        {
            // get the property value
            propertyValue    =    document.defaultView.getComputedStyle(element,null).getPropertyValue(property);
        }

        // return the hex code
        return propertyValue=='transparent' ? 'transparent' : propertyValue;
    }

/*
Function: kap_getColorOpacity
    Gets the color for an arguments color, and it's associated opacity

Arguments:
    color - the color who's opacity is to be returned
    opacity - the opacity rate for that color
    backgroundColor - the mask to which the new color shoulpd be rendered against

Notes:
    - Cross browser and independent of mootools and any other function

Returns:
    The new color at the proper rate of the opacity sent in
*/

    // function to retrieve an opacity of a color
    function kap_getColorOpacity(color,opacity,backgroundColor)
    {
        // color to be returned as array
        returnColor    =    new Array();
        // loop through the color sent in (since array)
        for(i=0; i< color.length; i++)
        {
        	// store the color's (via rgb positioning) oppacity color
            returnColor[i] = Math.round(color[i]*opacity) + Math.round(backgroundColor[i]*(1.0-opacity));
        }
        // return the faded color
        return returnColor;
    }

/*
Function: kap_explodeRGB
    Explodes an RGB sent in to an array

Arguments:
    color - a color in RGB format

Notes:
    - Should be expanded to allow the color to be anything, and have it checked and translated to an rgb as needed

Returns:
    The rgb sent in as a string as an *array*
*/

    // function to get an rgb code as an array
    function kap_explodeRGB(color)
    {
        // get the rgb codes by splitting
        red      =    color.split('(');
        red      =    red[1].split(',');
        green    =    red[1];
        blue     =    red[2].split(')');
        blue     =    blue[0];
        red      =    red[0];
        // return the array
        return new Array(red,green,blue);
    }

/*
Function: kap_implodeRGB
    Implodes an rgb array into a string

Arguments:
    color - an *array* which is needed as a string

Notes:
    - Function should be expanded to allow any color to be sent in, and have it detected, and then if needed converted to an rgb and returned as a string

Returns:
    The rgb color as a string (coming in from an array)
*/

    // function to get an rgb array as a string
    function kap_implodeRGB(color)
    {
        // return it right away
        return 'rgb('+color[0]+','+color[1]+','+color[2]+')';
    }