/*!
illuminate.js - funky box shadow blinkiblinki 

some code copied verbatim from jQuery UI (http://jqueryui.com):
Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
Dual licensed under the MIT or GPL Version 2 licenses. <-- WE CHOOSE THE MIT LICENSE HERE
http://jquery.org/license
http://docs.jquery.com/UI 

some code reused/adapted from jquery-cssHooks (https://github.com/brandonaaron/jquery-cssHooks/blob/master/boxshadow.js):
Copyright (c) 2010 Burin Asavesna (http://helloburin.com)
Licensed under the MIT License (https://github.com/brandonaaron/jquery-cssHooks/blob/master/LICENSE.txt).

*/

//get a color as RGB array
function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
				return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
				return [parseInt(result[1],10), parseInt(result[2],10), parseInt(result[3],10)];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
				return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
				return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
				return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Look for rgba(0, 0, 0, 0) == transparent in Safari 3
		if (result = /rgba\(0, 0, 0, 0\)/.exec(color))
				return [255,255,255];

		return [255,255,255];
}


(function($){

    // boxShadow get hooks
    var div = document.createElement('div'),
        divStyle = div.style,
        support = $.support,
        rWhitespace = /\s/,
        rParenWhitespace = /\)\s/;

    //determine the browser specific attribute for boxShadow
    support.boxShadow =
        divStyle.MsBoxShadow     === ''? 'MsBoxShadow'     :
        (divStyle.WebkitBoxShadow === ''? 'WebkitBoxShadow' :
        (divStyle.OBoxShadow      === ''? 'OBoxShadow'      :
        (divStyle.BoxShadow       === ''? 'BoxShadow'       :
        (divStyle.boxShadow       === ''? 'boxShadow'       :
        (divStyle.MozBoxShadow     === ''? 'MozBoxShadow'    :
        false)))));
    
    div = null;
    
    // helper function to inject a value into an existing string
    // is there a better way to do this? it seems like a common pattern
    function insert_into(string, value, index) {
        var parts  = string.split(rWhitespace);
        parts[index] = value;
        return parts.join(" ");
    }
    
    if ( support.boxShadow ) {
        
        //boxShadow css hook
        $.cssHooks.boxShadow = {
            get: function( elem, computed, extra ) {
                return elem.style[support.boxShadow];
            },
            set: function( elem, value ) {
                
                elem.style[support.boxShadow] = value;
            }
        };
        
        //boxShadow blur css hook
        $.cssHooks.boxShadowBlur = {
            get: function ( elem, computed, extra ) {
                return $(elem).css("boxShadow").split(rWhitespace)[2];
            },
            set: function( elem, value ) {
                var val = insert_into($.css(elem, "boxShadow"), value, 2);
                $(elem).css('boxShadow', val);
            }
        };
        
        //box shadow color css hook
        $.cssHooks.boxShadowColor = {
            get: function ( elem, computed, extra ) {
                
                var arr = $.css(elem, "boxShadow").split(rWhitespace);
                
                var val =  arr[3]+' '+arr[4]+' '+arr[5];
                
                return val;
            },
            set: function( elem, value ) {
                
                var arr = $.css(elem, "boxShadow").split(rWhitespace);
                var newval =  arr[0]+ ' '+ arr[1]+' '+ arr[2]+' '+ value;
                
                $(elem).css('boxShadow', newval);
                
            }
        };
        
        //fix webkit boxshadow* hooks
        if (support.boxShadow == 'WebkitBoxShadow') {
            $.cssHooks.boxShadowBlur = {
                get: function ( elem, computed, extra ) {
                    var val = $(elem).css("boxShadow").split(rWhitespace)[5];
                
                    return val;
                },
                set: function( elem, value ) {
                    var val = insert_into($.css(elem, "boxShadow"), value, 5);
                    $(elem).css('boxShadow', val);
                }
            };
        
            $.cssHooks.boxShadowColor = {
                get: function ( elem, computed, extra ) {
                    var arr = $.css(elem, "boxShadow").split(rWhitespace);
                    var val =  arr[0]+' '+arr[1]+' '+arr[2];
                    return val;
                },
                set: function( elem, value ) {
                    var arr = $.css(elem, "boxShadow").split(rWhitespace);
                    var newval =  value + arr[3]+ ' '+ arr[4]+' '+ arr[5];
                    $(elem).css('boxShadow', newval);
                }
            };
        }
        
        //blur step function for $.animate
        $.fx.step[ "boxShadowBlur" ] = function( fx ) {
            $.cssHooks[ "boxShadowBlur" ].set( fx.elem, fx.now + fx.unit );
        };
        
        //color step function for $.animate
        $.fx.step["boxShadowColor"] = function(fx) {
            
            if (!fx.colorInit) {
                fx.start = getRGB($.cssHooks[ "boxShadowColor" ].get(fx.elem));
                fx.end = getRGB(fx.end);
                fx.colorInit = true;
            }
            $.cssHooks["boxShadowColor"].set(fx.elem, 'rgb(' +
                Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0], 10), 255), 0) + ',' +
                Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1], 10), 255), 0) + ',' +
                Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2], 10), 255), 0) + ')'
            );
        };
    }
})(jQuery);

