// Init behaviors
$(document).ready(function() {
  assignClientsBehavior();
  assignSearchBehavior();
  assignPriceBehavior();
  assignLeftMenuBehavior();
  assignSearchPlaceholder();
});

// Clients page behaviors
function assignClientsBehavior() {
  $("a[id*=element]").click(function() {
    $(this).toggleClass("active");
    var id = $(this).attr("id");
    $("div[id*=element]:not(div#"+id+"-text)").slideUp("fast");
    $("a[id*=element]:not(a#"+id+")").removeClass("active");
    $("#"+id+"-text").slideToggle("fast");
    return false;
  });
  
  $("a.popup").click(function() {
    var href = $(this).attr("href");
    var w = window.open(href, "Billmaster", "status=0,toolbar=0,location=0,menubar=0,width=800,height=570");
    return false;
  });
}

// Search block behavior
function assignSearchBehavior() {
  $("form div.next1 a").click(function() {
    $("form[name=search]").submit();
    return false;
  });
}

function assignPriceBehavior() {
  if ($("input[name*=form_checkbox_billmaster]").attr("checked")) {
    $("input[name*=checkbox_bm]")
      .removeAttr("checked")
  }
  
  if ($("input[name*=form_checkbox_storemaster]").attr("checked")) {
    $("input[name*=checkbox_sm]")
      .removeAttr("checked")
  }
  
  if ($("input[name*=form_checkbox_tower]").attr("checked")) {
    $("input[name*=checkbox_tt]")
      .removeAttr("checked")
  }
  
  $("div[class*=checkboxArea]").live("click", function() {
    var check = $(this).next();
    if (check.attr("name") == "form_checkbox_billmaster[]") {
      if(check.attr("checked")) {
        $("input[name*=checkbox_bm]").removeAttr("disabled");
      } else {
        $("input[name*=checkbox_bm]")
        .removeAttr("checked")
        .attr("disabled","disabled");
      }
    }
    
    if (check.attr("name") == "form_checkbox_storemaster[]") {
      if(check.attr("checked")) {
        $("input[name*=checkbox_sm]").removeAttr("disabled");
      } else {
        $("input[name*=checkbox_sm]")
        .removeAttr("checked")
        .attr("disabled","disabled");
      }
    }
    
    if (check.attr("name") == "form_checkbox_tower[]") {
      if(check.attr("checked")) {
        $("input[name*=checkbox_tt]").removeAttr("disabled");
      } else {
        $("input[name*=checkbox_tt]")
        .removeAttr("checked")
        .attr("disabled","disabled");
      }
    }
    
  });  
  
}

// Behaviors for the left menu in Products section
function assignLeftMenuBehavior() {
  $(".sub_menu p.active-trail + ul").show();
  $(".sub_menu ul ul.active-trail").show();
}

// Placeholder for the search field
function assignSearchPlaceholder() {
  $('input.search').example('Поиск по сайту');
}