var memberClass = function(name,id,premium,country){
  this.name = name;
  this.id = id;
  this.premium = premium;
  this.country = country;
};

var schoolArr = new Array();
var agentArr = new Array();

var replacedCharacters = function(st){
  st = st.replace("é","e");
  return st;
};

var sortByName = function (a, b, order) {
  if(order=="country")
  {
    return sortByCountry(a,b);
  }
  var nameA = replacedCharacters(a.name.toLowerCase());
  var nameB = replacedCharacters(b.name.toLowerCase());

  var modifier = a.premium == "Premium" ? -100000 : 0;
  //console.log(nameA);
  if (order == "az") {
      //console.log(nameA.localeCompare(nameB));
      return (a.premium == "Premium" && b.premium == "Premium") || (a.premium != "Premium" && b.premium != "Premium") ? nameA.localeCompare(nameB) : a.premium == "Premium" ? -1 : 1;
  }
  else {
      return (a.premium == "Premium" && b.premium == "Premium") || (a.premium != "Premium" && b.premium != "Premium") ? nameB.localeCompare(nameA) : a.premium == "Premium" ? -1 : 1;
    }
};

var sortByCountry = function (a, b) {
    var nameA = replacedCharacters(a.country.toLowerCase());
    var nameB = replacedCharacters(b.country.toLowerCase());

    var modifier = a.premium == "Premium" ? -1000 : 0;
    //console.log(nameA);

    return (a.premium == "Premium" && b.premium == "Premium") || (a.premium != "Premium" && b.premium != "Premium") ? nameA.localeCompare(nameB) : a.premium == "Premium" ? -1 : 1;

};

