var dataTable = null;

function DestroyDataTable(){
  if (dataTable != null){
    dataTable.fnClearTable();
    dataTable.fnDestroy();
  }
}

function UpdateDataTable(){
    
  dataTable = $('.datatable').dataTable( {
    "sPaginationType": "full_numbers",
    "bDestroy":true,
    "sDom": '<"top"fp>rt<"bottom"ip><"clear">',  
    "oLanguage": {
  "sSearch": "Filter Table Below "
    },
    "aaSorting": [[ 3, "asc" ]],
    "aoColumns": [ 
      /* Distance */ { "bSearchable": false,
                       "bVisible":    false },
      /* State */    { "bSearchable": true,
                       "bVisible":    true },
      /* City */     { "bSearchable": true,
                       "bVisible":    true },
      /* Area Covered */  { "bSearchable": false,
                       "bVisible":    true,
                       "sType": 'string-case'   },
      /* Address */    { "bSearchable": false,
                       "bVisible":    true },
      /* Hours */    { "bSearchable": false,
                       "bVisible":    true },
      /* Contact Info */    { "bSearchable": false,
                       "bVisible":    true },
      /* Equipment Offered */    { "bSearchable": false,
                       "bVisible":    true }
    ]
  });
  
  $('.dataTables_wrapper').addClass('clearfix');
  

}

$(document).ready(function(){

  $('.list > div:last-child').addClass('last');
  $("input[placeholder]").placeHeld();
  $('.specialbutton:nth-child(3n), tr > td:last-child, tr > th:last-child').addClass('last');
  
  $('.breadcrumb li:not(:last)').append(' > ');
        
  $('.tabs').tabs();
  
  $('.zebra tr:nth-child(2n)').addClass('even');
  
  
  if(typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, ''); 
    }
  }
  
  /* Define two custom functions (asc and desc) for string sorting */
  jQuery.fn.dataTableExt.oSort['string-case-asc']  = function(x,y) {
    var x = parseFloat(x.replace('Within','').replace('miles','').trim());
    var y = parseFloat(y.replace('Within','').replace('miles','').trim());
    return ((x < y) ? -1 : ((x > y) ?  1 : 0));
  };

  jQuery.fn.dataTableExt.oSort['string-case-desc'] = function(x,y) {
    var x = parseFloat(x.replace('Within','').replace('miles','').trim());
    var y = parseFloat(y.replace('Within','').replace('miles','').trim());
    return ((x < y) ?  1 : ((x > y) ? -1 : 0));
  };
  
  UpdateDataTable();
  $('#cycle').cycle({
    fit: 1,
    pager: '.cycle-nav',
    pagerEvent: 'mouseover',
    allowPagerClickBubble: 1,
    pagerAnchorBuilder: function(idx, slide) { 
          return '<a href="'+$(slide).attr('data-href')+'" class="thumb"><img src="'+$(slide).attr('data-thumb')+'" alt="'+$(slide).attr('alt')+'" /></a>'; 
       } 
  });    
  $('#cycle img').click(function (){
    document.location.href = $(this).attr('data-href');
  }).css('cursor', 'pointer');
  
  $('#cycle2').cycle({
    pager: '.cycle2-nav'
  });
  
  $('#cycle3').cycle({
    next:   '#next2', 
    prev:   '#prev2',
    cleartypeNoBg: true,
    height: 220
  });
  
  $('.popup').fancybox({
    padding: 0,
    margin: 0,
    'onComplete':loadPopup
  });
  
  $('.validate').validator({ messageClass: 'FormErrorMsg'}).attr('novalidate', 'novalidate');
  //$('input[required]').prev('label').append('* ');
     
  $(".CallUsLink").fancybox({
    scrolling:'no', 
    titleShow: false, 
    width:280, 
    height:255, 
    margin: 0, 
    padding: 0,
    'type'        : 'iframe'
  }); 
      
  function loadPopup() {     
    $('#fancybox-close').html('Close window <img src="images/popup_close.png" alt="close" />');
  }
   
  function closeCallUs() {
    $.fancybox.close();
  }  
});

function postEq(){
  document.getElementById("form_eq").submit();
}
