// Added because the international center has pages running on multiple servers/hosts.
var HOSTS = ["international.missouri.edu", "studioabroaddev.missouri.edu", "international-dev.missouri.edu", "studioabroad.missouri.edu"];
var PROTOCOL = (("https:" == document.location.protocol) ? "https" : "http");

$(document).ready(function(){
    //Primary nav fx___________________________________________________
    $(function(){
        $("#nav ul li").hoverIntent({
            sensitivity: 1,
            interval: 100, 
            over: drops_show, 
            timeout: 300, 
            out: drops_hide
        });
        $("#nav ul li").addClass('with-js');
    });
    function drops_show(){ 
        $("div",$(this)).fadeIn();
        $(this).addClass('show'); 
        $(this).removeClass('with-js');
    }
    function drops_hide(){ 
        $(this).removeClass('show'); 
        $(this).addClass('with-js');  
    }
    
    //Tabs___________________________________________________
    $(function () {
        var tabContainers = $('div.tabs > div');
        tabContainers.hide().filter('a[href=#first]').show();
        
        $('div.tabs ul.tabNavigation a').click(function () {
                tabContainers.hide();
                tabContainers.filter(this.hash).show();
                $('div.tabs ul.tabNavigation a').removeClass('selected');
                $(this).addClass('selected');
                return false;
        }).filter('a[href=#first]').click();
    });
    
    //Textarea maxlength______________________________________
        //Add the attribute 'maxlength' to any textarea and the value will be the maxlength. 
        //'<p class="charsRemaining"></p>' after textarea will display the remaining characters.
        //http://sigswitch.com/2008/07/textarea-maxlength-with-jquery/
    $(function(){
        $('textarea[maxlength]').keyup(function(){
            var max = parseInt($(this).attr('maxlength'));
            if($(this).val().length > max){
                $(this).val($(this).val().substr(0, $(this).attr('maxlength')));
            }
    
            $(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
        });
    });
    
    //Link icons___________________________________________
    $(function() {
        // Customize variables for your site
        // External link icon image
        var external = '<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/external.png" alt="external link" title="external link" class="external-icon"/>';
        // Email link icon image
        var email = '&nbsp;<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/email.png" alt="email link" title="email link" class="inline-icon"/>';
        // Each file type image
        var file_types = {
            pdf: '&nbsp;<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/pdf.png" alt="PDF link" title="PDF link" class="inline-icon"/>',
            doc: '&nbsp;<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/doc.png" alt="Word document link" title="Word document link" class="inline-icon"/>',
            docx: '&nbsp;<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/doc.png" alt="Word document link" title="Word document link" class="inline-icon"/>',
            xls: '&nbsp;<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/xls.png" alt="Excel document link" title="Excel document link" class="inline-icon"/>',
            xlsx: '&nbsp;<img src="' + PROTOCOL + '://international.missouri.edu/images/icons/xls.png" alt="Excel document link" title="Excel document link" class="inline-icon"/>'
        };
        // If anchor has this class, it will not add icon
        var no_icon = 'no-icon'

        // Add this class to each element you'd like the icons to appear in EDITED: JLR
        var container = '.ex-icons'
        
        // Select each anchor that doesn't contain an image or have the no_icon class
        $(container + ' ' + 'a:not(:has(img))a:not(.' + no_icon + ')').each(function() {
     
            var $a = $(this);
            var link_class = $a.attr('class');
            var href = $a.attr('href');
            var href_array = false;
            
            if(href) href_array = href.split('.');
            
            var extension = href_array[href_array.length - 1];
            var image = file_types[extension];
                    
            // If an extenal link, insert icon inside the anchor tag
            // if (this.hostname && this.hostname !== location.hostname){
            if (this.hostname && jQuery.inArray(this.hostname, HOSTS) == -1){
                // alert("Appending external link: " + external);
                $a.append(external);
            }
            // If the href is an email link insert icon after the anchor tag
            if (href && href.match(/^mailto:/)){
                $a.after(email);
            }
            // If the href extension matches one in the file_types array insert icon after the anchor tag
            if (image){
                $a.after(image);
            }
        });
    });
});