$(document).ready(function () {
        var agentName = "";
        var schoolName = "";
  
        $(".agentListing").each(function() {
          agentName = $(this).text().replace('"','');
          var id = $(this).attr('id').replace('listing_','');
          var country = $(this).find('.country').text().replace("'","").replace('"','');
          agentArr[agentArr.length] = new memberClass(agentName,$(this).attr('id').replace('listing_',''),'Light',country);
        });
  
        $(".schoolListing").each(function() {
          schoolName = $(this).find('.schoolTitle').text().replace('"','').replace("'","");
          var memberType = "Light";
          var id = $(this).attr('id').replace('listing_','');
          var country = $(this).find('.country').text().replace("'","").replace('"','');
          if($(this).find('.premiumStar').length > 0)
          {
            memberType = "Premium";
          }
          schoolArr[schoolArr.length] = new memberClass(schoolName,id,memberType,country);
        });
  
  
        for(var i=0;i<schoolArr.length;i++)
        {
          var thisCountry = schoolArr[i].country.toUpperCase();
          $("#schoolCountry option[value='" + thisCountry + "']").removeClass("notfound").addClass("found");
        }
        for(var i=0;i<agentArr.length;i++)
        {
          var thisCountry = agentArr[i].country.toUpperCase();
          $("#schoolCountry option[value='" + thisCountry + "']").removeClass("notfound").addClass("found");
        }
      $(".notfound").remove();
  
      $("#showSchools").live("click", function() {
      if($(this).attr('checked') == 'checked')
      {
         $("#schools").slideDown(500);
      } else
      {
         $("#schools").slideUp(500);
      }
    });
    $("#showFirst").live("change", function() {
      var first = $(this).attr('value');
      var schoolDiv = document.getElementById("schools");
      var agentDiv = document.getElementById("agents");
      document.getElementById("listings").removeChild(schoolDiv);
      document.getElementById("listings").removeChild(agentDiv);
      if(first == 'schools')
      {
        document.getElementById("listings").appendChild(schoolDiv);
        document.getElementById("listings").appendChild(agentDiv);
      }
      if(first == 'agents')
      {
        document.getElementById("listings").appendChild(agentDiv);
        document.getElementById("listings").appendChild(schoolDiv);
      }
    });
    $("#showAgents").live("click", function() {
      if($(this).attr('checked') == 'checked')
      {
         $("#agents").slideDown(500);
      } else
      {
         $("#agents").slideUp(500);
      }
    });
    $("#agentInbound").live("click", function() {
      if($(this).attr('checked') == 'checked')
      {
         $(".moreInfo").slideUp(300);
         $(".agentListing[name='Inbound']").show();
      } else
      {
         $(".moreInfo").slideUp(300);
         $(".agentListing[name='Inbound']").hide();
      }
    });
    $("#agentOutbound").live("click", function() {
      if($(this).attr('checked') == 'checked')
      {
         $(".moreInfo").slideUp(300);
         $(".agentListing[name='Outbound']").show();
      } else
      {
         $(".moreInfo").slideUp(300);
         $(".agentListing[name='Outbound']").hide();
      }
    });
  
  
    $(".agentListing").live("click", function() {
      var id = $(this).attr('id').replace('listing_', '');
        $.ajax({
          url: "/get-more-info.aspx?id=" + id,
          success: function(data) {
            $("#moreInfo_" + id).html(data);
            var logoDiv = $("#agent_" + id).find(".agentLogo");
            if($(logoDiv).find("img").length==0) {
              if($(logoDiv).html()!="") $(logoDiv).html('<img src="' + $(logoDiv).html() + '" />');
            }
            if($("#listing_" + id).hasClass("activeList")) {
            $("#" + id).animate({rotate: '0deg'}, 500);
              $("#moreInfo_" + id).slideUp(500);
              $("#listing_" + id).removeClass("activeList");
            } else {
            $("#" + id).animate({rotate: '90deg'}, 500);
            $("#" + id).css('-webkit-transform','rotate(-90deg', '-moz-transform', 'rotate(-90deg)', 'filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation=2)');
              $("#moreInfo_" + id).slideDown(500);
              $("#listing_" + id).addClass("activeList");
            }
          }
        });
    });
  
    $(".schoolListing").live("click", function() {
      var id = $(this).attr('id').replace('listing_', '');
        $.ajax({
          url: "/get-more-info.aspx?type=School&id=" + id,
          success: function(data) {
            $("#moreInfo_" + id).html(data);
            var picDiv = $("#school_" + id).find(".schoolLarge");
            if($(picDiv).find("img").length==0) {
              if($(picDiv).html()!="") $(picDiv).html('<img src="' + $(picDiv).html() + '" />');
            }
            var logoDiv = $("#school_" + id).find(".schoolLogo");
            if($(logoDiv).find("img").length==0) {
              if($(logoDiv).html()!="") $(logoDiv).html('<img src="' + $(logoDiv).html() + '" />');
            }
            if($("#listing_" + id).hasClass("activeList")) {
            $("#" + id).animate({rotate: '0deg'}, 500);
              $("#moreInfo_" + id).slideUp(500);
              $("#listing_" + id).removeClass("activeList");
            } else {
            $("#" + id).animate({rotate: '90deg'}, 500);
              $("#moreInfo_" + id).slideDown(500);
              $("#listing_" + id).addClass("activeList");
            }
          }
        });
    });
  
  $("#schoolCountry").live("change", function(){
    var selectedCountry = $(this).val().toLowerCase();
    for(var i=0;i<schoolArr.length;i++)
    {
      var thisCountry = schoolArr[i].country.toLowerCase();
      if(thisCountry==selectedCountry || selectedCountry=="any")
      {
        $("#school_" + schoolArr[i].id).show();
      }
      else
      {
        $("#school_" + schoolArr[i].id).hide();
      }
    }
    for(var i=0;i<agentArr.length;i++)
    {
      var thisCountry = agentArr[i].country.toLowerCase();
      if(thisCountry==selectedCountry || selectedCountry=="any")
      {
        $("#agent_" + agentArr[i].id).show();
      }
      else
      {
        $("#agent_" + agentArr[i].id).hide();
      }
    }
  });

  
  $("#alphabetical").live("change", function(){
    var order = $(this).attr('value');
    schoolArr.sort(function(a,b){return sortByName(a,b,order)});
    agentArr.sort(function(a,b){return sortByName(a,b,order)});
    
    arrangeItems("school",schoolArr);
    arrangeItems("agent",agentArr);
  });
});

var arrangeItems = function(prefix,arr){
  var objArray = new Array(arr.length);
  var listDiv = document.getElementById(prefix + "s");

  for(var i=0;i<arr.length;i++)
  {
      objArray[i] = document.getElementById(prefix + "_" + arr[i].id);
      listDiv.removeChild(objArray[i]);
  }
  for(var i=0;i<arr.length;i++)
  {
      listDiv.appendChild(objArray[i]);
  }
};

