﻿jQuery(document).ready(function() {
    jQuery(".IR_container").each(function(i) {
        var interval = 5;
        var fadeTime = 1700;

        if (this.className && this.className.indexOf("interval_") != -1 && this.className.indexOf("fixed") == -1) {
            var classNames = this.className.split(" ");
            for (var j = 0, jj = classNames.length; j < jj; j++) {
                var cName = classNames[j];
                if (cName.indexOf("interval_") == 0) {
                    interval = cName.substring(cName.lastIndexOf("_") + 1);
                    break;
                }
            }
            if (fadeTime * 2 > interval * 1000) //Make sure the total interval is not less than the total time for the fade in and out.
                interval = Math.ceil((fadeTime * 2) / 1000);

            jQuery(this).addClass("fixed");
            if (!this.id)
                this.id = "wpImageRotator_" + i;

            if (jQuery(this).children().length > 1) {
                //First do some magic so that first fade do not "flash"...
                jQuery(this).children().hide();
                jQuery(this).children(".show").show();
                setInterval(function(that) {
                    return function() {
                        var current = (jQuery("#" + that.id + " .show") ? jQuery("#" + that.id + " .show") : jQuery("#" + that.id + " :first"));
                        var next = ((current.next().length) ? ((current.next().hasClass("show")) ? jQuery("#" + that.id + " :first") : current.next()) : jQuery("#" + that.id + " :first"));

                        if (that.style.minHeight && that.style.minHeight.length > 0) {
                            var minHeight = parseInt(that.style.minHeight, 10);
                            var currentHeight = parseInt(current.height(), 10);
                            if (minHeight < currentHeight)
                                that.style.minHeight = currentHeight + "px";
                        }
                        else {
                            that.style.minHeight = current.height() + "px";
                            if (jQuery.browser.msie && 7 == parseInt(jQuery.browser.version)) {
                                //Since IE7 do not get the correct height the first time, we tries once more...
                                that.style.minHeight = current.height() + "px";
                            }
                        }

                        current.removeClass("show").fadeOut(fadeTime);
                        next.fadeIn(fadeTime).addClass("show");
                    }
                } (this), (interval * 1000));
            }
        }

    });
});
