﻿	var currentPage;
    var currentLink;
    var currentTextColor;
    var navigating = false;
    var next_color;
	var text_next_color;
	var colStep = true;
	var rotateCol = true;
	var nextIndex;
	
	function animateBGColor(lastIndex, rand)
    {
    
    if (rotateCol == true){	
	var color_array = [	"#97db20",
						"#fede00",
						"#ff8207",
						"#ff0000",
						"#5d04ff"									
						];
						
	var text_color_array = ["#d5eeab",
						"#fef8d0",
						"#ffd9b2",
						"#ffb2b2",
						"#cdb2ff"								
						];
											
	var length = color_array.length;
	var text_length = text_color_array.length;
	next_color = "black", nextIndex=0;
	text_next_color = "black", nextIndex=0;
	
		if (lastIndex >= length-1)
		{ 
			nextIndex = 0;
		} 
		else 
		{
			nextIndex = lastIndex+1;
		}
		next_color = color_array[nextIndex];
		text_next_color = text_color_array[nextIndex];
		currentTextColor = text_next_color;

	var fadeTime = 20000, pauseTime = 1; //was9000
			
  $("#b1").animate({
    backgroundColor: next_color,
    color: text_next_color
  }, {
    queue: false, duration: fadeTime,
    step: function() {
      if (colStep == true) {  
      //Get current colors
      var bCol =  $("#colBox").color;
      $("#colHeader").html(bCol);
      $("#colHeader").animate({color: next_color},{ queue:false, duration:fadeTime});
      $("#colList").animate({color: next_color},{ queue:false, duration:fadeTime});
      colStep = false;      
      } else {
      var tCol =  $("#colHeader").css("color");
      var pCol =  $("#colHeader").css("background-color");
      $("#colHeader").html(rgb2hex(tCol));
      $(".colList li").css("border-top-color",tCol);
      $(".colCtrl").css("color",tCol);
      $(".colCtrl").css("background-color",pCol);
      $(".faceBook").css("color",tCol);
      $(".faceBook").css("background-color",pCol);
      $(".twitter").css("color",tCol);
      $(".twitter").css("background-color",pCol);
      }
    }, 
    complete: function() {
      setTimeout( function() {
      animateBGColor(nextIndex, rand);
      colStep = true;
      }, pauseTime );
    }
   
  });

}

}

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 function hex(x) {
  hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}





function linkOn(next){
     var l = '#' + currentLink;
     var n = '#' + next;
     //get current menu color
     var color = $('#b1').css('color');
     if (n != l) {
     //Get the old link to fade out and new to go white
     $(l).animate({ color: color}, 500, function(){
				$(l).removeAttr("Style"); 
			});
     $(n).animate({ color: "#FFFFFF"}, 1);
     //Set the currentLink to new link (next)
     currentLink = next;
     navigating = true;
     }   
     }
     
     function changePage(next) {
     navigating = true;
     var c = '#' + currentPage;
     var n = '#' + next;
     if (n != c) {
     $(n).animate({ color: "#FFFFFF"}, 500);
     currentPage = next;
     currentLink = next;
     }
     }
     

     function resetLink() {
     
     setTimeout( function()
      {
     //get current menu color
     if (navigating == false) {
     var color = $('#b1').css('color');
     var c = '#' + currentPage;
     var l = '#' + currentLink;
     if (l != c) {
        $(l).animate({ color: color}, 1200, function(){
				$(l).removeAttr("Style"); 
			});
        $(c).animate({ color: "#FFFFFF"}, 1200);
        currentLink = currentPage;
        }
        }
      }, 500);
     

     }
     

     function pausePlay(e) {
     var ctrl = "#" + e;
     if ($(ctrl).html() == "II") {
     $(".background").stop();
     $("#colHeader").stop();
     $("#colList").stop();
     rotateCol = false;
     $(ctrl).html("►");
     } else {
     $(ctrl).html("II");
     rotateCol = true;
     colStep = true;
     animateBGColor(nextIndex, false);
     }
     }
