  /*
   * Update the counters in the add to brochure boxes.
   */
    updateCounters = function(){
        var htmlnum = 0;
        if ($("#brochure").children(".box").length < 5) {
            $(".emptyHomeThumbBox").removeClass("hide");
            htmlnum = 5 - $("#brochure").children(".box").length;
            html = "<span>" + htmlnum + "</span>";
            $("#brochure").children(".emptyHomeThumbBox").html(html);
        }
        else if ($("#brochure").children(".box").length == 5) {
                $("#brochure").children(".emptyHomeThumbBox").addClass("hide");
        }
        if(htmlnum != 5)
        { //Call brochure code, if brochure has homes in it. -Drew 12/28
          if($("div#printPreview").length){
          //console.log($("div#printPreview").length);
            $.ajaxSetup({async: false});
            $.getScript("/javascript/printBrochure.js");
            $.ajaxSetup({async: true});
          }
          $("#printPreview").css("visibility","visible");
        }
        else{
          $("#printPreview").css("visibility","hidden");
        }
        if ($("#brochure").children(".mhcBox").length < 3) {
            $(".emptyCenterThumbBox").removeClass("hide");
            htmlnum = 3 - $("#brochure").children(".mhcBox").length;
             html = "<span>" + htmlnum + "</span>";
            $("#brochure").children(".emptyCenterThumbBox").html(html);
            //$(".emptyCenterThumbBox").html(3 - $("#brochure").children(".mhcBox").length);
        }
        else if ($("#brochure").children(".mhcBox").length == 3) {
                $("#brochure").children(".emptyCenterThumbBox").addClass("hide");
            }
    }
  
    //Remove brochure from the list
    remove = function(ev){
        ev.preventDefault();
        modelnum = $(ev.target).parent().attr("id");
        $(ev.target).addClass("ajaxImg");
        $.ajax({
            url: "cfc/AJAX.cfc",
            data: {
                method: "updateBrochure",
                model: modelnum,
                add: ev.data.add
            },
            error: function(){
              $(ev.target).removeClass("ajaxImg");
                //$ajImg.hide();
                alert("There was an error processing your request.  Please try again.")
            },
            success: function(data){
              $(ev.target).removeClass("ajaxImg");
                $("#brochure").children("#" + modelnum).remove();
                $(".favContainer").children("#" + modelnum).children(".addBtn").show();
                $(".favContainer").children("#" + modelnum).children(".remBtn").hide();
                
                //$(ev.target).parent().remove();
                updateCounters();
            }
        });
        
        return true;
    }

  /*
   * Add home to your brochure, caps 5 homes, 3 MHCs
   */
  brochureAdd = function(ev, ui){
      ev.preventDefault(); //prevents anchor from doing any default action
      modelnum = $(ev.target).parent().attr("id");
      $(ev.target).addClass("ajaxImg");
      $.ajax({
          url: "cfc/AJAX.cfc",
          data: {
              method: "updateBrochure",
              model: modelnum,
              add: ev.data.add
          },
          error: function(){
              $(ev.target).removeClass("ajaxImg");
              //$ajImg.hide();
              alert("There was an error processing your request.  Please try again.")
          },
          success: function(data){
              if(data == "false"){
                alert("Your brochure is full, there is a maximum 5 homes and 3 home centers per brochure.");
                $(ev.target).removeClass("ajaxImg");
                return false;
              }
              var tar = $(ev.target);
              tar.hide();
              tar.removeClass("ajaxImg");
              tar.siblings(".remBtn").show();
              if (typeof(ui) == "undefined") 
                  obj = tar.parent().clone();
              else 
                  obj = ui.draggable.clone();
              obj.children(".remBtn").bind("click", {
                  add: false
              }, remove);
              obj.css("visibility", "").children(".addBtn").hide();
              obj.css("visibility", "").children(".remBtn").show();
              //$("#brochure").children("#empty").hide();
              //$("#brochure").children("#" + obj.attr("id")).remove(); //remove duplicates and filler box
              
              //Is this obj a MHC or Model?
              if (!$("#brochure").children("#" + obj.attr("id")).length && (tar.parent().parent().attr("id") == "mhcContainer" || tar.parent().parent().hasClass('stealthMHCContainer'))) {
                  $("#brochure").children(".emptyCenterThumbBox:first").before(obj.removeClass().addClass('mhcBox'));
              }
              else 
                  if (!$("#brochure").children("#" + obj.attr("id")).length) {
                      $("#brochure").children(".emptyHomeThumbBox:first").before(obj.removeClass().addClass('box'));
                  }
              updateCounters();
          }
      });
      
      return true;
  }
   
