$(document).ready(function() {
  setWrapperHovers();
  setHideMails();
  setCustomSelectors();
  setLinks();
});

function setWrapperHovers(){

		$(".sectext").css({opacity:0.0, visibility:"visible"});
		$(".wrapper").each(function (i){
			$(this).mouseover(function(){ 
				$thisText = $(this).siblings(".sectext:first");
				$parentID = $(this).parent("div").attr("id");
				if($thisText.css("opacity") == 0.0){
					hideVisibles();
					$thisText.animate({opacity:1},"fast");
					trackIt($parentID+"/");
				}
			});
		});
	
}

function hideVisibles(){
	$(".sectext").each(function (i){
		if($(this).css("opacity") > 0.0){
			$(this).animate({opacity:0.0},"fast");
		}
	});
}

/* Email Protection Script. */

at = '@';
mailer = 'mailto:';

function setHideMails() {
    $('.hidemail').each(function () {
        var user = $(this).attr("user");
        var domain = $(this).attr("domain");
        var tld = $(this).attr("tld");
        var html = $(this).attr("html"); /* html left blank sets it to the email address provided */

        if (user && domain && tld) {
            var emailLink = getmail(user, domain, tld, html);
            $(this).html(emailLink);
        }
    });
}

function getmail(name, domain, suffix, text){ 
    /* user supplied email parts and alt text */
	hiddenMail=(name + at + domain + '.' + suffix);
	if (! text){ 
        /* if no alt text then use full email address */
		text = hiddenMail;
}

    email = '<a href="' + mailer + hiddenMail + '" onclick="javascript:_gaq.push([\'_trackPageview\', \'/mailto/' + hiddenMail + '\']);">' + text + '</a>';
    return email;
}

/* Auto Link Management */
function setCustomSelectors() {
    /* create custom jquery selectors to match external links and mailto links */
    jQuery.extend(jQuery.expr[":"], {
        external: function (obj) {
            return obj.href && !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/) && (obj.hostname && (obj.hostname != location.hostname));
        },
        mailto: function (obj) {
            return obj.href && obj.href.match(/^mailto\:/);
        },
        pdf: function (obj) {
            return obj.href && obj.href.match(/\.pdf$/);
        },
        zip: function (obj) {
            return obj.href && obj.href.match(/\.zip$/);
        }
    });
}

function setLinks() {
    /* Add CSS class to all external or mailto links */
    $("a:external").addClass("external");
    $("a:mailto").addClass("mailto");
    $("a:pdf").addClass("pdf");
    $("a:zip").addClass("zip");

    /* Add Google Analytics tracking to them */
    setLinkAnalytics();
}


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9829790-1']);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


function trackIt(qstring, ignoredir) {
    if (_gaq) {
        if (ignoredir) {
            if (qstring.length == 0) {
                _gaq.push(['_trackPageview']);
            }else{
                _gaq.push(['_trackPageview', '/' + qstring]);
            }
        }else{
            if (qstring.length == 0){
                /* get page URL, hash and QueryString for pass-through */
                qs = location.search;
                hash = location.hash;
                path = location.pathname;
                if (path.length < 2){
                    path = '';
                }else{
                    /* removes starting slash */
                    path = path.substring(1);
                }
				
                /* combine */
                fullpath = path + qs + hash;

                _gaq.push(['_trackPageview', '/' + fullpath]);
            }else{
                _gaq.push(['_trackPageview', '/' + qstring]);
            }
        }
    }
}

function setLinkAnalytics() {
    /* Add click event for Google Analtyics */
    $("a:pdf, a:zip").live("click", function () {
        var thisHref = $(this).attr('href');
        var lp = location.pathname;
        var lastSlash = location.pathname.lastIndexOf('/');
        if (thisHref.substr(0, 1) != '/') {
            /* is a relative link */
            thisHref = location.pathname.substr(1, lastSlash) + thisHref;
        }
        trackIt(thisHref, false);
    });
    $("a:external").live("click", function () {
        trackIt('linkout/' + this.href.replace('http://', '').replace('https://', ''), true);
    });
    $("a:mailto").live("click", function () {
        trackIt('mailto/' + this.href.replace('mailto:', ''), true);
    });
}
