function RGB2HTML(red, green, blue)
{
    var decColor = red + 256 * green + 65536 * blue;
    return decColor.toString(16);
}
$(function(){
	var tilength = $('figure').length;
	if (tilength == 1)
	{
		$('a, h3, figcaption,#osimg').hide();
		$('h1 a').show();
	}
});
$(window).load(function(){ 
	var tilength = $('figure').length;
	$('#osimg').slideDown('slow');
	if (tilength == 1)
	{
		var cssColorToHex = function(colorStr){
	    	var hex = '#';
	    	$.each(colorStr.split(','), function(i, str){
	        	var h = ($.trim(str.replace(')',''))*1).toString(16);
	        	hex += (h.length == 1) ? "0" + h : h;
	    	});
	    	return hex;
		};

		myImage = $('#osimg');
		dominantColor = getDominantColor(myImage);
		paletteArray = createPalette(myImage, 10); // 2nd argument sets the # of colors in palette
		var tots = new Array;

		$.each(paletteArray, function(){
			var rgbtot = 0;
			var rgbstr = "";
			var colstring = this[0].toString() + "," + this[1].toString() + "," + this[2].toString();

			var colvals = colstring.split(",");
			for (var i = 0; i < 3; i++) {
				rgbtot = rgbtot + parseInt(colvals[i],10);
				rgbstr += colvals[i];
				if (i < 2)
				{
					rgbstr += ',';
				}			
			};	
			var hexcode = cssColorToHex(rgbstr);
			var temp = new Array();
			temp[0] = rgbtot;
			temp[1] = rgbstr;
			temp[2] = hexcode;
			tots.push(temp);
		});
		tots.sort();

		var hexcodefin = tots[2][2];
		var hexcodefin2 = tots[4][2];
		var rgbtotfin = tots[2][0];
		var rgbtotfin2 = tots[4][0];

		$('h2, h3, ficaption').css({'color':hexcodefin, 'border-color':hexcodefin}).show();
		$('a').css({'color':hexcodefin, 'border-color':hexcodefin}).show();
		if (rgbtotfin > 550)
		{
			$('h2, h3, ficaption').css({'text-shadow':'2px 2px 2px #000'});
		}
		if (rgbtotfin2 > 550)
		{
			$('a').css({'text-shadow':'2px 2px 2px #000'});
		}
		$('a').each(function(){
			$(this).hover(
				function(){
					$(this).css({'color':hexcodefin2,'border-color':hexcodefin2});
					if (rgbtotfin2 > 550)
					{
						$(this).css({'text-shadow': '2px 2px 2px #000'});
					}
					else
					{
						$(this).css({'text-shadow': '2px 2px 2px #fff'});
					}
				},
				function(){
					$(this).css({'color':hexcodefin,'border-color':hexcodefin});
					if (rgbtotfin2 > 550)
					{
						$(this).css({'text-shadow': '2px 2px 2px #000'});
					}
					else
					{
						$(this).css({'text-shadow': '2px 2px 2px #fff'});
					}
				}
			);
		});
		$('body').css({'border-top':'10px solid ' + hexcodefin});
		$('a, h2, h3, figcaption').show();
	}
});
