﻿(function ($) {
    $.fn.createButtonRollOver = function () {
        ToggleContent = function toggleContent(t) {
            var id = $(t).attr('id');

            // Swap "hide" / "show" text and arrow image on toggle
            if ($('.showText', t).hasClass('hidden')) {
                $('.showText', t).removeClass('hidden');
                $('.hideText', t).addClass('hidden');
                $(t).parent().addClass('collapse-off');
                $('#' + id + '-content').addClass('hidden');
            }
            else {
                $('.showText', t).addClass('hidden');
                $('.hideText', t).removeClass('hidden');
                $(t).parent().removeClass('collapse-off');
                $('#' + id + '-content').removeClass('hidden');
            }
        }

        // Toggle collapsing content if a class of 'toggleOnLoad' is applied
        $('.rc_collapseBar, .groupingBarText ').each(function () {
            var id = $(this).attr('id') + '-content';
            var isClosing;

            if ($('#' + id).attr('class') != null) {
                ($('#' + id).attr('class').indexOf('toggleOnLoad') > -1) ? isClosing = true : isClosing = false;
            }

            if (isClosing) {
                ToggleContent(this);
            }

        });

        // TODO: NEED TO UNIFY DROPDOWN CODE AND DROPDOWN BEHAVIOUR

        // Toggle drop down and arrow
        $('.dropdown_trigger').click(function () {

            // TODO: REWRITE THIS TO BE A LITTLE LESS DESTRUCTIVE

            var dd_id = $('a:first', this).attr('id');
            var ctn_id = dd_id + '-menu';

            if ($('#' + ctn_id).css('display') == 'block') {
                $('#' + ctn_id).css({ display: 'none' });
            }
            else {
                $('#' + ctn_id).css({ display: 'block' });
            }

            // finding parent container
            var parentContainer = $(this).parents('.contentPadding');
            // find all the dropdowns on the page, setting their z-index -low-
            $('.dropdown_wrapper', parentContainer).css("z-index", "1");
            // move the current dropdown above the rest
            $(this).parent('.dropdown_wrapper').css("z-index", "16");

            return false;
        });

        // This is to fix an issue with the footer in IE7 involving Blue buttons on the solutions page(Defect 360).
        var IEVersion = getInternetExplorerVersion();
        if (IEVersion < 8 && IEVersion > 0) {
            $($('.rc_blueBtn').get().reverse()).each(function () {
                var cursorType = $(this).css('pointer');
                $(this).css({ cursor: 'pointer' });
                $(this).css('cursor', cursorType);
            });
        }

        // Trimming this down as this used to do the CSS changes but that messed up IE7 stuff.
        $('.rc_blueBtn, .rc_collapseBar').hover(function () {
            if ($(this).parent().parent().attr('class').indexOf('LeftDisabled') == -1 && $(this).parent().parent().attr('class').indexOf('RightDisabled') == -1) {
                $(this).css({ cursor: 'pointer' });
            }
            else {
                $(this).click(function () {
                    return false;
                });
            }
        });

        $('.rc_moreBtnGallery').hover(function () {
            if ($(this).parent().parent().attr('class').indexOf('LeftDisabled') == -1 && $(this).parent().parent().attr('class').indexOf('RightDisabled') == -1) {
                $(this).css({ cursor: 'pointer' });
                // Create a usable string from the array
                var classString = $(this).attr('class');
                // Create an array with all classes listed
                // Running it through the __getCleanArray to ensure no blank space
                var class_arr = __getCleanArray($(this).attr('class').split(' '));

                // Apply the original class string and add "-over" to the last class
                // which needs to be the button class
                $(this).attr('class', classString + ' ' + class_arr[class_arr.length - 1] + '-over');
            }
            else {
                $(this).click(function () {
                    return false;
                });
            }

        }, function () {
            // Create an array with all classes listed
            // Running it through the __getCleanArray to ensure no blank space
            var class_arr = __getCleanArray($(this).attr('class').split(' '));
            // Create a class name string excluding the last item from the array
            var classString = __getClassString(class_arr);

            if (classString == "") {
                classString = "rc_whiteBtn";
            }

            // Apply the class string formatted
            $(this).attr('class', classString);
        });

        var isReturn = false;

        // Create the click function to show / hide collapsing content
        $('.rc_collapseBar, .groupingBarText').click(function () {
            if (isReturn) {
                return true;
            }
            else {
                ToggleContent(this);
                return false;
            }
        });

        $('.rc_collapseBar a').click(function () {
            isReturn = true;
        });


        // Cleaning out the blank spaces
        function __getCleanArray(arr) {
            var cleanArr = new Array();
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != '') { cleanArr.push(arr[i]); }
            }
            return cleanArr;
        }

        // Format a string from an array with blank spaces between each item
        function __getClassString(arr) {
            var str = '';
            for (var i = 0; i < arr.length - 1; i++) {
                str += arr[i] + ' ';
            }
            return str;
        }
    };
})(jQuery);
