 
var playerUi = {
    me : this,
    isOnMove: false,
    loaded : false,
 
    htmlElement : {
        playSound:      	    '.launch-sound',
        playerListAdd:  	    '.playerList-add',
        playerControl : 	    '.player-control',
	    container:		        '.playerContainer',       
        loadBar:         	    '#player_load_bar',
        playBar:        	    '#player_play_bar',
        playTime:      		    '#player_play_time',
        playerProgressPercent: 	'#player_progress_percent',
        play:                	'#player_play',
        pause:            	    '#player_pause',
        stop:                   '#player_stop',
        previous:        	    '#player_previous',
        next:              	    '#player_next',
        volumeBar:      	    '#player_volume_bar',
        volumeBarValue:         '#player_volume_bar_value',
        volumeOff:    		    '#player_volumeOff',
        volumeOn:    		    '#player_volumeOn',
        title:                  '#player_track',
        playerOff:              '.player-off',
        volume:                 0,
        switchComBox:           '#player_com', 
        postCom:                '#post_com',
        playerArtiste:          '#player_artiste',
        playerSound:            '#player_sound',   
    },
    init: function () {
        s2tLog('-- construct player html --');

        this.constructHtml()
        this.initBinds();
        this.setVolumeFromCookie();
         
    },
     initBinds: function () {
         var me = this;
         var _html = me.htmlElement;

         //Controls
            $j(_html.playerOff).click(function() {
                s2tWarePlayer.closeInterface();
                return void(0);
            });
            $j(_html.play).click(function() {
                s2tWarePlayer.playPause();
                return void(0);
            });
            $j(_html.pause).click(function() {
                s2tWarePlayer.playPause();
                return void(0);
            });
            $j(_html.stop).click(function() {
        	s2tWarePlayer.stop()       
                return void(0);
            });
            $j(_html.previous).click(function() {
                if(s2tool.isUser()) {
                    s2tWarePlayer.previous() 
                } else {
                    playerUi.alertNonUser()
                }
            	return void(0)
            });
            $j(_html.next).click(function() {
                 if(s2tool.isUser()) {
                    s2tWarePlayer.next();
                } else {
                    playerUi.alertNonUser()
                }
                return void(0)
            });
            $j(_html.volumeBar).click(function(e) {
                var val = 100 - parseInt( s2tWare.getTheDamnOffset(e).y * 100 /$j(_html.volumeBar).height(),10)
                me.setVolume(val);
            });
        
            $j(_html.loadBar).click(function(e) {
                me.setPlayPosition(e,$j(_html.loadBar));
            });
        
            $j(_html.volumeOff).live('click', function() {
                $j(this).hide();
                $j(_html.volumeOn).show()
                popupPlayer.current.unmute(); 
                return void(0);
            });

            $j(_html.volumeOn).live('click', function() {
                $j(this).hide();
                popupPlayer.current.mute(); 
                $j(_html.volumeOff).show()
                return void(0);
            });
            $j(_html.switchComBox).live('click', function() {
                  me.switchComBox(); 
            });
            $j(_html.postCom).click(function(){ 
                  me.sendComment();
            });
            $j(_html.playerArtiste).click(function() {
                me.goToArtistePage(); 
            });
            $j(_html.playerSound).click(function() {
                me.goToSoundPage(); 
            })

    },
    alertNonUser: function() {
        alert("Fonction accessible au utilisateurs enregistrés uniquement."); 
    },
    constructHtml: function() {
        var HTML =  '<div class="player-control">'
                                + '<ul class="player-controls">'
                                    + '<li><a href="javascript:void(0)" title="Jouer le son" id="player_play" class="player-play" tabindex="1"></a></li>'
                                    + '<li><a href="javascript:void(0)" title="Mettre le son en pause" id="player_pause" class="player-pause" tabindex="1"></a></li>'
                                    + '<li><a href="javascript:void(0)" title="Stopper la lecture" id="player_stop" class="player-stop" tabindex="1"></a></li>'
                                    + '<li><a href="javascript:void(0)" title="Son précédent" id="player_previous" class="player-previous" tabindex="1"></a></li>'
                                    + '<li><a href="javascript:void(0)" title="Son suivant" id="player_next" class="player-next" tabindex="1"></a></li>'
                                    + '<li><a href="javascript:void(0)" title="Couper le son" id="player_volumeOn" class="player-volumeOn" tabindex="1"></a></li>'
                                    + '<li class="scd"><a href="javascript:void(0)" title="Ajouter un commentaire" id="player_com" class="player-com" tabindex="1"></a></li>'
                                    + '<li class="scd"><a href="javascript:void(0)" title="Aller sur le profil de l\'artiste" id="player_artiste" class="player-artiste" tabindex="1"></a></li>' 
                                    + '<li><a href="javascript:void(0)" id="player_volumeOff" title="Activer le son" class="player-volumeOff" tabindex="1"></a></li>' 
                                    + '<li class="scd"><a href="javascript:void(0)" id="player_sound" title="Aller à la page du son" class="player-sound" tabindex="1"></a></li>'   
                                    + '<li><a href="javascript:void(0)" id="player_off" class="player-off" title="Fermer le lecteur" tabindex="1"></a></li>'
                                + '</ul>'
                                + '<marquee id="player_track" class="player-title" scrolldelay="150">- Aucun son chargé dans le lecteur -</marquee>'
                                + '<div class="player-progress">'
                                    + '<span id="player_progress_percent"></span>'
                                    + '<div id="player_load_bar" class="player-load-bar">'
                                        +' <span id="player_play_time" class="player-play-time"></span>'
                                        + '<div id="player_play_bar" class="player-play-bar"></div>'
                                    + '</div>'
                                + '</div>'
                                +' <div id="player_volume_bar" class="player-volume-bar">'
                                    +' <div id="player_volume_bar_value" class="player-volume-bar-value"></div>'
                                +' </div>'
                                +'<div id="comBox" class="context">'
                                    + '<form id="comments-player-form" name="comments-form" action="javascript:void(0)">'
                                    + '<textarea id="comments-form-comment" name="comment" cols="3" row="3"></textarea>'
                                    + '<a href="javascript:void(0);" id="post_com" title="Poster le commentaire"><ins></ins></a>'
                                    + '<input type="hidden" name="jtxf" id="jtxf" value="JCommentsAddComment" />'
                                    + '<input type="hidden" name="object_id" id="object_id" value="0" />'
                                    + '<input type="hidden" name="object_group" id="object_group" value="com_remository" />'
                                    + '</form>'
                                +'</div>'
                    + '</div>' 
                    + '<div class="playerContainer">'
                         +  '<div class="player-ui">'
                         +  '</div>'
                    +' </div>';
        if($j('.playerContainer').length==0) 
         { if($j('body').prepend(HTML)) {this.loaded=true;return true;} else { return false; } }
        
    },
    resetInterface: function () {
        var me = this;
        var _html = me.htmlElement;
        $j(_html.playBar).css({"width":"0"});
        $j(_html.loadBar).css({"width":"0"});
        $j(_html.playerProgressPercent).empty();
        $j(_html.title).html('Aucun son chargé dans le player2teuf');
        $j(_html.playTime).empty();
    },
    setPlayPosition: function(evt,elem) {
        sound = popupPlayer.current; 
        sound.setPosition(parseInt(s2tWare.getTheDamnOffset(evt).x/elem.parent().width() * sound.durationEstimate,10));
    },
    switchPlayPause: function(state) {
        var _html = this.htmlElement;	
        if(state){
            $j(_html.play).hide();
            $j(_html.pause).show();
        }else{
            $j(_html.pause).hide();
            $j(_html.play).show();
        }
    },
    setVolumeFromCookie: function () {
        if(s2tool.getCookie("s2tCookie[player_volume]")==null) {
            s2tool.setCookie("s2tCookie[player_volume]", 50, 2);
        } 
        s2tWarePlayer.volume = s2tool.getCookie("s2tCookie[player_volume]");
        s2tLog('-- Get player volume --');
        this.setVolume(s2tWarePlayer.volume);
    },
    setVolume: function(val) {
        s2tLog("-> set volume");
        var _html = this.htmlElement
        var volHeight =  val * $j(_html.volumeBar).height() / 100;
        $j(_html.volumeBarValue).css({height:volHeight})
        s2tWarePlayer.volume = val;
        s2tool.setCookie("s2tCookie[player_volume]", val, 2);
        if(typeof(popupPlayer.current) != 'undefined') {
            popupPlayer.current.setVolume(val);
        }
   }, 
   update: function () { 
       s2tLog('-- update player --');
       var cu = popupPlayer.current;
        if(typeof(cu)!='undefined'){
         $j(this.htmlElement.title).html(cu.autorUsername + " - " + cu.title);
         if(s2tWarePlayer.volume){ this.setVolume(s2tWarePlayer.volume); }
         $j('#object_id').attr('value',cu.soundId);
       } 
    },
    updateLoadingProgress: function (percent) {
        $j(this.htmlElement.loadBar).css({
            width: percent + "%"
        });
       $j(this.htmlElement.playerProgressPercent).html(percent + "%");
    },
    updateSoundTime: function(element) {
        var position = element.position;
        var duration = element.duration;
        var playTime;

        actSec = parseInt(position/1000 % 60,10);
        if(actSec<10) { actSec = "0"+actSec; }
        actMin = parseInt(position/1000/60,10);
        totSec = parseInt(duration/1000 % 60,10);
        if(totSec<10) { totSec = "0"+totSec; }
        totMin = parseInt(duration/1000/60,10);
        playTime = actMin+":"+actSec+"/"+totMin+":"+totSec;
        percentPlayed = Math.round(position/duration*100);
        $j(this.htmlElement.playTime).html(playTime)
        $j(this.htmlElement.playBar).css({
            width: percentPlayed + "%"
        });
    },
    switchComBox: function () {
        var me = this;
            if(typeof(s2tSound)!='undefined') {
                $j('#object_id').attr('value',popupPlayer.current.soundId);
                if($j("#player_com").hasClass('on')) {
                    $j("#player_com").removeClass('on');
                    $j('#comBox').removeClass('on');
                } else { 
                    $j("#player_com").addClass('on');   
                    $j("#comBox").addClass('on');
                } 
            }
    },
    sendComment: function(){
        if($j("#comments-form-comment").attr('value') != "" && $j('#object_id').attr('value') != '0') {
            s2tLog($j('#object_id').attr('value'));
            $j.post("/index.php?option=com_jcomments&tmpl=component&", $j("#comments-player-form").serialize(), function(data) {
                $j("#player_com, #comBox").removeClass('on');
            }); 
       }
    },
    goToArtistePage: function () {
        if(typeof(s2tSound)!='undefined') {
            url = "/" + popupPlayer.current.autorUsername; 
            document.location.href=url;
        } 
    },
    goToSoundPage: function () {
        if(typeof(s2tSound)!='undefined') {
            url = "/index.php?option=com_remository&func=fileinfo&id=" + popupPlayer.current.soundId; 
            document.location.href=url;
        } 
    }

};

