// This function will be called on DOM ready
$(function() {
  linkifyFeatureBoxes();
  initSlideshow();
  attachValidationToFormSubmits();
});

function collapseUnselectedSubmenus() {
  $("ul.navigation_sub ul").each(function() {
    var ul = $(this);
    if(ul.find("a.selected").size() == 0 && ul.closest("li").find("a.selected").size() == 0)
      ul.hide();
  });
}

// Make the sidebar line up with the rest of the content if there aren't any feature boxes.
function performHeightEqualization() {
  if($("div.main_with_subnav").size() > 0 && $("div.feature").size() == 0) {
    equalHeight($("div.main_with_subnav div.body, div#sidebar div.content"));
  } else {
    //if there are feature boxes, we have to take them into account, and it gets a little more complicated
    var body = $("div.main_with_subnav div.body").get(0);
    if(body && body.offsetTop) {
      var feature = $("div.feature div.content").get(0);
      var height = body.offsetHeight + feature.offsetHeight + 22;
      var sidebar = $("div#sidebar div.content");
      if(height >= sidebar.height()) {
        sidebar.height(height);
      } else {
        $(body).height(sidebar.height() - feature.offsetHeight - 14);
      }        
    }
  }  
}

// images take longer to load and complicate the equation, so we wait 'til they've loaded
function equalizeContentAndSidebarHeights() {
  setInterval(performHeightEqualization, 50);
}

function initSlideshow() {
	$("div#slideshow ul").innerfade({
		speed: "slow",
		timeout: 4000,
		type: "sequence",
    containerheight: "190px"
	}); 
	
	$("div#slideshow img.arrow").hover(function() {
	    var src = $(this).attr("src");
	    $(this).attr("src", src.substring(0, src.indexOf("arrow.png")) + "arrow_over.png");
	  }, function() {
	    var src = $(this).attr("src");
	    $(this).attr("src", src.substring(0, src.indexOf("arrow_over.png")) + "arrow.png");
	  });
}

function linkifyFeatureBoxes() {
  $("div#network_solutions").bind("click", function() { window.location.href = "/ai/solutions/category/ad_networks/"; });
  $("div#advertiser_solutions").bind("click", function() { window.location.href = "/ai/solutions/category/advertisers_agencies/"; });
  $("div#search_solutions").bind("click", function() { window.location.href = "/ai/solutions/category/search_engines/"; });
  $("div#customers").bind("click", function() { window.location.href = "/ai/resources/category/customer_testimonials/"; });
  $("div#resources").bind("click", function() { window.location.href = "http://www.prweb.com/releases/2010/03/prweb3672354.htm"; });
  
  $("body#home div.feature").not(".major").hover(function() { 
    $(this).addClass("feature_over");
  }, function() {
    $(this).removeClass("feature_over");
  });
  
  $("body#home div.major").hover(function() { 
    $(this).addClass("feature_major_over");
  }, function() {
    $(this).removeClass("feature_major_over");
  });  
}

/* thanks to http://www.cssnewbie.com/equal-height-columns-with-jquery/ for the idea for this */
function equalHeight(group) {
    var tallest = 0;
    group.each(function(i) {
        thisHeight = $(this).height();
        if(i == 1)
          thisHeight -= 6;
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.eq(0).height(tallest);
    group.eq(1).height(tallest + 6);
}

function attachValidationToFormSubmits() {
  $("form input[type=submit]").bind("click", validateForm);
}

/* Performs validation by making sure that the sibling of every label
 * with class "required" has been filled out. Cancels the form submit if any
 * required fields haven't been changed from their default settings, and adds
 * class "error" to the labels for those not-yet-filled-out forms. */
function validateForm() {
  var success = true;
  var empty_fields = [];
  $("form label.required").siblings().each(function() {
    if((this.tagName.toLowerCase() == "input" && this.value == "") ||
      (this.tagName.toLowerCase() == "select" && this.selectedIndex == 0)) {
      success = false;
      empty_fields.push($(this).siblings("label"));
    }
  });
  
  if(!success) {
    $("form label").removeClass("error");
    $.each(empty_fields, function() { this.addClass("error"); });
    alert("All fields except for 'Comments' are required. Please fill out any fields that may have accidentally been left blank. Thanks!");
    return false;
  }
}
