// JavaScript Document
var keys = [65,82,71,89,76,69];
var nextKey = 0;
var o = 2;
var c = "#000";

$(document).ready(function() {


        if ($.cookie("o") != null){
		o = parseInt($.cookie('o'));
                $("#second_back").fadeTo(1,o*.1);
        }

        if ($.cookie("c") != null){
		c = $.cookie('c');
                $("#third_back").animate({backgroundColor:c, opacity: 0.25}, 1);
                $("#footer").animate({backgroundColor:changeColor(c, .2, true)}, 1);
        }

		if ($("#footer").offset().top-$("#glowTop").height()-$("#glowBottom").height() < 0){
			$("#glowEdge").css("display","none");
		} else {
			$("#glowEdge").height($("#footer").offset().top-$("#glowTop").height()-$("#glowBottom").height());
		}
		
                $("#second_back").height($("#footer").offset().top);
                $("#third_back").height($("#footer").offset().top);

		
		
		$(window).keydown(function(e){
			var key = e.which;
			if (key === keys[nextKey])
				nextKey++;
			else
				nextKey = 0;
				
			Argyler();
			
			if(key == '27'){
				 $("#argyler").fadeOut("slow");
			}
 
			if(key == '49' && $("#argyler").is(':visible') ){
				 $("#third_back").fadeTo(1,0);
                                 $("#footer").animate({backgroundColor:"#050F28"}, 1);
                                 $.cookie("c", null);
			}

			if(key == '50' && $("#argyler").is(':visible') ){
                                 c = randomColor();
				 $("#third_back").animate({backgroundColor:c, opacity: 0.25}, 1);
                                 $("#footer").animate({backgroundColor:changeColor(c, .2, true)}, 1);
                                 $.cookie("c", c, { expires: 30000 });
			}
		
		});

	
	   $("#Aplus").click(function(e){
			o += 2;
			if (o > 10){
				o = 10;
			}
			$("#second_back").fadeTo(1,o*.1);
                        $.cookie("o", o, { expires: 30000 });
	   });
	   
	   $("#Aminus").click(function(e){
			o -= 2;
			if (o < 0){
				o = 0;
			}
			$("#second_back").fadeTo(1,o*.1);
                        $.cookie("o", o, { expires: 30000 });
	   });
	   
	   $(".bio h2").each(function(index){ 
		
                if($(this).find("span").length){
                     var name = $(this).find("span").text();
                } else {
                     var name = $(this).text();
                }

		$("#peopleMenu").append('<li onclick="slideItems('+index+')">'+name+'</li>');
				
		});

		

});

function reHeight(){
	$("body").height($("#footer").offset().top);
	$("#second_back").height($("#footer").offset().top+50);
	$("#glowEdge").height($("#footer").offset().top-$("#glowTop").height()-$("#glowBottom").height()+50);
}

function slideItems(i){
	$(".bio").css("border","none");
	$("#peopleMenu li").removeClass('selected');
	$("#peopleMenu li:eq("+i+")").addClass('selected');
	$(".box").css("height",$("#bio").height());
	$(".bio").each(function(index){
		if (index != i){
			$(this).slideUp("slow", reHeight);	
		} else {
			$(this).slideDown("slow");
		}
	});
}

function Argyler(){
  var maxKeyIndex = keys.length - 1;
  if(nextKey > maxKeyIndex) {
     
	 if ($("#argyler").css("display") == "none"){
	 	$("#argyler").fadeIn("slow");
     }else{
		 $("#argyler").fadeOut("slow");
	 }

     nextKey = 0;
  }
}

function randomColor(){
   var rgbColor = "";
   for ( i = 1; i < 4; i++ ) {
      rgbColor +=  Math.floor(Math.random() * (250+1));
      if ( i != 3 ) rgbColor += ',';
   }
   //window.status = rgbColor;
   return "rgb("+rgbColor+")";
 }


// Ratio is between 0 and 1
var changeColor = function(color, ratio, darker) {
    // Trim trailing/leading whitespace
    color = color.replace(/^\s*|\s*$/, '');

    // Expand three-digit hex
    color = color.replace(
        /^#?([a-f0-9])([a-f0-9])([a-f0-9])$/i,
        '#$1$1$2$2$3$3'
    );

    // Calculate ratio
    var difference = Math.round(ratio * 256) * (darker ? -1 : 1),
        // Determine if input is RGB(A)
        rgb = color.match(new RegExp('^rgba?\\(\\s*' +
            '(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
            '\\s*,\\s*' +
            '(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
            '\\s*,\\s*' +
            '(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])' +
            '(?:\\s*,\\s*' +
            '(0|1|0?\\.\\d+))?' +
            '\\s*\\)$'
        , 'i')),
        alpha = !!rgb && rgb[4] != null ? rgb[4] : null,

        // Convert hex to decimal
        decimal = !!rgb? [rgb[1], rgb[2], rgb[3]] : color.replace(
            /^#?([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i,
            function() {
                return parseInt(arguments[1], 16) + ',' +
                    parseInt(arguments[2], 16) + ',' +
                    parseInt(arguments[3], 16);
            }
        ).split(/,/),
        returnValue;

    // Return RGB(A)
    return !!rgb ?
        'rgb' + (alpha !== null ? 'a' : '') + '(' +
            Math[darker ? 'max' : 'min'](
                parseInt(decimal[0], 10) + difference, darker ? 0 : 255
            ) + ', ' +
            Math[darker ? 'max' : 'min'](
                parseInt(decimal[1], 10) + difference, darker ? 0 : 255
            ) + ', ' +
            Math[darker ? 'max' : 'min'](
                parseInt(decimal[2], 10) + difference, darker ? 0 : 255
            ) +
            (alpha !== null ? ', ' + alpha : '') +
            ')' :
        // Return hex
        [
            '#',
            pad(Math[darker ? 'max' : 'min'](
                parseInt(decimal[0], 10) + difference, darker ? 0 : 255
            ).toString(16), 2),
            pad(Math[darker ? 'max' : 'min'](
                parseInt(decimal[1], 10) + difference, darker ? 0 : 255
            ).toString(16), 2),
            pad(Math[darker ? 'max' : 'min'](
                parseInt(decimal[2], 10) + difference, darker ? 0 : 255
            ).toString(16), 2)
        ].join('');
};



