$(document).ready(function() {
   
   // activate contact button
   contactButton();
   
   // register AJAX modals
   setupModals();
   
   // setup external links
   setupExternalLinks();
   // setup Google Analytics on URL 
   // fragments, document downloads,
   // and mailto links
   setupGA();
});

function contactButton() {
    
    $('a.contact-us').mouseover(function() {
        $('div.contact-us-button-content').show();
        $(this).addClass('contact-us-hover');
    });
    
    $('div.contact-us-button').mouseleave(function() {
        $('div.contact-us-button-content').hide();
        $('a.contact-us').removeClass('contact-us-hover');
    });
    
}

function setupModals() {
   
   $('a.ajax-modal').attr('rel', '#overlay');
   
   // Setup a basic iframe for use inside overlays.
   var theframe = $('<iframe frameborder="0" scrolling="no"></iframe>');
   
   $('a.ajax-modal').overlay({
         top: 20,
         fixed: false,
         onBeforeLoad: function() {
            
            // grab wrapper element inside content
            var wrap = this.getOverlay().find(".contentWrap");

            if (wrap.is(":empty")) {
               // load the page specified in the trigger
               //wrap.load(this.getTrigger().attr("href"));
               var link = this.getTrigger().attr("href");
               
               //Add the link and style attributes to the basic iframe
               $(theframe).attr({ src: link, style: 'height:540px; width:790px; border:none;' });
               //Write the iframe into the wrap
               wrap.html(theframe);
            }
            
   		}

   	});
   
}

function setupExternalLinks() {
   
   var regex_loc_url = /(^$)|(^\/.*)|(^#(.*))$/;
   var regex_mailto_url = /^mailto:(.*)$/;
   var regex_doc_url = /^.+\.(pdf|doc|docx|xls|xlsx|ppt|pptx|txt)$/i;
   

   $('a').each(function() {
      
      // skip over nyromodal links
      if($(this).hasClass("ajax-modal")) {
         
         return false;
         
      }
      
      var href = $(this).attr("href");
      
      if (href) {

         if ((!(regex_loc_url.test(href)) || // is this a root-relative url?
            (regex_mailto_url.test(href)) || // is this a mailto? 
            (regex_doc_url.test(href)) // is a download link?
         
         )) {
            
            // open the link in a new window
            $(this).attr("target", "_blank");

            // add external-link or pdf-link class if this
            // does not have an image inside it
            if (!($(this).find('img').length))
               $(this).addClass('external');         
         }
      }
   });   
}

function setupGA() {
   
   // local URLs are blank or begin with a '/' or '#'
   var rxLocalURL = /(^$)|(^#.*$)|(^\/.*$)/;
   var rxMailtoURL = /^mailto:(.*)$/;
   var rxDocURL = /^.+\.(pdf|doc|docx|xls|xlsx|ppt|pptx|txt)$/i;
   
   $('a').each(function() {
      
      var href = $(this).attr('href');
      
      if (href) {
         
         var isLocal = href.search(window.location.hostname) > -1 || rxLocalURL.test(href);
         var isMailto = rxMailtoURL.test(href);
         var isDoc = rxDocURL.test(href);
         
         if (isMailto) {
            
            $(this).click(function() {
               
               // track a mailto event
               _gaq.push(["_trackEvent", "mailto", "Click", $(this).attr('href').replace(/mailto:/, "")]);
               return true;
            });
            
         } else if (!isDoc && isLocal) {
            
            $(this).click(function() {
               
               // track an a pageview local.
               _gaq.push(["_trackPageview", $(this).attr('href')]);
               return true;
            });

         } else if (isDoc && isLocal) {
            
            $(this).click(function() {
               
               // track an event and a pageview for local downloads
               _gaq.push(["_trackEvent", "Downloads", "Click", $(this).attr('href')]);
               _gaq.push(["_trackPageview", "/downloads" + $(this).attr('href')]);
               return true;
            });
         
         } else if (!isLocal) {
            
            $(this).click(function() {
               
               // track an event for outbound
               _gaq.push(["_trackEvent", "Outbound", "click", $(this).attr('href')]);
               return true;
            });
            
         } else {
            
            // don't need to do anything else to track local links
         }
      }      
   });
}

