function extractRGBValues(s) {
    var rgb = new Array(3);
    if (/^rgb/.test(s)) {
        //var re = /(\d*)/g;
        //s = re.exec(s);
        s = s.replace('rgb(', '');
        s = s.replace(')', '');
        var rgb = s.split(',');
        for (var i = 0; i < 3; i++) {
            rgb[i] = parseInt(rgb[i]);
        }
    } else if (/^#/.test(s)) {
        rgb[0] = parseInt(s.substring(1, 3), 16);
        rgb[1] = parseInt(s.substring(3, 5), 16);
        rgb[2] = parseInt(s.substring(5, 7), 16);
    }
    return rgb;
}

function easeInOut(minValue, maxValue, totalSteps, actualStep, powr) {
    var delta = maxValue - minValue;
    var stepp = minValue + (Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
    return Math.ceil(stepp);
}

function addFade(elem, cssProperty, startRGB, endRGB, finalColor) {
    doFade(elem, cssProperty, startRGB, endRGB, finalColor, 75, 20, 4);
}

function setColor(elem, cssProperty, color) {
    switch (cssProperty) {
        case 'background-color':
            elem.style.backgroundColor = color;
            break;
        case 'border-color':
            elem.style.borderColor = color;
            break;
        case 'color':
            elem.style.color = color;
            break;
    }
}

function doFade(elem, cssProperty, startRGB, endRGB, finalColor, steps, intervals, powr) {
    var interval = cssProperty + 'fadeInt';
    if (elem[interval]) {
        window.clearInterval(elem[interval]);
    }
    if (typeof startRGB == 'string') {
        startRGB = extractRGBValues(startRGB);
    }
    if (typeof endRGB == 'string') {
        endRGB = extractRGBValues(endRGB);
    }
    var actStep = 0;
    elem[interval] = window.setInterval(function() {
        // does not work in Safari...
        //$(elem).css(cssProperty, 'rgb(' + easeInOut(startRGB[0], endRGB[0], steps, actStep, powr) + ',' + easeInOut(startRGB[1], endRGB[1], steps, actStep, powr) + ',' + easeInOut(startRGB[2], endRGB[2], steps, actStep, powr) + ')');
        var rgb = 'rgb(' + easeInOut(startRGB[0], endRGB[0], steps, actStep, powr) + ',' + easeInOut(startRGB[1], endRGB[1], steps, actStep, powr) + ',' + easeInOut(startRGB[2], endRGB[2], steps, actStep, powr) + ')';
        setColor(elem, cssProperty, rgb);
        actStep++;
        if (actStep > steps) {
            // does not work in Safari...
            //$(elem).css(cssProperty, finalColor);
            setColor(elem, cssProperty, finalColor);
            window.clearInterval(elem[interval]);
        }
    }, intervals);
}

function toggleValue() {
    if (!this.defaultValue) {
        this.defaultValue = this.value;
    }
    if (this.value == this.defaultValue) {
        this.value = '';
    } else if (this.value == '') {
        this.value = this.defaultValue;
        this.className = this.className.replace(/was-typed-in/, '');
    }
}

// init document...
$(document).ready(function() {

    // enhance search
    var SEARCH_ID = '#s';
    $(SEARCH_ID).not('.was-searched').focus(toggleValue).blur(toggleValue);
    $(SEARCH_ID).keyup(function() {
        if ($(this).is('.was-typed-in')) {
            $(this).addClass('was-typed-in');
        }
    });
    $('#content p a[@href$="' + SEARCH_ID + '"]').click(function() {
        var search = $(SEARCH_ID)[0];
        if (search) {
            search.focus();
        }
        return false;
    });

    // add fade
    $('#content h2 a, .archive h3 a').mouseover(function() {
        addFade(this, 'background-color', [255, 222, 0], [255, 255, 255], 'transparent');
        addFade(this, 'color', [0, 0, 0], $(this.parentNode).css('color'), $(this.parentNode).css('color'));
    });

    // remove error styles if something was typed in
    $('.error input, .error textarea').change(function() {
        var p = this.parentNode;
        p.className = p.className.replace(/error/, '');
    });

});


// Enhancing IE with some essential things...
// Use Conditional Compiling to safely sniff IE/Win - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsConditionalCompilation.asp.

/*@cc_on

// Fix CSS background image flicker
document.execCommand("BackgroundImageCache", false, true);

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
function getIEVersion() {
    var rv = -1; // Return value assumes failure
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re  = new RegExp('MSIE ([0-9]{1,}[\.0-9]{0,})');
        if (re.exec(ua) != null) {
            rv = parseFloat(RegExp.$1);
        }
    }
    return rv;
}

$(document).ready(function() {

    // fix quotations...
    var INNER_Q_CLASS = 'innerq';
    $('q q').append('"').prepend('"').addClass(INNER_Q_CLASS);
    $('q:not(.' + INNER_Q_CLASS + ')').append('\'').prepend('\'');

    // whatever:hover...
    if (getIEVersion() <= 7) {
        $('.submit').hover(function() {
            this.className += ' submithover';
        }, function() {
            this.className = this.className.replace(/submithover/, '');
        });

        $('form').addClass('activated').hover(function() {
            this.className += ' formhover';
        }, function() {
            this.className = this.className.replace(/formhover/, '');
        });

        $('input[@type="text"], input[@type="password"], textarea').focus(function() {
            this.className += ' inputfocus';
        }).blur(function() {
            this.className = this.className.replace(/inputfocus/, '');
        });
    }

});
@*/