
// global variable holder
// so i won't pollute the browser/page namespace'
var game={};
game.debug=0;

var GameObject = new Class({
	initialize: function(){
		this.x=0;
		this.y=0;
		this.z=0;
		this.HTMLIncarnation=null;
                this.world=null;
                this.type=null;
	},
        comeIntoExistence: function(world){
            this.HTMLIncarnation.injectInside(world.HTMLIncarnation);
            this.world=world;
        }
});

var World = new Class({
        Extends: GameObject,
	initialize: function(width,height){
		this.parent();
		this.HTMLIncarnation = new Element('div',{'class': 'world'});
                this.width = width;
                this.height = height;
                this.type='world';
                this.worldWindow={width: 0,height: 0};
                this.scrollStep=2*game.standardWidth;
                this.viewport=$('viewport');
                this.verticalMove = new Fx.Tween(this.HTMLIncarnation, {duration: 300});
                this.horizontalMove = new Fx.Tween(this.HTMLIncarnation, {duration: 300});
	},
	start: function(){
            this.scrollOffset=0;
          this.HTMLIncarnation.injectInside(this.viewport);
          this.worldWindow.width=parseInt(this.viewport.getStyle('width'));
          this.worldWindow.height=parseInt(this.viewport.getStyle('height'));
          this.HTMLIncarnation.setStyle('width',this.width);
          this.HTMLIncarnation.setStyle('height',this.height);
    },
    changeWorldParameters: function(width,height){
        this.width = width;
        this.height = height;
        this.HTMLIncarnation.setStyle('width',this.width);
        this.HTMLIncarnation.setStyle('height',this.height);
    },
    moveLeft: function(){
        var step=this.scrollStep;
        if(this.x-step+game.worldWidth<=this.worldWindow.width)
            step=this.x+game.worldWidth-this.worldWindow.width;
        if(step>0){
          this.horizontalMove.start('left',this.x,this.x-step);
          this.x=this.x-step;
        }
    },
    moveRight: function(){
        var step=this.scrollStep;
        if(this.x+step>0)
            step=(-1)*this.x;
        if(step>0){
          this.horizontalMove.start('left',this.x,this.x+step);
          this.x=this.x+step;
        }
    },
    moveUp: function(){
        var step=this.scrollStep;
        if(this.y-step+game.worldHeight<=this.worldWindow.height)
            step=this.y+game.worldHeight-this.worldWindow.height;
        if(step>0){
          this.verticalMove.start('top',this.y,this.y-step);
          this.y=this.y-step;
        }
    },
    moveDown: function(){
        var step=this.scrollStep;
        if(this.y+step>0)
            step=(-1)*this.y;
        if(step>0){
          this.verticalMove.start('top',this.y,this.y+step);
          this.y=this.y+step;
        }
    },
    resetIntoView: function(){
        if(this.x!=0){
            this.horizontalMove.set('left',0);
            this.x=0;
        }
        if(this.y!=0){
            this.verticalMove.set('top',0);
            this.y=0;
        }
    },
    insertObjectIntoWorld: function(obiect){
        obiect.comeIntoExistence(this);
    },
    getWorldWrapper: function(){
        return this.viewport;
    }
});

function initWorld(){
    initGame();
    if(game.bgsound)
        game.sound.playBGSound();
    if(game.debug==1){
        game.debugMessage = '';
        game.debugConsole = new Element('div',{'class': 'debug'});
        game.debugConsole.injectInside(document.body);
        game.appendLog = function(mesaj){
            game.debugMessage+='<br>'+mesaj;
        };
        game.writeLog=function(){
            this.debugConsole.set('html',this.debugMessage);
            this.debugMessage = '';
        };
        game.world.HTMLIncarnation.addEvent('click',function(e){
                                          var zone=util.getZoneAtPoint(e.event.clientX-150,e.event.clientY-20);
                                          game.clickedZone = zone;
                                          });
    }
}

function startGame() {

    var introDiv = new Element('div',{'class': 'intro'});
    game.sound = new DynaSound('DynaSound.swf');
    introDiv.set('html','<br>JS BOMBERMAN<BR>BACK IN GLORY<br>Arrows to move the hero and SPACE to launch a bomb.<br>P p ESC to pause<br><br>');
    var startButton = new Element('a',{'href': '#top','events': {'click': function(){
                                                      //loadImages();
                                                      var introEffect = new Fx.Tween(this,{'duration': 500});
                                                      introEffect.addEvent('onComplete',function(){this.destroy();initWorld();}.bind(this));
                                                      introEffect.start('opacity',1,0);
                                                    }.bind(introDiv)}});
    startButton.set('text','START');
    var startDebugButton = new Element('a',{'href': '#top','events': {'click': function(){
                                                      //loadImages();
                                                      var introEffect = new Fx.Tween(this,{'duration': 500});
                                                      introEffect.addEvent('onComplete',function(){game.debug=1;this.destroy();initWorld();}.bind(this));
                                                      introEffect.start('opacity',1,0);
                                                    }.bind(introDiv)}});
    startButton.set('text','START');
    startButton.inject(introDiv);
    var br=new Element('br');
    br.inject(introDiv);
    br.clone(false).inject(introDiv);
    startDebugButton.set('text','START-DEBUGMODE');
    startDebugButton.inject(introDiv);
    $('soundLevelIndicator').setOpacity(0.5);
    game.soundLevelIndicator = document.newTextNode('volume: 100%');
    $('soundLevelContainer').insertBefore(game.soundLevelIndicator,$('soundLevelIndicator'));
    $('soundLevelContainer').addEvent('click',function(e){
        var evt = new Event(e);
        evt.stopPropagation();
        var coords=$('soundLevelContainer').getCoordinates();
        var xEvent = evt.client.x;
        var volume = xEvent - coords.left;
        $('soundLevelIndicator').setStyle('width',volume);
        volume = parseInt(100*(volume/coords.width));
        if(Browser.Engine.trident)
            game.soundLevelIndicator.data='volume: '+volume+'%';
        else
            game.soundLevelIndicator.textContent='volume: '+volume+'%';
        game.sound.setVolume(volume);
    });

    introDiv.inject($('viewport'));
    $('bgsoundcheckbox').addEvent('click',function(){
    game.bgsound=game.bgsound ? false : true;
    if(game.bgsound){
        game.sound.playBGSound();
        this.blur();
    }
    else {
        game.sound.stopBGSound();
        this.blur();
    }
    });
}

function flashLoadedHandler(){
    game.sound.initSounds();
}
// used for debugging purposes . it adds to the window.status the x,y : where the mouse points on the screen
function mouseHoverProcessor(e){
    //var status = window.status.split(' --- ')[0];
    //window.status=status+' --- '+e.clientX+' '+e.clientY;
}

if(game.debug==1)
  document.onmousemove=mouseHoverProcessor;

/**
 * loadImages
 */
/*
function loadImages() {
    game.images = {};
    game.images.heroStop=Asset.image('img/player-walking-stop.gif');
    game.images.heroWalkingUp=Asset.image('img/player-walking-back.gif');
    game.images.heroWalkingDown=Asset.image('img/player-walking-front.gif');
    game.images.heroWalkingLeft=Asset.image('img/player-walking-left.gif');
    game.images.heroWalkingRight=Asset.image('img/player-walking-right.gif');
} */


