/*jslint browser: true, white: false, onevar: false */
/*global jQuery, $, window, net */

// Only do stuff here that is __truly__ global, please!

// Setup our namespace
if (!window.net) {
    window.net = {};
}
if (!window.net.sf) {
    window.net.sf = {};
}

// Our own browser sniffing util, based on jQuery's deprecated code
net.sf.ua = navigator.userAgent.toLowerCase();
net.sf.Browser = {
    safari: /webkit/.test( net.sf.ua ) && !/chrome/.test( net.sf.ua ),
    chrome: /chrome/.test( net.sf.ua ),
    opera: /opera/.test( net.sf.ua ),
    msie: /msie/.test( net.sf.ua ) && !/opera/.test( net.sf.ua ),
    mozilla: /mozilla/.test( net.sf.ua ) && !/(compatible|webkit)/.test( net.sf.ua )
};

// Setup HTML 5 support in IE
/* Disabled until we can get HTML 5 support in HTML Tidy
document.createElement('header');
document.createElement('footer');
document.createElement('section');
document.createElement('aside');
document.createElement('nav');
document.createElement('article');
*/

jQuery(function(jq) {

    // Prevent click when we're in dynamic-mode file list tree
    $('.treeTable .dynamic').click(function(e){
        e.preventDefault();
    });
   
    // Make sure the search input clears its text on focus
    jq('.search input[type=text]').clearingInput();
    
    // Make sure any download links in the page work for IE
    if (net.sf.Browser.msie) {
        jq('.dload').click(function() {
            // DIST-795: setup a temporary cookie with a token so that 
            // the mirror redirector knows this request is from us.
            var expiry = new Date();
            expiry.setSeconds(expiry.getSeconds() + 120);
            // Would be nice to pull this value from config, but not gonna 
            // fly for JS.
            jq.cookie('download_token', '554c46152c349563a845b24b15fad9a8', {expires: expiry, path: '/'});
            var release_url = jq(this).metadata().url;
            if (release_url) {
                window.open(release_url, 'download_window', 'toolbar=0,location=no,directories=0,status=0,scrollbars=0,resizeable=0,width=1,height=1,top=0,left=0');
                window.focus();
            }
        });
    }

    // Add a closing box to notification messages
    jq('#messages .message a.closer').click(jq.notify_closer);
    
    // Add a timer to any existing messages
    jq('#messages .message').each(function() {
        var self = this;
        setTimeout(function() {
            jq(self).fadeOut();
        }, 15000);
    });
    
    // Make all textareas autogrow, per SFPY-103
    jq('textarea').autoResize();
});

// Temp function to delete cookies
(function ($) {
     $.ad_wrapper = function(value, options){
         $.cookie('ad_wrapper', value, options);
     };
}(jQuery));

