// Content Fixes and Interactions

(function ($) {
    $.extend({
        bioShow: function () {
            $(".bio_list li p.moredetailstext a.exp-d").each(function () {
                $(this).click(function () {
                    var parentItem = $(".bio_list li .Inside").has(this);
                    $(this).hide();
                    $(".IntroText", parentItem).hide();
                    $(".FullText", parentItem).slideDown();
                    $(".more_info", parentItem).slideDown();
                    $("a.collapse-d", parentItem).show();
                    return false;
                });
            });
            $(".bio_list li p.moredetailstext a.collapse-d").each(function () {
                $(this).click(function () {
                    var parentItem = $(".bio_list li .Inside").has(this);
                    $(this).hide();
                    $(".FullText", parentItem).slideUp();
                    $(".more_info", parentItem).slideUp();
                    $(".IntroText", parentItem).show();
                    $("a.exp-d", parentItem).show();
                    return false;
                });
            });
        },

        equalHeight: function () {
            $('.tmfEqualHeight').children().height('auto');
            $('.tmfEqualHeight').each(function () {
                var maxHeight = 0;
                $(this).children().each(function () {

                    if ($(this).height() > maxHeight) {

                        maxHeight = $(this).height();
                    }

                });

                $(this).children().height(maxHeight);

            });
        },
        equalHeight01: function () {
            $('.tmfEqualHeight01').children().children().height('auto');
            $('.tmfEqualHeight01').each(function () {
                var maxHeight = 0;
                $(this).children().children().each(function () {

                    if ($(this).height() > maxHeight) {

                        maxHeight = $(this).height();
                    }

                });

                $(this).children().children().height(maxHeight);

            });
        },
        cycleApply: function () {
            if ($('.i_cour').length) {
                $('.i_cour li').css({ "visibility": "visible" });
                $('.i_cour').cycle({
                    speed: 2500,
                    timeout: 15000,
                    fx: 'fade',
                    pause: true
                });
            }
        },
        init: function () {

            $(window).resize(function () {
            });
            $(window).load(function () {

                $.bioShow();
                $.equalHeight();
                $.equalHeight01();

                //SelectBox -------------------------------------------------------
                if ($(".tmfJobSearch select").length) $(".tmfJobSearch select").selectbox();

                //Accordion -------------------------------------------------------
                if ($(".tmfAccordionContacts").length) $(".tmfAccordionContacts").accordion({
                    autoHeight: false
                });
                if ($(".tmfAccordionServices").length) $(".tmfAccordionServices").accordion({
                    autoHeight: false
                });

                //Roll Over -------------------------------------------------------
                $(".has_tip").hover(
                    function () {
                        $(this).children(".tmfRollOver").show();
                    },
                    function () {
                        $(this).children(".tmfRollOver").hide();
                    }
                );

                //LightBox -------------------------------------------------------
                $(".tmfBox .tmfGalleryList li a").lightBox();

                //SiteMap -------------------------------------------------------
                $(".tmfSiteMapLink").click(function () {
                    var scrollPosition = $(window).scrollTop();

                    $(".tmfSiteMapBlock").slideDown(50, function () {
                        $.equalHeight();
                        var destination = $(".tmfSiteMapBlock").height() + scrollPosition + 79;

                        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, 800);
                    });

                    return false;


                });
                $(".tmfSiteMapBlock .tmfTitleRow a.tmfClose").click(function () {
                    $(".tmfSiteMapBlock").slideUp(50, function () {
                        $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0 }, 1000);
                    });
                    return false;
                });


                //Clear inputs on focus -------------------------------------------------------
                var inputs = $("input[type='text']");
                //alert(inputs.length);
                for (var i = 0; i < inputs.length; i++) {
                    inputs[i].onfocus = function () {
                        if ($(this).attr("alt") && this.value.indexOf($(this).attr("alt")) == 0) { this.value = ''; }
                    };
                    inputs[i].onblur = function () {
                        if (this.value == '') { this.value = $(this).attr("alt"); }
                    };
                }
                $.cycleApply();

            });
        }
    });

    $(function () {
        var pdfFolder = "/media/Files/Brochures/";
        var extention = ".ashx";
        $('a[href*="' + pdfFolder + '"]').click(function () {
            var jthis = $(this);
            var href = jthis.attr("href");
            if (href.length < pdfFolder.length + extention.length)
                return;
            var fileName = href.substring(pdfFolder.length, href.length - extention.length);
            _gaq.push(['_trackPageview', '/downloads/pdfs/' + fileName + '.pdf']);
        });

        var domainRegExp =  new RegExp(document.domain, 'i');
        $('a').click(function () {
            if (!domainRegExp.test(this.href)) {
                return recordOutboundLink(this, 'Outbound Links', $(this).attr('href'));
            }
        });
    });
})(jQuery);

jQuery( function( $ ) {
	$.init(); // Inits content fixes
});

function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100);
}
