
/* landing page functions for carousel ads */

/* Words Play 'tile laying' */
function scrabbleWord () {
	var words = [ 'joyous', 'fun', 'playful', 'mirthful', 'carefree', 'ebullient', 'gladsome' ];
	var rootId = '#app-1:first', ele = rootId+' .adjective', articleEle = rootId+' .article';
	var _this = this;
	
	var currentWord = $(ele).text();
	var newWord = currentWord;
	while (newWord === currentWord) {
		newWord = words[Math.floor(getRandom(words.length))];
	}
	
	$(ele).css({ 'color': 'transparent', 'width': 'auto' });
	$(ele).text(newWord);
	var width = $(ele).width();
	$(ele).text('');
	$(ele).css({ 'width': width+'px', 'color': '#000' });
	$(articleEle).text((vowelStarts(newWord) ? 'An' : 'A'));
	
	var scrabbleIntvTime = 1500, letters = newWord.replace(/./g, ' ');
	//console.log('CURRNT: '+newWord+' WIDTH: '+width+' LETTERS: "'+letters+'"');
	_this.letterIntv = setInterval(function () {
		var currWord = $(ele).text();
		currWord = currWord.replace(/\&nbsp\;/, ' ');
		
		var i = 0;
		console.log(letters+' ?= '+currWord);
		while (letters == currWord) {
			var charPos = Math.floor(getRandom(newWord.length));
			console.log(' CHAR POS: '+charPos+' LETTERS: "'+letters+'"');
			var regexp = new RegExp('^(.{'+(charPos)+'}).');
			letters = letters.replace(regexp, '$1'+newWord[charPos])
			if (i > 8) break;
		} 
		
		$(ele).text(letters);
		if (letters == newWord) {
			clearInterval(_this.letterIntv);
			// due to the nature of the current carousel, we cannot do repeating random words
			//_this.scrabbleIntv = setInterval(scrabbleWord, 1500);
		}
	}, 400);
}

/* returns true if the passed word starts with a vowel */
function vowelStarts (word) {
	return word[0].match(/^\s{0,}[AEIOUaeiou]/);
}

// display active nav element
// pass NULL type to turn on/off an element
// pass type => 'on' to just turn on an element
// pass type => 'off' to just turn off an element
function toggleAdNav(ad,type) {
	var num;
	if (typeof(ad) != 'number') {
		var adNumArr = ad.split('-');	
		if (adNumArr) { num = adNumArr[1]; }
	} else {
		num = ad;
	}
	
	var adId = '#app-nav-'+num;
	var adNav = '.app-nav li a';
	var activeClass = 'active';
	type == 'on' ? $(adId).addClass(activeClass) : $(adNav).removeClass(activeClass);
	if (!type) { $(adId).addClass(activeClass); }
	if (ad === 1) { scrabbleWord(); } 
}
