jQuery(function ($) {
    var MRUtils = {
        init: function () {
            $('#global_nav ul:first','#hd').superfish({
                delay:       1000,
                animation:   {opacity:'show',height:'show'},
                speed:       'fast',
                autoArrows:  false,
                dropShadows: false
            });
            if ($('#home_carousel').length > 0) this.carousel('/_images/_carousel/PhotoGallery.xml');
            if ($('.form_validator').length>00) $('.form_validator').validate();
            
            this.searchLabel($('#CAT_Search','#catsearchform72352'));
            this.zebraStripes($('table.data'));
            this.setBreadcrumbs();
            this.setErrorStyle();
            this.setCaptcha();
            this.level_boxes($('#promos .box'));
        },

        searchLabel: function (e) {
            $(document).ready(function () {
                if (e.length) {
                    if (e.val() === '') {
                        e.val('Search');
                    }
                    e.blur(function () {
                        if (e.val() === '') {
                            e.val('Search');
                        }
                    });
                    e.focus(function () {
                        if (e.val() === 'Search') {
                            e.val('');
                        }
                    });
                }
            });
        },

        zebraStripes: function (e) {
            $(document).ready(function () {
                if (e.length) {
                    $(e + 'tr:odd').addClass('alt_row');
                }
            });
        },

        setBreadcrumbs : function (e) {
            if ($('div.breadcrumbs').length > 0) {
                $('div.breadcrumbs').hide();
                $('#breadcrumb_nav').html($('div.breadcrumbs').html());
            }
        },

        setErrorStyle : function (e) {
            if ($('.f_error').length > 0 && $('.f_error').html() !== '') $('.f_error').addClass('error');
        },

        setCaptcha : function () {
            $(document).ready(function () {
                if ($('#CaptchaV2').length > 0) {
                    $('#CaptchaV2').addClass('required');
                }
            });
        },
        
        carousel : function (feed) {
            var a = [],
                c = 0;

            $.get(feed,{},function (xml) {
                var baseURL = $('album',xml).attr('lgpath');

                $('img',xml).each(function () {
                    var imgObj = {};

                    imgObj.src = baseURL + $(this).attr('src');
                    if (c%2) {
                        imgObj.dir = 'up';
                    } else {
                        imgObj.dir = 'down';
                    }
                    a[c] = imgObj;
                    c++;
                });
                $('#home_carousel').crossSlide({
                    sleep:  2,
                    fade:   1
                },a);
            });
        },
        
        level_boxes : function (e) {
            var max_height = 0;
            $(e).each(function (i) {
                var height = this.offsetHeight;
                if (height > max_height) {
                    max_height = height;
                }
                $(this).attr('id','promo-' + i);
            });
            e.css('minHeight',max_height);
        }
    };

    MRUtils.init();
});