﻿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));
    };

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 = 125;
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()*17)+3;
var ya = -20; 
var count_hit = 0;
var count_shots = 0;
var currentx = rand(262);
var currenty = 210;
var roundnum = 1;
var whichduck = 0;
var rndEndTest = new Boolean(false);
var rndTime;
var pics = [];
var load = [];
var disp = [];
var disp2 = [];
for(i = 0; i < 10; i++){
    pics[i]="score-"+i+".gif";
    }              
for(i = 0; i < pics.length; i++){
    load[i] = new Image();
    load[i].src = pics[i];
    }

function startup() {
    begin = setInterval("start();",3500);
    rounddisp.setAttribute("src",'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", '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("duck");
    flyobj2 = new getObj("hotspot");
    dog = new getObj("dog");
    bullets();
    disp2[whichduck].setAttribute("src", '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","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() {
    clearInterval(tid);
    clearInterval(points);
    totscr = totscr + scr;
    hit_test = true;
	count_hit ++;
	count_shots ++;
    bullets();
    updateScore();
    disp2[whichduck].setAttribute("src", 'hit-status-2.gif');
    flyobj2.style.display = 'none';
    game_area.style.display = 'none';
    duckimg.src = "duck-shot.gif";    
    swap = setInterval("fall();",300);
    };

function miss() {
    scr = scr - 100;
    count_shots ++;
    bullets();
    };

function fall() {
    clearInterval(swap);
    duckimg.src = "duck-fall.gif";
    drop = setInterval("falling();",200);
    };

function move(dx, dy) {
   currentx += dx;
   currenty += dy;
   flyobj1.style.top = currenty;
   flyobj2.style.top = currenty;
   flyobj1.style.left = currentx;
   flyobj2.style.left = currentx;   
   if (currentx >= 265) {
    xa = -xa; 
    duckimg.src = "duck-fly-left.gif";}
    if (currentx <=5) {
    xa = -xa;
    duckimg.src = "duck-fly.gif";}
    if (currenty >=209) {
    ya = -ya; }
    if (currenty <=7) {
    ya = -ya; }
    };

function falling() {
    if (currenty <=200){
        flyobj1.style.top = currenty + 30;
        flyobj2.style.top = currenty + 30;
        currenty = currenty + 30;
        }
    else {
        clearInterval(drop);
        animate_dog();    
        }
    };

function climbing() {
    if (currenty >=-40){
        flyobj1.style.top = currenty - 30;
        flyobj2.style.top = currenty - 30;
        currenty = currenty - 30;
        }
    else {
        clearInterval(climb);
        flyingaway.style.display = 'none';
        disp2[whichduck].setAttribute("src", 'hit-status-0.gif');        
        doganim.src = "doganim2.gif";
        duckimg.src = "duck-fly.gif";
        reset_timer=setInterval("reset();",3500);        
        }
    };

function animate_dog() {
    doganim.src = "doganim.gif";
    duckimg.src = 'duck-fly.gif';
    reset_timer=setInterval("reset();",3500);
    };

function bullets() {
    if (count_shots == 3){
        if (hit_test == false){
			bulletsimg.src = 'bullets-0.gif';
        	flyaway();
			}
		else {
		bulletsimg.src = 'bullets-0.gif';
		}
        }
    if (count_shots == 2){
        bulletsimg.src = 'bullets-1.gif';
        }
    if (count_shots == 1){
        bulletsimg.src = 'bullets-2.gif';
        }
    if (count_shots == 0){
        bulletsimg.src = 'bullets-3.gif';
        }
    };

function flyaway() {
    duckimg.src = 'duck-fly-away.gif';
    flyingaway.style.display = 'block';
    clearInterval(tid);
    clearInterval(points);
	flyobj2.style.display = 'none';
	game_area.style.display = 'none';
    climb=setInterval("climbing();",200);
    };
    
function reset() {
    clearInterval(reset_timer);
    if (whichduck <= 8){
        whichduck ++;
        disp2[whichduck].setAttribute("src", 'hit-status-1.gif');
        rndEndTest = false;
        reset2();
        }
    else {
        if (count_hit >= 6){
            if (count_hit == 10){
                totscr = totscr+5000;
                updateScore();
                }
            whichduck = 0;
            roundnum ++;
            if (roundnum == 10){
                gameover.style.display = 'block';
                endgameTime = setInterval("endgame();",3500);
                }
            updateRound();
            for (i = 0; i < 10; i++){
                disp2[i].setAttribute("src", 'hit-status-0.gif');
                }
            count_hit = 0;
            disp2[whichduck].setAttribute("src", 'hit-status-1.gif');
            rndEndTest = true;
            speed = speed - 10;
            rndTime = setInterval("reset2();",3500);
            }
            else {
                gameover.style.display = 'block';    
                endgameTime = setInterval("endgame();",3500);
                }
            }
        };

function endgame(){
    clearInterval(endgameTime);
    strURL = "A_PHP_FILE_THAT_SAVES_THE_HIGHSCORE.php" + "?p=" + totscr;
    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()*17)+3;
    ya = -20; 
    cnt = 0;
    count_shots = 0;
    bullets();
    currentx = rand(262);
    currenty = 210;
    flyobj1 = new getObj("duck");
    flyobj2 = new getObj("hotspot");
    dog = new getObj("dog");
    flyobj1.style.display = 'block';
    flyobj2.style.display = 'block';
    game_area.style.display = 'block';
    tid = setInterval("move(xa,ya);",speed);
    points = setInterval("pointCalc();",100);
    };