﻿var channel_id_in_use = "";
var Dragging = false;
var artist_time_length;
var music_time_length;
var album_time_length;
var current_artist = 0;
var current_news_id = 0;
var running_artist_marquee;
var pub_is_running = false;
var SilverPlugin;
var MouseIn = false;
var repeat_playlist = false;
var xml_to_load = "";
var np_artist_name = "";
var np_music_name = "";
var np_album_name = "";
var np_capa_album = "";
var np_radio_name = "";
var currentTimelineBarMaskPos = 0;
var update_position_timmer;
var change_news_timmer;
var is_news_open = false;
var retry_next = true;
var def_volume = 0.5;
var spinning_button = false;
var is_playing = false;
var allowskip = false;

if (!window.ScriptWeb1)
	window.ScriptWeb1 = {};

ScriptWeb1.Scene = function() 
{
}

ScriptWeb1.Scene.prototype =
{
	handleLoad: function(plugIn, userContext, rootElement) 
	{
		this.plugIn = plugIn;
		
	}
}

function getQueryVariable(variable) {  var query = window.location.search.substring(1);  var vars = query.split("&");  for (var i=0;i<vars.length;i++) {    var pair = vars[i].split("=");    if (pair[0] == variable) {      return pair[1];    }  }   return "";}

function getOsAndBrowser () {
    try
    {
        var ua = null;

        ua = window.navigator.userAgent;
        
        var slua = {OS:'Unsupported',Browser:'Unsupported'};
        
        //Silverlight does not support pre-Windows NT platforms
        if (ua.indexOf('Windows NT') >= 0 || ua.indexOf('Mozilla/4.0 (compatible; MSIE 6.0)')>=0) {
            slua.OS = 'Windows';
        }
        else if (ua.indexOf('PPC Mac OS X') >= 0) {
            slua.OS = 'MacPPC';
        }
        else if (ua.indexOf('Intel Mac OS X') >= 0) {
            slua.OS = 'MacIntel';
        }
        
        if ( slua.OS != 'Unsupported' )
        {
            if (ua.indexOf('MSIE') >= 0) {
                if (navigator.userAgent.indexOf('Win64') == -1)
                {
                    if (parseInt(ua.split('MSIE')[1]) >= 6) {
                        slua.Browser  = 'MSIE';
                    }
                }
            }
            else if (ua.indexOf('Firefox') >= 0) {
                var versionArr = ua.split('Firefox/')[1].split('.');
                var major = parseInt(versionArr[0]);
                if (major >= 2) {
                    slua.Browser = 'Firefox';
                }
                else {
                    var minor = parseInt(versionArr[1]);
                    if ((major == 1) && (minor >= 5)) {
                        slua.Browser  = 'Firefox';
                    }
                }
            }
            
            else if (ua.indexOf('Safari') >= 0) {
                slua.Browser = 'Safari';
            }            
        }
        return slua;
    }
    catch (e)
    {
        return false;
    }
}


