// Make popups pop up
function popups() {   
  $('a[rel="popup"]').click( 
    function() {
      window.open( $(this).attr('href') );
      return false;
    }
  ).attr("title", "Opens in a new window");
}

/*// Creates collapsible tree navigation for subnav
function subnav() {
  if($('#subnav').length) {
    $("#subnav-tree").treeview({
      animated: "medium",
      persist: "location",
      collapsed: true,
      unique: true
    });
  }
}
*/

// Creates collapsible tree navigation for the documents page
function documentTree() {
  if($('ul#documents').length) {
    $("ul#documents").treeview({
      animated: "medium",
      persist: "location",
      collapsed: true,
      unique: true
    });
  }
}
// Sets questions to required in the survey based on a question
function conditionalValidation() {
  if($('li#ffacilities input')) {
    $("input#survey_facilities_0").change(
      function() {
        if($(this).attr('checked')) {
          $('#industry, #sqft').addClass('required');
        }
      }
    );
    $("input#survey_facilities_1").change(
      function() {
        if($(this).attr('checked')) {
          $('#industry, #sqft').removeClass('required');
        }
      }
    );
  }
}

function faceboxSurveyLink()
{
  if($('#survey-link a').length) {
    $('#survey-link a').click(
      function() {
        jQuery.facebox(
          function() {
            jQuery.get(
              '/survey/?ajax', 
              function(data) {
                jQuery.facebox('<div id="survey">' + data + '</div>');
                faceboxSurvey();
              }
            );
          }
        );
        return false;
      }
    );
  }
}

function faceboxSurvey()
{
  $("#facebox #survey form").ajaxForm({
    target: '#survey',
    url: '/survey/?ajax',
    success: surveyResponse,
    beforeSubmit: disableSurvey
  });
  conditionalValidation();
}

function disableSurvey()
{
  $("#facebox #survey form button").text('Sending... Please wait.').attr('disabled', 'disabled');
}

function surveyResponse(responseText, statusText)  { 
  $.scrollTo('#facebox', { duration: 1000 });
  faceboxSurvey();
}

function setupOutboundLinks()
{
	$('a[rel=outbound]').click(
		function() {
			var obj = $(this);
			try {
				var pageTracker=_gat._getTracker("UA-3522372-2");
				pageTracker._trackEvent("Outbound Links", obj.attr('href'));
				setTimeout('document.location = "' + obj.attr('href') + '"', 100)
				return false;
			} catch(err) {}
		}
	);
}

function doCufon() {
  // Cufon.replace(".iframe h2");
  // Cufon.replace("h6");
  // Cufon.replace("h1.chapter_title");
  // Cufon.replace("h1");
  // Cufon.replace(".header span.title_header");
  // Cufon.replace(".shortTeaser .title");
  // Cufon.replace(".latestNewsRss h2 span");
  // Cufon.replace(".newsList h2 span");
  // Cufon.replace(".newsViewer .h2Container h2");
  // Cufon.replace(".teaser h2");
  // Cufon.replace(".functionIndex h2");
  // Cufon.replace(".rangeDatasheetViewer h2");
  // Cufon.replace(".optimisticTitleContainer h2");
  // Cufon.replace(".agenda .h1Container h2");
  // Cufon.replace(".managerList .h1Container h2");
  // Cufon.replace(".managerViewer .h1Container h2");
  // Cufon.replace(".releaseList h2 span");
  // Cufon.replace(".releaseViewer .h1Container h2");
  // Cufon.replace(".documentList .h1Container h2");
  Cufon.replace("#masthead h2, h2.pageTitle, .verticalTeaserList h3, .teaser h3, #globallinks li.sifr-replace, div.domains h3, body.homepage h2");
}

doCufon();
jQuery(setupOutboundLinks);
jQuery(popups);
jQuery(conditionalValidation);
jQuery(faceboxSurveyLink);
jQuery(documentTree);