/* copyright Tomas Young 2009 */

var game_stag;

var items_ifile = new Array;
var items_sfile = new Array;
var items_equiv = new Array;
var num_items = 0;

var sentences_sfile = new Array;
var sentences_sz = new Array;
var sentences_list = new Array;
var sentences_prefill = new Array;
var num_sentences = 0;

var max_choices = 10;
var num_choices = 10;

var max_words = 5;

var selected_sentence = -1;

var hintTimerId1;
var hintTimerId2;

var monster_period = 30000;

function game_init() {
    if(max_words > 5) {
	dx = 0;
	xpos = 0;
	word_height = 0;
	if(screen.width >= 1400) {  // 1600x200, 1400x1050
	    dx = 950 - 200;
	    xpos = 200;
	    word_height = 120;
	}
	else {
	    dx = 900 - 150;
	    xpos = 150;
	    word_height = 100;
	}
	word_spacing = Math.floor(dx / max_words);
	word_width = Math.floor(word_spacing * 8 / 10);
	for(i = 0; i < max_words; i++) {
	    document.getElementById("word"+i+"-st").style.left = xpos;
	    document.getElementById("bar"+i+"-st").style.left = xpos;
	    document.getElementsByName("word"+i+"-i")[0].width = word_width;
	    document.getElementsByName("word"+i+"-i")[0].height = word_height;
	    document.getElementsByName("bar"+i+"-i")[0].width = word_width;
	    document.getElementsByName("bar"+i+"-i")[0].height = word_height;
	    xpos += word_spacing;
	}
    }
    setTimeout('check_monster()',monster_period);
}

var intro_ifile = new Array;
var intro_ifile2 = new Array;
var intro_sfile = new Array;
var intro_delay = new Array;
var num_intros = 0;
var doing_intro = true;

function game_add_intro(ifile,ifile2,sfile,d) {
    intro_ifile[num_intros] = ifile;
    intro_ifile2[num_intros] = ifile2;
    intro_sfile[num_intros] = sfile;
    intro_delay[num_intros] = d;
    num_intros++;
}

function game_add_item(ifile,sfile,equiv_item) {
    items_ifile[num_items] = ifile;
    items_sfile[num_items] = sfile;
    items_equiv[num_items] = equiv_item;
    num_items++;
}

function game_add_sentence(sfile,num_items,i0,i1,i2,i3,i4,i5,i6,i7,i8,i9) {
    sentences_sfile[num_sentences] = sfile;
    sentences_sz[num_sentences] = num_items;
    var items = new Array;
    items[0] = i0; items[1] = i1; items[2] = i2; items[3] = i3; items[4] = i4;
    items[5] = i5; items[6] = i6; items[7] = i7; items[8] = i8; items[9] = i9;
    sentences_list[num_sentences] = new Array;
    sentences_prefill[num_sentences] = new Array;
    for(i = 0; i < num_items; i++) {
	sentences_list[num_sentences][i] = items[i];
	sentences_prefill[num_sentences][i] = 0;  // 0 = no prefill; 1 = prefill
    }
    num_sentences++;
}

function game_prefill_sentence(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9) {
    var p = new Array;
    p[0] = p0; p[1] = p1; p[2] = p2; p[3] = p3; p[4] = p4; 
    p[5] = p5; p[6] = p6; p[7] = p7; p[8] = p8; p[9] = p9; 
    for(i = 0; i < sentences_sz[num_sentences-1]; i++) {
	sentences_prefill[num_sentences-1][i] = p[i];
    }
}

var choices = new Array;
var choices2words = new Array;
var word_map = -1;

var total_correct_picks = 0;
var user_pick = -1;

var first_hint1 = true;
var first_hint2 = true;

var root_dir = "./";

function hint() {
    if(total_correct_picks < sentences_sz[selected_sentence]) {
	first_hint1 = false;
	Sin(game_stag,root_dir+"build-sentence/level 1 hint 1.wav");
    }
    else {
	first_hint2 = false;
	Sin(game_stag,root_dir+"build-sentence/level 1 hint 2.wav");
    }
}

