
(function($) {
    $.fn.loopedSlider = function(options) {

        var defaults = {
            container: '.Slides_container',
            slides: '.slides',
            pagination: '.rotator_menu',
            containerClick: true, // Click container for next slide
            autoStart: '8500', // Set to positive number for auto interval and interval time
            slidespeed: 600, // Speed of slide animation
            fadespeed: 600, // Speed of fade animation
            autoHeight: false, // Set to positive number for auto height and animation speed
            cookie_name: "OPX_Cookie",
            ContentDiv : "impact_content"
        };

        this.each(function() {

            var obj = $(this);
            var o = $.extend(defaults, options);
            var pagination = $(o.pagination + ' a img', obj);
            var slider = $(o.slides + ' img', obj);
            
//            var ImpactBox = ($(ImactBox_Div ,obj));
            var ImpactBox = document.getElementById('ImactBox_Div');
            var m = 0;
            var t = 1;

            var s = $(o.slides, obj).children().size();
            var w = $(o.slides, obj).children().outerWidth();
            var DynImpactContent= "cntnt_0"+t;
            var ContentDivv = $(o.ContentDiv + ' div', obj);
//            var ImpactContant = ($(DynImpactContent+ ' p',obj));
//            $(ImpactContant, obj)[0].style.visibility = "visible";
            var p = 0;
            var u = false;
            var n = 0;

            $(o.slides, obj).css({ width: (s * w) });

            $(o.slides, obj).children().each(function() {
                $(this).css({ position: 'absolute', left: p, display: 'block' });
                p = p + w;
            });

            $(pagination, obj).each(function() {

                n = n + 1;
                $(pagination.eq(0), obj).attr("src", "/Portals/_default/Skins/OPX/images/impact_dot_on.png");
            });

            $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ position: 'absolute', left: -w });
            write_cookie();
//            $(ImpactBox, obj)[0].className = "impact_box01";
             var CntntDiv12 = document.getElementById("impact_content_Text");

                CntntDiv12.innerHTML =Cntnt1;

            
            if (o.autoHeight) { autoHeight(t); }

            $('.next_rotator', obj).click(function() {
                if (u === false) {
                    animate('next', true);
                    if (o.autoStart) { clearInterval(sliderIntervalID); }
                } return false;
            });

            $('.previous_rotator', obj).click(function() {
                if (u === false) {
                    animate('prev', true);
                    if (o.autoStart) { clearInterval(sliderIntervalID); }
                } return false;
            });

            if (o.containerClick) {
                $(o.container, obj).click(function() {
                    if (u === false) {
                        animate('next', true);
                        if (o.autoStart) { clearInterval(sliderIntervalID); }
                    } return false;
                });
            }

            $(pagination, obj).click(function() {
               {
                    t = $(this).attr('id');
                    t=t.replace('img','');
                    for (var pageCntr = 0; pageCntr <= 4; pageCntr++) {
                        $(pagination.eq(pageCntr), obj).attr("src", "/Portals/_default/Skins/OPX/images/impact_dot_of.png");
                    }
                    $(pagination, obj).parent().siblings().removeClass('active');
                    $(this).attr("src", "/Portals/_default/Skins/OPX/images/impact_dot_on.png");
                    animate('fade', t);
                    if (o.autoStart) { clearInterval(sliderIntervalID); }
                    
                } return false;
            });

            if (o.autoStart) {
                sliderIntervalID = setInterval(function() {
                    if (u === false) { animate('next', true); }
                }, o.autoStart);
            }
        function write_cookie() {
                var ImgPath ='';
                var index = '';
                var hostname = "http://"+window.location.hostname;
                ImgPath = $(slider + '[id="img' + (t*10) + '"]', obj)[0].src;
               
                if (document.cookie) {
                    if(document.cookie.indexOf(o.cookie_name) != -1)
                    {
                    index = document.cookie.indexOf(o.cookie_name);
                    }
                    else {
                    index = -1;
                }
                } else {
                    index = -1;
                }
            ImgPath = ImgPath.replace (hostname, "");  
            
//                if (index == -1) {
                    document.cookie = o.cookie_name + "=" + ImgPath + "; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
//                } else {
//                    countbegin = (document.cookie.indexOf("=", index) + 1);
//                    countend = document.cookie.indexOf(";", index);
//                    if (countend == -1) {
//                        countend = document.cookie.length;
//                        document.cookie = o.cookie_name + "=" + ImgPath + "; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
//                    }
//                }
            }


            function getCookie() {
                if (document.cookie) {
                    index = document.cookie.indexOf(cookie_name);
                    if (index != -1) {
                        countbegin = (document.cookie.indexOf("=", index) + 1);
                        countend = document.cookie.indexOf(";", index);
                        if (countend == -1) {
                            countend = document.cookie.length;
                        }
                        count = document.cookie.substring(countbegin, countend);
                        return count;
                    }
                }
                return ("-2");
            }
            function current(t) {
                
                if (t === s + 1) { t = 1; }
                if (t === 0) { t = s; }
               
                for (var pageCntr_1 = 0; pageCntr_1 <= 4; pageCntr_1++) {
                    $(pagination.eq(pageCntr_1), obj).attr("src", "/Portals/_default/Skins/OPX/images/impact_dot_of.png");
                }

                $(pagination + '[id="img' + (t) + '"]', obj).attr("src", "/Portals/_default/Skins/OPX/images/impact_dot_on.png");
                var CntntDiv1 = document.getElementById("impact_content_Text");

                if(t==1)
                {
                
                CntntDiv1.innerHTML =Cntnt1;
                }
                else if(t==2)
                {
                
                CntntDiv1.innerHTML =Cntnt2;
                }
                else if(t==3)
                {
                
                CntntDiv1.innerHTML =Cntnt3;
                }
                else if(t==4)
                {
                
                CntntDiv1.innerHTML =Cntnt4;
                }
                else if(t==5)
                {
                
                CntntDiv1.innerHTML =Cntnt5;
                }
            };

            function autoHeight(t) {
                if (t === s + 1) { t = 1; }
                if (t === 0) { t = s; }
                var getHeight = $(o.slides, obj).children(':eq(' + (t - 1) + ')', obj).outerHeight();
                $(o.container, obj).animate({ height: getHeight }, o.autoHeight);
            };

            function animate(dir, clicked) {
                u = true;
                switch (dir) {
                    case 'next':
                        
                        t = t + 1;
                        if (t == 6) {
                            t = 1;
                        }

                        m = (-(t * w - w));
                        current(t);
                        write_cookie();
                        
                 $(ImpactBox, obj)[0].className = "impact_box0"+ t;
                        if (o.autoHeight) { autoHeight(t); }
                        $(o.slides, obj).children().fadeOut(o.fadespeed, function() {
                            $(o.slides, obj).css({ left: m });
                            $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ left: s * w - w });
                            $(o.slides, obj).children(':eq(0)').css({ left: 0 });
                            if (t === s) $(o.slides, obj).children(':eq(0)').css({ left: (s * w) });
                            if (t === s - 1) $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ left: s * w - w });
                            $(o.slides, obj).children().fadeIn(o.fadespeed);
                            u = false;
                        });
                        break;
                    case 'prev':
                        
                        t = t - 1;
                        if (t == 0)
                        { t = 5; }
                        m = (-(t * w - w));
                        current(t);
                        write_cookie();
                        
                 $(ImpactBox, obj)[0].className = "impact_box0"+ t;
                        if (o.autoHeight) { autoHeight(t); }
                        $(o.slides, obj).children().fadeOut(o.fadespeed, function() {

                            $(o.slides, obj).css({ left: m });
                            $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ left: s * w - w });
                            $(o.slides, obj).children(':eq(0)').css({ left: 0 });
                            if (t === 2) $(o.slides, obj).children(':eq(0)').css({ position: 'absolute', left: 0 });
                            if (t === 1) $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ position: 'absolute', left: -w });
                            $(o.slides, obj).children().fadeIn(o.fadespeed);
                            u = false;
                        });
                        break;
                    case 'fade':
                        t = [t] * 1;
                        m = (-(t * w - w));
                        current(t);
                        write_cookie();
                        
                 $(ImpactBox, obj)[0].className = "impact_box0"+ t;
                        if (o.autoHeight) { autoHeight(t); }
                        $(o.slides, obj).children().fadeOut(o.fadespeed, function() {
                            $(o.slides, obj).css({ left: m });
                            $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ left: s * w - w });
                            $(o.slides, obj).children(':eq(0)').css({ left: 0 });
                            if (t === s) { $(o.slides, obj).children(':eq(0)').css({ left: (s * w) }); }
                            if (t === 1) { $(o.slides, obj).children(':eq(' + (s - 1) + ')').css({ position: 'absolute', left: -w }); }
                            $(o.slides, obj).children().fadeIn(o.fadespeed);
                            u = false;
                        });
                        break;
                    default:
                        break;
                }
            };
        });
    };
})(jQuery);
