//jQuery.noConflict();

jQuery(function(){
  
  if(first_time){
    jQuery.facebox({ajax:"/docs/splash"});
  }
  //pagination links
  $('.pagination a').live('click', function() {
    show_load();
    $.ajax({
      type: 'get',
      url: $(this).attr('href'),
      success: function(data){
        stop_load();
        $("#data").html(data);
      }
    });
    return false;
  });
  
  $('a.ajax').live('click', function() {
    show_load();
    var me = $(this);
    var div = me.attr('target');
    $.ajax({
      type: 'get',
      url: me.attr('href'),
      success: function(data){
        stop_load();
        if(me.attr('target').length > 0){ 
          $(div).html(data);
        }
      }
    });
    return false;
  });
  
  $('a.shortlist').live('click', function() {
    show_load();
    var me = $(this);
    $.ajax({
      type: 'get',
      url: me.attr('href'),
      success: function(data){
        stop_load();
        //still need to update the count
        if(me.text() == "Shortlisted (remove)"){
          me.text("Add to shortlist");
        }else{
          me.text("Shortlisted (remove)");
        }
      }
    });
    return false;
  });
  
  


  $('form.ajax').live('submit', function() {
   var f = $(this);
   var div = f.attr('target');
   $.ajax({
     type: f.attr('method') || "POST",
     url: f.attr("action"),
     data: f.serialize(),
     success: function(data){
       stop_load();
       $(div).html(data);
     }
   });
   return false;
  });

  

  //ajax links
  $('#searchform').change(function() {
    search_submit();
  });
  
  $('.info_toggle').live('click',function(){
    var me = $(this);
    var root = me.closest('.rt-white');
    if(me.text() == "More"){
      me.text("Less");
      me.addClass("less");
      root.find('.more-info-box').show();
    }else{
      me.text("More");
      me.removeClass("less");
      root.find('.more-info-box').hide();
    }
  });

});


jQuery.ajaxSetup({
  'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript")}
});


function search_submit(){
  show_load();
  var f = $('#searchform');
  var div = f.attr('target');
  $.ajax({
    type: f.attr('method') || "POST",
    url: f.attr("action"),
    data: f.serialize(),
    success: function(data){
      stop_load();
      $(div).html(data);
    }
  });
}
function stop_load(){
  $('#spinner').hide();
  
}

function show_load(){
  $('#spinner').show();
  
}

function UpdateSliderNumbers(value,large_id,small_id,multiplier,decimal_places){
  // Set default values
  multiplier = typeof(multiplier) != 'undefined' ? multiplier : 1;
  decimal_places = typeof(decimal_places) != 'undefined' ? decimal_places : 1;
  
  // Perform the calculation
  if(decimal_places > 0){
    large_val = (value/multiplier).toFixed(decimal_places)
  }
  else {
    large_val = Math.floor(value/multiplier)
  }
  $(large_id).html(large_val);
  
  
  if($(small_id).length != 0 ){
    if($(small_id) != null) {
      $(small_id).html(Math.floor(value%multiplier));
    }
  }
}