function cancel_hint_timers() {
    clearTimeout(hintTimerId1);
    clearTimeout(hintTimerId2);
}

function start_hint_timer_1() {
    if(first_hint1) {
	hintTimerId1 = setTimeout('hint()',15000);
    }
    else {
	hintTimerId1 = setTimeout('hint()',300000);
    }
}

function start_hint_timer_2() {
    if(first_hint2) {
	hintTimerId2 = setTimeout('hint()',15000);
    }
    else {
	hintTimerId2 = setTimeout('hint()',300000);
    }
}

var ss = -1;
var ii = 0;

function do_intro() {
    if(intro_ifile[ii] != "") {
	Iin("intro-i",intro_ifile[ii]);
    }
    if(intro_ifile2[ii] != "") {
	Iin("intro-i2",intro_ifile2[ii]);
    }
    if(intro_sfile[ii] != "") {
	Sin(game_stag,intro_sfile[ii]);
    }
    if(ii == num_intros) {
	doing_intro = false;
	Iin("intro-i",root_dir+"blank.gif");
	Iin("intro-i2",root_dir+"blank.gif");
	Sin(game_stag,sentences_sfile[selected_sentence]);
	start_hint_timer_1();
    }
    else {
	setTimeout('do_intro()',intro_delay[ii++]);
    }
}

var first_run = true;

function game_run() {
    if(selected_sentence < 0) {
	for(i = 0; i < max_words; i++) {
	    Iin("bar"+i+"-i",root_dir+"blank.gif");
	    Iin("word"+i+"-i",root_dir+"blank.gif");
	}
	for(i = 0; i < max_choices; i++) {
	    Iin("speakerC"+i+"-i",root_dir+"blank.gif");
	    Iin("speakerE"+i+"-i",root_dir+"blank.gif");
	}
	// selected_sentence = (Math.floor(Math.random()*100)) % num_sentences;
	if(ss == -1) {
	    ss = (Math.floor(Math.random()*100)) % num_sentences;
	}
	selected_sentence = ss++;
	if(ss == num_sentences) {
	    ss = 0;
	}
	for(i = 0; i < sentences_sz[selected_sentence]; i++) {
	    Iin("bar"+i+"-i",root_dir+"bar.gif");
	}
	for(i = 0; i < max_choices; i++) {
	    choices[i] = -1;
	    choices2words[i] = -1;
	}
	for(i = 0; i < sentences_sz[selected_sentence]; i++) {
	    var s = -1;
	    do {
		s = (Math.floor(Math.random()*100)) % num_choices;
	    } while(choices[s] >= 0);
	    choices[s] = sentences_list[selected_sentence][i];
	    choices2words[s] = i;
	}
	for(i = 0; i < num_choices; i++) {
	    if(choices[i] < 0) {
		var s = -1;
		do {
		    s = (Math.floor(Math.random()*100)) % num_items;
		    for(j = 0; j < sentences_sz[selected_sentence]; j++) {
			if((s == sentences_list[selected_sentence][j]) || (s == items_equiv[sentences_list[selected_sentence][j]])) {
			    s = -1;
			    break;
			}
		    }
		} while(s < 0);
		choices[i] = s;
	    }
	}
	if(first_run) {
	    for(i = 0; i < num_choices; i++) {
		Iin("choice"+i+"-i",items_ifile[choices[i]]);
		Iin("speakerC"+i+"-i",root_dir+"speaker 2.gif");
		Iin("speakerE"+i+"-i",root_dir+"speaker 1.gif");
	    }
	    ii = 0;
	    first_run = false;
	    do_intro();
	    return;
	}
	Sin(game_stag,sentences_sfile[selected_sentence]);
	for(i = 0; i < num_choices; i++) {
	    Iin("choice"+i+"-i",items_ifile[choices[i]]);
	    Iin("speakerC"+i+"-i",root_dir+"speaker 2.gif");
	    Iin("speakerE"+i+"-i",root_dir+"speaker 1.gif");
	}
	start_hint_timer_1();
    }
}

