/*
  formatting modifications apply to the following elements:
  .empty and dd.empty - removes parent element
  .format_commas - adds a space after the comma
  .format_semicolon - replaces semicolons with commas (placed after the format commas script)
  .rating_out_of_100 - makes a rating out of one hundred become a rating out of ten
  .truncate
*/

$(document).ready(function(){

// populate login inputs with sample text.
  $("#SQ_LOGIN_USERNAME").val("username");
  $("#SQ_LOGIN_PASSWORD").val("******");

  $("#SQ_LOGIN_USERNAME").focus(function(){
    $("#SQ_LOGIN_USERNAME").val("");
    $("#SQ_LOGIN_PASSWORD").val("");
  });

    // Insert jquery sort function
    jQuery.fn.sort = function() {
        return this.pushStack( jQuery.makeArray( [].sort.apply( this, arguments ) ) );
    }; 
// hide parent if its child .empty is empty
  $(".empty:empty").parent().remove();


/* rate venues out of 10 not 100 
$("table.venue tr td").children("span.rating_out_of_100").each(function() {
  x = parseFloat($(this).text()) / 10;
  $(this).text(x);
}); 
*/

$("span.rating_out_of_100").each(function() {
  $(this).text(parseFloat(($(this).text()) / 10));
});   



jQuery.fn.sort = function() {
  return this.pushStack( [].sort.apply( this, arguments ), []);
};

// truncations
// remove child elements because truncate plugin is buggy
var content = $('.sidebar_feature .truncate_200').text();
$('.sidebar_feature .truncate_200').html(content); 

//    $(".sidebar_feature p.summary").truncate( 120, {
//        chars: /\s/,
//        trail: [ " <a href='#' class='truncate_show'>+</a> "]
//    });


    $(".truncate_50").truncate( 50, {
        chars: /\s/,
        trail: [ " ... "]
    });

    $(".truncate_100").truncate( 100, {
        chars: /\s/,
        trail: [ " ... "]
    });


    $(".truncate_200").truncate( 200, {
        chars: /\s/,
        trail: [ " ... "]
    });


// Place a space after a comma
var allNeededDiv = document.getElementsByTagName("span");
for(var jh=0; jh<allNeededDiv.length; jh++)
{
  if(allNeededDiv[jh].className.indexOf("format_commas")!=-1)
  {
    if(allNeededDiv[jh].innerHTML.indexOf(",")!=-1)

    {
      var tempVal = allNeededDiv[jh].innerHTML;
      tempVal = tempVal.split(",");

      var tempLength = tempVal.length;
      var tempValFinal = "";
      for(var rr=0; rr<tempLength; rr++)
      {
        tempValFinal = tempValFinal+tempVal[rr]+", ";
      }
      tempValFinal = tempValFinal.slice(0,tempValFinal.lastIndexOf(","));
      allNeededDiv[jh].innerHTML=tempValFinal;
    }
  }
}


// replace semi-colons with commas

var allNeededSpan = document.getElementsByTagName("span");

for(var jh=0; jh<allNeededSpan.length; jh++)
{
  if(allNeededSpan[jh].className.indexOf("format_semicolon")!=-1)
  {
    if(allNeededSpan[jh].innerHTML.indexOf(";")!=-1)
    {
      var tempVal = allNeededSpan[jh].innerHTML;
      tempVal = tempVal.split(";");
      var tempLength = tempVal.length;
      var tempValFinal = "";
      for(var rr=0; rr<tempLength; rr++)
      {
        tempValFinal = tempValFinal+tempVal[rr]+", ";
      }
      tempValFinal = tempValFinal.slice(0,tempValFinal.lastIndexOf(","));
      allNeededSpan[jh].innerHTML=tempValFinal;
    }
  }
}


// Restrict Straight To form selections

$("#queries_region_query_side option:not(.first)").click(function(){
  var selected = $(this).text();
  $("#queries_suburb_query_side optgroup.selected").removeClass("selected");
  $("#queries_suburb_query_side optgroup").filter(function(){return $(this).attr("label") == selected;}).addClass("selected");
  $("#queries_suburb_query_side optgroup").hide();
  $("#queries_suburb_query_side optgroup.selected").show();
});

$("#queries_region_query_side option.first").click(function(){
  $("#queries_suburb_query_side optgroup.selected").removeClass("selected");
  $("#queries_suburb_query_side optgroup").show();
});



// Restrict Restaurant Initial Search for selections

$("#category_test option:not(.first)").click(function(){
  var selected = $(this).text();
  $("#queries_suburb_query_side optgroup.selected").removeClass("selected");
  $("#queries_suburb_query_side optgroup").filter(function(){return $(this).attr("label") == selected;}).addClass("selected");
  $("#queries_suburb_query_side optgroup").hide();
  $("#queries_suburb_query_side optgroup.selected").show();
});

$("#category_test option.first").click(function(){
  $("#queries_suburb_query_side optgroup.selected").removeClass("selected");
  $("#queries_suburb_query_side optgroup").show();
});


// pagination
  var no_links = $("p.pagination span.result_page_index a").size();
  if (no_links == 0) $("p.pagination").remove();




});
