window.inDrag = false;

var Shoplist = {
   delete_category_msg: "All category items will be permanently deleted along with the category.\n\nClick 'OK' to continue with delete.",   
   demo_user_msg: "Sorry, this option does not work for guest users.\n You may use it with your lists once you create an account.\n\nClick 'OK' to continue.",   
   demo_user_msg2: "Sorry, this option is available for registered users only.\n\nClick 'OK' to return.",   
   action_postfix: '_shoplist',
   delete_item_msg: "Deleting an item from a category is permanent.\n\nClick 'OK' to continue with delete.",
   current_action: null,

    hide_pop: function() {
        popdiv = top.window.document.getElementById('feedback');
        popdiv.style.display="none";
    },
    
    show_pop: function() {
        Element.show('feedback');
    },
    
    toggle_style_slection: function() {
      StyleRb = document.getElementById('list_style_rb');
      if(StyleRb.style.display == 'none')
      {
         new Effect.BlindLeftOut('list_style_rb');
      }
      else
      {
         new Effect.BlindLeftIn('list_style_rb');
      }
   },
  
   do_submit: function(form) {
      // calling form.submit seems to override the onsubmit handler...
      $(form).onsubmit();
   },
      
   forgot_password: function() {   
      Element.hide('forgot-link');
      Element.show('forgot_password');
      Field.focus('user_email');
   },
   

   cancel_change_email: function() {   
      Element.show('options');
      Element.hide('change_email_form');
   }, 

   cancel_change_password: function() {   
      Element.show('options');
      Element.hide('change_password_form');
   }, 
   
     
   show_new_category_form: function() {   
      //new Effect.BlindDown('new_category_form');
      Element.show('new_category_form');
      Element.hide('add-category-button');
   },

   cancel_new_category: function() {
      Form.reset('new-category-form');
      //new Effect.BlindUp('new_category_form');
      Element.hide('new_category_form');
      Element.show('add-category-button');
   },

   trim: function(str) {
       return str.replace(/^\s+|\s+$/g,'');
   },

   new_category_name_validation: function(FieldID) {
    FormField = document.getElementById(FieldID);
    var s=FormField.value.replace(/^\s+|\s+$/g,''); // my trim function
    if (s=='' || s=='Enter Category Name') { // my validation function
        alert("Please enter a valid name"); // field is not valid
        return false; // don't allow focus change
    }
    if (s!=FormField.value) FormField.value=s;
    return true;
   },  
   
   new_item_name_validation: function(NameFieldID, CommentFieldID) {
    FormField = document.getElementById(NameFieldID);
    CommentField = document.getElementById(CommentFieldID);
    var c=CommentField.value;
    var s=FormField.value.replace(/^\s+|\s+$/g,''); // my trim function
    if (s==''|| s=='Enter Item Name') { // my validation function
        alert("Please enter a valid name"); // field is not valid
        return false; // don't allow focus change
    }
    if (s!=FormField.value) {FormField.value=s;}
    if (c=='' || c=='Item Comment (Optional)') {CommentField.value='...';}
    return true;
   },   

   new_name_check: function(FieldID) {
    FormField = document.getElementById(FieldID);
    var s=FormField.value.replace(/^\s+|\s+$/g,''); // my trim function
    if (s=='') { // my validation function
        alert("Enter valid data"); // field is not valid
        FormField.value.focus();
        FormField.value.select(); // highlight field
        return false; // don't allow focus change
    }
    // field is valid, store trimmed/normalized version, if different
    if (s!=FormField.value) FormField.value=s;
    return true; // allow focus change
   },     
 
   on_new_category_loading: function() {
      setTimeout('document.images["loading_new_category"].src = "/images/wait.gif"', 100); 
      Element.hide('new_category_edit_buttons');
      Element.show('loading_new_category');
   },

   on_new_category_complete: function() {
      Element.show('add-category-button');
      Element.hide('loading_new_category');
      Form.reset('new-category-form');
      Element.hide('new_category_form');
      Element.show('new_category_edit_buttons');
   },

   show_user_item_move: function(UserItemID, UserCategoryID) {   
      Element.show('user_item_move_category_form_'+ UserItemID);
   },
   
   cancel_user_item_move: function(UserItemID, UserCategoryID) {   
      Element.hide('user_item_move_category_form_'+ UserItemID);
   },
      
   show_user_item_edit: function(UserItemID, UserCategoryID) {   
      Element.hide('item_name_' + UserCategoryID + '_' + UserItemID);
      Element.show('item_name_form_'+ UserItemID);
      Field.focus('user_item_name_'+ UserItemID );
      Field.select('user_item_name_'+ UserItemID );
   },

   cancel_user_item_edit: function(UserItemID, UserCategoryID) {
      Form.reset('list-item-form-' + UserItemID);
      Element.hide('item_name_form_'+ UserItemID);
      Element.show('item_name_' + UserCategoryID + '_' + UserItemID);
   },   
 
   on_user_item_edit_loading: function(UserItemID, UserCategoryID) {
      pbar = 'user_item_edit_loading_' + UserItemID
      setTimeout('document.images[pbar].src = "/images/wait.gif"', 100); 
      
      Element.hide('user_item_edit_buttons_' + UserItemID );
      Element.show('user_item_edit_loading_' + UserItemID);
   },
   
   on_user_item_edit_complete: function(UserItemID, UserCategoryID) {
      new Effect.Highlight('item_name_' + UserCategoryID + '_' + UserItemID);
   },
   
   show_user_category_edit: function(UserCategoryID) {   
      Element.hide('user_category_name_' + UserCategoryID);
      Element.show('category_name_form_'+ UserCategoryID);
      Field.focus('user_category_name_edit_'+ UserCategoryID);
      Field.select('user_category_name_edit_'+ UserCategoryID );
   },

   cancel_user_category_edit: function(UserCategoryID) {
      Form.reset('category-name-form-' + UserCategoryID);
      Element.hide('category_name_form_'+ UserCategoryID);
      Element.show('user_category_name_' + UserCategoryID);
   },   
 
   on_user_category_edit_loading: function(UserCategoryID) {
      pbar = 'category_edit_loading_' + UserCategoryID
      setTimeout('document.images[pbar].src = "/images/wait.gif"', 100); 
      
      Element.hide('category_name_edit_buttons_' + UserCategoryID );
      Element.show('category_edit_loading_' + UserCategoryID);
   },

   on_user_category_edit_complete: function(UserCategoryID) {
      new Effect.Highlight('item_name_' + UserCategoryID + '_' + UserItemID);
   },
   
   show_save_as_form: function() {   
      //new Effect.Highlight('name_form'); 
      Element.hide('newListBtn1');
      new Effect.BlindDown('list_save_as_form');
      Field.focus('shopping_list_name1');
   },
   
   show_import_save_as_form: function() {   
      //new Effect.Highlight('name_form'); 
      new Effect.BlindDown('list_save_as_form');
      Field.focus('shopping_list_name');
      Field.select('shopping_list_name');
   },

   cancel_save_as_form: function() {
      new Effect.BlindUp('list_save_as_form');
      Element.show('newListBtn1');
   },
   
   show_clean_list_form: function() {   
      new Effect.BlindUp('list_save_as_form');
      new Effect.BlindDown('clean_list_form');
      Field.focus('new_list_name');
   },

   cancel_clean_list_form: function() {
      new Effect.BlindUp('clean_list_form');
   },
   
   show_new_list_form: function() {   
      //new Effect.Highlight('name_form'); 
      Element.hide('newListBtn');
      new Effect.BlindDown('new_list_form');
      Field.focus('shopping_list_name');
   },

   cancel_new_list_form: function() {
      //Form.reset('new-category-form');
      Element.hide('new_list_form');
      Element.show('newListBtn');      
   },   

   show_share_list_form: function(cb) {   
      //Element.show('share_list');
      new Effect.BlindDown('share_list');
      Element.scrollTo('share_list');
   },
   
   on_share_list_complete: function() {
      var link_items = document.getElementsByClassName('public-url');
      Element.hide('share_form_cancel');
      Element.show('share_form_close');
      
      if(link_items[0].style.display == 'none')
      {
         Element.show.apply( Element, link_items );
         new Effect.Highlight('public_url');
         //$('share_form_close').innerHTML = 'Close'
      }
      else
      {
         Element.hide.apply( Element, link_items );
         //$('share_form_close').innerHTML = 'Close'
      }
      Element.show('sharing-update-message');
      new Effect.Highlight('sharing-update-message');
      
   },

   cancel_share_list_form: function() {   
      Form.reset('shopping-list-sharing-form');
      //Element.hide('share_list');
      new Effect.BlindUp('share_list');
   },

   close_share_list_form: function() {   
      new Effect.BlindUp('share_list');
      Element.hide('sharing-update-message');
   },

   show_edit_list_tags_form: function() {   
      Element.hide('list_tags');
      Element.show('edit_tags');
      //new Effect.BlindDown('edit_tags');
      Field.focus('tag_list_field');
      Field.select('tag_list_field');
   },
   
   cancel_edit_list_tags_form: function(ListID) {   
      Form.reset('shopping-list-tags-form-'+ ListID);
      Element.hide('edit_tags');
      Element.show('list_tags');
   },

   close_edit_list_tags_form: function(ListID) {   
      Element.hide('edit_tags');
      Element.show('list_tags');
      //new Effect.BlindUp('edit_tags');
   },
   
   on_edit_list_tags_loading: function() {
      Element.hide('tags_buttons');
   },

   on_edit_list_tags_complete: function(ListID) {
      Element.hide('tags_form_cancel');
      Element.show('tags_form_close');
      Element.show('tags_buttons');
      new Effect.Highlight('list_tags');
   },
         
   show_new_category_item_form: function(CategoryID) {   
      //new Effect.BlindDown('category_' + CategoryID + '_new_item_form');
      Element.show('category_' + CategoryID + '_new_item_form');
      Field.focus('category_' + CategoryID + '_new_item_name');
   },

   cancel_new_category_item: function(CategoryID) {
      Form.reset('new-category-item-form-' + CategoryID);
      //new Effect.BlindUp('category_' + CategoryID + '_new_item_form');
      Element.hide('category_' + CategoryID + '_new_item_form');
   },   
 
   on_new_category_item_loading: function(CategoryID) {
      pbar = 'new_item_loading_category_' + CategoryID
      setTimeout('document.images[pbar].src = "/images/wait.gif"', 100); 
      
      Element.show('new_item_loading_category_' + CategoryID);
      
   },
   
   on_new_category_item_complete: function(CategoryID) {
      Form.reset('new-category-item-form-' + CategoryID);
      Element.show('category_' + CategoryID + '_new_item_form');
   },

   toggle_category_edit_control: function(CategoryID) {
      var category_items = document.getElementsByClassName('edit_category_' + CategoryID);
      if(category_items[0].style.display == 'none')
      {
         Element.show.apply( Element, category_items );
         $('toggle_'+ CategoryID).innerHTML = 'Done with Delete'
      }
      else
      {
         Element.hide.apply( Element, category_items );
         $('toggle_'+ CategoryID).innerHTML = 'Delete Item'
      }
   },
   
   on_mark_item_chosen: function(ItemID, CategoryID) {
       new Effect.Fade( 'nonchosen_'+ CategoryID + '_' + ItemID, {
              afterFinish: function() { 
                 Element.remove('nonchosen_'+ CategoryID + '_' + ItemID);
              }
           });
        },
    
      
   mark_item_chosen: function(cb, ItemID) {
      Element.hide('item_'+ ItemID +'_check');
      Element.show('item_'+ ItemID +'_loading');
      this.do_submit( cb.form.id );
   },
     
   on_mark_item_nonchosen: function(ItemID, CategoryID) {
       new Effect.Fade( 'chosen_'+ CategoryID + '_' + ItemID, {
              afterFinish: function() { 
                 Element.remove('chosen_'+ CategoryID + '_' + ItemID);
              }
           });
        },
    
      
   mark_item_nonchosen: function(cb, ItemID) {
      Element.hide('item_'+ ItemID +'_uncheck');
      Element.show('item_'+ ItemID +'_uncheck_loading');
      this.do_submit( cb.form.id );
   },
   
   check_all_category_items: function(CategoryID, url) {
         var opts = {
            onComplete: function(data) {
               if( data.responseText == "SUCCEED" )
               {
                  new Effect.Highlight('category_' + CategoryID + '_chosen');
                }
               else
               {
                  alert('An error occured trying to check all items.\n\n'+ data.responseText );
               }
            }
         }
         // Send request to server...
         var req = new Ajax.Request(url, opts);         
   },
   
   user_category_toggle: function(cb, CategoryID) {
      this.do_submit( cb.form.id );
   },
     
   on_user_category_toggle: function(CategoryID) {
      new Effect.Highlight('user_categories_category_' + CategoryID);  
   },
   
   
   toggle_category_items: function(x, CatID) {
      if(x.className == 'open')
      {
         Element.show( 'category_items_'+ CatID );
         x.className = 'close'
      }
      else
      {
         Element.hide( 'category_items_'+ CatID );
         x.className = 'open'
      }
      //x.className = (x.className == 'open')?'close':'open'
   },
   
   toggle_list_categories: function(x, ListID) {
      if(x.className == 'open')
      {
         Element.show( 'list_categories_'+ ListID );
         x.className = 'close'
      }
      else
      {
         Element.hide( 'list_categories_'+ ListID );
         x.className = 'open'
      }
   },   
      
   set_item_attention_level: function(ListItemID, CategoryID, option, url) {
         var opts = {
            onComplete: function(data) {
                  new Effect.Highlight( option + '_'+ CategoryID +'_'+ ListItemID , {});
             }
         }
         // Send request to server...
         var req = new Ajax.Request(url, opts);
   },
   
   demo_user_message: function() {
         if( alert(this.demo_user_msg) ) {
         return
         }
   },
   
   demo_user_message2: function() {
         if( alert(this.demo_user_msg2) ) {
         return
         }
   },
   
   delete_user_category: function(UserCategoryID, url) {
         if( confirm(this.delete_category_msg) ) {
         var opts = {
            onComplete: function(data) {
               if( data.responseText == "SUCCEED" )
               {
                  new Effect.Fade( 'category_'+ UserCategoryID , {
                   afterFinish: function() { 
                     Element.remove('category_'+ UserCategoryID);
                   }
                  });
                }
               else
               {
                  alert('Failed to delete the list category (it may have been deleted already by a sharing user), please reload the list to continue.\n\n');
               }
            }
         }
         // Send request to server...
         var req = new Ajax.Request(url, opts);
         }         
   },
  delete_user_item: function(UserItemID, UserCategoryID, option, url) {
         if( confirm(this.delete_item_msg) ) {
         var opts = {
            onComplete: function(data) {
               if( data.responseText == "SUCCEED" )
               {
                  new Effect.Fade( option + '_'+ UserCategoryID +'_'+ UserItemID, {
                   afterFinish: function() { 
                     Element.remove(option + '_'+ UserCategoryID +'_'+ UserItemID);
                   }
                  });
                }
               else
               {
                  alert('Failed to delete the list item (it may have been deleted already by a sharing user), please reload the list to continue.\n\n' );
               }
            }
         }
         // Send request to server...
         var req = new Ajax.Request(url, opts);
         }         
   }   

}