var ns = 0;
var dustZ;

function new_sentence() {
    if(ns == 0) {
	ns++;
	dustZ = document.getElementById("dust-st").style.zIndex;
	document.getElementById("dust-st").style.zIndex = 300;
	Iin("fairy-i",root_dir+"fairy dust.gif");
	setTimeout('new_sentence()',200);
    }
    else if(ns == 1) {
	ns++;
	Iin("dust-i",root_dir+"pixie dust.gif");
	Sin(game_stag,root_dir+"pixydust 1.wav");
	setTimeout('new_sentence()',600);
    }
    else if(ns == 2) {
	ns++;
	Iin("fairy-i",root_dir+"fairy hover.gif");
	setTimeout('new_sentence()',1200);
    }
    else {
	ns = 0;
	Iin("dust-i",root_dir+"blank.gif");
	document.getElementById("dust-st").style.zIndex = dustZ;
	game_run();
    }
}

function game_button() {
    if(doing_intro) {
	return;
    }
    if(total_correct_picks < sentences_sz[selected_sentence]) {
	Sin(game_stag,sentences_sfile[selected_sentence]);
    }
    else {
	cancel_hint_timers();
	selected_sentence = -1;
	total_correct_picks = 0;
	new_sentence();
    }
}

function say_word(x,y) {
    if(doing_intro) {
	return;
    }
    if(choices[y] >= 0) {
	if(x == 0) {
	    Sin(game_stag,items_sfile[choices[y]]);
	}
	else if(x == 1) {
	    var str=items_sfile[choices[y]];
	    Sin(game_stag,str.replace(/.wav/,"E.wav"));
	}
    }
}

var zz = 0;

function say_sentence() {
    if(total_correct_picks == sentences_sz[selected_sentence]) {
	if(zz == sentences_sz[selected_sentence]) {
	    Sin(game_stag,sentences_sfile[selected_sentence]);
	    Iin("bar"+(zz-1)+"-i",root_dir+"bar.gif");
	    zz = 0;
	    start_hint_timer_2();
	    return;
	}
	Iin("bar"+zz+"-i",root_dir+"bar blink.gif");
	if(zz > 0) {
	    Iin("bar"+(zz-1)+"-i",root_dir+"bar.gif");
	}
	Sin(game_stag,items_sfile[sentences_list[selected_sentence][zz]]);
	zz++;
	if(zz < sentences_sz[selected_sentence]) {
	    if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
		setTimeout('say_sentence()',1000);
	    }
	    else if(/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
		setTimeout('say_sentence()',1350);
	    }
	}
	else {
	    cancel_hint_timers();
	    if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
		setTimeout('say_sentence()',1500);
	    }
	    else if(/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
		setTimeout('say_sentence()',1800);
	    }
	}
    }
}

var monster_run_ifile = "";
var monster_on = false;
var magic_loop = 0;
var mmMax = 11;
var mm = 0;
var msY = 0;
var msW = 0;
var msH = 0;
var msL = 0;

function monster_go() {
    document.getElementById("monster-st").style.top = msY;
    document.getElementsByName("monster-i")[0].width = msW;
    document.getElementsByName("monster-i")[0].height = msH;
    if(msL == 3) {
	Iin("monster-i",monster_run_ifile);
	msL++;
    }
    msY -= 10;
    msW *= 0.9;
    msH *= 0.9;
    if(msY > 0.05) {
	setTimeout('monster_go()',150);
    }
    else {
	Iin("monster-i",root_dir+"blank.gif");
	monster_on = false;
	if(mm > mmMax) {
	    msL = 0;
	    msY = 25;
	    msW = 20;
	    msH = 16;
	    monster_come();
	}
    }
}

