var classes = ["lightest", "light", "mid", "dark", "darkest"];
var colors = new Array();

google.load("search", 1);

$(document).ready(function(){
	
	dp.SyntaxHighlighter.ClipboardSwf = "js/clipboard.swf";
	dp.SyntaxHighlighter.HighlightAll("code");

    applyClasses();
    
	// get some colors
	var colors = ["zen", "ocean", "mist", "pea", "grasshopper", "kiwi"];
	var theme = colors[Math.floor(Math.random() * colors.length)];
	var key = "E61EE908708850BC58964C82FB4B2065";
	$.ajax({type: "GET", 
						url: "http://timrobles.com/serviceproxy.php",
						data: {url: "http://kuler-api.adobe.com/rss/search.cfm?searchQuery=tag:" + theme + 
						            "&itemsPerPage=5&key=" + key}, 
						dataType: "xml",
						success: successHandler,
						error: errorHandler
						});
	
	$("span.autolink").each(autolink);

});

function applyClasses()
{
    $("h1,h2").addClass("darkest");
    $("#posts").addClass("dark");
    $("a").addClass("mid");
    $(".post").addClass("bg-lightest");
    $("#sidebar a").addClass("bg-light");
}

function autolink(i)
{
	var searchControl = new google.search.SearchControl();
	searchControl.addSearcher(new google.search.WebSearch());
	searchControl.draw();
	searchControl.term = $("span.autolink").eq(i).text();
	searchControl.setSearchCompleteCallback(this, searchResultHandler);
	searchControl.execute(searchControl.term);
}

function searchResultHandler(searchControl, searcher)
{
    var result = searcher.results[0].url;
    var ele = $("span.autolink:contains('" + searchControl.term + "')");
    ele.html("<a href='" + result + "'>" + ele.text() + "</a>");
    applyClasses();
}

function errorHandler(XMLHttpRequest, textStatus, errorThrown)
{
	console.error("kuler error: " + textStatus + ", " + errorThrown);
}

function successHandler(result)
{
    $(result).find("kuler\\:themeSwatches").eq(Math.floor(Math.random() * 5))
             .find("kuler\\:swatchHexColor").each(function(i){
        colors.push(parseInt($(this).text(), 16));
    });
    var sort = function sortColors(a, b) {return b - a;}
    colors.sort(sort);
    
    var testValue = 0x333333;
    if (Math.abs(colors[1] - colors[2]) < 0x333333)
        colors[2] += 0x333333;
        
    applyStyle();
    applyStyle("bg-", "background-color");
}

    
function applyStyle(prefix, attribute)
{    
    if (!attribute)
        attribute = "color";
    
    if (!prefix)
        prefix = "";
    
    if (!colors)
        return;
        
    for (var i = 0; i < 5; i++)
    {
        $("." + prefix + classes[i]).css(attribute, "#" + colors[i].toString(16));
    }
    $("a").hover(function(){
        
                    if (!this.outColor)
                        this.outColor = $(this).css("color");
                    
                    if (!this.overColor)
                    {
                        var color = rgbToColor($(this).css("color"));
                        this.overColor = "#" + (color + 0x222222).toString(16);
                    }
                    
                    $(this).css("color", this.overColor);
                 },
                 function(){
                    $(this).css("color", this.outColor);
                 });
}

function rgbToColor(value)
{
    var matches = value.match(/\d{2,3}/g);
    return ((parseInt(matches[0]) & 0xFF) << 16) + ((parseInt(matches[1]) & 0xFF) << 8) + (parseInt(matches) & 0xFF);
}