$(document).ready(function() {
  if (typeof(jQuery.fn.overlabel) == "function") {
  	$("label.overlabel").overlabel();
  }
  
  if (typeof(jQuery.fn.equalHeights) == "function") {
  	$("#footer_siteinfo .siteinfo, #footer_siteinfo .security").equalHeights();
    $("#product_benefits li").equalHeights();
    $("#offers .box").equalHeights();
    $(".affiliate").equalHeights();
  }
  
  if (typeof(jQuery.fn.makeacolumnlists) == "function") {
  	$("#product_benefits_list")
		  .removeAttr("id")
      .wrap($('<div id="product_benefits_list"></div>'))		  
		  .makeacolumnlists({cols: 2,colWidth: 284,equalHeight: false,startN: 1});
  }
    
  printLink();  
  
  if (typeof(jQuery.facebox) == "function") {
    popup("a[rel=security],a[rel$=sample_report]");
  }
  
  newWindowLinks();
      
});

/**
 * Sets up external links and links that open PDF's
 */
function newWindowLinks() {   
  //Automagically open all external links in a new window
  $("a[href^='http']:not(a[href*='privacymatters.com/']):not(a[href*='Offers/'])")
    .click(function(){
      window.open($(this).attr("href"));
      return false;  
    });
    
  //Open PDF links in new window without any chrome
  $("a.pdf")
    .click(function(){
      window.open($(this).attr("href"), "pdfwin", "directories=no,menubar=no,status=no,titlebar=no,toolbar=no,location=no");
      return false;
    });
}

/**
 * Adds a "print" link to the page_actions component
 */
function printLink() {
  $(".page_actions li:first-child").after('<li class="print"><a><img src="/images/page_actions_print.png" width="54" height="16" alt="print" /></a></li>'); 
  /* normally, one should just be able to include the 'href="#"' in the HTML above, but our .NET environment has some weird behaviour where it overwrites the href with the current pages URL if the href doesn't look like a regular URL. So, in IE 7 and below, the URL was being changed from "#" to (for example) "http://privacymatters.com/about-us/about-us.aspx#". So, whenever you clicked on the print link, the page would refreash in a new window after the print dialogue closed. */
  $(".print a").attr("href", "#").click(function(){ window.print(); return false;  });
}

/**
 * Manages all of the popup windows using facebox (inspired by headscape.com. Thanks Paul Boag)
 */
function popup(Elem) {
  //simple sanity check
  if (!$(Elem).attr("rel")) { return; }
  
	$(Elem).click(function(){  
    var link = this;
    var theUrl = this.href;
	  var addr = (theUrl + " #content_main" + " > *");  //I heart jquery for this feature alone

    $.facebox.settings.opacity = 0.5;
    $.facebox.settings.loadingImage = '/assets/facebox/loading.gif';
    $.facebox.settings.closeImage = '/assets/facebox/closelabel.gif'; 
    
    $.facebox(function() {
      
      var c;
      if (link.rel == "security") {
        c = $('<div class="security"></div>');  
      } 
      else if (link.rel.indexOf("sample_report") > 0) {
        c = $('<div class="' + link.rel + ' sample_report"></div>'); 
      }  
      
      $(c).load(addr, function(){
        
        //remove non-essential elements from popup
        $(c).find("#breadcrumb").remove();
        $(c).find(".page_actions").remove();
        
        //add link that takes user to "page" version
        $(c).append('<a class="leave_popup" href="' + theUrl + '">View printable version of this page</a>');
        
        //sample report popup tweaks
        /*if (link.rel == "sample_report") {
          //$(c).find("h1").remove(); //for some reason, removing the h1 causes the container to expand down the height of the image
        }*/
        
        $.facebox(c);
      });
    });
  
		return false;
	});
}




