/**
 * A class representing an image gallery.
 *
 * @class net.groupertl.webportals.funradio.radio.media.Gallery
 * @author RTL Group DTIT software development team
 * @version $Revision$
 */
dojo.declare("Gallery", null, {

    /**
     * Constructor of gallery.
     *
     * @constructor {protected} Gallery
     * @param {Element} page Elements containing list of links pointing to an images
     * @param {Element} photo div where image will be show
     * @param {Element} loaderPhoto div id where the loader will be show
     * @param {Element} title  div where is the title of the gallery
     */
    constructor: function(gallerypage, galleryphoto, loaderphoto, gallerytitle) {
        this.loaderphoto = loaderphoto;
        this.galleryphoto = galleryphoto;
        this.links = gallerypage.getElementsByTagName('a');
        this.gallerytitle = gallerytitle;
        this.currentLink = this.links[0];
        
        var _this = this;
        dojo.forEach(this.links, function(link) {
            dojo.connect(link, "onclick", function(e){
                e.preventDefault();
                if(link != _this.currentLink){
                    _this.selectLink(link);
                }
            });
        });
    },

    selectLink: function(link){
        this.loaderphoto.style.display='block';
        
        this.galleryphoto.src = link.href;
        this.galleryphoto.alt = link.title;
        this.gallerytitle.innerHTML = link.title;

        var _this = this;

        //When image is load we hide the loader
        dojo.connect(this.galleryphoto, "onload", function(e){
            _this.loaderphoto.style.display='none';
        });
        dojo.connect(this.galleryphoto, "onerror", function(e){
            _this.loaderphoto.style.display='none';
        });
        
        //we change the current class to the currentlink
        dojo.removeClass(this.currentLink,"current"); 
        dojo.addClass(link,"current");
        this.currentLink = link;
    }
});


/**
 * A class creating player.
 *
 * @class net.groupertl.webportals.funradio.radio.media.Tracks
 * @author RTL Group DTIT software development team
 * @version $Revision$
 */
dojo.declare("Tracks", null, {
	
    /**
     * Constructor of Tracks.
     *
     * @constructor {protected} Tracks
     * @param {Element} tracks Elements containing list of links pointing to a sound
     * @param {String} playerid Id of the creating div which containing the creating player.
     */
    constructor: function(tracks, playerid){
        this.player_div = playerid;
        this.currenttrack = tracks[0];
        this.tracks=tracks;

        var _this = this;
        dojo.forEach(tracks, function(track) {
        
            link = track.getElementsByTagName('a')[0];
            dojo.connect(link, "onclick", function(e){            	
                e.preventDefault();
                 
                _this.changeSong(track, playerid);
            });
        });
    },
    
    // If another song in list play it
    playNext: function(){      
      for(i=0; i<this.tracks.length-1; i++){      
        if(this.tracks[i].innerHTML.indexOf(this.player_div) > -1){
          if(i < this.tracks.length){            
            this.changeSong(this.tracks[i+1], this.player_div);
            return false;
          }
        }        
      }
    },
    
    // Play the selected track
    changeSong: function(track, playerid){
      
      if(this.currenttrack != track){                  
            if(this.currenttrack != null){
                this.currenttrack.getElementsByTagName('div')[0].innerHTML="";
            }
            
            // Create new player object                                       
            track.getElementsByTagName('div')[0].innerHTML = this.createPlayer(track.getElementsByTagName('a')[0], playerid);
            
            this.currenttrack = track;
        }
    },

    
    /**
    *
    * @param {Element} Link of the sound 
    * @return A player with the given sound
    */
    createPlayer: function(sound, playerid){  
        var quality='high';
        var wmode='transparent';
        var allowFullscreen='true';
        var swLiveConnect='true';
        var allowScriptAccess='always';
        var movie=_funradioStaticHttpPath +'/swf/playerFUN.swf?mp3='+ sound +'&amp;autoplay=1&amp;callBack=SoundComplete';
          
        var player='<div class="play" id="'+ this.player_div +'">';
        player +='<object height="27" width="214" id="'+ playerid +'" name="'+ playerid +'" type="application/x-shockwave-flash" data="'+ movie +'">';
        player +='<param name="quality" value="'+ quality +'"/>';        
        player +='<param name="wmode" value="'+ wmode +'"/>';
        player +='<param name="allowFullscreen" value="'+ allowFullscreen +'"/>';
        player +='<param name="swLiveConnect" value="'+ swLiveConnect +'"/>';
        player +='<param name="allowScriptAccess" value="'+ allowScriptAccess +'"/>';
        player +='<param name="movie" value="'+ movie +'"/>';
        player +='</object>';
        player +='</div>';
        
        
        
        return player;
    },
    
    
    param : function text(node, name, value){
        node.name = name;
        node.value = value;
        return node;
    }
});

function SoundComplete() {
  trackObjects.playNext();
}

/**
 * OnLoad method on page player
 */
function loadPagePlayer() {
    var tracks = dojo.query("div.tracks");
    
    if (tracks != null){
        trackObjects = new Tracks(tracks, "playlistssoundsid");
    }
    
    var newssounds = dojo.query("#newssounds li");  
    if (newssounds != null){
      //Not in playlist: inject Tracks object in global ref trackObjects 
      if(document.getElementById('playlistssoundsid')==null){
          trackObjects = new Tracks(newssounds, "newssoundsid");             
      }else{
        new Tracks(newssounds, "newssoundsid");
      }
    }
}

/**
 * OnLoad method on page gallery
 */
function loadPageGallery() {
    var gallerypage = dojo.byId("gallerypage");
    var galleryphoto = dojo.byId("galleryphoto");
    var loaderphoto = dojo.byId("loaderphoto");
    var gallerytitle = dojo.byId("gallerytitle");
    if (gallerypage != null && galleryphoto != null && loaderphoto != null) {
        new Gallery(gallerypage,galleryphoto,loaderphoto,gallerytitle);
	}
}
