﻿
/*  start of top-scoring code */
function endphoenixhunt(){
	flyaway();
	endgame();
	$('indicator').style.display='none';
	$('indication').style.display='none';
	$('scoreboard').style.display='none';
	$('score').style.display='none';
	$('score-screen').style.display='none';
	$('bullets').style.display='none';
	
	$("endgame_button").style.display="none";
	$("startgame_button").style.display="block";
	$("bgone").style.display="none";
	$("bgtwo").style.display="none";
	
}
function startphoenixhunt(){
	$("bgone").style.display="block";
	$("bgtwo").style.display="block";
	Sound.play("/sounds/game/shot-hit.mp3");
	Sound.play("/phoenixhunt/shot.mp3");
	Sound.play("/sounds/game/chuckle2.mp3");
$("startgame_button").style.display="none";
$("endgame_button").style.display="block";
$('indicator').style.display='block';
$('indication').style.display='block';
$('scoreboard').style.display='block';
$('score').style.display='block';
$('score-screen').style.display='block';
$('bullets').style.display='block';

setTimeout(scrollTo, 0, 0, 1);startup();
}

var SCORE_SCRIPT = '/phoenixhunt/top_scores.php';

/*var ENTER_NAME_SCREEN = Object.extend(new Screen('congrats'), {
    show : function () {
        Screen.prototype.show.call(this);
        $('player_name').focus();
    }
});
 */


function get_scores() {
    new Ajax.Request(SCORE_SCRIPT, {
        method: 'get',
        onComplete: show_scores,
        onFailure: show_error
    });
}

var posted_scores     = false;
function post_score() {
    if (posted_scores) return; // prevent double post
    new Ajax.Request(SCORE_SCRIPT, {
        method: 'post',
        parameters: $H({
                        'name':  $F('player_name'),
						'email':  $F('player_email'),
					    'level': roundnum,
                        'score': totscr
                    }).toQueryString(),
        onComplete: endendgame,
        onFailure: show_error
    });
    posted_scores = true;
}


var low_high_score = 100;

function show_scores(req) {
    $('result').value = req.responseText;
    var scores = $A(req.responseXML.getElementsByTagName('score'));
    scores.each(function(node) {
        place = node.getAttribute('place');
        Element.update($('high_score_' + place), node.getAttribute('score'));
        Element.update($('high_name_' + place), node.getAttribute('name'));
        $('high_row_' + place).className = node.getAttribute('age'); // 'old' or 'new'
    });
    low_high_score = parseInt(scores[scores.length -1].getAttribute('score'));
}


function show_error(req){
    $('result').value = 'error';
}

/*  end of top-scoring code */

function getObj(name){ 
    if (document.getElementById){
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
        }
    else if (document.all){
        this.obj = document.all[name];
        this.style = document.all[name].style;
        }
    else if (document.layers){
        this.obj = document.layers[name];
        this.style = document.layers[name];
        }
    };

function rand(n){
    return (Math.floor(Math.random()*n+1));
    };


/* configured by thenumber */
var horizontal_step = 5;
var max_right_edge = 700;
var max_left_edge = 5;
var max_top_edge = 20;
var max_bottom_edge = 550;
var fall_speed = 20; //originally 30
var climb_speed = 50; //originally 30
var shot_freeze_time = 500;
var fall_interval = 30; //orig 200
var base_speed = 12; //orig 3
var speed_factor = 17; //orig 17
var speed_increment = 7;
var flyaway_interval = 120;



var flyobj1;
var flyobj2;
var begin;
var dog;
var tid;
var points;
var startscr = "000000";
var scr = 1500;
var totscr = 0;
var swap;
var drop;
var speed = 95;
var endgameTime;
var strMsg;
var strName;
var strPlayerIni;
var strURL;
var climb;
var cnt = 0;
var reset_timer;
var hit_test = false;
var xa = Math.floor(Math.random()*speed_factor)+base_speed;
var ya = -20; 
var count_hit = 0;
var count_shots = 0;
var currentx = rand(max_right_edge-1);
var currenty = max_bottom_edge-1;
var roundnum = 1;
var whichphoenix = 0;
var rndEndTest = new Boolean(false);
var rndTime;
var pics = [];
var load = [];
var disp = [];
var disp2 = [];
for(i = 0; i < 10; i++){
    pics[i]="/phoenixhunt/score-"+i+".gif";
    }              
