﻿/// <reference path="main.js" />

var trackList = new Array();
var albumList = new Array();
var artistList = new Array();


Player = function() {
    this.swf = null;
    this.currentPlaylist = null;
    this.playListTracks = new Array();
    this.currentPos = 0;
    this.access = true;
    this.leavePlayListOpen = false;
    this.isPluginMissing = true;
    this.isPaused = false;
    this.showPlaylistLayer = false;
    this.onLayer = false;
    this.isSliderDisabled = false;

    this.activate = function(a, b) {
        this.access = a;
    }
    this.showFlashMissing = function() {
        contextMenu.showHint("Bitte installiere die aktuelle Version des kostenlosen Adobe Flash Players.</br> <a target='_blank' href='http://www.adobe.com/go/getflashplayer_de'>Jetzt herunterladen</a>");
    }
    this.initPlayer = function() {
        this.addSWF();

        var flexApp = swfobject.getObjectById("HVM_Player");
        this.swf = flexApp;
        this.initDefaultPlaylist();
        this.initPlayerEvents();
    }
    this.addSWF = function() {
        var playerVersion = swfobject.getFlashPlayerVersion();
        if (playerVersion.major < 9) {
            this.showFlashMissing();
            return;
        }
        this.isPluginMissing = false;

        /* Add Flash content */
        swfobject.createSWF({ data: "Content/steereo_player_v2.05.swf", width: "0", height: "0", version: "9.0.0",
            expressInstallSwfurl: "Content/expressInstall.swf"
        },
		    { allowScriptAccess: "sameDomain" },
		    "HVM_Player");
    }
    this.initPlayerEvents = function() {

        $("#player_timeline").slider({
            range: "min",
            value: 0,
            min: 0,
            max: 100,
            slide: function(event, ui) {
                _player.swf.SetProgress(ui.value);
            }
        });

        $("#player_timeline").bind("mousedown", function() {
            if (_player.isSliderDisabled == true) {
                contextMenu.showHint("Das Springen innerhalb eines Titels ist bei einem Radio nicht möglich");
            }
        });

        $("#player_volume").slider({
            value: 50,
            min: 0,
            max: 100,
            step: 10,
            slide: function(event, ui) {
                _player.swf.SetVolume(ui.value);
            }
        });
        /* tooltip */
        $("#player_volume").bind("mouseout", function() { _player.hideToolTip(); });
        $("#player_volume").bind("mouseover", function() { _player.showToolTip("LAUTSTÄRKE", GetCoordinateX(document.getElementById("player_volume"))); });
        //

        $("#player_cover_div").bind("mouseover", function() { $("#player_cover_div").css('border', 'solid 2px White'); });
        $("#player_cover_div").bind("mouseout", function() { $("#player_cover_div").css('border', 'solid 2px Transparent'); });
        $("#player_cover_div").click(function() {
            var track = _player.getCurrentTrack();
            if (track != null) {
                //var hash = "Musik/" + track.ArtistName + "/" + track.AlbumName;
                var hash = "Musik/" + escapeUrl(track.AlbumArtistName) + "/" + escapeAlbumNameUrl(track.AlbumName, track.AlbumId);
                //updatePage(hash);

                // Martin says:
                // hash already is escaped but updatePage always escapes it again thus one gets double escaped strings that don't work.
                // first try of a fix: unescape the escaped hash to escape it again afterwards (probably not a very smart solution...)
                updatePage(unescape(hash));
            }

        });

        $("#player_Play").bind("mouseover", function() { $("#player_Play").attr("src", "static/images/player-play-hover.gif"); });
        $("#player_Play").bind("mouseout", function() { $("#player_Play").attr("src", "static/images/player-play.gif"); });

        $("#player_Pause").bind("mouseover", function() { $("#player_Pause").attr("src", "static/images/pause-button-hover.gif"); });
        $("#player_Pause").bind("mouseout", function() { $("#player_Pause").attr("src", "static/images/pause-button.gif"); });
        $("#player_Pause").click(function() { _player.Pause(); });


        $("#player_Next").bind("mouseover", function() { $("#player_Next").attr("src", "static/images/next-hover.gif"); });
        $("#player_Next").bind("mouseout", function() { $("#player_Next").attr("src", "static/images/next.gif"); });

        $("#player_Love").bind("mouseover", function() { $("#player_Love").attr("src", "static/images/love_red_gray.gif"); });
        $("#player_Love").bind("mouseout", function() { $("#player_Love").attr("src", "static/images/love_white_gray.gif"); });
        $("#player_Love").click(function() {
            _player.LoveTrack();
        });
        /* tooltip */
        $("#player_Love").bind("mouseout", function() { _player.hideToolTip(); });
        $("#player_Love").bind("mouseover", function() { _player.showToolTip("ZU FAVORITEN", GetCoordinateX(document.getElementById("player_Love"))); });
        //

        $("#player_Ban").bind("mouseover", function() { $("#player_Ban").attr("src", "static/images/ban_red_gray.gif"); });
        $("#player_Ban").bind("mouseout", function() { $("#player_Ban").attr("src", "static/images/ban_white_gray.gif"); });
        $("#player_Ban").click(function() {
            _player.BanTrack();
        });
        /* tooltip */
        $("#player_Ban").bind("mouseout", function() { _player.hideToolTip(); });
        $("#player_Ban").bind("mouseover", function() { _player.showToolTip("VERBANNEN", GetCoordinateX(document.getElementById("player_Ban"))); });
        //

        $("#player_Buy").bind("mouseover", function() { $("#player_Buy").attr("src", "static/images/buy_blue_gray.gif"); });
        $("#player_Buy").bind("mouseout", function() { $("#player_Buy").attr("src", "static/images/buy_white_gray.gif"); });
        $("#player_Buy").click(function() {
            var track = _player.getCurrentTrack();
            if (track != null) {
                showBuyLayer(track.ArtistName, track.ArtistId, track.AlbumName, track.AlbumId, track.Name, track.Id, playerModes.Titel);
            }
        });
        /* tooltip */
        $("#player_Buy").bind("mouseout", function() { _player.hideToolTip(); });
        $("#player_Buy").bind("mouseover", function() { _player.showToolTip("MP3s, CDs, KONZERTKARTEN", GetCoordinateX(document.getElementById("player_Buy"))); });
        //

        $("#player_Send").bind("mouseover", function() { $("#player_Send").attr("src", "static/images/send_blue_gray.gif"); });
        $("#player_Send").bind("mouseout", function() { $("#player_Send").attr("src", "static/images/send_white_gray.gif"); });
        $("#player_Send").click(function() {
            var track = _player.getCurrentTrack();
            if (track != null) {
                //_share.shareArtist(track.ArtistName, track.ArtistId);
                //_share.shareTrack(track.ArtistName, track.ArtistId, track.Name, track.Id);
                //_share.shareTrack(track.AlbumArtistName, track.AlbumArtistId, track.AlbumName, track.AlbumId, track.Name, track.Id);
                _share.shareTrack(track.AlbumArtistName, track.AlbumArtistId, track.AlbumName, track.AlbumId, track.Name, track.Id, track.ArtistName, track.ArtistId);
            }
        });
        /* tooltip */
        $("#player_Send").bind("mouseout", function() { _player.hideToolTip(); });
        $("#player_Send").bind("mouseover", function() { _player.showToolTip("WEITERSAGEN", GetCoordinateX(document.getElementById("player_Send"))); });
        //

        $("#playlist_share").click(function() {
            if (_player.currentPlaylist.Type == playerModes.SavedPlaylist) {
                _share.sharePlaylist(_player.currentPlaylist.Id, _player.currentPlaylist.Name, _player.currentPlaylist.Owner);
            }
            if (_player.currentPlaylist.Type == playerModes.Album) {
                var track = _player.getCurrentTrack();
                if (track != null) {
                    //_share.shareAlbum(track.ArtistName, track.ArtistId, track.AlbumName, track.AlbumId);
                    _share.shareAlbum(track.AlbumArtistName, track.AlbumArtistId, track.AlbumName, track.AlbumId); ///###
                }
            }
        });
        $("#playlist_save").click(function() {
            if (contextMenu.isLoggedIn) {
                contextMenu.savePL();
            }
            else {
                contextMenu.showLoginHint(triggerTypes.Save, null, null);
            }
        });
        $("#playlist_delete").click(function() { contextMenu.emptyPlaylist(); });
        $("#playlist_rename").click(function() { contextMenu.renamePL(); });

        $("#player").mouseenter(function() {
            if (_player.showPlaylistLayer) {
                $("#player").height(313);
                _player.onLayer = true;
            }
        }).mouseleave(function() {
            if (!_player.leavePlayListOpen) {
                setTimeout("_player.hidePlaylist()", 1000);
                _player.onLayer = false;
            }
            else {
                $(document).bind("mousedown", _player.hidePlaylist);
            }
        });
    }
    this.hidePlaylist = function() {
        if (!this.onLayer) {
            $("#player").height(83);
        }
        $(document).unbind("mousedown", _player.hidePlaylist);
    }
    this.getCurrentTrack = function() {
        var track = this.currentPlaylist.Tracks[this.currentPos];
        if (trackList == 'undefined') return null;
        else return track;
    }
    this.getCurrentTrackAsItem = function() {
        var track = this.getCurrentTrack();
        if (track == null) return null;
        var currentItem = {};
        currentItem.TrackId = track.Id; currentItem.TrackName = track.Name;
        currentItem.AlbumId = track.AlbumId; currentItem.AlbumName = track.AlbumName;
        currentItem.AlbumArtistName = track.AlbumArtistName;
        currentItem.ArtistId = track.ArtistId; currentItem.ArtistName = track.ArtistName;
        return currentItem;
    }

    this.initDefaultPlaylist = function() {
        $.getJSON("Playlist/Default",
                function(data) {
                    if (data.Radio) {
                        _player.initPlaylist(data, playerModes.Radio, false, radioModes.ChartsRadio);
                    }
                    else {
                        _player.initPlaylist(data, playerModes.SavedPlaylist, false);
                    }
                });
    }
    this.removeTrackFromPlaylist = function(trackid) {
        for (var i = 0; i < this.currentPlaylist.Tracks.length; i++) {
            if (trackid == this.currentPlaylist.Tracks[i].Id) {
                this.currentPlaylist.Tracks.splice(i, 1);
                break;
            }
        }
        this.swf.UpdatePlaylist(this.currentPlaylist.Tracks, this.currentPlaylist.Type);
        this.showPlaylistTracks();
    }
    this.removeAllTracks = function() {
        this.currentPlaylist.Tracks.length = 0;
        this.showPlaylistTracks();
    }
    this.trackExistsInPlaylist = function(id) {
        for (var i = 0; i < this.currentPlaylist.Tracks.length; i++) {
            if (id == this.currentPlaylist.Tracks[i].Id) {
                return true;
            }
        }
        return false;
    }
    this.addTrack = function(id, name, artistId, artist, albumId, album, image, genre, albumArtistId, albumArtistName, licensor, canBePlayed) {
        if (this.currentPlaylist.UserOwned) {
            // Add to current user owned playlist
            if (!this.trackExistsInPlaylist(id)) {
                this.currentPlaylist.Tracks.push(new Track(id, name, artistId, artist, albumId, album, image, genre, albumArtistId, albumArtistName, licensor, canBePlayed));
                this.showPlaylistTracks();
                contextMenu.showHint("Der Titel wurde zur ungespeicherten Wiedergabeliste hinzugefügt.");
            }
            else {
                contextMenu.showHint("Dieser Titel wurde bereits zu dieser Wiedergabeliste hinzugefügt.");
            }
            /* Update playlist in player */
            _player.swf.UpdatePlaylist(this.currentPlaylist.Tracks, this.currentPlaylist.Type);
        }
        else {
            // empty playlist, play song, display "unsaved playlist, enabled links "
            var data = new Object(); data.Name = "Ungespeicherte Wiedergabeliste"; data.Tracks = new Array(); data.Tracks.push(new Track(id, name, artistId, artist, albumId, album, image, genre, albumArtistId, albumArtistName, licensor, canBePlayed));
            this.initPlaylist(data, playerModes.UnSavedPlaylist, true);
            this.Play();
        }
        /*Animation to indicate that track was added */
        $("#player").height(135); setTimeout("$('#player').height(83)", 500);
    }




    this.showPlaylistTracks = function() {

        var content = String.format("<div style='height:{0}px;width:230px;'><ul class='PlayList'>", this.currentPlaylist.Tracks.length * 25);

        var index = 0;
        $.each(this.currentPlaylist.Tracks, function(i, item) {
            if (item.CanBePlayed) {
                content += "<li onmouseout=\"pl_imgout(this)\" onmouseover=\"pl_imgover(this)\" class=\"plLI\">";
                content += String.format("<a style=\"float: left; margin-top: 3px;\" onclick=\"AdManager.play({0},'fromPlaylist',{});return false;\" href=\"javascript:void(0)\">{1}</a>", index, (item.Name.length > 30) ? item.Name.substring(0, 30) + "..." : item.Name);
            }
            else {
                content += "<li onmouseout=\"_toolTip.hide();pl_imgout(this)\" onmouseover=\"pl_imgover(this);showIsForbiddenToPlayTooltipForTrack(this, 60 + document.documentElement.scrollTop, 270 + document.documentElement.scrollLeft, 'left');\" class=\"plLI\">";
                content += String.format("<a class=\"Forbidden\" style=\"float: left; margin-top: 3px;\" href=\"javascript:void(0)\">{0}</a>", (item.Name.length > 30) ? item.Name.substring(0, 30) + "..." : item.Name);
            }
            content += String.format("<img src=\"static/images/multiaktiv_button_deaktiv.gif\" onmouseout=\"imgout(this)\" onmouseover=\"imgover(this)\" onclick=\"contextMenu.show(this,'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},{9},'{10}',{11},'{12}','{13}','{14}','{15}','{16}')\" />",
                        item.ArtistId,
                        escape(item.ArtistName),
                        item.AlbumId,
                        escape(item.AlbumName),
                        item.Id,
                        escape(item.Name),
                        _player.currentPlaylist.Id,
                        escape(_player.currentPlaylist.Name),
                        playerModes.Titel,
                        "ContextMenuType.Tracklist",
                        "",
                        _player.currentPlaylist.UserOwned,
                        "",
                        "",
                        item.AlbumArtistId,
                        escape(item.AlbumArtistName),
                        escape(item.Licensor)
                        );
            index++;
        });
        content += "</ul></div>";

        $("#playListTab").html(content);
        this.showPlaylistLayer = true;
        try {
            $("#playListTab").jScrollPane({ dragMaxHeight: 25, dragMinHeight: 25 });
        } catch (e) { }
    }
    this.playFromPlaylist = function(index) {
        if (this.isPluginMissing) { this.showFlashMissing(); }
        _player.swf.SetPlaylistTrack(index);
    }
    this.errorMessage = function(message) {
        contextMenu.showHint(message);
        $("#player_Play").show();
        $("#player_Pause").hide();
    }
    this.openLink = function() {
        if (this.currentPlaylist.Type == playerModes.Album) {
            var track = this.getCurrentTrack();
            if (track != null) {
                updatePage("Musik/" + escapeUrl(track.AlbumArtistName) + "/" + escapeAlbumNameUrl(track.AlbumName, track.AlbumId));
            }
        }
        if (this.currentPlaylist.Type == playerModes.Artist) {
            var track = this.getCurrentTrack();
            if (track != null) {
                updatePage("Musik/" + escapeUrl(track.AlbumArtistName));
            }
        }
        if (this.currentPlaylist.Type == playerModes.SavedPlaylist) {
            updatePage("User/" + this.currentPlaylist.Owner + "/" + escapeUrl(this.currentPlaylist.Name));
        }
        if (this.currentPlaylist.Type == playerModes.Radio) {
            if (this.currentPlaylist.RadioType == radioModes.ChartsRadio) {
                updatePage("Chart");
            }
            else if (this.currentPlaylist.RadioType == radioModes.GenreRadio) {
                updatePage("Genre/" + this.currentPlaylist.Name.replace(/^\s+|\s+$/g, "").replace('/', '_'));
            }
            else if (this.currentPlaylist.RadioType == radioModes.UserRadio) {
                updatePage("user/" + this.currentPlaylist.Name);
            }
        }
    }
    this.showPlayList = function() {
        /* Show Playlist Layer*/
        this.showPlaylistLayer = false;
        var content = "";
        $("#player_timeline").slider('enable'); this.isSliderDisabled = false;


        if (this.currentPlaylist.Type == playerModes.Album) {
            $("#playList").show();

            $("#playlistInfo").html("Album: " + this.currentPlaylist.Name);
            $("#pl_current").html("Es läuft: <br/> Album </br> <div class=\"pl_albumlink\" onclick='_player.openLink()' style='cursor:pointer'>" + this.currentPlaylist.Name + "</div>");
            $("#playlist_save").attr("class", "disabledLink");
            $("#playlist_share").attr("class", "enabledLink");
            $("#playlist_delete").attr("class", "disabledLink");
            $("#playlist_rename").attr("class", "disabledLink");

            this.showPlaylistTracks();
        }
        if (this.currentPlaylist.Type == playerModes.SavedPlaylist) {
            $("#playList").show();

            if (this.currentPlaylist.UserOwned) {
                $("#playlist_save").attr("class", "enabledLink");
                $("#playlist_delete").attr("class", "enabledLink");
                $("#playlist_rename").attr("class", "enabledLink");
                $("#playlist_share").attr("class", "enabledLink");
            }
            else {
                $("#playlist_save").attr("class", "disabledLink");
                $("#playlist_share").attr("class", "enabledLink");
                $("#playlist_delete").attr("class", "disabledLink");
                $("#playlist_rename").attr("class", "disabledLink");
            }

            if (this.currentPlaylist.Owner == "steereo_welcome") {
                $("#pl_current").html("Es läuft:<br/> Radio " + this.currentPlaylist.Name);
                $("#playlist_share").attr("class", "disabledLink");
            }
            else {
                $("#playlistInfo").html("Wiedergabeliste: <a title='" + this.currentPlaylist.Name + "'>" + this.currentPlaylist.Name + "</a>");
                $("#pl_current").html("Es läuft: <br/> Wiedergabeliste </br><div onclick='_player.openLink()' style='cursor:pointer' title='" + this.currentPlaylist.Name + "' class='pl_current_playlist'>" + this.currentPlaylist.Name + "</div>");
            }

            this.showPlaylistTracks();
        }
        if (this.currentPlaylist.Type == playerModes.UnSavedPlaylist) {
            $("#playList").show();
            $("#playlistInfo").html("Ungespeicherte Wiedergabeliste");
            $("#pl_current").html("Es läuft: <br/>Wiedergabeliste <br/>(ungespeichert)");

            $("#playlist_save").attr("class", "enabledLink");
            $("#playlist_share").attr("class", "disabledLink");
            $("#playlist_delete").attr("class", "enabledLink");
            $("#playlist_rename").attr("class", "disabledLink");

            this.showPlaylistTracks();
        }
        if (this.currentPlaylist.Type == playerModes.Titel) {
            $("#playList").hide();
            $("#pl_current").html("Es läuft: <br/>  Einzeltitel");
        }
        if (this.currentPlaylist.Type == playerModes.Artist) {
            $("#playList").hide();
            $("#player_timeline").slider('disable'); this.isSliderDisabled = true;
            $("#pl_current").html("Es läuft:<br/> <div onclick='_player.openLink()' style='cursor:pointer' class='pl_current_artist'>" + this.currentPlaylist.Name + " Radio</div>"); ;
        }
        if (this.currentPlaylist.Type == playerModes.Radio) {
            $("#playList").hide();
            $("#player_timeline").slider('disable'); this.isSliderDisabled = true;
            if (this.currentPlaylist.Name == "steereo")
                $("#pl_current").html("Es läuft:<br/> Radio " + this.currentPlaylist.Name);
            else
                $("#pl_current").html("Es läuft:<br/><span onclick='_player.openLink()' class='pl_current_radio'>" + this.currentPlaylist.Name + " Radio</span>");
        }
    }

    this.initPlaylist = function(data, mode, owned, radiotype) {
        this.currentPlaylist = new Playlist(data.Id, data.Name, mode, null, owned, data.Owner, radiotype);
        var list = new Array();
        $.each(data.Tracks, function(i, item) {
            list.push(item);
        });
        this.currentPlaylist.Tracks = list;

        if (this.currentPlaylist.Tracks.length != 0) {
            this.currentPos = 0;
            this.showTrackInfo();
        }
        else {
            /* TODO - show some text if no playlists are available */
        }
        this.showPlayList();
    }
    this.showTrackInfo = function() {
        if (this.currentPlaylist.Tracks.length > 0) {
            var track = this.currentPlaylist.Tracks[this.currentPos];
            $("#pl_artistName").html(track.ArtistName).attr("href", "/Musik/" + escapeUrl(track.ArtistName));
            $("#pl_artistName").attr("title", track.ArtistName);
            //$("#pl_track").html(track.Name).attr("href", "#Musik/" + escape(track.ArtistName) + "/_/");
            $("#pl_track").html(track.Name).attr("href", "/Musik/" + escapeUrl(track.AlbumArtistName) + "/" + escapeAlbumNameUrl(track.AlbumName, track.AlbumId));
            $("#pl_track").attr("title", track.Name);
            $("#player_cover").attr("src", "/Content/AlbumImages/" + track.Image);
        }
        else {
            $("#pl_artistName").html("").attr("href", "");
            $("#pl_artistName").attr("title", "");
            $("#pl_track").html("").attr("href", "");
            $("#pl_track").attr("title", "");
            $("#player_cover").attr("src", "static/images/UnknownAlbum.gif");
        }
    }
    this.playAlbum = function(id) {
        this.isPaused = false;
        $.getJSON("/Playlist/Album?id=" + id,
                function(data) {
                    _player.initPlaylist(data, playerModes.Album, false, radioModes.AlbumRadio);
                    _player.Play();
                    _log.logRadioMode(radioModes.AlbumRadio, id);
                });
    }
    this.playUserRadio = function(name) {
        this.isPaused = false;
        $.getJSON("/Playlist/User/" + name,
                function(data) {
                    if (data.State == "Success") {
                        _player.initPlaylist(data, playerModes.Radio, false, radioModes.UserRadio);
                        _player.Play();
                        _log.logRadioMode(radioModes.UserRadio, null, name);
                    }
                });
    }
    this.playChartsRadio = function() {
        this.isPaused = false;
        $.getJSON("/Playlist/Charts",
        function(data) {
            if (data.State == "Success") {
                _player.initPlaylist(data, playerModes.Radio, false, radioModes.ChartsRadio);
                _player.Play();
                _log.logRadioMode(radioModes.ChartsRadio);
            }
        });
    }
    this.playGenreRadio = function(name) {
        this.isPaused = false;
        $.getJSON("/Playlist/Genre/" + name,
        function(data) {
            if (data.State == "Success") {
                _player.initPlaylist(data, playerModes.Radio, false, radioModes.GenreRadio);
                _player.Play();
                _log.logRadioMode(radioModes.GenreRadio, null, name);
            }
        });
    }
    this.playDefaultPlaylist = function() {
        $.getJSON("/Playlist/Default",
                function(data) {
                    if (data.Radio) {
                        _player.initPlaylist(data, playerModes.Radio, false);
                    }
                    else {
                        _player.initPlaylist(data, playerModes.SavedPlaylist, false);

                    }
                    _player.isPaused = false;
                    _player.Play();
                });
    }
    this.playPlaylist = function(id) {
        this.isPaused = false;
        $.getJSON("/Playlist/info/" + id,
                function(data) {
                    if (data.State == "Success") {
                        _player.initPlaylist(data, playerModes.SavedPlaylist, data.Owned, radioModes.UserRadio);
                        _player.Play();
                    }
                });
    }
    this.playArtist = function(id) {
        this.isPaused = false;
        $.getJSON("/Playlist/Artist?id=" + id,
                function(data) {
                    if (data.State == "Success") {
                        _player.initPlaylist(data, playerModes.Artist, false, radioModes.ArtistRadio);
                        _player.Play();
                        _log.logRadioMode(radioModes.ArtistRadio, id);
                    }
                    if (data.State == "Empty") {
                        /* Artist radio has no tracks */
                    }
                });
    }
    this.playTrack = function(id, name, artistId, artistName, albumId, albumName, image, genre, albumArtistId, albumArtistName, licensor) {
        this.isPaused = false;
        var data = new Object();
        data.Name = ""; data.Tracks = new Array(); data.Tracks.push(new Track(id, name, artistId, artistName, albumId, albumName, image, genre, albumArtistId, albumArtistName, licensor));
        this.initPlaylist(data, playerModes.Titel, false);
        this.initIframeParam(genre, artistId, artistName, iframeDefault.UserID, iframeDefault.UserGender, licensor);
        this.Play();
        _log.logRadioMode(radioModes.SingleTrack, id);
    }
    this.initIframeParam = function(genre, artistID, artistName, userID, userGender, licensor) {
        iframeDefault.Genre = genre;
        iframeDefault.ArtistID = artistID;
        iframeDefault.ArtistName = artistName;
        iframeDefault.UserID = userID;
        iframeDefault.UserGender = userGender;
        iframeDefault.Licensor = licensor;
    }
    this.Play = function() {
        if (!this.access) { this.accessDenied(); actionlog(13, 0, 0); return; }
        if (this.isPluginMissing) { this.showFlashMissing(); }
        if (!this.isPaused) {
            this.swf.SetPlayList(this.currentPlaylist.Tracks, this.currentPlaylist.Type, this.currentPlaylist.RadioType);
            this.showTrackInfo();
        }
        else {
		_timer.startTimer();
            this.swf.ResumeSong();
        }
        /*$("#player_Play").hide("slow");
        $("#player_Pause").show("slow");*/
    }
    this.getTracksIDs = function() {
        var result = new Array();
        for (var i = 0; i < this.currentPlaylist.Tracks.length; i++) {
            result.push(this.currentPlaylist.Tracks[i].Id);
        }
        return result;
    }
    this.updatePlaylist = function(id, name, owner) {
        var data = {}; data.Id = id; data.Owner = owner; data.Tracks = this.currentPlaylist.Tracks; data.Name = name;
        this.initPlaylist(data, playerModes.SavedPlaylist, true);
        /* Playlist was saved, update information about the playlist (state change, actions enable/disable)*/

    }
    this.updateTimer = function(timeValue, trackLength) {
        var minutes = Math.floor(timeValue / 60);
        var seconds = timeValue - (minutes * 60);
        if (seconds < 10) { seconds = "0" + seconds; }
        $("#timelabel").html(minutes + ":" + seconds);

        if (timeValue > 0 && timerCounter > 0 && timerCounter % 30 == 0 && timeValue < trackLength - 2) {
            changeIframeSourceByPlayerEmpty(); //change ads after 30 seconds
        }
    }

    this.updateProgress = function(progressValue) {
        if (progressValue != "NaN") { $("#player_timeline").slider("value", progressValue); }
    }
    this.trackStarted = function(trackPosition) {
        this.currentPos = trackPosition;
        var track = this.getCurrentTrack();
        if (track != null) {
            this.initIframeParam(track.Genre, track.ArtistId, track.ArtistName, contextMenu.userID, contextMenu.userGender, track.Licensor);
        }

        _timer.stopTimerWithoutRefreshIfame();
        changeIframeSourceByPlayerEmpty(); //change ads on track start
        _timer.startTimer();
    }

    this.updatePlayer = function(opened, trackPosition) {
        if (opened) {
            $("#player_Play").hide();
            $("#player_Pause").show();
        }
        else {
            $("#player_Play").show();
            $("#player_Pause").hide();
            _timer.stopTimerWithoutRefreshIfame();
        }
        if (this.currentPos == trackPosition) { }
        this.currentPos = trackPosition;
        this.showTrackInfo();

        var track = this.getCurrentTrack();
        if (track != null) {
            this.initIframeParam(track.Genre, track.ArtistId, track.ArtistName, contextMenu.userID, contextMenu.userGender, track.Licensor);
        }
    }

    this.Pause = function() {
        this.isPaused = true;
        $("#player_Play").show();
        $("#player_Pause").hide();
        _timer.pauseTimer();
        try {
            _player.swf.PauseSong();
        } catch (ex) { }
    }
    this.pause = function() {
        this.Pause();
    }
    this.Next = function() {
        this.isPaused = false;
        if (!this.access) { this.accessDenied(); actionlog(13, 0, 0); return; }
        if (this.isPluginMissing) { this.showFlashMissing(); }
        _player.swf.NextSong();

	_timer.stopTimerWithoutRefreshIfame();
    }
    this.LoveTrack = function() {
        var currentItem = this.getCurrentTrackAsItem();
        if (currentItem != null) {
            contextMenu.toFavorites(currentItem, playerModes.Titel);
            actionlog(5, track.Id, 0);
        }
    }
    this.BanTrack = function() {
        var currentItem = this.getCurrentTrackAsItem();
        if (currentItem != null) {
            contextMenu.banTrack(currentItem);
        }
    }
    this.accessDenied = function() {
        contextMenu.showHint("Aus lizenzrechtlichen Gründen ist die Nutzung von steereo leider nur innerhalb der Bundesrepublik Deutschland möglich. Sorry.");
    }
    this.showToolTip = function(tooltip, left) {
        var content = "<h2 style='text-align:center'>" + tooltip + "</h2>";
        $("#tooltipLayerContent").html(content);
        _toolTip.show(0, 150, left);
    }
    this.hideToolTip = function() {
        _toolTip.hide();
    }
    this.unLoad = function() {
        try {
            if (this.access && !this.isPluginMissing) {
                _player.swf.UnLoad();
            }
        } catch (e) { }
    }
    this.notificate = function(trackId) {
        _notification.show();
    }
}

