var intervalID;
var scrambleIntervalID;
var letterIntervalID;
var tabGlowIntervalID;

$(document).ready(function(){
	// Word scramble on homepage
	var scramble_phrases = Array();
	var count_phrases = 0;
	var current_phrase = 1;
	$("#home_scramble #fb-root").hide();
	$("#home_scramble p.scramble_hidden").each(function(){
		count_phrases++;
		scramble_phrases[count_phrases] = $("#scramble_"+count_phrases).text();
	});
	var rand_alpha = (lang_code=='ge')?Array("ა", "ბ", "გ", "დ", "ე", "ვ", "ზ", "თ", "ი", "კ", "ლ", "მ", "ნ", "ო", "პ", "ჟ", "რ", "ს", "ტ", "უ", "ფ", "ქ", "ღ", "ყ", "შ", "ჩ", "ც", "ძ", "წ", "ჭ", "ხ", "ჯ", "ჰ"):Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	var count_scramble_cycles = 0;
	var max_scramble_cycles = 5;
	function scramble_letter(path,letter) {
		var random = Math.floor(Math.random() * (26 - 0 + 1) + 0);
		setTimeout(function() {
			count_scramble_cycles++;
			if(count_scramble_cycles >= 20) {
				$(path).html(letter);
				count_scramble_cycles = 0;
			} else {
				$(path).html(rand_alpha[random]);
				scramble_letter(path,letter);
			}
		}, 20);
	}
	function scramble_interval() {
		scrambleIntervalID = window.setInterval(function(){ scramble_phrase(); }, 11000);
	}
	function scramble_phrase() {
		
		clearInterval(scrambleIntervalID);
		if (current_phrase < count_phrases) {
			current_phrase++;
		} else {
			current_phrase = 1;
		}
		var phrase_char_count = scramble_phrases[current_phrase].length;
		var row = 1;
		var col = 1;
		for(i=0;i<=phrase_char_count;i++) {
			var letter = scramble_phrases[current_phrase].charAt(i);
			if (letter == "|") {
				for(j=col;j<=22;j++) {
					$(".line_"+row+" .col_"+j).html("&nbsp;");
				}
				row++;
				col = 1;
			} else {
				if (letter == " ") {
					letter = "&nbsp;";
				}
				var path = ".line_"+row+" .col_"+col;
				scramble_letter(path,letter);
				col++;
			}
		}
		if(col < 22 || row < 4) {
			for(i=row;i<=4;i++) {
				for(j=col;j<=22;j++) {
					$(".line_"+i+" .col_"+j).html("&nbsp;");
				}
			}
		}
		scramble_interval();
		//alert("row = " + row + " col = "+col)
		//var line_bits = scramble_phrases[current_phrase].split()
	}
	scramble_phrase();
});


