(function($) {
    $.fn.rotatingbanner = function(vars) {
        var current = null;
        var slideId = this[0].id;
        var items = $("#" + slideId + "Content ." + slideId + "Image");
        var show = true;
        var mOver = false;
        var pause = false;
        var timeOutFn = null;

        var effect = (!vars || vars.effect == undefined) ? 'fade' : vars.effect;
        var timeOut = (!vars || vars.timeOut == undefined) ? 5000 : vars.timeOut;
        var fadeFactor = (!vars || vars.fadeFactor == undefined) ? 20 : vars.fadeFactor;
        var onMouseOverStop = (!vars || vars.onMouseOverStop == undefined) ? true : vars.onMouseOverStop;
        var pagging = (!vars || vars.pagging == undefined) ? false : vars.pagging;
        var control = (!vars || vars.control == undefined) ? false : vars.control;
        var tabnav = (!vars || vars.tabnav == undefined) ? false : vars.tabnav;

        if (onMouseOverStop) {
            items.each(function(i) {
                $(items[i]).mouseover(function() {
                    mOver = true;
                }).mouseout(function() {
                    mOver = false;
                    fadeElement();
                });
            });
        }

        var fadeElement = function() {
            highlight_current();
            timeout = (show) ? (timeOut / fadeFactor / 2) : timeOut;
            if (items.length > 0) {
                clearTimeout(timeOutFn);
                timeOutFn = setTimeout(rotate, timeout);
            }
        };

        var highlight_current = function() {
            if (pagging || tabnav) {
                currIdx = jQuery.inArray(current, items);
                panelName = tabnav ? 'tabnav_panel' : 'pagging_panel';
                var navs = $("#" + slideId + " ." + panelName + " ul li");
                navs.each(function(i) {
                    if (i == currIdx) {
                        $(navs[i]).addClass('highlight');
                    }
                    else {
                        $(navs[i]).removeClass('highlight');
                    }
                });
            }
        };

        var rotate = function() {
            if (!mOver && !pause) {
                current = (current != null) ? current : items[0];
                var currNo = jQuery.inArray(current, items);
                currNo = (currNo == items.length) ? 0 : currNo;
                if (show) {
                    $(items[currNo]).fadeIn((timeOut / fadeFactor), function() {
                        show = false;
                        current = items[currNo];
                        fadeElement();
                    });
                } else {
                    $(items[currNo]).fadeOut((timeOut / fadeFactor), function() {
                        show = true;
                        current = items[(currNo + 1)];
                        fadeElement();
                    });
                }
            }
        };

        var rotateByStep = function(v) {
            var currNo = jQuery.inArray(current, items);
            step = currNo + parseInt(v);
            if (step < 0) {
                rotateTo(items.length + step);
            } else if (step >= items.length) {
                rotateTo(step - items.length);
            } else {
                rotateTo(currNo + parseInt(v));
            }
        };
        var rotateTo = function(idx) {
            $(current).fadeOut((10));
            $(items[idx]).fadeIn((timeOut / fadeFactor), function() {
                show = false;
                current = items[idx];
                fadeElement();
            });
        };

        var buildControlPanel = function() {
            var buildPagination = function() {
                paggingPanel = $('#' + slideId + 'CP .pagging_panel');
                var html = '';
                $(items).each(function(i) {
                    html += '<li></li>';
                });
                paggingPanel.html('<ul>' + html + '</ul>');
                var pages = $("#" + slideId + "CP .pagging_panel ul li");
                $(pages).each(function(i) {
                    $(pages[i]).click(function() {
                        clearTimeout(timeOutFn);
                        rotateTo(i);
                    });
                });
                $("#" + slideId + "CP .pagging_panel ul").css('margin-left', (($('#' + slideId + 'CP').width()) - $('#' + slideId + 'CP .pagging_panel ul').width())/2);
            };

            var buildControlButton = function() {
                buttonPanel = $('#' + slideId + 'CP .button_panel');
                buttonPanel.html('<ul>' +
                                 '<li class="left_arrow" id="btn_rotatingbanner_left"></li>' +
                                 '<li class="right_arrow" id="btn_rotatingbanner_right"></li>' +
                                 '</ul>');                
                btns = [
                    {
                        id: 'btn_' + slideId + '_left',
                        icon: '&laquo',
                        nav: '-1'
                    }
                    ,
                    {
                        id: 'btn_' + slideId + '_right',
                        icon: '&raquo',
                        nav: '+1'
                    }
                ];
                var html = '';
                $.each(btns, function(i, v) {
                    var style = (v.nav == '-1' ? 'left_arrow' : (v.nav == '+1' ? 'right_arrow' : ''));
                    html += "<li id='" + v.id + "' class='" + style + "'></li>";
                });
                buttonPanel.html('<ul>' + html + '</ul>');
                $.each(btns, function(i, v) {
                    var btn = $('#' + v.id);
                    btn.click(function() {
                        switch (v.nav) {
                            case ('-1'): rotateByStep(v.nav); break;
                            case ('+1'): rotateByStep(v.nav); break;
                        }
                    });
                });
            };

            var buildTabNavigation = function() {
                tabnavPanel = $('#' + slideId + 'CP .tabnav_panel');
                var html = '';
                $(items).each(function(i) {
                    html += '<li ' + (i == 0 ? 'class="first"' : (i == items.length - 1) ? 'class="last"' : '') + '><span>' + $(items[i]).find('img').attr('title') + '</span></li>';
                });
                tabnavPanel.html('<ul>' + html + '</ul>');
                var pages = $("#" + slideId + "CP .tabnav_panel ul li");
                var w = (($('#' + slideId + 'CP').width()) / pages.length);
                $(pages).each(function(i) {
                    $(pages[i]).click(function() {
                        clearTimeout(timeOutFn);
                        rotateTo(i);
                    });
                    $(pages[i]).css("width", (i == 0 || i == pages.length - 1 || i == 2 ) ? w : (w - 2));
                });
            };
            
            if (pagging || control || tabnav) {
                if (pagging) {
                    buildPagination();
                }
                if (control) {
                    buildControlButton();
                }
                if (tabnav) {
                    buildTabNavigation();
                }
                highlight_current();
                $('#' + slideId + 'CP').removeClass('hide');
            }
        };
        rotate();
        buildControlPanel();
    };
})(jQuery);