function monster_come() {
    monster_on = true;
    if(msL < 2) {
	document.getElementById("monster-st").style.top = msY;
	document.getElementsByName("monster-i")[0].width = msW;
	document.getElementsByName("monster-i")[0].height = msH;
	if(msL == 0) {
	    Iin("monster-i",root_dir+"monster run.gif");
	    msL++;
	}
	msY += 10;
	msW *= 1.125;
	msH *= 1.125;
    }
    ymax = 235;
    if(screen.width < 1400) {
	ymax = 190;
    }
    if(msY < ymax) {
	setTimeout('monster_come()',150);
    }
    else {
	if(msL == 1) {
	    Iin("monster-i",root_dir+"monster stand.gif");
	    setTimeout('monster_come()',500);
	}
	else if(msL == 2) {
	    if(mm > mmMax) {
		Iin("monster-i",root_dir+"monster cookie.gif");
		Iin("plate-i",root_dir+"plate c0.gif");
		mm = 0;
		// window.status = mm;
		monster_run_ifile = root_dir+"monster run 2.gif";
	    }
	    else if(mm > 0) {
		Iin("monster-i",root_dir+"monster clap.gif");
		monster_run_ifile = root_dir+"monster run 2.gif";
	    }
	    else if(mm == 0) {
		Iin("monster-i",root_dir+"monster sad.gif");
		monster_run_ifile = root_dir+"monster run 3.gif";
	    }
	    setTimeout('monster_come()',500);
	}
	else if(msL == 3) {
	    monster_go();
	}
	msL++;
    }
}

function check_monster() {
    if(!monster_on) {
	msL = 0;
	msY = 25;
	msW = 20;
	msH = 16;
	monster_come();
    }
    setTimeout('check_monster()',monster_period);
}

function show_magic() {
    if(magic_loop == 0) {
	magic_loop++;
	Iin("fairy-i",root_dir+"fairy magic.gif");
	setTimeout('show_magic()',600);
    }
    else if(magic_loop == 1) {
	magic_loop++;
	Sin(game_stag,root_dir+"pixydust 1.wav");
	if(user_pick == 0) {
	    Iin("magic"+user_pick+"-i",root_dir+"magic shortest.gif");
	}
	else if(user_pick == 1) {
	    Iin("magic"+user_pick+"-i",root_dir+"magic shorter.gif");
	}
	else {
	    Iin("magic"+user_pick+"-i",root_dir+"magic.gif");
	}
	setTimeout('show_magic()',250);
    }
    else if(magic_loop == 2) {
	Iin("fairy-i",root_dir+"fairy hover.gif");
	Iin("magic"+user_pick+"-i",root_dir+"blank.gif");
	Iin("choice"+user_pick+"-i",root_dir+"blank.gif");
	Iin("word"+word_map+"-i",items_ifile[choices[user_pick]]);
	choices[user_pick] = -1;
	total_correct_picks++;
	unlockClick();
	if(mm < mmMax) {
	    Iin("plate-i",root_dir+"plate c"+(mm+1)+".gif");
	}
	else {
	    Iin("plate-i",root_dir+"plate c"+(mm+1)+".gif");
	    if(!monster_on) {
		msL = 0;
		msY = 25;
		msW = 20;
		msH = 16;
		monster_come();
	    }
	}
	mm++;
	// window.status = mm;
    }
    if(total_correct_picks == sentences_sz[selected_sentence]) {
	setTimeout('say_sentence()',1500);
    }
}

function game_pick(n) {
    if(doing_intro) {
	return;
    }
    cancel_hint_timers();
    start_hint_timer_1();
    if(!clickLock) {
	lockClick();
	/*
	for(i = 0; i < sentences_sz[selected_sentence]; i++) {
	    if(choices[n] == sentences_list[selected_sentence][i]) {
		user_pick = n;
		magic_loop = 0;
		show_magic();
		return;
	    }
	}
	*/
	if(choices2words[n] >= 0) {
	    user_pick = n;
	    word_map = choices2words[n];
	    choices2words[n] = -1;
	    magic_loop = 0;
	    show_magic();
	    return;
	}
	unlockClick();
    }
    if(mm > 0 && n < num_choices && choices[n] >= 0) {
	mm -= 1;
	Sin("monster-s",root_dir+"ding3.wav");
	Iin("plate-i",root_dir+"plate c"+mm+".gif");
    }
}

