/*
    Coded by Steven Bower (cc) 2009
    BPWebDesign http://bpwebdesign.com/
    TurnWheel Designs http://turnwheel.com/
*/

$(function() {
    // Load main navigation
    $('ul.sf-menu').supersubs({
        minWidth: 10,
        maxWidth: 15
    }).superfish();
    
    // Handle external links
    $('a[rel="external"]').live('click',function() {
        var href = $(this).attr('href');
        
        // Send link event to Google Analaytics
        try {
            pageTracker._trackEvent('External Links', href.split(/\/+/g)[1], href);
        } catch (e) {};
        
        // Open link in new window with randomize name
        window.open(href,'uni_'+Math.round(Math.random()*11));
        
        return false;
    }).addClass('external').attr('title',function(index, title) {
        // Do not change title if one is already defined
        if (title != '') return title;
        
        // Extract domain from url
        var parsed = this.href.match(new RegExp('(http|ftp|https)://(.*?)/.*$'));
        
        // Return domain if found or return full href tag
        return parsed[2] != undefined && parsed[2] != '' ? parsed[2] : this.href;
    });
    
    // Remove external class from images
    $('a[rel="external"] > img').parent().removeClass('external').attr('title','');
    
    // Handle "shownext" links
    $('a[rel="shownext"]').toggle(function() {
        $(this).parent().next(':hidden').slideDown('fast');
    },function() {
        $(this).parent().next(':not(:hidden)').slideUp('fast');
    });
    
    // Show date picker and load resources only when needed
    if ($('input.date').length > 0) {
        $.getScript('/js/jquery-ui.min.js',function() {
            $('input.date').datepicker({
                dateFormat: 'mm/dd/yy'
            });
        });
    }
});
