/********************************

	Notes:
		+ Functions/methods should never be initialized within this file. It contains no console command protection for IE.
		+ I rely on JQuery v1.3.2+

********************************/

// Global variable assignment
var lib = {
    listSlider: function (hidingElms, triggerElm, showFirst, callBack) {
        var cb = (typeof callBack == 'undefined') ? '' : callBack;
        if (typeof showFirst == 'undefined' || showFirst === '') {
            hidingElms.hide();
        } else {
            triggerElm.eq(0).toggleClass('expanded');
            hidingElms.slice(1).hide();
        }
        triggerElm.each(function () {
            jQuery(this).click(function () {
                if (jQuery(this).hasClass('expanded')) {
                    jQuery(this).next().slideUp('fast', cb);
                    jQuery(this).toggleClass('expanded');
                } else {
                    jQuery(this).next().slideDown('fast', cb);
                    jQuery(this).toggleClass('expanded');
                }
                newHead = '#' + this.id;
                return newHead;
            });
        });
    },

    // open external links in a new window
    externalLinks: function () {
        // Look for links whose href begins with 'http' and open in new window
        setTimeout(function () {
            jQuery('a[href^="http"]').not('.colorbox, .folio-link, .iframe, .sidenav-link, .addthis_button, .return-url, .press-room-link, .preventExt').each(function () {
                var $this = jQuery(this);
                if ($this.attr('href').indexOf('roundtable') === -1) {
                    //.log($this.attr("class"));
                    $this.addClass('extlink');
                    var $href = $this.attr('href');
                    var host = location.host; // get host from URL
                    if ($href.indexOf(host) === -1) { // check $href for presence of host
                        $this.bind('click', function () {
                            window.open(this.href);
                            return false;
                        });
                    }
                }
            });
        }, 1000);
    },

    // opens links to pages within site(example: Selection Guide) in new window
    newWindowLinks: function () {
        jQuery('a.new-window').not('.thickbox').click(function () {
            window.open(this.href);
            return false;
        });
    },

    // open Downloadable files in a new window
    fileLinks: function () {
        jQuery('a[href*=".pdf"], a[href*=".xls"], a[href*=".ppt"], a[href*=".doc"], a[href*=".zip"], a[href*=".pot"]').not('.preventExt').click(function () {
            window.open(this.href);
            return false;
        });
    },

    // add file extension classes to downloadable links
    linkFileTypes: function () {
        var $a = jQuery('a', '.download');
        $a.each(function (x) {
            var $this = jQuery(this);
            var $fileType = $this.attr('href').split('.').reverse()[0].toLowerCase();
            if ($this.find('img').length != 0) {
                return;
            } else {
                if ($fileType !== 'html') {
                    $this.parent().addClass($fileType);
                }
            }
        });
    },

    // add classes to form inputs for css use
    addFormClasses: function () {
        if (jQuery('input').length) {
            jQuery('input').each(function () {
                jQuery(this).addClass(this.type);
            });
        }
    },

    // Preload images when activated
    // - accepts array as input
    preloadImages: function (imgArray) {
        jQuery.each(imgArray, function (e) {
            jQuery(new Image()).attr('src', this);
        });
    },

    inputDefault: function () {
        // Auto-hide default input text
        // add class of "default" to inputs to activate
        jQuery('input[type="text"].default', 'form').each(function () {
            var $this = jQuery(this);
            var $dVal = $this.val();
            // hide default text on focus
            $this.focus(function () {
                if ($this.val() === $dVal) {
                    $this.val('');
                }
            })
            // show default text if empty on blur
			.blur(function () {
			    if ($this.val() === '') {
			        $this.val($dVal);
			    }
			});
        });
    },

    // Conditionally add wmode parameter to SWFObject calls if browser is not FF2
    addWMode: function () {
        if (typeof params !== 'undefined') {
            // Get browser version number
            var browserVersion = jQuery.browser.version;
            // Trim version number to X.X (major version for FF)
            var parsedBrowserVersion = Number(browserVersion.split('.').slice(0, 2).join('.'));
            // If browser is NOT FireFox 2, add wmode param
            if (!(jQuery.browser.mozilla && parsedBrowserVersion < 1.9)) {
                params.wmode = "transparent";
            };
        }
    },

    // Find the tallest element in the passed-in group and set
    // all group members to that same height
    equalHeight: function (group) {
        var tallest = 0;
        group.each(function () {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        //console.log('tallest: ', tallest);
        group.height(tallest);
    },

    //Set up tooltips and footnotes
    addTooltips: function () {
        jQuery('.toolTip').each(function () {
            jQuery(this).cluetip({
                arrows: true,
                dropShadow: false,
                hoverIntent: true,
                splitTitle: '|',
                sticky: true,
                mouseOutClose: true,
                closePosition: 'title',
                showTitle: true,
                closeText: '<img src="/images/gui/cluetip/close.gif" alt="close" />'
            });
        });
    }
};
