/* begin code copyright (c) 2010 squaredesign, all rights reserved */
function draw($) {
	// first we remove all old squares in case this is a redraw
	$(".square").remove();
	// hard-coded number of squares wide
	numX = 21;
	// width expressed in percent
	sqW = 100/numX;
	// initialize height
	sqH = "";
	//number of 'columns' set to hard-coded value
	sqX = numX;
	// number of 'rows' depends on the height of the container
	sqY = Math.floor($("#effects").height()/($("#effects").width()/numX))+1;

	var alt=0;
	var addCls = "";
	// loop that draws the squares
	for(var x = 0;x<sqX*sqY;x++) {
	if(alt%2) {addCls="alt";}
	//for(var x = 0;x<1;x++) {
		// start by cloning an existing element, matching its bg color to the body bg
		$("<div><span></span></div>")
		.css({
			width : sqW+"%",
			background : $("body").css("background-color")
		})
		.appendTo("#effects")
		.addClass("square")
		.addClass("sq"+x)
		.addClass(addCls);// .text(x)
		addCls="";
		alt++;
	}
	
	$("#effects").height(sqY*$(".sq0").width());
	/* so far we have a lot of flat squares. this sets all their heights equal to
		the width of the first one (which will read as px), and makes them visible */
	$(".square").height($(".sq0").width()).removeClass("hide");

	// this is the initial "tweak" to get the random colors
	var thesquares = $('.square');
	return thesquares;
}


function tweak(elem) {
	jQuery(elem).css("background",
		getShade(
			jQuery("#effects").css("background-color"),
			jQuery("body").css("background-color")
		)
	);
}

// getShade - takes in two hex colors, returns a random shade between them
function getShade(col1, col2) {
	if(col1.charAt(0) == "#" || col1.length == "6") {
		theCol1 = hex2num(col1);
	} else {
		theCol1 = col1.replace("rgb(","").replace(")","").split(",");
	}

	if(col2.charAt(0) == "#" || col2.length == "6") {
		theCol2 = hex2num(col2);
	} else {
		theCol2 = col2.replace("rgb(","").replace(")","").split(",");
	}
	
	var rnd = Math.random();
	// round ( ( abs val of (value 2 less value 1) times random ) plus smaller of value 2 or value 1
	var r = Math.round((Math.abs(theCol2[0]-theCol1[0])*rnd) + Math.min(theCol2[0], theCol1[0]));
	var g = Math.round((Math.abs(theCol2[1]-theCol1[1])*rnd) + Math.min(theCol2[1], theCol1[1]));
	var b = Math.round((Math.abs(theCol2[2]-theCol1[2])*rnd) + Math.min(theCol2[2], theCol1[2]));
	return "rgb(" + r + "," + g + "," + b + ")";
}

function fixFooter(targ) {
	var footoff = jQuery(targ).offset();
	if((footoff.top+jQuery(targ).height())<jQuery(window).height()) {
		jQuery(targ).addClass('fixed');
	}
}

jQuery(document).ready(function($){
	$('.even').each(function(){
		var maxH = 0;
		$(this).children().each(function() {
			if($(this).height() > maxH) {
				maxH = $(this).height();
			}
		});
		$(this).children().each(function() {
			// jQuery.support statement that returns false in IE6-8
			if(jQuery.support.leadingWhitespace) {
				$(this).css('min-height',maxH);
			} else {
				$(this).height(maxH);
			}
		});
	});
});
/* end copyrighted code */

// http://www.openjs.com/scripts/graphics/hex_color_rbg_value_converter.php
//Convert a hex value to its decimal value - the inputted hex must be in the
//	format of a hex triplet - the kind we use for HTML colours. The function
//	will return an array with three values.
function hex2num(hex) {
	if(hex.charAt(0) == "#") hex = hex.slice(1); //Remove the '#' char - if there is one.
	hex = hex.toUpperCase();
	var hex_alphabets = "0123456789ABCDEF";
	var value = new Array(3);
	var k = 0;
	var int1,int2;
	for(var i=0;i<6;i+=2) {
		int1 = hex_alphabets.indexOf(hex.charAt(i));
		int2 = hex_alphabets.indexOf(hex.charAt(i+1)); 
		value[k] = (int1 * 16) + int2;
		k++;
	}
	return(value);
}
//Give a array with three values as the argument and the function will return
//	the corresponding hex triplet.
function num2hex(triplet) {
	var hex_alphabets = "0123456789ABCDEF";
	var hex = "#";
	var int1,int2;
	for(var i=0;i<3;i++) {
		int1 = triplet[i] / 16;
		int2 = triplet[i] % 16;

		hex += hex_alphabets.charAt(int1) + hex_alphabets.charAt(int2); 
	}
	return(hex);
}