$(function(){
  $('.home_banner').bannerIt();
  
  //Select all function for checkbox fields
  $('.check_select_all').click(function(e){
	$(this).closest('fieldset').find('.checkbox_list').find('input[type="checkbox"]').attr('checked', true);
	e.preventDefault();
  });
  
    //Clears input field when user focuses on Username and Password
  $('#username').bind({
    focus: function(){if ($(this).val() == 'Username'){$(this).val('');}},
    blur: function(){if ($(this).val().length < 1){$(this).val('Username');}}
  });
  
  $('#password').bind({
    focus: function(){if ($(this).val() == 'Password'){$(this).val('');}},
    blur: function(){if ($(this).val().length < 1){$(this).val('Password');}}
  });
    
    ///////////////////////////////////
  //Begin script for Reports module//
  ///////////////////////////////////
  
  //Reports range datepicker
  var dates = $( "#date_from, #date_to" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      maxDate: 0,
      onSelect: function( selectedDate ) {
        var option = this.id == "date_from" ? "minDate" : "maxDate",
          instance = $( this ).data( "datepicker" );
          date = $.datepicker.parseDate(
            instance.settings.dateFormat ||
            $.datepicker._defaults.dateFormat,
            selectedDate, instance.settings );
        dates.not( this ).datepicker( "option", option, date );
      }
    });
    
  $('.datepicker').datepicker({
    dateFormat: 'yy-mm-dd'    
  }); 

  //Reset button
  $('#sidebar .finder .pane input[type="reset"]').click(function(){
    $(this).closest('.pane').find('input[type="text"]').val('');
    $(this).parent().find('input[type="checkbox"]').removeAttr('checked').next('label').removeClass('selected');
  });

  //Ellipsis for too long extracts
  ellipsis();

  //Script for the Quicksand sorting plugin 
  $('.reports').clone().appendTo('#main_content').removeClass().addClass('source').hide();
  
  var check_cat = [];
  var sortData = [];
  //sortNow();
  
  $('.finder .pane:not(".date") input').bind('change keypress', function(){
      //sortNow();
  });
  
  //Animation for column extract to appear on hover
  $('.reports').delegate('.col', 'hover', function(){
    $(this).find('.hover').stop(true,true).toggleClass('show', 500);
  });
  
  //Sets label class when category is selected on page load
  $('.finder .category input:checked, .reports .sidebar .status input:checked').next().addClass('selected');
  
  //Sets label class when category is clicked 
  $('.finder .category label').click(function(){
    var t = $(this);
    t.toggleClass('selected');
  });
  
  //Script for Filters accordion
  var sidebar_title = $('.finder .title');
  
  sidebar_title.addClass('selected').next('.pane').show();
  
  sidebar_title.click(function(){
    var t = $(this);
    t.next('.pane').slideToggle(400);
    t.toggleClass('selected');
  });
  
  //Reports page initiate report update on start
  if ($('.reports').length > 0){
    updateReport();
    $('.datepicker').change(function(){
      updateReport();
    });
  }
  
  ////////////////////////
  //-----Functions------//
  ////////////////////////
  
  //Function for ellipsis
  function ellipsis(){
    $('p.extract').wrapInner('<span class="ellipsis_text">');
    $('p.extract').ThreeDots({max_rows:4});
  }
  
  //Function for report sorting
  function sortNow(){
      sortData = [];

      //Returns checked categories to check_cat
      $('.finder .pane.category input:checked').each(function(i){
          var c = $(this).attr('data-cat');
          return check_cat[i] = c;          
        });
      
      var check_keywords = $('.finder .pane.search input').val();
      
      if (check_keywords.length < 1){
        check_keywords = ' '; 
      }
      
      $('.source .col').each(function(){
        var col_cat = $(this).attr('data-cat');
        var col_keywords = $(this).html();
        
        var contains = $(col_keywords).is(':contains(' + check_keywords + ')');
        if(check_cat.indexOf(col_cat) != -1 && contains == true){
          var t = $(this);
          sortData.push(t[0]);
        }
      });
      
      $('.reports').quicksand(sortData);  
  }
  
  //Function for report updating
  function updateReport()
  {
    /*$.post(ajaxReportUrl,{
        ds: $('#date_from').val(),
        de: $('#date_to').val(),
        q: $('#q-search').val()
      },function(txt){
        if(txt!=''){
          $('.reports, .source').html(txt);
          ellipsis();
          //sortNow();
        }
      });*/
  }
});
