/**
 *  jQuery Rotator Plugin
 *  @requires jQuery v1.2.6 or greater
 *  http://hernan.amiune.com/labs
 *
 *  Copyright (c)  Hernan Amiune (hernan.amiune.com)
 *  Licensed under MIT license:
 *  http://www.opensource.org/licenses/mit-license.php
 * 
 *  Version: 1.0
 */
 
(function ($) {
    $.fn.rotator = function (options) {

        var defaults = {
            ms: 7000
        };

        var options = $.extend(defaults, options);

        return this.each(function (index) {

            var $this = $(this);
            $this.width("2000px");
            $this.children(".bannerItem").height("248px");
            $this.children(".bannerItem").height("948px");

            $this.children(".bannerItem").each(function (index) {
                $(this).addClass("item_" + (index + 1));

                var btnItem = $("<div class='rotbtn btn" + (index + 1) + "' rel='item_" + (index + 1) + "'><a href='javascript:void(0)'>" + (index + 1) + "</a><div>");
                if (index == 0) {
                    btnItem.addClass("active");
                }
                $(this).attr("rel", "btn" + (index + 1));
                $(".btnContainer").append(btnItem);
                btnItem.click(function () {
                    var className = $(this).attr("rel");
                    var item = $(".banner_single ." + className);
                    clearTimeout($this.data("timer"));

                    var second = $this.children().filter(":nth-child(2)");
                    var first = $this.children().filter(":first-child");
                    
                    if (!first.hasClass(className)) {
                        first.after(item);
                        moveNext();
                    }
                   

                });
            });
            function moveNext() {
                $(".bannerItem").each(function () {
                    $(this).stop();
                });

                $this.children().css("position", "absolute");
                var firstChild = $this.children().filter(":first-child");
                var nextChild = $this.children().filter(":nth-child(2)");
                var childwidth = firstChild.width();
                firstChild.show();
                nextChild.show();

                $this.children().not('::first-child').not(':nth-child(2)').hide();
                $this.children().filter(":nth-child(2)").css("left", (childwidth) + "px");

                var animParams = { "opacity": "0" };

                firstChild.animate(
                        animParams, { duration: 2000, specialEasing: { width: 'linear', height: 'easeOutBounce'} }).appendTo($this);
                        
		setTimeout(function(){firstChild.hide()},2000);


                var animParamsLeft = { "left": "-=" + childwidth + "px", "opacity": "0" };

                nextChild.animate(
                        animParamsLeft, { duration: 0, specialEasing: { width: 'linear', height: 'easeOutBounce'} });

                var animParams2 = { "opacity": "1" };

                nextChild.animate(
                        animParams2, { duration: 2000, specialEasing: { width: 'linear', height: 'easeOutBounce'} });


                $(".btnContainer").children().removeClass("active");
                var className = nextChild.attr("rel");
                $("." + className).addClass("active");
            }
            function beginRotation() {
                $this.data("timer", setInterval(function () {

                    moveNext();

                }, options.ms));

            }

            $this.mouseover(function () {
                clearTimeout($this.data("timer"));
            });

            $this.mouseout(function () {
                if ($this.children(".bannerItem").length > 1) {
                    beginRotation();
                }
            });

            if ($this.children(".bannerItem").length > 1) {
                beginRotation();
            }



        });


    }
})(jQuery);
