﻿$(function () {

    if($.browser.msie && Number($.browser.version)<="6.0")
    {
        alert("This website does not support this version of Internet Explorer.  Please upgrade to IE 7 or higher to view this website or use a different browser.");
    }
    /* =============================================================================
    Main Navigation Menu
    ========================================================================== */

    // Determine the correct sub-menu to open
    var open_submenu = function (e) {
        e.preventDefault();

        var btn = $(this);
        var btn_id = btn.parent().attr('id');
        var menu_id = btn.attr('rel');
        var active_menu_item;

        // If button was NOT "Request Demo"...
        if (btn_id != 'menu-item-5') {

            // And if the sub-menu is already open...
            if ($('#sub-menu').is(':visible')) {

                // …the active-sub-menu is whatever menu is currently visible...
                active_sub_menu = '#' + $('.sub-menu:visible').attr('id');

                // Although, if the sub-menu that is open is also the menu that should be switched to...
                if (active_sub_menu === menu_id) {

                    // Kill the sub-menu item and roll up the sub-menu entirely
                    $(active_sub_menu).stop().fadeOut('fast', function () { $('#sub-menu').stop().slideUp(); });

                    // Otherwise fade out the current sub-menu item and fade in the new one	
                } else {
                    $(active_sub_menu).fadeOut('fast', function () { $(menu_id).fadeIn(); });
                }

                // But if the sub-menu is NOT open...			
            } else {

                // The active_menu_item is whatever button was pressed to open the menu
                active_menu_item = btn_id;
                $('#sub-menu').stop().slideDown(function () { $(menu_id).fadeIn(); });
            }
        }
        else window.location = btn.attr("href");
    };

    $('#primary-nav .menu-item a').click(open_submenu);

    /* =============================================================================
    jQuery Cycle Plugin & Banner Controls
    ========================================================================== */
    if (jQuery().cycle && $(window).width() > 480) {

        $('.b-img-container').cycle({
            fx: 'fade',
            speed: 'slow',
            next: '#b-next',
            prev: '#b-prev',
            after: function (curr, next, opts) {
                var caption = '' + (opts.currSlide + 1) + ' of ' + opts.slideCount;
                $("#banner .b-counter").html(caption);
            }
        });



        $('#b-pause').click(function (e) {
            e.preventDefault();
            $('.b-img-container').togglePause();
        });

        $(window).resize(function () {

            var ww = $(window).width();

            if (ww < 480) {
                $('#b-imgs').cycle('destroy');
            } else {
                $('#b-imgs').cycle({
                    fx: 'fade',
                    speed: 'slow',
                    next: '#b-next',
                    prev: '#b-prev'
                });
            }
        });
    }

    /* =============================================================================
    Tabs & Tab Controls
    ========================================================================== */
    if (jQuery().tabs) {
        $('.tabs').tabs('.panes > .pane', {
            current: 'active'
        });

        // Tab Next/Prev Buttons
        var jt_api = $('.tabs').data('tabs');

        $('.next-tab').click(function (e) {
            e.preventDefault();
            jt_api.next();
        });

        $('.prev-tab').click(function (e) {
            e.preventDefault();
            jt_api.prev();
        });
    } else {
        return;
    }

    /* =============================================================================
    Scrollables
    ========================================================================== */
    if (jQuery().scrollable) {
        $('.s-container').scrollable({
            items: '.s-items'
        });
    } else {

    }

    /* =============================================================================
    Video Player Block
    ========================================================================== */
    var sVideoItemTemplate = '<a style="font-size:1em;" rel="[id]" title="[title]" ><img src="[thumbnail]" alt="[title]" /></a>';
    $(".s-videos").each(function () {
        $(this).rmsVideoPlayer({
            userName: "RubbermaidMedical",
            iframe: '.yt-video > iframe',
            template: "" + sVideoItemTemplate,
            onVideosLoaded: function (count) {
                if (count > 3) {
                    $('.video-gallery-slider').append('<a class="s-btn prev ir">Back</a><a class="s-btn next ir">Next</a>');
                }
            }
        });
    });

    /* =============================================================================
    Video Playlists
    ========================================================================== */
    var template = '<li class="sn-vl-item fll clf"><div class="sn-vl-thumb fll"><a href="" rel="[id]" ><img src="[thumbnail]" alt="" /></a></div><div class="sn-vl-text fll"><h2><a style="font-size:1em;" href="" rel="[id]" >[title]</a></h2><p>[description]</p></div></li>';

    $(".sn-video-tabs .pane").each(function () {
        var id_str = "";
        if ($(this).attr("id")) id_str = $(this).attr("id");
        $(this).rmsVideoPlayer({
            userName: "RubbermaidMedical",
            maxResults: 4,
            iframe: '.ytplayer > iframe',
            template: "" + template,
            favorites: id_str.indexOf("favorites") >= 0
        });
    });


    /* =============================================================================
    Custom Checkbox & Radio Button Inputs
    ========================================================================== */
    if (jQuery().customInput && !($.browser.msie && Number($.browser.version) < 9)) {
        $('input[type=checkbox]').css('display', 'none');
        $('input[type=checkbox]').customInput();
    } else {

    }

    /* =============================================================================
    Fancy Box Modal Windows
    ========================================================================== */
    if (jQuery().fancybox) {

        $("a.fancy-modal").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'speedIn': 600,
            'speedOut': 200,
            'overlayShow': true,
            'overlayColor': '#000',
            'overlayOpacity': 0.3,
            'padding': 16,
            'showNavArrows': true
        });
    }
    // http://bit.ly/ishiv | WTFPL License
    window.innerShiv = function () { function h(c, e, b) { return /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i.test(b) ? c : e + "></" + b + ">" } var c, e = document, j, g = "abbr article aside audio canvas datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video".split(" "); return function (d, i) { if (!c && (c = e.createElement("div"), c.innerHTML = "<nav></nav>", j = c.childNodes.length !== 1)) { for (var b = e.createDocumentFragment(), f = g.length; f--; ) b.createElement(g[f]); b.appendChild(c) } d = d.replace(/^\s\s*/, "").replace(/\s\s*$/, "").replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "").replace(/(<([\w:]+)[^>]*?)\/>/g, h); c.innerHTML = (b = d.match(/^<(tbody|tr|td|col|colgroup|thead|tfoot)/i)) ? "<table>" + d + "</table>" : d; b = b ? c.getElementsByTagName(b[1])[0].parentNode : c; if (i === !1) return b.childNodes; for (var f = e.createDocumentFragment(), k = b.childNodes.length; k--; ) f.appendChild(b.firstChild); return f } } ();

    jQuery.fn.loadShiv = function (url, params, callback) {
        var off, selector, self, type;

        if (!this.length || typeof url !== 'string') {
            return this;
        }

        off = url.indexOf(' ');
        if (off >= 0) {
            selector = url.slice(off, url.length);
            url = url.slice(0, off);
        }

        type = 'GET';

        if (params) {
            if (jQuery.isFunction(params)) {
                callback = params;
                params = null;
            } else if (typeof params === 'object') {
                params = jQuery.param(params, jQuery.ajaxSettings.traditional);
                type = 'POST';
            }
        }

        self = this;

        jQuery.ajax({
            url: url,
            type: type,
            dataType: 'html',
            data: params,
            complete: function (res, status) {
                var shivved;

                if (status === 'success' || status === 'notmodified') {
                    shivved = jQuery(innerShiv((selector ? '<div>' : '') + res.responseText, false));

                    if (selector) {
                        shivved = shivved.find(selector);
                    }

                    self.empty().append(shivved);
                }

                if (callback) {
                    self.each(callback, [res.responseText, status, res]);
                }
            }
        });

        return this;
    }

});