for(i = 0; i < pics.length; i++){
    load[i] = new Image();
    load[i].src = pics[i];
    }

function startup() {
//	Sound.enable();
    begin = setInterval("start();",3500);
    $('rounddisp').setAttribute("src",'/phoenixhunt/round-1.gif');
    $('rounddisplay').style.display = 'block';
    loadscore();
    loadindication();
    }

function loadscore() {
    for(i = 0; i < 6; i++){
        disp[i] = document.createElement("img"); 
        disp[i].setAttribute("src", load[0].src); 
        disp[i].setAttribute("id", "dgts'+i+'");
        disp[i].setAttribute("width", "8"); 
        disp[i].setAttribute("height", "7"); 
        document.getElementById("score").appendChild(disp[i]);
        }
    };
    
function loadindication() {
    for (i = 0; i < 10; i++){
        disp2[i] = document.createElement("img");
        disp2[i].setAttribute("src", '/phoenixhunt/hit-status-0.gif');
        disp2[i].setAttribute("id", "indic'+i+'");
        disp2[i].setAttribute("width", "8");
        disp2[i].setAttribute("height", "7");
        document.getElementById("indication").appendChild(disp2[i]);
        }
    };

function start() {
    clearInterval(begin);
    $('rounddisplay').style.display = 'none';
    flyobj1 = new getObj("phoenix");
    flyobj2 = new getObj("hotspot");
    dog = new getObj("dog");
    bullets();
    disp2[whichphoenix].setAttribute("src", '/phoenixhunt/hit-status-1.gif');
    flyobj1.style.display = 'block';
    flyobj2.style.display = 'block';
    $('game_area').style.display = 'block';
    tid = setInterval("move(xa,ya);",speed);
    points = setInterval("pointCalc();",100);
    };

function pointCalc() {
    cnt ++;
    if (cnt == 100) {
        flyaway();
        }
    scr = scr - 10;
    };
    
function updateRound(){
    $('rounddisp').setAttribute("src","/phoenixhunt/round-"+roundnum+".gif");
    $('rounddisplay').style.display = 'block';
    };
    
function updateScore() {
	var v1, v2, v3;
	v1 = totscr.toString();
	v2 = v1.split("");
	v3 = 6 - v2.length;
	for(i = 0; i < v2.length; i++){
		disp[i+v3].setAttribute("src", load[v2[i]].src);
		}
	};
    
function shot() {
 	Sound.play("/sounds/game/shot-hit.mp3");
  	
    clearInterval(tid);
    clearInterval(points);
    totscr = totscr + scr;
    hit_test = true;
	count_hit ++;
	count_shots ++;
    bullets();
    updateScore();
    disp2[whichphoenix].setAttribute("src", '/phoenixhunt/hit-status-2.gif');
    flyobj2.style.display = 'none';
    $('game_area').style.display = 'none';
    $('phoeniximg').src = "/phoenixhunt/phoenix-shot.gif";    
    swap = setInterval("fall();",shot_freeze_time);
    };

function miss() {
   Sound.play("/phoenixhunt/shot.mp3")

	scr = scr - 100;
    count_shots ++;
    bullets();
    };

function fall() {
    clearInterval(swap);
    $('phoeniximg').src = "/phoenixhunt/phoenix-fall.gif";
    drop = setInterval("falling();",fall_interval);
    };

function move(dx, dy) {
//	console.log(flyobj1.style.top,flyobj1.style.left);
   currentx += dx;
   currenty += dy;
   $('phoenix').style.top = currenty;
   $('hotspot').style.top = currenty;
   $('phoenix').style.left = currentx;
   $('hotspot').style.left = currentx;   
   if (currentx >= max_right_edge) {
    xa = -xa; 
    $('phoeniximg').src = "/phoenixhunt/phoenix-fly-left.gif";}
    if (currentx <= max_left_edge) {
    xa = -xa;
    $('phoeniximg').src = "/phoenixhunt/flying-phoenix.gif";}
    if (currenty >= max_bottom_edge) {
    ya = -ya; }
    if (currenty <= max_top_edge) {
    ya = -ya; }
    };

function falling() {
    if (currenty <=max_bottom_edge){
        $('phoenix').style.top = currenty + fall_speed;
        $('hotspot').style.top = currenty + fall_speed;
        currenty = currenty + fall_speed;
        }
    else {
        clearInterval(drop);
        animate_dog();    
        }
    };

