

function bgMusic(){

    this.playing = false;
    this.playerInterval = null;
    this.playerStep=0;
    this.hovered = false;

    var scope = this;
    var bg_player_src = "assets/mp3/niftyplayer.swf";
    var bg_mp3= "assets/mp3/loop.mp3";
    var bg_ogg= "assets/mp3/loop.ogg";

    var bg_embed= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="niftyPlayerBg" ><param name="movie" value="'+bg_player_src+'?file='+bg_mp3+'&amp;as=0"><param name="quality" value="high"><embed src="'+bg_player_src+'?file='+bg_mp3+'&amp;as=0" quality="high" bgcolor="#FFFFFF" width="0" height="0" name="niftyPlayerBg" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
    var bg_audio_embed = "<audio id='bg_sound' src='%audio_file%' preload='auto'></audio>";
    
    this.init = function(){

        if(clientSettings.hasAudioSupport){
                
            var formatted_bg_embed = bg_audio_embed.replace(/\%audio_file\%/g,clientSettings.isSafari?bg_mp3:bg_ogg);

            new Element("span",{
                "html" : formatted_bg_embed,
                "class" : "audio_player"
            }).inject("player");
                
            scope.audioPlayer = new Audio();
            scope.audioPlayer.src = document.getElementById("bg_sound").src;
            
            if(window.attachEvent)
                scope.audioPlayer.attachEvent("ended",function(){
                    scope.audioPlayer.load();
                    scope.audioPlayer.play();
                });
            else scope.audioPlayer.addEventListener("ended",function(){
                    scope.audioPlayer.load();
                    scope.audioPlayer.play();
                },false);
            
            scope.audioPlayer.load();
        }
        else if(clientSettings.hasFlashSupport){
            
            new Element("span",{
                "html" : bg_embed,
                "class" : "audio_player"
            }).inject("player");

        }
        $("player").addEvent("mouseenter",function(){

            scope.hovered = true;
            $("player").setStyle("background-position","-"+(scope.playerStep*26)+"px "+(scope.hovered?"-26":"0")+"px");

        })

        $("player").addEvent("mouseleave",function(){

            scope.hovered = false;
            $("player").setStyle("background-position","-"+(scope.playerStep*26)+"px "+(scope.hovered?"-26":"0")+"px");

        })

    }

    this.togglePlaying = function(){

        if(clientSettings.hasAudioSupport){
            
            if(scope.playing){
                scope.audioPlayer.pause();
                scope.stopAnimatePlayer();
                if(clientSettings.isHomePage)
                    setCookie("playerMuted",1,365);
            }
            else {
                scope.audioPlayer.load();
                scope.audioPlayer.play();
                scope.startAnimatePlayer();
                if(clientSettings.isHomePage)
                    setCookie("playerMuted",0,365);
            }
        }
        else if(clientSettings.hasFlashSupport){
            niftyplayer("niftyPlayerBg").registerEvent("onSongOver",'niftyplayer("niftyPlayerBg").play()')

            if(scope.playing){
                niftyplayer("niftyPlayerBg").pause();
                scope.stopAnimatePlayer();
                if(clientSettings.isHomePage)
                    setCookie("playerMuted",1,365);
            }
            else {
                
                niftyplayer("niftyPlayerBg").play();
                scope.startAnimatePlayer();
                if(clientSettings.isHomePage)
                    setCookie("playerMuted",0,365);
            }
        
        }

        scope.playing=!scope.playing;

    }

    this.startAnimatePlayer = function(){

        scope.interval = setInterval(scope.animatePlayer,200);

    }

    this.stopAnimatePlayer = function(){

        clearInterval(scope.interval);

    }

    this.animatePlayer = function(){

        if(scope.playerStep!=3){
            scope.playerStep++;
        }
        else {
            scope.playerStep = 0;
        }

        $("player").setStyle("background-position","-"+(scope.playerStep*26)+"px "+(scope.hovered?"-26":"0")+"px");

    }
}