function launch () {
    SilverPlugin.content.findName("On").begin();
    is_playing = true;
    if (getQueryVariable("interview_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/asx/frame_interview.asp&interview_id=' + getQueryVariable("interview_id"));
    } else if (getQueryVariable("song_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/asx/frame_preview.asp&song_id=' + getQueryVariable("song_id"));
    } else if (getQueryVariable("wma")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/asx/frame_static_file.asp&wma=' + getQueryVariable("wma"));
    } else if (getQueryVariable("audio_type_id")!="") {
        if (getQueryVariable("initial_song")!="") {
            loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/radio.asp&audio_type_id=' + getQueryVariable("audio_type_id") + '&initial_song=' + getQueryVariable("initial_song") + '&sl=1');
            xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/radio_playlist.aspx&audio_type_id=' + getQueryVariable("audio_type_id") + '&initial_song=' + getQueryVariable("initial_song") + '&sl=1';
        } else {
            loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/radio.asp&audio_type_id=' + getQueryVariable("audio_type_id") + '&sl=1');
            xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/radio_playlist.aspx&audio_type_id=' + getQueryVariable("audio_type_id") + '&sl=1';
        }
        repeat_playlist = true;
    } else if (getQueryVariable("audio_sub_type_id")!="") {
        if (getQueryVariable("initial_song")!="") {
            loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/radio.asp&audio_sub_type_id=' + getQueryVariable("audio_sub_type_id") + '&initial_song=' + getQueryVariable("initial_song") + '&sl=1');
            xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/radio_playlist.aspx&audio_sub_type_id=' + getQueryVariable("audio_sub_type_id") + '&initial_song=' + getQueryVariable("initial_song") + '&sl=1';
        } else {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/radio.asp&audio_sub_type_id=' + getQueryVariable("audio_sub_type_id") + '&sl=1');
        xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/radio_playlist.aspx&audio_sub_type_id=' + getQueryVariable("audio_sub_type_id") + '&sl=1';
        }
        repeat_playlist = true;
    } else if (getQueryVariable("sound_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/asx/frame_sound.asp&sound_id=' + getQueryVariable("sound_id"))
    } else if (getQueryVariable("channel_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/channel.asp&channel_id=' + getQueryVariable("channel_id"));
        channel_id_in_use = getQueryVariable("channel_id");
    } else if (getQueryVariable("radio_id")!="" && getQueryVariable("cotoneteownerid")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/my_radio_playlist.asp&version=7&user_radio_id=' + getQueryVariable("radio_id") + '&cotoneteownerid=' + getQueryVariable("cotoneteownerid"));
        repeat_playlist = true;
        xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/user_radio_playlist.aspx&version=7&user_radio_id=' + getQueryVariable("radio_id") + '&cotoneteownerid=' + getQueryVariable("cotoneteownerid");
    } else if (getQueryVariable("dj_radio_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/dj_radio_playlist.asp&dj_radio_id=' + getQueryVariable("dj_radio_id"));
    } else if (getQueryVariable("vip_radio_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/vip_radio_playlist.asp&sl=1&vip_radio_id=' + getQueryVariable("vip_radio_id"));
        repeat_playlist = true;
        xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/vip_radio_playlist.asp&vip_radio_id=' + getQueryVariable("vip_radio_id");
    } else if (getQueryVariable("user_jingle_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/asx/frame_user_jingle.asp&version=7&user_jingle_id=' + getQueryVariable("user_jingle_id"))
    } else if (getQueryVariable("artist_radio")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/artist_radio_playlist.aspx');
        repeat_playlist = true;
        xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/artist_radio_playlist.aspx';
    } else if (getQueryVariable("related_artist_radio_id")!="") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/artist_related_playlist.aspx&sl=1&artist_radio_id=' + getQueryVariable("related_artist_radio_id"));
        repeat_playlist = true;
        xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/artist_related_playlist.aspx&sl=1&artist_radio_id=' + getQueryVariable("related_artist_radio_id");
    } else if (getQueryVariable("fast_radio_id") != "") {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/fast_radio_playlist.aspx&fast_radio_id=' + getQueryVariable("fast_radio_id") + '&version=sl');
        repeat_playlist = true;
        xml_to_load = '/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/aspx/fast_radio_playlist.aspx&fast_radio_id=' + getQueryVariable("fast_radio_id");
    } else {
        loadXML('/listen/silver/convert_asx_to_xml.ashx?url=http://cotonete.clix.pt/listen/channel.asp&channel_id=11');
        channel_id_in_use = 11;
    }
    $.ajax({ method: "get", url: "/webservices/proxy.ashx", data: "file=news.asmx&method=GetXMLNews&fk_site_id=5&start_line=1&end_line=10", beforeSend: function() { }, complete: function() { },
        success: PopulateNews,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //Run(channel_id);
            //FailedCallback();
        }
    });
    //WebService.GetNews(5, 1, 10, PopulateNews, FailedCallback, "XmlDocument");
}

function StartMeUp(sender, eventArgs) {
    //define start volume position
    SilverPlugin = document.getElementById("silverlightPlugIn");
    var VolumeSlider = SilverPlugin.content.findName("VolumeSlider");
    var VolumeSliderPosition = SilverPlugin.content.findName("volumeSliderPosition");
    var PlayerControl = SilverPlugin.content.findName("player");
    var currentPosition = PlayerControl.Volume;

    //SilverPlugin.content.findName("player_env_sounds").Volume = currentPosition;
    //SilverPlugin.content.findName("player_pub").Volume = currentPosition;
    
    var length = VolumeSlider.width;
    if (length>0) {
      var relativePosition = length*currentPosition - (VolumeSliderPosition.Width / 2);
      var VolumeBarMask = SilverPlugin.content.findName("VolumeBarMask");
      VolumeSliderPosition['Canvas.Left'] = VolumeSlider['Canvas.Left'] + relativePosition - (VolumeSliderPosition.width / 2) -5;
      VolumeBarMask.Rect = "55, 210, " + relativePosition + ", 50"
    }
    def_volume = PlayerControl.Volume;
    var percentage = PlayerControl.Volume * 100;
    
    SilverPlugin.content.findName("VolumePercentage").Text = percentage.toFixed(0) + "%";
    SilverPlugin.content.findName("VolumePercentage")["Canvas.Left"] = VolumeSliderPosition["Canvas.Left"] - 5;
    SilverPlugin.content.findName("VolumeHidePercentage").Begin();
    SilverPlugin.content.findName("HideMinutesInfo").Begin();

    setTimeout(showLoad, 2000);
    setTimeout(launch, 4000);
}

function showLoad() {
    SilverPlugin.content.findName("arranque").begin();
}

function changeStatus(new_status) {
    if (new_status != null) {
        SilverPlugin.content.findName("status_field").Text = new_status;
    }
}

function MarqueeAlbum() {
    SilverPlugin.content.findName("albumMarquee").stop();

    album_value = SilverPlugin.content.findName("album_field").Text;
    album_time_length = Math.round((album_value.length - 18 + 7)/7)+7;
    if (album_time_length >= 60) {
      album_time_length = 59;
    }
    SilverPlugin.content.findName("album_field").width = album_value.length * 9;
    LeftPosValue = -(album_value.length * 9)
    temp_opac_time = album_time_length-2;
    if (temp_opac_time < 7) {
      temp_opac_time = 7;
    }
    temp_opac_start_time = temp_opac_time - 3;
    SilverPlugin.content.findName("MarqueeAlbumTimeLimit")['KeyTime'] = '00:00:' + album_time_length;
    SilverPlugin.content.findName("MarqueeAlbumOpacTimeLimit")['KeyTime'] = '00:00:' + temp_opac_time;
    SilverPlugin.content.findName("MarqueeAlbumOpacStartTime")['KeyTime'] = '00:00:' + temp_opac_start_time;
    SilverPlugin.content.findName("MarqueeAlbumTimeLimit")['Value'] = LeftPosValue;
    SilverPlugin.content.findName("MarqueeAlbumOpac")['Value'] = 1;

    SilverPlugin.content.findName("albumMarquee").begin();

    temp_marquee_time = (album_time_length - 2)*1000;
    setTimeout(StopMarqueeAlbum, temp_marquee_time);
    
    SilverPlugin.content.findName("butMarqueeAlbum")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaAlbum")["Visibility"]="Visible";
    SilverPlugin.content.findName("albumMarqueeRoscaOver").stop();
    SilverPlugin.content.findName("albumMarqueeRoscaOver").begin();

    //document.getElementById("CurrentMediaItem").innerText += "Album time length: " + album_time_length + " \n ";
    //document.getElementById("CurrentMediaItem").innerText += "Album left value: " + LeftPosValue + " \n ";
}

function MarqueeArtist() {
    SilverPlugin.content.findName("artistMarquee").stop();

    artist_value = SilverPlugin.content.findName("artist_field").Text;
    artist_time_length = Math.round((artist_value.length - 18 + 7)/7)+7;
    if (artist_time_length >= 60) {
      artist_time_length = 59;
    }
    SilverPlugin.content.findName("artist_field").width = artist_value.length * 9;
    LeftPosValue = -(artist_value.length * 9)
    temp_opac_time = artist_time_length-2;
    if (temp_opac_time < 7) {
      temp_opac_time = 7;
    }
    temp_opac_start_time = temp_opac_time - 3;
    SilverPlugin.content.findName("MarqueeArtistTimeLimit")['KeyTime'] = '00:00:' + artist_time_length;
    SilverPlugin.content.findName("MarqueeArtistTimeLimit")['Value'] = LeftPosValue;
    SilverPlugin.content.findName("MarqueeArtistOpacTimeLimit")['KeyTime'] = '00:00:' + temp_opac_time;
    SilverPlugin.content.findName("MarqueeArtistOpacStartTime")['KeyTime'] = '00:00:' + temp_opac_start_time;
    SilverPlugin.content.findName("MarqueeArtistOpac")['Value'] = 1;

    SilverPlugin.content.findName("artistMarquee").begin();
    temp_marquee_time = (artist_time_length - 2)*1000;
    setTimeout(StopMarqueeArtist, temp_marquee_time);

    SilverPlugin.content.findName("butMarqueeArtist")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaArtist")["Visibility"]="Visible";
    SilverPlugin.content.findName("artistMarqueeRoscaOver").stop();
    SilverPlugin.content.findName("artistMarqueeRoscaOver").begin();

    //document.getElementById("CurrentMediaItem").innerText += "Artist time length: " + artist_time_length + " \n ";
    //document.getElementById("CurrentMediaItem").innerText += "Artist left value: " + LeftPosValue + " \n ";
}

function MarqueeMusic() {
    SilverPlugin.content.findName("musicMarquee").stop();

    music_value = SilverPlugin.content.findName("music_field").Text;
    music_time_length = Math.round((music_value.length - 18 + 7)/7)+7;
    if (music_time_length >= 60) {
      music_time_length = 59;
    }
    SilverPlugin.content.findName("music_field").width = music_value.length * 9;
    LeftPosValue = -(music_value.length * 9)
    temp_opac_time = music_time_length-2;
    if (temp_opac_time < 7) {
      temp_opac_time = 7;
    }
    temp_opac_start_time = temp_opac_time - 3;
    SilverPlugin.content.findName("MarqueeMusicTimeLimit")['KeyTime'] = '00:00:' + music_time_length;
    SilverPlugin.content.findName("MarqueeMusicOpacTimeLimit")['KeyTime'] = '00:00:' + temp_opac_time;
    SilverPlugin.content.findName("MarqueeMusicOpacStartTime")['KeyTime'] = '00:00:' + temp_opac_start_time;
    SilverPlugin.content.findName("MarqueeMusicTimeLimit")['Value'] = LeftPosValue;
    SilverPlugin.content.findName("MarqueeMusicOpac")['Value'] = 1;

    SilverPlugin.content.findName("musicMarquee").begin();
    temp_marquee_time = (music_time_length - 2)*1000;
    setTimeout(StopMarqueeMusic, temp_marquee_time);

    SilverPlugin.content.findName("butMarqueeMusic")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaMusic")["Visibility"]="Visible";
    SilverPlugin.content.findName("musicMarqueeRoscaOver").stop();
    SilverPlugin.content.findName("musicMarqueeRoscaOver").begin();

    //document.getElementById("CurrentMediaItem").innerText += "Music time length: " + music_time_length + " \n ";
    //document.getElementById("CurrentMediaItem").innerText += "Music left value: " + LeftPosValue + " \n ";
}

function MarqueeRadioName() {
    SilverPlugin.content.findName("radioNameMarquee").stop();

    radio_name_value = SilverPlugin.content.findName("radio_name_field").Text;
    radio_name_time_length = Math.round((radio_name_value.length - 18 + 7)/7)+7;
    if (radio_name_time_length >= 60) {
      radio_name_time_length = 59;
    }
    SilverPlugin.content.findName("radio_name_field").width = radio_name_value.length * 9;
    LeftPosValue = -(radio_name_value.length * 9)
    temp_opac_time = radio_name_time_length-2;
    if (temp_opac_time < 7) {
      temp_opac_time = 7;
    }
    temp_opac_start_time = temp_opac_time - 3;
    SilverPlugin.content.findName("MarqueeRadioNameTimeLimit")['KeyTime'] = '00:00:' + radio_name_time_length;
    SilverPlugin.content.findName("MarqueeRadioNameOpacTimeLimit")['KeyTime'] = '00:00:' + temp_opac_time;
    SilverPlugin.content.findName("MarqueeRadioNameOpacStartTime")['KeyTime'] = '00:00:' + temp_opac_start_time;
    SilverPlugin.content.findName("MarqueeRadioNameTimeLimit")['Value'] = LeftPosValue;
    SilverPlugin.content.findName("MarqueeRadioNameOpac")['Value'] = 1;

    SilverPlugin.content.findName("radioNameMarquee").begin();
    temp_marquee_time = (radio_name_time_length - 2)*1000;
    setTimeout(StopMarqueeRadioName, temp_marquee_time);

    SilverPlugin.content.findName("butMarqueeRadioName")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaRadioName")["Visibility"]="Visible";
    SilverPlugin.content.findName("radioNameMarqueeRoscaOver").stop();
    SilverPlugin.content.findName("radioNameMarqueeRoscaOver").begin();

    //document.getElementById("CurrentMediaItem").innerText += "Music time length: " + music_time_length + " \n ";
    //document.getElementById("CurrentMediaItem").innerText += "Music left value: " + LeftPosValue + " \n ";
}

function RunMarquee() {
    //Runs all marquees if needed!
    
    SilverPlugin.content.findName("butMarqueeArtist")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("butMarqueeAlbum")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("butMarqueeMusic")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("butMarqueeRadioName")["Visibility"]="Collapsed";

    SilverPlugin.content.findName("RoscaMusic")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaArtist")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaAlbum")["Visibility"]="Collapsed";
    SilverPlugin.content.findName("RoscaRadioName")["Visibility"]="Collapsed";
    
    album_value = SilverPlugin.content.findName("album_field").Text;
    if (album_value.length >= 25) {
      MarqueeAlbum();
    }
    artist_value = SilverPlugin.content.findName("artist_field").Text;
    if (artist_value.length >= 25 || array_artists.length>0) {
      //if (array_artists.length>0) {
          //CreateMarqueeArtists()
      //}
      running_artist_marquee = true;
      MarqueeArtist();
    }
    music_value = SilverPlugin.content.findName("music_field").Text;
    if (music_value.length >= 25) {
      MarqueeMusic();
    }
    radio_name_value = SilverPlugin.content.findName("radio_name_field").Text;
    if (radio_name_value.length >= 25) {
      MarqueeRadioName();
    }
}

function StopMarqueeArtist() {
  if (array_artists.length>0 && current_artist != array_artists.length) {
      changeArtistValue(array_artists[current_artist][1]);
      current_artist_id = array_artists[current_artist][0];
      SilverPlugin.content.findName("artistMarquee").stop();
      current_artist ++;
      MarqueeArtist();
  } else if (current_artist == array_artists.length) {
      changeArtistValue(first_artist_name);
      current_artist_id = first_artist_id;
      SilverPlugin.content.findName("artistMarquee").stop();
      SilverPlugin.content.findName("butMarqueeArtist")["Visibility"]="Visible";
      running_artist_marquee = false;
      current_artist = 0;
  } else {
    SilverPlugin.content.findName("artistMarquee").stop();
    SilverPlugin.content.findName("butMarqueeArtist")["Visibility"]="Visible";
    running_artist_marquee = false;
  }
}

function StopMarqueeAlbum() {
    SilverPlugin.content.findName("albumMarquee").stop();
    SilverPlugin.content.findName("butMarqueeAlbum")["Visibility"]="Visible";
}

function StopMarqueeMusic() {
    SilverPlugin.content.findName("musicMarquee").stop();
    SilverPlugin.content.findName("butMarqueeMusic")["Visibility"]="Visible";
}

function StopMarqueeRadioName() {
    SilverPlugin.content.findName("radioNameMarquee").stop();
    SilverPlugin.content.findName("butMarqueeRadioName")["Visibility"]="Visible";
}

function changeRadioName(new_radio_name) {
    SilverPlugin.content.findName("radio_name_field").Text = new_radio_name;
}

function changeArtistValue(new_artist_name) {
    SilverPlugin.content.findName("artist_field").Text = new_artist_name;
    SilverPlugin.content.findName("artist_field").width = new_artist_name.length * 9;
}

function changeAlbumValue(new_album_name) {
    SilverPlugin.content.findName("album_field").Text = new_album_name;
}

function changeMusicValue(new_music_name) {
    SilverPlugin.content.findName("music_field").Text = new_music_name;
}

function changeCapaAlbum(new_capa) {
    if (new_capa.toString().toUpperCase().substr(new_capa.toString().length-3, 3) == "GIF") {
        new_capa = "http://cotonete.clix.pt/images/capa_indisponivel.jpg";
    }
    SilverPlugin.content.findName("capa_album").Source = new_capa;
}

function changeNowPlayingInfo(artist_name, music_name, album_name, capa_album, radio_name) {
    SilverPlugin.content.findName("NewSongSwipeOut").begin();

    //SilverPlugin.content.findName("fade").begin();

    //if (!window.document.hasFocus) {
    //document.getElementById("standard_pub").contentWindow.focus();
    if (artist_name != "") {
        window.document.title = artist_name + " : " + music_name;
    } else {
        window.document.title = music_name;
    }

    np_radio_name = radio_name;
    np_artist_name = artist_name;
    np_music_name = music_name;
    np_album_name = album_name;
    np_capa_album = capa_album;

    //window.alert();
    //window.focus();
    //window.blur();
    //}
}

function changeNewsText(news_title) {
    SilverPlugin.content.findName("news_text").Text = news_title;
    current_news_index++;
    if (current_news_index==10) {
        current_news_index = 0;
    }
}

function NewSongSwipeOut_Completed(sender, eventArgs) {
    changeArtistValue(np_artist_name);
    changeAlbumValue(np_album_name);
    changeMusicValue(np_music_name);
    changeCapaAlbum(np_capa_album);
    changeRadioName(np_radio_name);

    SilverPlugin.content.findName("NewSongSwipeIn").begin();
    if (current_music_id != "") {
        SilverPlugin.content.findName("AddSongIn").begin();
    } else {
        SilverPlugin.content.findName("AddSongOut").begin();
    }

    setTimeout(RunMarquee, 1000);
}

function showBigImage(sender, eventArgs) {
    SilverPlugin.content.findName("MouseOverCapaImage").begin();
}

function showNormalImage(sender, eventArgs) {
    SilverPlugin.content.findName("MouseOutCapaImage").begin();
}

function QualidadeOnCall(sender, eventArgs) {
    SilverPlugin.content.findName("QualidadeOn").begin();
}

function QualidadeOffCall(sender, eventArgs) {
    SilverPlugin.content.findName("QualidadeOff").begin();
}

function MouseOverTextBlock(sender, eventArgs) {
    string_value = sender.Text;
    if (sender.name == "radio_name_field") {
        //mouseoveranimation = sender.findName("MouseOverTextBlockAnim");
        //sender["Cursor"] = "Hand"
        //sender["TextDecorations"] = "Underline";
        //mouseoveranimation.stop();
        //mouseoveranimation["Storyboard.TargetName"] = sender.name;
        //mouseoveranimation.begin();

        if (string_value.length >= 25) {
          MarqueeRadioName();
        }
    } else if (sender.name == "artist_field" && current_artist_id != "") {
        mouseoveranimation = sender.findName("MouseOverTextBlockAnim");
        sender["Cursor"] = "Hand"
        sender["TextDecorations"] = "Underline";
        mouseoveranimation.stop();
        mouseoveranimation["Storyboard.TargetName"] = sender.name;
        mouseoveranimation.begin();

        if ((string_value.length >= 25 || array_artists.length > 0) && !running_artist_marquee) {
          running_artist_marquee = true;
          MarqueeArtist();
        }
    } else if (sender.name == "music_field" && current_music_id != "") {
        mouseoveranimation = sender.findName("MouseOverTextBlockAnim");
        sender["Cursor"] = "Hand"
        sender["TextDecorations"] = "Underline";
        mouseoveranimation.stop();
        mouseoveranimation["Storyboard.TargetName"] = sender.name;
        mouseoveranimation.begin();

        if (string_value.length >= 25) {
          MarqueeMusic();
        }
    } else if (sender.name == "album_field" && current_album_id != "") {
        mouseoveranimation = sender.findName("MouseOverTextBlockAnim");
        sender["Cursor"] = "Hand"
        sender["TextDecorations"] = "Underline";
        mouseoveranimation.stop();
        mouseoveranimation["Storyboard.TargetName"] = sender.name;
        mouseoveranimation.begin();

        if (string_value.length >= 25) {
          MarqueeAlbum();
        }
    } else if (sender.name == "news_text" && current_news_id != "") {
        mouseoveranimation = sender.findName("MouseOverTextBlockAnim");
        sender["Cursor"] = "Hand"
        sender["TextDecorations"] = "Underline";
        mouseoveranimation.stop();
        mouseoveranimation["Storyboard.TargetName"] = sender.name;
        mouseoveranimation.begin();

        if (string_value.length >= 50) {
          MarqueeNews();
        }
    }
}

function MouseClickTextBlock(sender, eventArgs) {
    targethref = ""
    if (sender.name == "artist_field" && current_artist_id != "") {
        targethref = "http://cotonete.clix.pt/artistas/home.aspx?id=" + current_artist_id;
    } else if (sender.name == "music_field" && current_music_id != "") {
        targethref = "http://cotonete.clix.pt/artistas/song_home.aspx?id=" + current_music_id;
    } else if (sender.name == "album_field" && current_album_id != "") {
        targethref = "http://cotonete.clix.pt/artistas/album_home.aspx?id=" + current_album_id;
    } else if (sender.name == "news_text" && current_news_id != "") {
        targethref = "http://cotonete.clix.pt/noticias/body.aspx?id=" + current_news_id;
    }
    if (targethref != "") {
        var win = window.open(targethref, "cotonete_main");
    }
}

function MouseOutTextBlock(sender, eventArgs) {
    sender["TextDecorations"] = "None";
    sender["Cursor"] = "Arrow"
    mouseoutanimation = sender.findName("MouseOverTextBlockAnim");
    mouseoutanimation.stop();
}

function MouseOverMarquee(sender, eventArgs) {
    if (sender.name == "butMarqueeRadioName") {
        string_value = sender.FindName("radio_name_field").Text;

        if (string_value.length >= 25) {
          MarqueeRadioName();
        }
    } else if (sender.name == "butMarqueeArtist") {
        string_value = sender.FindName("artist_field").Text;
        if ((string_value.length >= 25 || array_artists.length > 0) && !running_artist_marquee) {
          running_artist_marquee = true;
          MarqueeArtist();
        }
    } else if (sender.name == "butMarqueeMusic") {
        string_value = sender.FindName("music_field").Text;

        if (string_value.length >= 25) {
          MarqueeMusic();
        }
    } else if (sender.name == "butMarqueeAlbum") {
        string_value = sender.FindName("album_field").Text;

        if (string_value.length >= 25) {
          MarqueeAlbum();
        }
    }
}

function Buffering(sender, eventArgs) {
    if (is_playing) {
        changeStatus("Buffering " + Math.round(sender.BufferingProgress * 100) + " %");
        if (Math.round(sender.BufferingProgress * 100) == 100) {
          changeStatus("");
          if (spinning_button) {
              SilverPlugin.content.findName("playlisting").stop();
              SilverPlugin.content.findName("BufferDone").begin();
              spinning_button = false;
          }
        } else {
          if (!spinning_button) {
              SilverPlugin.content.findName("playlisting").begin();
              spinning_button = true;
          }
        }
    }
}

function NewMedia (sender, eventArgs) {
    changeStatus("Playing");
    updatePosition();
// Variable to hold the MediaAttribute.
    var attribute;
   
    // Get the MediaAttribute named Title
    try
    {
        var attributesCollection = sender.Attributes;
        attribute = attributesCollection.getItemByName("Title");
    }
    catch(errorObj)
    {
        alert(errorObj.message);
    }
    
    // Display the Value of the MediaAttribute
    if(attribute != null)
    {
      arr_info = attribute.value.split('&');

      var song_id, next_song_id, jingle_spot_id, channel_id, interview_id, sound_id, soundid_id, dj_radio_id, version;
      var content_type_id, content_sub_type_id, user_jingle_id, audio_type_id, audio_sub_type_id, radio_id, vip_radio_id;
      var cotoneteownerid, stream_jingle_id, alert_ringtone, artist_radio_id;
      song_id = "";
      next_song_id = "";
      jingle_spot_id = "";
      channel_id = "";
      interview_id = "";
      sound_id = "";
      soundid_id = "";
      dj_radio_id = "";
      content_type_id = "";
      content_sub_type_id = "";
      user_jingle_id = "";
      audio_type_id = "";
      audio_sub_type_id = "";
      radio_id = "";
      vip_radio_id = "";
      version = "";
      cotoneteownerid = "";
      stream_jingle_id = "";
      alert_ringtone = "";
      artist_radio_id = ""

      for (i = 0; i <= arr_info.length-1; i++) {
          arr_str_temp = arr_info[i].split('=');

          switch (arr_str_temp[0]) {
            case "stream_jingle_id":
                stream_jingle_id = arr_str_temp[1];
                break;
            case "alert_ringtone":
                alert_ringtone = arr_str_temp[1];
                break;
            case "song_id":
                song_id = arr_str_temp[1];
                break;
            case "next_song_id":
                next_song_id = arr_str_temp[1];
                break;
            case "jingle_spot_id":
                jingle_spot_id = arr_str_temp[1];
                break;
            case "channel_id":
                channel_id = arr_str_temp[1];
                break;
            case "interview_id":
                interview_id = arr_str_temp[1];
                break;
            case "sound_id":
                sound_id = arr_str_temp[1];
                break;
            case "soundid_id":
                soundid_id = arr_str_temp[1];
                break;
            case "dj_radio_id":
                dj_radio_id = arr_str_temp[1];
                break;
            case "content_type_id":
                content_type_id = arr_str_temp[1];
                break;
            case "content_sub_type_id":
                content_sub_type_id = arr_str_temp[1];
                break;
            case "user_jingle_id":
                user_jingle_id = arr_str_temp[1];
                break;
            case "audio_type_id":
                audio_type_id = arr_str_temp[1];
                break;
            case "audio_sub_type_id":
                audio_sub_type_id = arr_str_temp[1];
                break;
            case "radio_id":
                radio_id = arr_str_temp[1];
                break;
            case "vip_radio_id":
                vip_radio_id = arr_str_temp[1];
                break;
            case "version":
                version = arr_str_temp[1];
                break;
            case "cotoneteownerid":
                cotoneteownerid = arr_str_temp[1];
                break;
            case "artist_radio_id":
                artist_radio_id = arr_str_temp[1];
                break;
          }
      }

      if (pub_is_running) {
          EndPlayPub();
      }

      if (jingle_spot_id!="") {
          allowskip = false;
          PlayPubStream("/listen/miolo_jingle_spot.asp?jingle_spot_id=" + jingle_spot_id);
      } else if (radio_id!="" && song_id!="") {
          allowskip = true;
          WebService.GetCurrentUserRadioInfo(radio_id, song_id, ShowPrivateRadioInfo, FailedCallback, "XmlDocument");
      } else if (dj_radio_id!="") {
          allowskip = false;
          WebService.GetCurrentDjSetInfo(dj_radio_id, ShowDjSetInfo, FailedCallback, "XmlDocument");
      } else if (vip_radio_id!="") {
          allowskip = true;
          WebService.GetCurrentVipRadioInfo(vip_radio_id, song_id, ShowVipRadioInfo, FailedCallback, "XmlDocument");
      } else if (content_type_id!="" && content_sub_type_id!="") {
          allowskip = true;
          WebService.GetContentTypeInfo(content_type_id, content_sub_type_id, radio_id, ShowContentTypeInfo, FailedCallback, "XmlDocument");
      } else if (stream_jingle_id!="") {
          //This is only for standard web radio sites
          //PlayPubStream();
      } else if (user_jingle_id!="") {
          allowskip = false;
          PlayPubStream("/listen/miolo_user_jingle.asp?user_jingle_id=" + user_jingle_id + "&radio_id=" + getQueryVariable("radio_id"));
      } else if (alert_ringtone!="") {
          //Not active
          //PlayAlertRingtone();
      } else if (interview_id!="") {
          allowskip = false;
          WebService.GetInterviewInfo(interview_id, ShowInterviewInfo, FailedCallback, "XmlDocument");
      } else if (sound_id!="") {
          allowskip = false;
          WebService.GetSoundInfo(sound_id, ShowSoundInfo, FailedCallback, "XmlDocument");
      } else if (channel_id!="") {
          allowskip = false;
          RunRadio(channel_id);
      } else if (soundid_id!="") {
          //Nothing
      } else if (audio_type_id!="" && song_id!="") {
          allowskip = true;
          WebService.GetCurrentAudioTypeInfo(audio_type_id, song_id, ShowSongIdAudioType, FailedCallback, "XmlDocument");
      } else if (audio_sub_type_id!="" && song_id!="") {
          allowskip = true;
          WebService.GetCurrentAudioSubTypeInfo(audio_sub_type_id, song_id, ShowSongIdAudioSubType, FailedCallback, "XmlDocument");
      } else if (getQueryVariable("related_artist_radio_id")!="" && song_id!="") {
          allowskip = true;
          WebService.GetCurrentArtistRadioInfo(getQueryVariable("related_artist_radio_id"), song_id, ShowSongIdArtistRadio, FailedCallback, "XmlDocument");
      } else if (getQueryVariable("fast_radio_id") != "" && song_id != "") {
          allowskip = true;
          WebService.GetCurrentFastRadioInfo(getQueryVariable("fast_radio_id"), song_id, ShowSongIdFastRadio, FailedCallback, "XmlDocument");
      } else if (song_id != "") {
          allowskip = false;
          if (song_id=="0") {
              PlayPubStream("/listen/promos/promo_iol.asp");
          } else if (song_id=="-1") {
              PlayPubStream("/listen/promos/promo_compal.asp");
          } else if (song_id=="-2") {
              PlayPubStream("/listen/promos/isep.asp");
          } else if (song_id=="-3") {
              PlayPubStream("/listen/promos/electronica.asp");
          } else if (song_id=="-4") {
              PlayPubStream("/listen/promos/r_comercial.asp");
          } else if (song_id=="-5") {
              PlayPubStream("/listen/promos/indie.asp");
          } else if (song_id=="-6") {
              PlayPubStream("/listen/promos/radio_clube.asp");
          } else if (song_id=="-7") {
              PlayPubStream("/listen/promos/best_rock.asp");
          } else if (song_id=="-8") {
              PlayPubStream("/listen/promos/cidadefm.asp");
          } else if (song_id=="-9") {
              PlayPubStream("/listen/promos/mix.asp");
          } else if (song_id=="-10") {
              PlayPubStream("/listen/promos/rnacional.asp");
          } else if (song_id=="-11") {
              PlayPubStream("/listen/promos/powerplay.asp");
          } else if (song_id=="-12") {
              PlayPubStream("/listen/promos/classe_a.asp");
          } else if (song_id=="-13") {
              PlayPubStream("/listen/promos/portuguesa.asp");
          } else if (song_id=="-14") {
              PlayPubStream("/listen/promos/cotton_club.asp");
          } else if (song_id=="-15") {
              PlayPubStream("/listen/promos/fado.asp");
          } else if (song_id=="-16") {
              PlayPubStream("/listen/promos/nostalgia.asp");
          } else if (song_id=="-17") {
              PlayPubStream("/listen/promos/emotions.asp");
          } else if (song_id=="-18") {
              PlayPubStream("/listen/promos/caliente.asp");
          } else if (song_id=="-19") {
              PlayPubStream("/listen/promos/poprock.asp");
          } else if (song_id=="-20") {
              PlayPubStream("/listen/promos/geracao_80.asp");
          } else if (song_id=="-21") {
              PlayPubStream("/listen/promos/caofm.asp");
          } else if (song_id=="-22") {
              PlayPubStream("/listen/presidenciais/frame_player_info.asp?channel_id=21")
          } else if (song_id=="-23") {
              PlayPubStream("/listen/promos/foxx.asp");
          } else if (song_id=="-24") {
              PlayPubStream("/listen/promos/clubdisco.asp");
          } else if (song_id=="-25") {
              PlayPubStream("/listen/promos/radioportugal.asp");
          } else if (song_id=="-26") {
              PlayPubStream("/listen/promos/kulto.asp");
          } else if (song_id=="-27") {
              PlayPubStream("/listen/promos/rtb.asp");
          } else if (song_id=="-28") {
              PlayPubStream("/listen/promos/rum.asp");
          } else if (song_id=="-29") {
              PlayPubStream("/listen/promos/thehousebox.asp");
          } else if (song_id=="-30") {
              PlayPubStream("/listen/promos/romantica.asp");
          } else if (song_id=="-31") {
              PlayPubStream("/listen/promos/m80.asp");
          } else {
              WebService.GetCurrentSongInfo(song_id, ShowSongId, FailedCallback, "XmlDocument");
          }
      }
    } else if (getQueryVariable("wma")!="") {
        allowskip = false;
        ShowWMAInfo(getQueryVariable("album_cover"), getQueryVariable("bold1") + " " + getQueryVariable("linha1"), getQueryVariable("bold2") + " " + getQueryVariable("linha2"), getQueryVariable("bold3") + " " + getQueryVariable("linha3"), getQueryVariable("bold4") + " " + getQueryVariable("linha4"))
    }
}

function LoadFail (sender, eventArgs) {
    clearTimeout(update_position_timmer);

    document.getElementById("CurrentMediaItem").innerText += "FAILLLLLLLL - " + SilverPlugin.content.findName("player").Source + " \n" ;
    if (totalPlayListItems>currentPlayingItem) {
        if (number_ref_in_curr_entry==1 && !retry_next) {
            currentPlayingItem ++;
            current_ref = 0;
        } else if (!retry_next) {
            current_ref ++;
            if (number_ref_in_curr_entry==current_ref) {
                currentPlayingItem ++;
                current_ref = 0;
            }
        }
        ret_value = PlayPlaylist();
        
        if (!ret_value) {
            return;
        }

//        if (original_cena.indexOf("alert_ringtone=1")!=-1) {
        retry_next = !retry_next;
//        }
        if (totalPlayListItems<=currentPlayingItem) {
            //playlist has ended
            if (repeat_playlist) {
                loadXML(xml_to_load);
            } else {
                changeStatus("Fim Playlist");
            }
        }
    } else {
        if (repeat_playlist) {
            loadXML(xml_to_load);
        } else {
            changeStatus("Fim Playlist");
        }
    }
}

function MediaEnd (sender, eventArgs) {
    clearTimeout(update_position_timmer);
    //return pointer to start position
    var progressPointer = SilverPlugin.content.findName('TimelineBarMask');
    var progressBar = SilverPlugin.content.findName('Timeline');
    progressPointer['Rect'] = "7, 170, 0, 40";
    currentTimelineBarMaskPos = 0;

    //document.getElementById("CurrentMediaItem").innerText += "MediaEnded!!!\n";

    //Go to next media in playlist
    currentPlayingItem ++;
    current_ref = 0;

    if (totalPlayListItems>currentPlayingItem) {
//    document.getElementById("CurrentMediaItem").innerHTML += "totalPlayListItems --> " + totalPlayListItems + " .... currentPlayingItem --> " + currentPlayingItem + "<br>";
        ret_val = PlayPlaylist();
        if (ret_val == false && !window.ActiveXObject) {
            MediaEnd(sender, eventArgs);
        }
    } else {
        if (repeat_playlist) {
            currentPlayingItem = 0;
            loadXML(xml_to_load);
        } else {
            changeStatus("Fim Playlist");
            clearTimeout(update_position_timmer);
        }
    }
}

function LaunchSWF(filename) {
    document.getElementById("standard_pub").src = "http://cotonete.clix.pt" + filename;
    //document.getElementById("CurrentMediaItem").innerText += document.getElementById("standard_pub").src;
    document.getElementById("standard_pub").style.display="block";
}

function PlayAlertRingtone() {
/*
    playerpub = SilverPlugin.content.findName('player_pub');
    playerpub.Source = "loading_pub.wmv";
    pub_is_running = true;
    playerpub['Visibility'] = "Visible";
*/

//    SilverPlugin.content.findName("PubTmnOpen").begin();
/*
    SilverPlugin.content.findName("player_env_sounds").stop();
    SilverPlugin.content.findName("player_env_sounds").Source = "tmn.mp3"
    SilverPlugin.content.findName("player_env_sounds").play();
*/
//    pub_is_running = true;
    //LaunchSWF();
}

function PlayPubStream(filename) {
/*
    playerpub = SilverPlugin.content.findName('player_pub');
    playerpub.Source = "loading_pub.wmv";
    pub_is_running = true;
    playerpub['Visibility'] = "Visible";
*/
    //SilverPlugin.content.findName("PubTmnOpen").begin();
/*
    SilverPlugin.content.findName("player_env_sounds").stop();
    SilverPlugin.content.findName("player_env_sounds").Source = "tmn.mp3"
    SilverPlugin.content.findName("player_env_sounds").play();
*/
    pub_is_running = true;
    var slua = getOsAndBrowser();
    if (slua.Browser == "Firefox" && slua.OS == "Windows") {
        document.getElementById("silverplace").style.background="url(Sources/bg_silver_firefox.jpg)";
        SilverPlugin.width = 0;
        SilverPlugin.height = 0;
    } else {
        changeNowPlayingInfo("", "", "", "", "")
    }
    LaunchSWF(filename);
}

function EndPlayPub() {
/*
    playerpub = SilverPlugin.content.findName('player_pub');
    playerpub.stop();
    pub_is_running = false;
    playerpub['Visibility'] = "Collapsed";
*/
    var slua = getOsAndBrowser();
    if (slua.Browser == "Firefox" && slua.OS == "Windows") {
        document.getElementById("silverplace").style.background="url(Sources/bg_dropboxes.jpg)";
        SilverPlugin.width = 340;
        SilverPlugin.height = 268;
    }

    pub_is_running = false;
    document.getElementById("standard_pub").style.display="none";
    document.getElementById("standard_pub").src = "";

    //SilverPlugin.content.findName("PubTmnClose").begin();
}

function Play(sender, eventArgs) {
  var player = SilverPlugin.content.findName("player");

  if (player.currentState == 'Paused') {
      SilverPlugin.content.findName("On").begin();
      player.play();
      changeStatus("Playing");

      SilverPlugin.content.findName("player_env_sounds").stop();
      SilverPlugin.content.findName("player_env_sounds").play();
  } else if (player.currentState == 'Stopped') {
    curr_source = player.Source;
    player.Source = curr_source;
    SilverPlugin.content.findName("On").begin();
    player.play();
    changeStatus("Playing");

    SilverPlugin.content.findName("player_env_sounds").stop();
    SilverPlugin.content.findName("player_env_sounds").play();
  }
}

function Stop(sender, eventArgs) {
  var player = SilverPlugin.content.findName("player");

  SilverPlugin.content.findName("player_env_sounds").stop();
  SilverPlugin.content.findName("player_env_sounds").play();
  
  if (player.currentState == 'Playing') {
    SilverPlugin.content.findName("Off").begin();
    player.pause();
    if (player.currentState == 'Playing') {
      player.stop();
    }
    changeStatus("Paused");
  }
}

function Skip(sender, eventArgs) {
    if (allowskip) {
        clearTimeout(update_position_timmer);
        //return pointer to start position
        var progressPointer = SilverPlugin.content.findName('TimelineBarMask');
        var progressBar = SilverPlugin.content.findName('Timeline');
        progressPointer['Rect'] = "7, 170, 0, 40";
        currentTimelineBarMaskPos = 0;

        //document.getElementById("CurrentMediaItem").innerText += "MediaEnded!!!\n";

        //Go to next media in playlist
        currentPlayingItem ++;
        current_ref = 0;

        if (totalPlayListItems>currentPlayingItem) {
//        document.getElementById("CurrentMediaItem").innerHTML += "totalPlayListItems --> " + totalPlayListItems + " .... currentPlayingItem --> " + currentPlayingItem + "<br>";
            ret_val = PlayPlaylist();
            if (ret_val == false && !window.ActiveXObject) {
                Skip(sender, eventArgs);
            }
        } else {
            if (repeat_playlist) {
                currentPlayingItem = 0;
                loadXML(xml_to_load);
            } else {
                changeStatus("Fim Playlist");
                clearTimeout(update_position_timmer);
            }
        }
    } else {
        //Skip is not alowed
    }
}

function MouseOverOnBut(sender, eventArgs) {
    if (is_playing) {
        SilverPlugin.content.findName("OnButHoverIn").begin();
    } else {
        SilverPlugin.content.findName("OffButHoverIn").begin();
    }
}

function MouseOutOnBut(sender, eventArgs) {
    if (is_playing) {
        SilverPlugin.content.findName("OnButHoverOut").begin();
    } else {
        SilverPlugin.content.findName("OffButHoverOut").begin();
    }
}

function ChangePlayStatus(sender, eventArgs) {
    SilverPlugin.content.findName("OnButHoverIn").stop();
    SilverPlugin.content.findName("OnButHoverOut").stop();
    SilverPlugin.content.findName("OffButHoverIn").stop();
    SilverPlugin.content.findName("OffButHoverOut").stop();
    if (is_playing) {
        Stop();
        is_playing = false;
    } else {
        Play();
        is_playing = true;
    }
}

function LinkQualidade(sender, eventArgs) {
    window.document.location="speed.asp?" + window.location.search.substring(1);
}

function VolumeMouseEnter (sender, eventArgs) {
    SilverPlugin.content.findName("VolumePercentage").Opacity = 1.0;
    MouseIn = true;
}

function VolumeMouseLeave (sender, eventArgs) {
   if (!Dragging)
    {
        SilverPlugin.content.findName("VolumeHidePercentage").Begin();
    }
    
    MouseIn = false;
}

function VolumeMouseLeftButtonDown(sender, eventArgs) {
  if (sender.CaptureMouse())
    {
        Dragging = true;
    }
}

function VolumeMouseMove(sender, eventArgs) {
  if (Dragging)
    {
        var VolumeSlider = SilverPlugin.content.findName("VolumeSlider");
        var VolumeSliderPosition = SilverPlugin.content.findName("volumeSliderPosition");
        var VolumeBarMask = SilverPlugin.content.findName("VolumeBarMask");

        var PlayerControl = SilverPlugin.content.findName("player");
        var position = eventArgs.getPosition(VolumeSlider).x;
        
        if (position < 0)
        {
            position = 0;
        }
        else if (position > VolumeSlider.Width)
        {
            position = VolumeSlider.Width;
        }

        VolumeSliderPosition["Canvas.Left"] = VolumeSlider["Canvas.Left"] + position - (VolumeSliderPosition.width / 2) -5;
        VolumeBarMask.Rect = "55, 210, " + position + ", 50"
        
        PlayerControl.Volume = ((VolumeSlider.Width + position) / VolumeSlider.Width) - 1;
        //SilverPlugin.content.findName("player_env_sounds").Volume = PlayerControl.Volume;
        //SilverPlugin.content.findName("player_pub").Volume = PlayerControl.Volume;

        def_volume = SilverPlugin.content.findName("player").Volume;
        var percentage = PlayerControl.Volume * 100;

        if (SilverPlugin.content.findName("sound_icon").Source == "Sources/soundofficon.png") {
            SilverPlugin.content.findName("sound_icon").Source = "Sources/soundicon.png";
        }
        
        SilverPlugin.content.findName("VolumePercentage")["Canvas.Left"] = VolumeSliderPosition["Canvas.Left"] - 5;
        SilverPlugin.content.findName("VolumePercentage").Text = percentage.toFixed(0) + "%";

        //window.status = "Volume: " + percentage.toFixed(0)  + "%";
    }

}

function VolumeMouseLeftButtonUp(sender, eventArgs) {
 if (Dragging)
    {
       Dragging = false;
       
       sender.releaseMouseCapture();

       if (!MouseIn)
       {
           SilverPlugin.content.findName("VolumeHidePercentage").Begin();
       }

    }
}

function ShowSoundTooltip(sender, eventArgs) {
    SilverPlugin.content.findName("tooltiptext").text = "Mute";
    SilverPlugin.content.findName("SoundTooltipOn").begin();
}

function HideSoundTooltip(sender, eventArgs) {
    SilverPlugin.content.findName("tooltiptext").text = "Mute";
    SilverPlugin.content.findName("SoundTooltipOff").begin();
}

function MuteSound(sender, eventArgs) {
    if (SilverPlugin.content.findName("sound_icon").Source == "Sources/soundicon.png") {
        SilverPlugin.content.findName("sound_icon").Source = "Sources/soundofficon.png";
        def_volume = SilverPlugin.content.findName("player").Volume;
        SilverPlugin.content.findName("player").Volume = 0;
    } else {
        SilverPlugin.content.findName("sound_icon").Source = "Sources/soundicon.png";
        SilverPlugin.content.findName("player").Volume = def_volume;
    }
}

function FriendTooltipOn(sender, eventArgs) {
    SilverPlugin.content.findName("tooltip2text").text = "Enviar a Amigo";
    SilverPlugin.content.findName("FriendTooltipOn").begin();
}

function FriendTooltipOff(sender, eventArgs) {
    SilverPlugin.content.findName("tooltip2text").text = "Enviar a Amigo";
    SilverPlugin.content.findName("FriendTooltipOff").begin();
}

function QualityTooltipOn(sender, eventArgs) {
    SilverPlugin.content.findName("tooltip3text").text = "Qualidade de Som";
    SilverPlugin.content.findName("QualityTooltipOn").begin();
}

function QualityTooltipOff(sender, eventArgs) {
    SilverPlugin.content.findName("tooltip3text").text = "Qualidade de Som";
    SilverPlugin.content.findName("QualityTooltipOff").begin();
}

function HelpTooltipOn(sender, eventArgs) {
    SilverPlugin.content.findName("tooltiptext").text = "Ajuda";
    SilverPlugin.content.findName("HelpTooltipOn").begin();
}

function HelpTooltipOff(sender, eventArgs) {
    SilverPlugin.content.findName("tooltiptext").text = "Ajuda";
    SilverPlugin.content.findName("HelpTooltipOff").begin();
}

function SkipTootipon(sender, eventArgs) {
    SilverPlugin.content.findName("SkipTootipon").begin();
}

function SkipTootipoff(sender, eventArgs) {
    SilverPlugin.content.findName("SkipTootipoff").begin();
}

function ShowNews(sender, eventArgs) {
   if (is_news_open) {
       SilverPlugin.content.findName("news_out").begin();
       is_news_open = false;
       clearInterval(change_news_timmer);
   } else {
       SilverPlugin.content.findName("news_in").begin();
       is_news_open = true;
       change_news_timmer = setInterval(ChangeNews, 10000);
   }
}

function ShowMinutesLeft(sender, eventArgs) {
    SilverPlugin.content.findName("ShowMinutesInfo").begin();
}

function HideMinutesLeft(sender, eventArgs) {
    //SilverPlugin.content.findName("HideMinutesInfo").stop();
    SilverPlugin.content.findName("HideMinutesInfo").begin();
}

function updatePosition() {
  var PlayerControl = SilverPlugin.content.findName("player");

  var currentPosition = PlayerControl.position.seconds;
  var length = PlayerControl.naturalDuration.seconds;
  if (length>0) {
      var progressBar = SilverPlugin.content.findName('Timeline');
      var progressPointer = SilverPlugin.content.findName('TimelineBarMask');

      var relativePosition = (currentPosition/length) * progressBar.width;

      progressPointer['Rect'] = "7, 170, " + relativePosition + ", 40";
      currentTimelineBarMaskPos = relativePosition;
      
      //minutes = Math.round(length - currentPosition) / 60;
      //seconds = Math.round(length - currentPosition) - (minutes * 60));
      if (SilverPlugin.content.findName("minutesinfo").Text != time(length - currentPosition)) {
          changeStatus("Playing");
      }
      SilverPlugin.content.findName("minutesinfo").Text = time(length - currentPosition);
      
      update_position_timmer = setTimeout(updatePosition, 500);
  }
  if (pub_is_running) {
      //document.getElementById("silverlightPlugInHost").style.display="none";
      //document.getElementById("divPlayer_0").style.display="none";
      document.getElementById("standard_pub").style.zIndex=5330;
  }
}

function send_friend(sender, eventArgs) {
    parent.document.getElementById("send_to_friend_iframe").src = '/popup/partilhar/sendFriendPlayer.asp?type=player&' + window.location.search.substring(1);
    parent.document.getElementById("mrec").style.display = "none";
    parent.document.getElementById("add_to_my_radios").style.display = "none";
    parent.document.getElementById("send_to_friend").style.display = "block";

    //window.open('/popup/enviar_amigo.asp?type=player&querystring=' + window.location.search.substring(1),'enviar_amigo','width=650,height=255');
}

function HelpPage(sender, eventArgs) {
//Skip();
    window.open('/ajuda/index.aspx', 'cotonete_main')
    //MM_openBrWindow('/popup/ajudas/player.asp','','width=620,height=485');
}

//ALTERA FUNCTION ADDSONG
function OldAddSong(sender, eventArgs) {
    var oWin = window.open('/popup/add_music/adicionar.asp?id='+ current_music_id,'add_song','width=620,height=310');
    if (oWin==null || typeof(oWin)=="undefined") {
        var truthBeTold = window.confirm("Não foi possível abrir a Janela de adicionar música ás suas rádios!\nProvavelmente devido a um bloqueador de \"pop ups\".\nSugerimos que desactive essa aplicação.\nDeseja consultar a Ajuda?");
        if (truthBeTold) {
            window.location = "/ajuda/faqs.aspx";
        }
    }
}
//ALTERA FUNCTION ADDSONG

function AddSong(sender, eventArgs) {
    parent.document.getElementById("add_song_to_my_radios").src = '/popup/add_music/addmusic2myradio.aspx?id=' + current_music_id;
    parent.document.getElementById("mrec").style.display = "none";
    //parent.document.getElementById("radios").style.display = "none";
    parent.document.getElementById("add_to_my_radios").style.display = "block";
    parent.document.getElementById("send_to_friend").style.display = "none";    
}

function ShowAddSongToolTip(sender, eventArgs) {
    SilverPlugin.content.findName("tooltip4text").text = "Adicionar ás Minhas Músicas";
    SilverPlugin.content.findName("AddSongTooltipOn").begin();
}
 
function HideAddSongToolTip(sender, eventArgs) {
    SilverPlugin.content.findName("AddSongTooltipOff").begin();
}

function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function time(ms) {
//var sec = Math.floor(ms/1000)
var sec = Math.floor(ms)
//ms = ms % 1000
//t = three(ms)

var min = Math.floor(sec/60)
sec = sec % 60
//t = two(sec) + ":" + t
t = two(sec)


var hr = Math.floor(min/60)
min = min % 60
t = two(min) + ":" + t
/*
var day = Math.floor(hr/60)
hr = hr % 60
t = two(hr) + ":" + t
t = day + ":" + t
*/
return t
}

function showChangePlayerDiv() {
    if (navigator.appName == "Microsoft Internet Explorer") {
        document.getElementById("change_player_version").style.display = "block";
    }
}