$(function(){ 
	if( typeof brand == 'undefined' ){
		brand = {},
		brand.lang = 'EN'; //default parameter to english
	} 
 	var sels = ['.favContainer'], //selectable containers
    	$sels = $(sels.toString()),
		drops = ['#brochure'], //droppable containers
    	$drops = $(drops.toString()),
		langID = /es/i.test(brand.lang) ? 1 : 0,
		phrases = {
			addBrochure: "Add to brochure|Añadir al folleto".split("|"),
			remBrochure: "Remove from brochure|Remover de folleto".split("|")
		};
	
    //View more/collapse functionality for groups that have more than 3 homes
    var viewMore = $(".favContainer");
    for(var i =0; i < viewMore.length; i++){
      if(viewMore.eq(i).children().length > 3){
       viewMore.addClass("collapsed");        
       viewMore.eq(i).prev().append("<a class=\"more\" href=\"\">view all</a>");
      }
    }

    $(".more").bind("click", function(ev){
        ev.preventDefault();
        if ($(this).html() == "view all") {
            $(this).parent().next().removeClass("collapsed");
            $(this).html("collapse");
        }
        else {
            $(this).parent().next().addClass("collapsed");
            $(this).html("view all");
        }
    });
   
    $sels.show();
    $drops.show();
    //Add brochure buttons appropriately 
    $sels.children(".box, .mhcBox").append("<a class=\"btn addBtn\" href=\"/\">" + phrases["addBrochure"][langID] + "</a><a class=\"btn remBtn\">"+ phrases["remBrochure"][langID] +"</a>");
    $drops.children(".box, .mhcBox").append("<a class=\"btn addBtn\" href=\"/\">" + phrases["addBrochure"][langID] + "</a><a class=\"btn remBtn\">"+ phrases["remBrochure"][langID] +"</a>");
    $drops.children(".box, .mhcBox").children(".addBtn").css("display", "none"); //hide add buttons from brochure list
    //Register button click events
    $(".box, .mhcBox").children(".addBtn").bind("click", {
        add: true
    }, brochureAdd)
    	.bind("mouseover", function() { $(this).addClass('addBtnOVER') })
    	.bind("mouseout", function() { $(this).removeClass('addBtnOVER') });
    $(".box, .mhcBox").children(".remBtn").bind("click", {
        add: false
    }, remove)
    	.bind("mouseover", function() { $(this).addClass('remBtnOVER') })
    	.bind("mouseout", function() { $(this).removeClass('remBtnOVER') });
    //Dynamically assign add/remove buttons
    $sels.children(".box, .mhcBox").children(".remBtn").hide(); //hide remove buttons from favorites list
    $sels.children(".box, .mhcBox").children(".addBtn").show(); //hide remove buttons from favorites list
    updateCounters();

    $(".favContainer").children(".box").favorites({ //initialize the favorites ajax system
        bindAdd: "addFavorite",
        bindRem: "remFavorite",
        ajaxImgClass: "favLoading",
        ajaxImgSrc: "/brands/default/images/ajax-loader.gif",
        type: "af",
        remove: true
    });
    $(".favContainer").children(".mhcBox").favorites({ //initialize the favorites ajax system
        bindAdd: "addFavorite",
        bindRem: "remFavorite",
        ajaxImgClass: "favLoading",
        ajaxImgSrc: "/brands/default/images/ajax-loader.gif",
        type: "mhc",
        remove: true
    });
    
    //Set the correct hide/add buttons on the brochure section
    var indexes = new String();
    for (var i = 0; i < mBrochArr.length; i++) {
        indexes += "#" + mBrochArr[i] + ", ";
    }
    for (var i = 0; i < dBrochArr.length; i++) {
        indexes += "#" + dBrochArr[i] + ", ";
    }
    indexes = indexes.substr(0, indexes.length - 2);
    if(indexes.length > 0){
      $sels.children(indexes).children(".addBtn").hide();
      $sels.children(indexes).children(".remBtn").show();
    }
});