/* Objects */
function Playlist(id, name, type, tracks, userOwned, owner, radioType) {
   this.Id = id;
   this.Name = name;
   this.Type = type;
   this.Tracks = tracks,
   this.UserOwned = userOwned;
   this.Owner = owner;
   this.RadioType = typeof (radioType) != 'undefined' ? radioType : radioModes.SingleTrack; //default value is ChartsRadio
}

function Track(id, name, artistId, artistName, albumId, albumName, image, genre, albumArtistId, albumArtistName, licensor, canBePlayed) {
    this.Id = id;
    this.Name = unescape(name);
    this.ArtistId = artistId;
    this.ArtistName = unescape(artistName);
    this.AlbumId = albumId;
    this.AlbumName = unescape(albumName);
    this.Image = image;
    this.Genre = genre;
    this.AlbumArtistId = albumArtistId; //track.Artist.Id != track.Album.Artist.Id
    this.AlbumArtistName = albumArtistName; //track.Artist.Name != track.Album.Artist.Name
    this.Licensor = licensor;
    this.CanBePlayed = typeof (canBePlayed) != 'undefined' ? (canBePlayed.toLowerCase() === "true" ? true : false) : true;
    //refractoring needed! - should use objects
}

function Artist(_id, _name) {
    this.Id = _id;
    this.Name = _name;
}

function Album(_id, _name, _artistId, _artistName) {
    this.Id = _id;
    this.Name = _name;
    this.ArtistId = _artistId;
    this.ArtistName = _artistName;
}

/* Effects  */
function imgover(t) {
    $(t).attr("src", "static/images/multiaktiv_button_black_aktiv.gif");
}

function imgout(t) {
    $(t).attr("src", "static/images/multiaktiv_button_deaktiv.gif");
}

function pl_imgover(t) {
    $(t).removeClass("plLI");
    $(t).addClass("plLIActive");
}
function pl_imgout(t) {
    $(t).removeClass("plLIActive");
    $(t).addClass("plLI");
}

function GetCoordinateX(object)
  {
    var left = 0;
    if(object.offsetParent)
        while(true) 
        {
          left += object.offsetLeft;
          if(!object.offsetParent)
            break;
          object = object.offsetParent;
        }
    else if(object.x)
        left += object.x;
    return left;
  }

  function GetCoordinateY(object)
  {
    var top = 0;
    if(object.offsetParent)
        while(true)
        {
          top += object.offsetTop;
          if(!object.offsetParent)
            break;
          object = object.offsetParent;
        }
    else if(object.y)
        top += object.y;
    return top;
}

