var phrase;
var cat;
var newPhrase;
var newCat;
var newCardWaitLength = 805;
var newCardRequestTime;
var newCardFadeTime = 800;
var nexting = false;

var nextNum = 1;
function Next() {
	if (nexting) { } else { if (nextNum == 2) { document.location.reload(false); }
		nexting = true;
		//Timer
		//Scoreboard
		//cEffect.opacity('card', 100, 0, newCardFadeTime);
		HideV($('card'));
		d = new Date();
		newCardRequestTime = d.getTime();
		cProcess.process('/process/randomPhrase_v2.php?rand='+Math.random(), 'Getting Phrase');
		nextNum++;
	}
}
function replacePhrase() {
	//cEffect.opacity('card', 0, 100, newCardFadeTime);
	ShowV($('card'));
	$('clue').innerHTML = newPhrase;
	$('cat').innerHTML = newCat;
	nexting = false;
}

function ShowV(obj) { obj.style.visibility = 'visible'; }
function HideV(obj) { obj.style.visibility = 'hidden'; }

function SelectCategoryClick(e) {
	aa = GetTarget(e); 
	cProcess.process('/process/updateCategories.php?c='+aa.id);
	if (aa.className == 'selected') {
		aa.className = '';
	} else {
		aa.className = 'selected';
	}
}

function PrintCards() {
	cProcess.process('/process/genPrinted_v2.php?num='+$V('num'), 'Generating Cards', 'prePrint()');
	Show($('generatingCards'));
}
function prePrint() {
	window.print();
}


function SetupBalloons() {
	balloons = getElementsByClassName($('container'), 'span', 'balloonTxt');
	for(i=0; i<balloons.length; i++) {
		if (balloons[i]) {
			newWrapper = document.createElement('div');
			newWrapper.className = 'balloon_wrapper';
			newWrapper.innerHTML += '<img src="/i/balloon.png" width="269" height="147" class="trans" />';
			newWrapper.innerHTML += '<div class="balloonInner">' + balloons[i].innerHTML + '</div>';
			balloons[i].parentNode.insertBefore(newWrapper, balloons[i]);
		
			removeNode(balloons[i]);	
		}
	}
}

function FieldHighlight(obj) {
if (obj.style) {} else { obj = this; }
	obj.style.borderColor = '#FF7F00';
}
function FieldOff(obj) {
	if (obj.style) {} else { obj = this; }
	obj.style.borderColor = '#75FD66';
}

function ChangeSuggestType(type) {
	Hide($('suggestWrapper_'+$V('suggest_type')));
	Show($('suggestWrapper_'+type));
	$('suggest_type').value = type;
}


function OSBRatingInsert(rating, total, r) {
	output = OutputRatingStars(rating, total, r);
	document.write(output);
}
function OutputRatingStars(rating, total, r) {
	var output = '';
	for(i=0; i < total; i++) {
		dif = rating - i; 
		if (dif >= 1) {
			type = 'full';
		} else if (dif >= 0.5) {
			type = 'half';
		} else {
			type = 'empty';
		}
		s = i + 1;
		sHalf = s - 0.5;
		//output += '<div class="Ratingleft star_'+s+' ' + type + '" onmouseover="SetRating(this, ' + sHalf + ')" onmouseout="ClearRating(this, ' + rating +')" onclick="SaveRating(this, ' + sHalf + ', ' + r + ', ' + total + ', ' + rating + ')"></div>';
		output += '<div class="Ratingleft star_'+s+' ' + type + '" onmouseover="SetRating(this, ' + s + ')" onmouseout="ClearRating(this, ' + rating +')" onclick="SaveRating(this, ' + sHalf + ', ' + r + ', ' + total + ', ' + rating + ')"></div>';
		output += '<div class="Ratingright star_'+s+' '+ type + '" onmouseover="SetRating(this, ' + s + ')" onmouseout="ClearRating(this, ' + rating +')" onclick="SaveRating(this, ' + s + ', ' + r + ', ' + total + ', ' + rating + ')"></div>';
	}	
	//output += '<span><span></span></span>'; //uncomment for text display
	return output;
}
var clearRating = setTimeout('', 1);
function SetRating(img, rating) {
	rating = parseInt(rating); //No halfs
	clearTimeout(clearRating);
	//display = img.parentNode.getElementsByTagName('span'); //uncomment for text display
	stars = img.parentNode.getElementsByTagName('div');
	StarType = 'full';
	for(key in stars) {
		num = parseInt(key);
		if (rating < .5) {
			StarType = 'empty';
		} else if (rating == (num + 1) / 2) {
			if (Math.ceil(rating) != rating) {
				StarType = 'half';
			}
		} 
		
		var classText = '' + stars[key].className;
		stars[key].className = classText.replace(/empty|full|half/,StarType);
		
		if (rating == (num + 1) / 2) { StarType = 'empty'; }
	}	
	//display[0].innerHTML = rating + '<span></span>';  //uncomment for text display
}
var clearImg = '';
var savedRating;
function ClearRating(img, rating) {
	clearImg = img;
	clearRating = setTimeout('SetRating(clearImg,'+rating+')', 50);
}
function SaveRating(img, rating, r, total, oldRating) {
	spinner = AjaxFields.addSpinner($('stars_1'));
	cProcess.process('/process/saveRating.php?p='+newP+'&rating='+rating+'&r='+ r+'&old='+ oldRating, 'Saving Rating for '+newPhrase, "AjaxFields.FinishSpinner('"+spinner+"')");
	document.getElementById('stars_' + r).innerHTML = OutputRatingStars(rating, total, r);	
}

function Report() {
	cProcess.process('/process/report.php?p='+newP, 'Reporting '+newPhrase, "alert('Thanks, your report as been sent.')");
	Next();
}

function CheckSpace(e) {
	code = OnPress(e);
	if (code == 32) {
		Next();	
	}
}

function OnPress(e) {	
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = code;
	return code;	
}

//window.onload = function () {

	cProcess = new cProcess();
	
	if ($('card') && $('card').className != 'noOverride') {
		$('card').style.display = 'block';
		cProcess.process('/process/randomPhrase_v2.php?force=1', 'Getting Phrase');
		window.onkeypress = CheckSpace;
	}
	
	opts = getElementsByClassName(document.body, 'ul', 'catSelect');
	for(i=0; i<opts.length; i++) {
		if (opts[i]) {
			As = opts[i].getElementsByTagName('a');
			for(c=0; c<As.length; c++) {
				if (As[c]) {
					As[c].onclick = SelectCategoryClick;	
				}
			}
		}
	}
	
	textFields = document.getElementsByTagName('input');
	for(i=0; i<=textFields.length; i++) {
		if (textFields[i] && textFields[i].type != 'button' && textFields[i].type != 'submit') {
			textFields[i].onfocus = FieldHighlight;	
			textFields[i].onblur = FieldOff;	
		}
	}
	
	textAreas = document.getElementsByTagName('textarea');
	for(i=0; i<=textAreas.length; i++) {
		if (textAreas[i]) {
			textAreas[i].onfocus = FieldHighlight;	
			textAreas[i].onblur = FieldOff;	
		}
	}
	