/**
 * functions for execution in project room sidebar
 */

jQuery(function($){    
   
    $("#mediaFiles").sortable({
        items: '> .mediaSort',
        distance: 10,
        scroll: true,
        scrollSensitivity: 200,
        scrollSpeed: 15,
        //containment: '#mediaFiles',
        start: function(e,ui) { 
            $("[id^=detailsSlider_]").hide();
            $('.media-separator').remove();
        },
        stop: function(e,ui) { 
          if ($('#mediaFiles').hasClass('sort8N')) {
            //$('#mediaFiles').find('div[@id^=mediaFiles_]:nth-child(8n)').after('<hr class="media-separator" style="clear:both;width:100%;color:#fff;background:#fff" />');
          } else if ($('#mediaFiles').hasClass('sort4N')) {
            $('#mediaFiles').find('div[@id^=normal_]:nth-child(4n)').after('<hr class="media-separator" style="clear:both;width:100%;color:#fff;background:#fff" />');
          } else {
            $('#mediaFiles').find('div[@id^=normal_]:nth-child(2n)').after('<hr class="media-separator" style="clear:both;width:100%;color:#fff;background:#fff" />');
          }
        },
        update: function(e, ui) { 
          if ($('#mediaFiles').hasClass('autoSave')) {
              $('#mediaFiles').sortable('disable');

              tb_show('','#TB_inline?a=1&height=50&width=220&inlineId=processingModal&modal=true',false);
              
              $.ajax({
                type: 'GET',
                url: '/locationScout/image/sort.php',
                data: $('#mediaFiles').sortable('serialize') + '&locationId=' + $('#mediaFiles').attr('rel'),
                complete: function (XMLHttpRequest, textStatus) { 
                  // resequence the binderList ids
                  $('#mediaFiles').find('div[@id^=normal_]').attr('id',function(idx){ return 'normal_' + (idx + 1); }).end()
                  .find('div[@id^=normal_]').attr('id',function(idx){ 
                    $(this).find('.result_counter').text('' + (idx + 1) + ' of ' + $('#mediaFiles > div[@id^=normal_]').length);
                    return 'normal_' + (idx + 1); 
                  });
                  
                  tb_remove();
                  $('#mediaFiles').sortable('enable');
                },

                //success: function (data,textStatus) {alert(data);},
                error: function (XMLHttpRequest, textStatus, errorThrown) { alert('Update failed. Please try again in a moment.') }
              }); 
          }
        }
    }); 

    $('#btnSaveSort').click(function() {
        $('#mediaFiles').sortable('disable');

        tb_show('','#TB_inline?a=1&height=50&width=220&inlineId=processingModal&modal=true',false);
        
        $.ajax({
          type: 'GET',
          url: '/locationScout/image/sort.php',
          data: $('#mediaFiles').sortable('serialize') + '&locationId=' + $('#mediaFiles').attr('rel'),
          complete: function (XMLHttpRequest, textStatus) { 
            // resequence the binderList ids
            $('#mediaFiles').find('div[@id^=normal_]').attr('id',function(idx){ return 'normal_' + (idx + 1); }).end()
            .find('div[@id^=normal_]').attr('id',function(idx){ 
              $(this).find('.result_counter').text('' + (idx + 1) + ' of ' + $('#mediaFiles > div[@id^=normal_]').length);
              return 'normal_' + (idx + 1); 
            });
            
            tb_remove();
            $('#mediaFiles').sortable('enable');
          },
          //success: function (data,textStatus) {alert(data);},
          error: function (XMLHttpRequest, textStatus, errorThrown) { alert('Update failed. Please try again in a moment.') }
        }); 
        return false;
    });
   
      $('#showHideHelp').click(function() {
          $("#top").slideToggle(200);
          
          if($(this).find('a').text() == 'Show Help') {
            $(this).find('a').text('Hide Help');
						$(this).find('a').addClass('toggleup');
            $.cookie('showHideHelp','true');
          } else {
            $(this).find('a').text('Show Help');
						$(this).find('a').removeClass('toggleup');
            $.cookie('showHideHelp',null);
          }
          return false;
      });
      
      if ($.cookie('showHideHelp') == 'true') {
          $('#showHideHelp').click();
      }
   
     
     /* show hide notes */ 
    $('.show-hide').click(function() {
        $("#detailsSlider_" + $(this).attr('rel')).slideToggle(300);
        if($(this).find('img').attr('src') == '/images/down.gif') {
          $(this).find('img').attr('src','/images/down.gif');
        } else {
          $(this).find('img').attr('src','/images/up.gif');
        }
        return false;
    });
        
      $('#btnDelete,#btnDownload,#btnShare').click(function() {
          if(!$('.mediaCheckbox[checked]').length) {
            alert('Please select at least one file.');
          } else {
            if ($(this).attr('id') != 'btnDelete' || confirm('Are you Sure?')) {
              $('#formMediaIds').attr('action', $(this).attr('rel')).submit();
            }
          }
          return false;
      });

      
      $('#btnEnableSortAutoSave').click(function() {
          $('#formMediaIds').attr('action', $(this).attr('rel')).submit();
      });
      
      $('#selectPrint').change(function() {
          $('#formMediaIds').attr('action', $(this).attr('rel')).submit();
      });
        
        $("a.sort-tooltip").tooltip({ 
            bodyHandler: function() { 
            return $($(this).attr("href")).html(); 
            }, 
            showURL: false 
        });

});