function climbing() {
    if (currenty >=-200){
        $('phoenix').style.top = currenty - climb_speed;
        $('hotspot').style.top = currenty - climb_speed;
        currenty = currenty - climb_speed;        
        }

    else {
        clearInterval(climb);
        $('flyingaway').style.display = 'none';
        disp2[whichphoenix].setAttribute("src", '/phoenixhunt/hit-status-0.gif');        
        $('doganim').src = "/phoenixhunt/instigatoranim2.gif";
        $('phoeniximg').src = "/phoenixhunt/flying-phoenix.gif";
	     setTimeout("Sound.play('/sounds/game/chuckle2.mp3');",500);        
	

        reset_timer=setInterval("reset();",3500);        
        }
    };

function animate_dog() {
    $('doganim').src = "/phoenixhunt/instigatoranim.gif";
    $('phoeniximg').src = '/phoenixhunt/flying-phoenix.gif';
  reset_timer=setInterval("reset();",3500);
    };

function bullets() {
    if (count_shots == 3){
        if (hit_test == false){
			$('bulletsimg').src = '/phoenixhunt/bullets-0.gif';
        	flyaway();
			}
		else {
		$('bulletsimg').src = '/phoenixhunt/bullets-0.gif';
		}
        }
    if (count_shots == 2){
        $('bulletsimg').src = '/phoenixhunt/bullets-1.gif';
        }
    if (count_shots == 1){
        $('bulletsimg').src = '/phoenixhunt/bullets-2.gif';
        }
    if (count_shots == 0){
        $('bulletsimg').src = '/phoenixhunt/bullets-3.gif';
        }
    };

function flyaway() {
    $('phoeniximg').src = '/phoenixhunt/phoenix-fly-away.gif';
    $('flyingaway').style.display = 'block';
    clearInterval(tid);
    clearInterval(points);
	flyobj2.style.display = 'none';
	$('game_area').style.display = 'none';
    climb=setInterval("climbing();",flyaway_interval);
    };
    
function reset() {
    clearInterval(reset_timer);
    if (whichphoenix <= 8){
        whichphoenix ++;
        disp2[whichphoenix].setAttribute("src", '/phoenixhunt/hit-status-1.gif');
        rndEndTest = false;
        reset2();
        }
    else {
        if (count_hit >= 6){
            if (count_hit == 10){
                totscr = totscr+5000;
                updateScore();
                }
            whichphoenix = 0;
            roundnum ++;
            if (roundnum == 10){
                gameover.style.display = 'block';
                endgameTime = setInterval("endgame();",3500);
                }
            updateRound();
            for (i = 0; i < 10; i++){
                disp2[i].setAttribute("src", '/phoenixhunt/hit-status-0.gif');
                }
            count_hit = 0;
            disp2[whichphoenix].setAttribute("src", '/phoenixhunt/hit-status-1.gif');
            rndEndTest = true;
            speed = speed - speed_increment;
            rndTime = setInterval("reset2();",3500);
            }
            else {
                $('gameover').style.display = 'block';    
                endgameTime = setInterval("endgame();",3500);
                }
            }
        };

		function endgame(){
		    clearInterval(endgameTime);
			$('end_game_screen').style.display='block'; 
			$('player_name').focus(); 
			//	post_score();
		 	    };

			function endendgame(){
			   strURL = "/top_scores" ;//+ "?score=" + totscr + '&name=chris&level=2';
			    window.location = strURL;
			    };


function reset2() {
    if (rndEndTest == true){
        clearInterval(rndTime);
        rndEndTest = false;
        $('rounddisplay').style.display = 'none';
        }
    flyobj1 = null;
    flyobj2 = null;
    begin = null;
    dog = null;
    tid = null;
    swap = null;
    drop = null;
    reset_timer = null;
    hit_test = false;
    scr = 1500;
    xa = Math.floor(Math.random()*speed_factor)+base_speed;
    ya = -20; 
    cnt = 0;
    count_shots = 0;
    bullets();
	 currentx = rand(max_right_edge-1);
	 currenty = max_bottom_edge-1;
    flyobj1 =  $('phoenix');
    flyobj2 =  $('hotspot');
    dog = $("dog");
    $('phoenix').style.display = 'block';
  $('hotspot').style.display = 'block';
    $('game_area').style.display = 'block';
    tid = setInterval("move(xa,ya);",speed);
    points = setInterval("pointCalc();",100);
    };