﻿/* SSPL */
/*Selim Saraçoğlu Play List selimforever@hotmail.com*/
/**/
/**/
/**/
var mp3Plist = new Object();
window.mp3Plist = mp3Plist;
document.mp3Plist = mp3Plist;
function SSPL(parentId, idPlayer, defSongs, defs,onAddItem) {
    var name = idPlayer + parentId;
    if (mp3Plist[name]) return mp3Plist[name];

    this.Volume = 50;
    this.Load = 0;
    this.Time = 0;
    this.Status = 0;
    this.OnAddItem = onAddItem;//fonksiyon
    this.Name = name;
    this.TableId = 'MyPlayList' + Math.round(Math.random() * 1000); //yeni tablo için id
    jQuery("#" + parentId).append('<table class="LayoutTable ' + this.TableId + '" style="width:98%;" cellpadding="1" cellspacing="0" id="' + this.TableID + '"></table>');
    this.Table = new jQuery('#' + this.TableID, "#" + parentId);
    this.IdPlayer = idPlayer;
    this.PlayList = function() {
        return jQuery("tr", "." + this.TableId);
    }
    this.GetItem = function(index) {
        return this.PlayList()[index];
    }
    this.DefaultSongs = new Array({ SongLink: '/Sound/sound.mp3', SongName: 'Exotic Dancer' });
    if (defSongs && defSongs.length && defSongs.length > 0) {
        this.DefaultSongs = defSongs;
    }
    this.Defaults = {
        AutoStart:false,
        Loop: true,
        Play: 'Oynat',
        Pause: 'Duraklat',
        Stop: 'Durdur',
        PlayingMessage: " Çalınıyor...",
        PausedMessage: " Duraklatıldı...",
        BufferingMessage: " Yükleniyor...",
        BufferingComplateMessage: " Yüklendi...",
        ErrorMessage: "Hata Oluştu"

    };
    if (defs) {
        jQuery.extend(this.Defaults, defs);
    }
    
    
    this.Status = 0; //0 boş 1 yükleniyor 2 çalınıyor 3 duraklatıldı
    this.ActiveSongIndex = -1;
    this.ActiveSongOriginalName = "";


    this.OnPlay = function() {
        var pb = jQuery("td.playbutton img", this.GetItem(this.ActiveSongIndex));
        pb[0].src= myImagesPath + "player/pause.gif";
        pb[0].title=this.Defaults.Pause;
        var obj = this;
        pb[0].click = function() {
            obj.Play(obj.ActiveSongIndex);
        };
        jQuery(this.GetItem(this.ActiveSongIndex)).addClass('active');
        this.Status = 2;

    };
    this.OnPause = function() {
        var pb = jQuery("td.playbutton img", this.GetItem(this.ActiveSongIndex));
        pb[0].src= myImagesPath + "player/play.gif";
        pb[0].title=this.Defaults.Play;
        var obj = this;
        pb[0].click = function() {
            obj.Play(obj.ActiveSongIndex);
        };
        jQuery(this.GetItem(this.ActiveSongIndex)).addClass('active');
        this.Status =3;
    }
    this.OnStop = function() {
        var pb = jQuery("td.playbutton img", this.GetItem(this.ActiveSongIndex));
        pb[0].src= myImagesPath + "player/play.gif";
        pb[0].title=this.Defaults.Play;
        jQuery(this.GetItem(this.ActiveSongIndex)).removeClass('active');
        this.Status = 0;
        this.ActiveSongIndex = -1;
    };

    this.LoadPList = function() {
        for (ix = 0; ix < this.DefaultSongs.length; ix++) {
            this.AppendItem(this.DefaultSongs[ix].SongLink, this.DefaultSongs[ix].SongName);
            CloseBottomLists("#PlayerPanelContent");
        }
        loadPlayer("mp3/no.mp3", false, 0);
        if (this.PlayList().length > 0 && this.Defaults.AutoStart) {
            try {

                this.Play(0, true);
                this.SetVolume(this.Volume);
            } catch (ex) {
                window.setTimeout(function() { try { this.Play(0); this.SetVolume(this.Volume); } catch (ex1) { } }, 10);
            }
        }

    };
    this.AppendItem = function(SongLink, SongName) {

        var songId = 0;
        if (a = SongLink.match(/(\d+).mp3$/i)) {
            songId = a[1];
        }
        var MyPLItem = '<tr>'
                + '<td class="playbutton">'
                + '<img alt="' + this.Defaults.Play + '" onclick="mp3Plist[\'' + this.Name + '\'].Play(' + this.PlayList().length + ')" src="' + myImagesPath + 'player/play.gif' + '" style="cursor:pointer"/>'
                + '</td>'
                + '<td class="song">'
                + SongName
                + '</td>'
                + '<td class="state">'
                + '</td>'
                + '<td class="addcart">'
                + '<a href="Order/ShoppingCart.aspx?P=Music&Id=' + songId + '" target="' + frame + '"><img src="' + myImagesPath + 'icons/sepet2.gif"/></a>'
                + '</td>'
                + '<td class="addfavorits">'
                + '<a href="Member/Favorits.aspx?P=Music&Id=' + songId + '" target="' + frame + '"><img src="' + myImagesPath + 'icons/favorilereekle.gif"/></a>'
                + '</td>'
        + '</tr>';
        MyPLItem = new jQuery(MyPLItem);
        MyPLItem[0].MediaFile = SongLink;
        MyPLItem[0].OriginalName = SongName;
        MyPLItem[0].AppendId = this.PlayList().length;
        this.Table.append(MyPLItem);
        if (typeof (this.OnAddItem) == "function") {
            this.OnAddItem();
        }


    };
    this.Play = function(index, isFirst) {
        if (this.PlayList().length > 0) {
            if (index < 0 && index >= this.PlayList().length)
                return this.Play(0);
            if (this.ActiveSongIndex == index) {
                if (this.Status == 2)
                { this.OnPause(); }
                else { this.OnPlay(); }
                sendEvent('playpause');
            } else {
                this.OnStop();
                this.ActiveSongIndex = index;
                loadPlayer("mp3/no.mp3", false, 0);
                var st = true;
                if (isFirst && !this.Defaults.AutoStart) {
                    st = false;
                }
                loadPlayer(this.GetItem(index).MediaFile, st, this.Volume);
                this.OnPlay();
            }


        }
    };
    this.Stop = function() {
        loadPlayer("mp3/no.mp3");
        this.OnStop();
    };
  
    this.Next=function()
    {
        this.Play(this.ActiveSongIndex + 1);
    }
    this.Previous=function()
    {
        this.Play(this.ActiveSongIndex - 1);
    }
    this.SetVolume = function(vol) {
        sendEvent('volume', vol);
        this.OnVolumeChanged(vol);
    };
    this.ChangeStatus = function(i) {
        alert(i);
    }
    mp3Plist[this.Name] = this;
    window.mp3Plist = mp3Plist;
    document.mp3Plist = mp3Plist;
    this.LoadPList();
    return this;
}
function loadPlayer(file,autostart,volume) {
      var FO = { movie: playerpath+"player.swf",
        id: idPlayer, width: "99%", height: "20",
        majorversion: "5", build: "0", bgcolor: "#FFFFFF",
        allowscriptaccess:'always',wmode:'transparent',
        flashvars: "allowscriptaccess=always&wmode=transparent&file=" + file + "&showdigits=true&autostart=" + autostart + "&repeat=false&volume=" + volume + "&enablejs=true"
    };
    UFO.create(FO, "PlayerDiv");
};
function sendEvent(typ, prm) {
    thisMovie(idPlayer).sendEvent(typ, prm);
};
function getUpdate(typ, pr1, pr2) {
    for (ix in mp3Plist) {
        if (typ == "state") {
            ix.ChangeStatus (pr1);
        } else if (typ == "volume") {
            ix.Volume = pr1;
        } else if(typ=="time") {
            ix.Time=pr1+","+pr2;
        }else if(typ=="load") {
            ix.Load = pr1;
        }
    } 
        
   
    //var id = document.getElementById(typ);
    //id.innerHTML = typ + ": " + Math.round(pr1);
    //pr2 == undefined ? null : id.innerHTML += ", " + Math.round(pr2);
    //alert(typ + "\n" + pr1 + "\n" + pr2);
};
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
};

