﻿(function($) {

    $.fn.ucdutils = function(options) {
        // build main options before element iteration
        var opts = $.extend({}, $.fn.ucdutils.defaults, options);
        // iterate and reformat each matched element
        return this.each(function() {
            $this = $(this);
            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

            // call our format function
        });
    };

    /*
    
    externalWrap
    
    Output pages:
    -template.ucdavis.edu
    -http://deltasolutions.ucdavis.edu/resources/
    -dev.terc.ucdavis.edu
    
    Usage:
    <script type="text/javascript">
    <!--
    $(document).ready(function() {
    $().ucdutils.externalWrap('#centercol a:not(#breadcrumbs a)');
    });
    //-->
    </script>
    
    */
    $.fn.ucdutils.externalWrap = function(anchors) {
        $(anchors).each(function(i) {
            if ($(this).attr('href')) {
                if ($(this).attr('href').indexOf('http://') != -1 && !$(this).hasClass('ignore')) {
                    var cssObj = {
                        'padding-right': '15px',
                        'border-bottom': '0px none',
                        'text-decoration': 'underline',
                        'background': 'url(http://template.ucdavis.edu/includes/img/ucd-utils/external-link.gif) no-repeat center right'
                    };
                    $(this).css(cssObj);

                    $(this).hover(
                        function() { $(this).css("text-decoration", "none"); },
                        function() { $(this).css("text-decoration", "underline"); }
                    );

                    if ($(this).attr('title').length == 0)
                        $(this).attr('title', 'Visit: ' + $(this).attr('href'));
                    $(this).click(function() {
                        try {
                            pageTracker._trackPageview('/external-link/' + $(this).attr('href').substr(1));
                        }
                        catch (err) {
                            return;
                        }
                    });
                }
            }
        });
    };

    /*
    
    linkTitles
    
    Output pages:
    -template.ucdavis.edu
    -dev.aeal.ucdavis.edu
    -dev.terc.ucdavis.edu
    
    Usage:
    <script type="text/javascript">
    <!--
    $(document).ready(function() {
    $().ucdutils.linkTitles('a');
    });
    //-->
    </script>
    
    */
    $.fn.ucdutils.linkTitles = function(anchors) {
        $(anchors).each(function(i) {
            if ($(this).attr('href')) {
                if (!$(this).attr('title') && !$(this).hasClass('ignore')) {
                    if ($(this).attr('href').indexOf('mailto:') != -1)
                        $(this).attr('title', 'Send an e-mail to: ' + jQuery.trim($(this).text()));
                    else
                        $(this).attr('title', jQuery.trim($(this).text()));
                }
            }
        });
    };

    /*
    
    E-mail Obfuscation
    
    Output pages:
    -template.ucdavis.edu
    -dev.terc.ucdavis.edu
    
    Usage:
    <script type="text/javascript">
    <!--
    $(document).ready(function() {
    $().ucdutils.obfuscate('.obfuscate');
    });
    //-->
    </script>
    
    HTML:
    <span class="obfuscate">dthunziker at ucdavis dot edu</span>
    <span class="obfuscate">dthunziker (at) ucdavis (dot) edu</span>
    <span class="obfuscate">warwick.vincent at bio.ulaval dot ca</span>
    
    */
    $.fn.ucdutils.obfuscate = function(wrapClass) {
        $(wrapClass).each(function(i) {
            var txt = jQuery.trim($(this).text());
            var parts = new Array();
            parts = txt.split(' ');
            if (parts.length == 5) {
                var e = parts[0] + '@' + parts[2] + '.' + parts[4];
                $(this).html('<a href="mailto:' + e + '">' + (($(this).attr('title')) ? $(this).attr('title') : e) + '</a>');
                if (!$(this).attr('title'))
                    $(this).attr('title', 'Send an e-mail to: ' + e);
            }
        });
    };

    /*
    
    hereClass
    
    Output pages:
    -template.ucdavis.edu
    -dev.aeal.ucdavis.edu
    -dev.terc.ucdavis.edu
    
    Usage:
    <script type="text/javascript">
    <!--
    $(document).ready(function() {
    $().ucdutils.hereClass('.primary_nav');
    });
    //-->
    </script>
    
    */
    $.fn.ucdutils.hereClass = function(wrapperDivs) {
        $(wrapperDivs).each(function(i) {
            $(this).find('a[href]').each(function(a) {
                if (document.location.href.indexOf($(this).attr('href')) != -1) {
                    $(wrapperDivs).find('li').removeClass('here');
                    $(this).parents('li:first').addClass('here');
                }
            });
        });
    };

    /*
    
    Slide
    
    Output pages:
    -template.ucdavis.edu
    -envnet.ucdavis.edu/admin/reports.aspx
    -http://johnmuir.ucdavis.edu/people/directory.aspx
    -http://deltasolutions.ucdavis.edu/people/index.html
    -http://deltasolutions.ucdavis.edu/resources/index.html
    -devche.ucdavis.edu/people/
    
    Usage:
    <script type="text/javascript">
    <!--
    $(document).ready(function() {
    $().ucdutils.slide('.toggle');
    });
    //-->
    </script>
    
    */
    $.fn.ucdutils.slide = function(toggleClass) {
        $(toggleClass).each(function(i) {
            var toggler = $(this);
            var cssObj = {
                'padding-right': '18px',
                'border-bottom': '0px none',
                'text-decoration': 'underline',
                'background-image': 'url(http://template.ucdavis.edu/includes/img/ucd-utils/arrow_down.gif)',
                'background-repeat': 'no-repeat',
                'background-position': 'center right'
            };
            $(toggler).css(cssObj);

            $(this).hover(
                function() { $(this).css("text-decoration", "none"); },
                function() { $(this).css("text-decoration", "underline"); }
            );

            // Set initial state of pane and toggler
            var pane = $(toggler).nextAll('div:first');
            if (pane.size() == 0)
                pane = $(toggler).parent().nextAll('div:first');
            $(pane).hide();
            $(toggler).attr('title', 'Expand: ' + jQuery.trim($(toggler).text()));

            var hash = document.location.href.substring(document.location.href.indexOf('#') + 1, document.location.href.length);
            if (hash.indexOf('p-' + i) != -1) {
                $(pane).show();
                $(toggler).css('background', 'url(http://template.ucdavis.edu/includes/img/ucd-utils/arrow_up.gif) no-repeat center right');
                $(toggler).attr('title', 'Collapse: ' + jQuery.trim($(toggler).text()));
            }
            $(toggler).bind('click', function() {
                if ($(pane).css('display') == 'none') {
                    if (document.location.href.indexOf('#panes:') < 0)
                        document.location.href += '#panes:';
                    document.location.href += 'p-' + i;
                    $(pane).slideDown("normal", function() {
                        $(toggler).css('background', 'url(http://template.ucdavis.edu/includes/img/ucd-utils/arrow_up.gif) no-repeat center right');
                        $(toggler).attr('title', 'Collapse: ' + jQuery.trim($(toggler).text()));
                    });
                    return false;
                }
                else {
                    var hash = document.location.href.substring(document.location.href.indexOf('#panes:') + 7, document.location.href.length);
                    document.location.href = document.location.href.replace(hash, hash.replace('p-' + i, ''));
                    $(pane).slideUp("fast", function() {
                        $(toggler).css('background', 'url(http://template.ucdavis.edu/includes/img/ucd-utils/arrow_down.gif) no-repeat center right');
                        $(toggler).attr('title', 'Expand: ' + jQuery.trim($(toggler).text()));
                    });
                    return false;
                }
            });
        });
        return false;
    };

    // plugin defaults
    $.fn.ucdutils.defaults = {
        external: true
    };

})(jQuery);