/*jslint nomen:false*/
/*global $,$H,Ajax,Event,console*/
/*global similar_array,genre_array,mood_array*/
/*global Options,Thumbs*/

//disable firebug debugging
try
{
  console.assert (1);
}

catch (e)
{
  console = {
    log:function (){},
    assert:function (){}
  };
}

Options = {};

Options._arrays = $w("_filter_array _search_array _genre_array _decade_array _mood_array _similar_array _artist_similar_array");
Options._arrays.each(function(array){
    Options[array] = [];
})

Options._remove_timestamp = function(array, timestamp) {
  return array.reject(function(item) {
      return item.indexOf(timestamp) >= 0;
  })
}

Options.remove = function (timestamp)
{
  Options._arrays.each(function(array){
      Options[array] = Options._remove_timestamp(Options[array], timestamp);
  });
};

Options.update_results = function ()
{
  $('filter_filter_options').value = Options._filter_array.join (',');
  $('filter_search_options').value = Options._search_array.join (',');
  $('filter_genre').value          = Options._genre_array.join (',');
  $('filter_decade').value         = Options._decade_array.join (',');
  $('filter_mood').value           = Options._mood_array.join (',');
  $('filter_similar').value        = Options._similar_array.join (',');
  $('filter_similar_artist').value = Options._artist_similar_array.join(',');
};

Options.add_filter = function (timestamp, filter)
{
  return Options.add("_filter_array", timestamp, filter);
};

Options.add_genre = function (timestamp, genre) {
  return Options.add("_genre_array", timestamp, genre);
}

Options.add_decade = function (timestamp, decade) {
  return Options.add("_decade_array", timestamp, decade);
}

Options.add_search = function (timestamp, search)
{
  return Options.add("_search_array", timestamp, search);
};

Options.add_similar = function(timestamp, track) {
  return Options.add("_similar_array", timestamp, track);
}

Options.add_similar_artist = function(timestamp, service_id, collection_name) {
  Options.add("_artist_similar_array", timestamp, service_id + "." + collection_name)
}

Options.add = function(array, timestamp, value) {
  console.log("Options.add", array, timestamp, value);
  Options.remove(timestamp);
  Options[array].push(timestamp + "_" + value);
  Options.update_results();
}

Event.observe (
  window,
  'load',
  function ()
  {
    console.log ('window.onload');
    $('filter_mood').value    = '';
    $('filter_genre').value   = '';
    $('filter_tempo').value   = '';
    $('filter_decade').value  = '';
    $('filter_similar').value = '';
    $('filter_cc').value      = '';
  }
);

function
filter_changed (timestamp)
{
  var textie          = $('textie_' + timestamp);
  var filter          = $('filter_' + timestamp);
  var filter_selector = $('filter_selector_' + timestamp);
  var container       = $('container_' + timestamp);
  var previous_filter = filter_selector.value;

  console.log ("filter_changed(", timestamp, ") called");
  console.log ("filter_selector.value: ", filter_selector.value);
  console.log ("textie: ", textie);

  Options.remove (timestamp);
  filter_selector.value = $('filter_' + timestamp).value;
}

function
remove_filter_row (timestamp)
{
  console.log ("remove_filter_row", timestamp);
  if (document.getElementsByClassName ('filter_row').length > 1)
    {
      var filter_selector = $('filter_selector_' + timestamp);
      Options.remove (timestamp);
      if ((filter_selector) && ( filter_selector.value === 'engine'))
      {
        $('filter_engine').value = 'all';
      }
      Options.update_results ();
      $('filter_row_' + timestamp).remove ();
    }
}

function
genre_changed (timestamp)
{
  Options.add_genre(timestamp, $('genre_selector_' + timestamp).value);
  Options.update_results();
}
function
decade_changed (timestamp, decade)
{  
  Options.add_decade(timestamp, decade);
  Options.update_results();
}

Thumbs ={};

Thumbs.up = function (id2)
{
  var id1 = Options._similar_array.join(",");
  var query_string = $H ({ 'id1': id1, 'id2':id2 }).toQueryString ();
  var r = new Ajax.Request ('/thumbs/up?' + query_string,
                            { method:'post' });
};

Thumbs.down = function (id2)
{
  var id1 = Options._similar_array.join(",");
  var query_string = $H ({ 'id1': id1, 'id2':id2 }).toQueryString ();
  var r = new Ajax.Request ('/thumbs/down?' + query_string,
                            { method:'post' });
};

function
now ()
{
  return new Date ().getTime ();
}

function
add_sounds_unlike (track, timestamp)
{
  console.log ("Sounds unlike: " + track);
  Options.add_search (timestamp, "unlike:" + track);
}

function
create_player(div_id, location)
{
  swfobject.embedSWF(
    //swfUrl
    '/swf/miniplayer.swf',
    //html id
    div_id,
    //width
    '150',//'17',
    //height
    '20',//'17',
    //version
    '8.0.0',
    false,
    //flashvars
    {
      'song_url': location.gsub('&', '%26')
    },
    //params
    false,
    //attributes
    {
      style:'background-color: #262626',
      bgcolor: '#262626'
    });
}

function
create_slider(timestamp, min, max, step, fix_values)
{
  if (!fix_values) {
    fix_values = Prototype.K
  }
  min = fix_values(min);
  max = fix_values(max);
  var handles = [$(timestamp + '_handle3'),
                $(timestamp + '_handle4')];
  var values  = [min, max];
  new Control.Slider(handles, timestamp + '_bpm',
   {
     range:       $R(min, max, false),
     increment:   step,
     restricted:  true,
     sliderValue: values,
     onSlide: function(values) {
       values[0] = fix_values(values[0])
       values[1] = fix_values(values[1])
       $(timestamp + '_bpm_values').innerHTML = values[0] + ' and ' + values[1];
       $(timestamp + '_bpm_values').value     = values;
     },
     onChange: function(values) {
       Options.add_filter(timestamp,
                         'bpm:[' + values[0] +
                          ' TO ' + values[1] + ']');
       Options.update_results()
     }
   }
  );
}


