var GLOBAL_ADDSONG_ID = "global_addsong";

function global_addsong( title, url, handler ){
  if( $(GLOBAL_ADDSONG_ID) ) global_hide_playlist_choice();
  var playlistnumber = 0;
  if( loggedIn == false ){
     playlistnumber = 0;
     if( handler ) handler( true );
     else{
       if( sessionplaylist_add( url, title, null ) == true ){
         dialogAlertTimeout( 'Song added successfully', 1000 );
       }
       return;
     }
  }else{
    var ajax = new Ajax( global_addsong_playlist_handler, ajaxError, { 'url': url, 'title': title, 'hander': handler } );
    var uri = "/media/listplaylist!ajax.action?"+generateLine()+"=0";
    ajax.get( uri, null, false );
  }
}

function global_addsong_playlist_handler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  var handler = obj.getParams().handler;
  if(json.status == 'failure'){
    if( handler ) handler( false, json.errorMessage );
    else{
      dialogAlert(json.errorMessage);
      return;
    }
  }else{
    if( json.Playlist == null || json.Playlist.length == 0 ){
      dialogAlertTimeout( 'Song added successfully', 1000 );
      return;
    }else{
      global_show_playlist_choice( json.Playlist, obj.getParams().url, obj.getParams().title, handler );
      return;
    }
  }
}

function global_show_playlist_choice( PlaylistArray, url , title, handler ){
  var width = 400;
  var width1 = width - 4;
  var width2 = width - 2;
  var width3 = width - 0;

  var html = ''+
      '<div class="round_header" style="width:'+width+'px;">'+
      ' <span class="round_header1" style="width:'+width1+'px;background-color:#333"></span>'+
      ' <span class="round_header2" style="width:'+width2+'px;background-color:#333"></span>'+
      '  <div class="round_header_content" style="width:'+width3+'px;background-color:#333;color:#FFF;">'+
      '   <div class="round_header_title" style="width:'+width3+'px">Select Playlist to Add Song</div>'+
      '   <div>';
  for( var a = 0; a < PlaylistArray.length; a++ ){
    html += '<div class="playlistlink" pn="'+PlaylistArray[a].playlist_number+'" style="padding:5px;">'+PlaylistArray[a].name+'</div>';
  }
  html += '' +
      '      <div><center><div class="whitelink" onclick="global_hide_playlist_choice();return false;" style="cursor:pointer">Close</div></center></div>'+
      '   </div>'+
      '  </div>'+
      ' <span class="round_header2" style="width:'+width2+'px;background-color:#333;"></span>'+
      ' <span class="round_header1" style="width:'+width1+'px;background-color:#333"></span>'+
      '</div>';
  var container = document.createElement('div');
  container.setAttribute('id', GLOBAL_ADDSONG_ID );
  var center = browser.getAdjustedCenter();
  var scroll = $(window).getScroll();
  var x = center.x - (width/2);
  var y = scroll.y + 80;
  container.setStyle('position', 'absolute');
  container.setStyle('top', y );
  container.setStyle('left', x );
  container.setStyle('z-index', 1000);
  container.setStyle('display', 'block');
  container.setStyle('opacity', 0.95);
  container.innerHTML = html;
  document.getElementsByTagName("body")[0].appendChild( container );


  $$('#'+GLOBAL_ADDSONG_ID+' .playlistlink').each( function(el){ el.removeEvents() } );

  $$('#'+GLOBAL_ADDSONG_ID+' .playlistlink').each( function(el){
    el.setStyle('cursor', 'pointer');
    el.set('class', 'whitelink');
    el.addEvent( 'click', function(o){
      var playlistNumber = o.el.getAttribute('pn');
      global_hide_playlist_choice();
      global_addsong_ajax( o.title, o.url, playlistNumber, o.handler );
    }.pass( { 'title':title, 'url':url, 'handler':handler, 'el':el  } ) );
  });
}


function global_hide_playlist_choice( ){
  if( $( GLOBAL_ADDSONG_ID ) != null ){
    document.getElementsByTagName("body")[0].removeChild( $( GLOBAL_ADDSONG_ID ) );
  }
}


function global_addsong_ajax( title, url, playlistnumber, handler ){
   var title = encodeURIComponent( title );
   var url   = encodeURIComponent( url );
   var ajax = new Ajax( global_addsong_ajax_handler, ajaxError, handler );
   var uri = "/media/addplaylistmedia!ajax.action?"+generateLine()+"=0&playlistNumber="+playlistnumber+"&title="+title+"&url="+url;
   ajax.get( uri, null, false );
}

function global_addsong_ajax_handler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  var handler = obj.getParams();
  if(json.status == 'success'){
    if( handler ) handler( true );
    else{
      dialogAlertTimeout( 'Song added successfully', 1000 );
      return;
    }
  }else{
    if( handler ) handler( false, json.errorMessage );
    else{
      dialogAlertTimeout(json.errorMessage, 1000);
      return;
    }
  }
}


function global_artist_search_tag( artistName ){
  var artistName = encodeURIComponent( artistName );
  var ajax = new Ajax( null, null );
  var uri = "/media/addartistsearchtag!ajax.action?"+generateLine()+"=0&artistName="+artistName;
  ajax.get( uri, null, false );
}
