
var SESSION_PLAYLIST = "sessionplaylist";

function sessionplaylist_add( url, title, errordiv ){
  url = trim( decodeURIComponent(url) );
  title = trim( decodeURIComponent(title) );
  if( title == null || title.length == 0 ){
     if( $(errordiv) != null ) $(errordiv).innerHTML = 'You must select a title for your song';
     else dialogAlert('You must select a title for your song');
     return false;
  }
  var lcurl = url.toLowerCase();
  var isVideo = false;
  var isMP3 = false;
  var re = new RegExp("http://www\.youtube\.com/watch\\?v=(.*)");
  if( re.exec(lcurl) != null ) isVideo = true;
  if( isMP3Url(lcurl) ) isMP3 = true;
  if( !isMP3 && !isVideo ){
    if( $(errordiv) != null ) $(errordiv).innerHTML = 'Invalid Media Url';
    else dialogAlert('Invalid Media Url');
    return false;
  }

  var playlistJson = Cookie.read(SESSION_PLAYLIST);
  var playlistArray;
  if( playlistJson == null ){
    playlistArray = new Array();
  }else{
    playlistArray = JSON.decode(playlistJson);
    for( var a = 0; a < playlistArray.length; a++ ){
      if( playlistArray[a].url == url && playlistArray[a].title == title ){
        dialogAlertTimeout( "Song already added to playlist", 1000 ); 
        return false;
      }
    }
  }
  var item = new Object();
  item.url = url;
  item.title = title;
  playlistArray.push( item );  
  cleanPlaylist( playlistArray );
  var json_before = JSON.encode(playlistArray);
  Cookie.write( SESSION_PLAYLIST, json_before, {duration:360, domain: 'flashwidgetz.com', path: '/'} );
  var json_after  = Cookie.read(SESSION_PLAYLIST);
  if( json_before != json_after ){
    dialogAlert("You must login and save your playlist before you can add more songs");
    return false;
  }
  return true;
}


function sessionplaylist_delete( songId ){
  var playlistJson = Cookie.read(SESSION_PLAYLIST);
  var playlistArray;
  if( playlistJson == null ){
    return false;
  }else{
    playlistArray = JSON.decode(playlistJson);
    for( var a = 0; a < playlistArray.length; a++ ){
      if( playlistArray[a].id == songId ){
        playlistArray.remove(a);
        cleanPlaylist( playlistArray );
        Cookie.write( SESSION_PLAYLIST, JSON.encode(playlistArray), {duration:360, domain: 'flashwidgetz.com', path: '/'} );
        return true;
      }
    }
  }
}

function sessionplaylist_empty(){
  var playlistJson = Cookie.read(SESSION_PLAYLIST);
  if( playlistJson == null ) return true;
  playlistArray = JSON.decode(playlistJson);
  if( playlistArray == null || playlistArray.length == 0 ) return true;
  return false;
}

function sessionplaylist_getplaylist(){
  var Playlist = new Array(1);
  Playlist[0] = new Object();
  Playlist[0].playlist_number = 0;
  var playlistJson = Cookie.read(SESSION_PLAYLIST);
  var playlistArray;
  if( playlistJson == null ){
    Playlist[0].songs = null;
  }else{
    playlistArray = JSON.decode(playlistJson);
    cleanPlaylist( playlistArray );
    Playlist[0].songs = playlistArray;
  }
  return Playlist;
}

function sessionplaylist_modify( songId, title, url, errordiv ){
  title = trim( decodeURIComponent(title) );
  url = trim( decodeURIComponent( url ) );
  if( title == null || title.length == 0 ){
    if( $(errordiv) != null ) $(errordiv).innerHTML = 'You must select a title for your song';
    else dialogAlert('You must select a title for your song');
    return false;
  }
  var lcurl = url.toLowerCase();
  var isVideo = false;
  var isMP3 = false;
  var re = new RegExp("http://www\.youtube\.com/watch\\?v=(.*)");
  if( re.exec(lcurl) != null ) isVideo = true;
  if( isMP3Url(lcurl) ) isMP3 = true;
  if( !isMP3 && !isVideo ){
    if( $(errordiv) != null ) $(errordiv).innerHTML = 'Invalid Media Url';
    else dialogAlert('Invalid Media Url');
    return false;
  }
  var playlistJson = Cookie.read(SESSION_PLAYLIST);
  if( playlistJson == null ) return false;
  var playlistArray = JSON.decode(playlistJson);
  for( var a = 0; a < playlistArray.length; a++ ){
    if( playlistArray[a].id == songId ){
      playlistArray[a].title = title;
      playlistArray[a].url = url;
      cleanPlaylist( playlistArray );
      Cookie.write( SESSION_PLAYLIST, JSON.encode(playlistArray), {duration:360, domain: 'flashwidgetz.com', path: '/'} );
      return true;
    }
  }
  return false;
}

function sessionplaylist_reorder( order ){
  if( order == null ) return;
  var playlistJson = Cookie.read(SESSION_PLAYLIST);
  if( playlistJson == null ){
    return;
  }
  var playlistArray = JSON.decode(playlistJson);
  var start   = -1;
  var end     = -1;
  var higher  = 0;
  var lower   = 0;
  var counter = 1;
  var array = order.split(".");
  for( var a = 0; a < array.length; a++ ){
    var s = parseInt(array[a])+1;
    if( counter != s ){
      if( start == -1 ) start = counter;
      end = counter;
      if( counter < s ) higher++;
      else lower++;
    }else if( start != -1 ) break;
    counter++;
  }
  if( start == -1 || end == -1 ) return;
  start--;
  end--;
  if( higher > lower ){
    var item = playlistArray[start];
    playlistArray.remove( start );
    playlistArray = playlistArray.insert( end, item );
  }else{
    var item = playlistArray[end];
    playlistArray.remove( end );
    playlistArray = playlistArray.insert( start, item );
  }
  cleanPlaylist( playlistArray );
  Cookie.write( SESSION_PLAYLIST, JSON.encode(playlistArray), {duration:360, domain: 'flashwidgetz.com', path: '/'} );
}

function cleanPlaylist( playlistArray ){
  if( playlistArray == null || playlistArray.length == 0 ) return;
  for( var a = 0; a < playlistArray.length; a++ ){
    playlistArray[a].id = (a+1);
  }
}

function isMP3Url(s) {
  s = trim( s );
  var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  if( regexp.test(s) == true ){
    return ( s.substring( s.length-4, s.length ) == '.mp3');
  }else{
    return false;
  }
}


Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
Array.prototype.insert = function(index,value){
   if(!(index>=0)) return;
   var original = this.slice();
   var temp = original.splice(index);
   original[index] = value;
   original = original.concat(temp);
   return original;
};
