function remove_selected_lightbox_and_close_modal(lightbox_list)
{
  var selected_value = lightbox_list.find('option:selected').remove();
  jQuery("#lightbox-picker-modal").dialog("close");
  return true;
}


jQuery(document).ready( function()
{
  jQuery("#lightbox-picker-modal").dialog({autoOpen: false, width:"auto", height: "auto", position: "center", overlay:{opacity: 0.5, background: 'black'}, modal: true, open: function()
                     { 
                       jQuery("#lightbox-picker-modal").show();
                     }});
  jQuery("#add-single-photo-to-lightbox").click(function()
  {
    jQuery("#lightbox-picker-modal").dialog("open"); 
    return false;
  });
 
  jQuery('#message-window').dialog({resizable: false, draggable: false, height: "100px", autoOpen: false, position: "center", 
                                    open: function(){
                                     setTimeout( function() { jQuery("#message-window").dialog("close");  }, 3000)
                                    }
                                  })
  var form_html = jQuery("#new-lightbox-form-container").html();

  function show_message( message )
  {
      jQuery("#message-window").text( message );
      jQuery("#message-window").dialog("open");
  }

  function show_lightbox_form()
  {
    jQuery('#new-lightbox-form-container').html( jQuery('#hidden-lightbox-form').html()  );
    jQuery('#new-lightbox-form-container').dialog("open");
  }

  function add_photos_to_lightbox( photo_ids, url )
  {
     var lightbox_list = jQuery('#current_user_default_lightbox_id');
     var photo_id_query_string = ''
     if( typeof(photo_ids) == "number")
     {
       photo_id_query_string = "&photo_ids[]=" + photo_ids;
     } else {
       jQuery.each( photo_ids, function(i, photo_id)
       {
          photo_id_query_string += "&photo_ids[]=" + photo_id;
       });
     }
    
     jQuery.ajax({
        type: "POST",
        global: false,
        url: url,
        data: "id=" + lightbox_list.val() + photo_id_query_string + "&authenticity_token=" + jQuery('#token').val(),
        complete: function(transport)
        {
          var response = eval('[' + transport.responseText + ']')[0];
          if( response && !response.lightbox )
          {
            show_message( response ) // some kind of error
          } else {
            for( var photo_id in response.lightbox.photo_lightbox_links)
            {
              jQuery('#photo_' + photo_id).data('lightbox_ids', response.lightbox.photo_lightbox_links[photo_id]);
            }
            highlight_photos_for_selected_lightbox();
            var existing_text = lightbox_list.find('option:selected').text();
            if( existing_text != response.lightbox.name_and_count  )
            {
              lightbox_list.find('option:selected').text( response.lightbox.name_and_count);
              lightbox_list.effect('highlight', {}, 1000);
            } 

            var message = "";
            if( typeof(photo_ids) == "number" )
            {
              if( response.lightbox.total_added == 0 )
              {
                message = "This photo already exists in the lightbox."
              } 
            }

            if( typeof(photo_ids) == "object" )
            {
              if( response.lightbox.total_added == 0 )
              {
                message = "All selected photos exist in the lightbox."
              }
              
              if( response.lightbox.duplicates.length > 0 )
              {
                message = "" + response.lightbox.duplicates.length + " photo(s) are duplicates."
              } 
              
              if( response.lightbox.total_added > 0 )
              {
                message += " " +  response.lightbox.total_added + " photo(s) were added to the lightbox."
              }

            }
            if( message != "")
            {
              show_message( message )
            }

          }
        }
     }); 
  }
  jQuery('a.add-photo-lightbox').click( function()
  {
     var photo_id = jQuery(this).parents('li:first').attr("id");
     photo_id     = parseInt( photo_id.replace(/[^\d]/g,''), 10);
     add_photos_to_lightbox( photo_id, this.href );
     return false;
  });

  jQuery("#add_checked_to_lightbox").click( function()
  {
    var checked_photos = jQuery('.photo_subtext input:checked', jQuery('li.photo') );
    if( checked_photos.size() > 0 )
    {
      var photo_ids      = checked_photos.parents('li.photo');
      var real_photo_ids = [];
      jQuery.each( photo_ids, function(i, photo_id)
      {
        real_photo_ids.push( jQuery(photo_id).attr("id").replace(/[^\d]/g, ''));
      });

      add_photos_to_lightbox( real_photo_ids, this.href );
    }
    return false;
  });

  jQuery('#view-selected-lightbox').click( function()
  {
    var selected_lightbox_id = jQuery('#current_user_default_lightbox_id').val();
    window.location = jQuery('#lightbox_url_prefix').val() + '/' + selected_lightbox_id;
    return false;
  });

  function highlight_photos_for_selected_lightbox(){
    var selected_lightbox_id = jQuery('#current_user_default_lightbox_id').find("option:selected").val();
    jQuery.each(jQuery('li.photo'), function(){
      var this_guy = jQuery(this);
      var photo_is_in_current_lightbox = (this_guy.data('lightbox_ids').indexOf(parseInt(selected_lightbox_id, 10)) > -1);
      var pink = "#ee1166";
      var grey = "#1c1c1c";
      var bgyellow = "#222215";
      var bgpink = "#5f0729";
      var bggrey = "#1c1c1c";
      if(photo_is_in_current_lightbox){
        // this photo is in the selected lightbox
        //this_guy.animate({ borderBottomColor: pink, borderTopColor: pink, borderRightColor: pink, borderLeftColor: pink }, 1000);
        this_guy.animate({ backgroundColor: bgpink }, 1000);
        this_guy.addClass('highlighted');
      }else{
        // this photo is not in the selected lightbox
        //this_guy.animate({ borderBottomColor: grey, borderTopColor: grey, borderRightColor: grey, borderLeftColor: grey }, 1000);
        this_guy.animate({ backgroundColor: bggrey }, 1000);
        this_guy.removeClass('highlighted');
      }
    });
  }
  
  function write_selected_lightbox_to_cookie(){
    var selected_lightbox_id = jQuery('#current_user_default_lightbox_id').find("option:selected").val();
    createCookie('current_lightbox_selection', selected_lightbox_id, 364);
  }
  

  jQuery('#current_user_default_lightbox_id').change( function(){
    highlight_photos_for_selected_lightbox();
    write_selected_lightbox_to_cookie();
  });
  highlight_photos_for_selected_lightbox();
});
