﻿var imagerotators = imagerotators==null||imagerotators==undefined?new Array():imagerotators;
(function($) {

    $.fn.ImageRotator = function(settings) {
        this.counter = 0;
        this.img = null;
        this.CurrentElement;
        this.RotatorInterval = settings.RotatorInterval < 1000 ? parseInt(settings.RotatorInterval) * 1000 : settings.RotatorInterval;
        this.ZoomFactor = settings.ZoomFactor;
        this.Effect = settings.Effect;
        this.AnimationTime = 80;
        this.Images = new Array();
        this.img = settings.images;
        this.Id = jQuery(this).attr("id");
        this.Prefix = this.Id + "_rotatoritem_";
        this.ih;
        this.iw;
        this.image;
        this.height = settings.Height;
        this.width = settings.Width;
        this.GalleryName = settings.GalleryName;
        this.ScaleToFit = settings.ScaleToFit;
        this.Timer;
        this.Layer = jQuery("#" + this.Id);
        this.currentslideritem = 0;
        this.Running = settings.Running;
        this.IndexNavigation = settings.IndexNavigation;
        this.PlayNavigation = settings.PlayNavigation;
        this.Randomise = settings.Randomise;

        this.UseGallery = settings.UseGallery;
        this.UseNews = settings.UseNews;
        this.LinkText = settings.LinkText;
        this.DateFormat = settings.DateFormat;
        this.NewsPage = settings.NewsPage;
        this.Category = settings.Category;
        this.NewsTemplate = settings.NewsTemplate;




        //function to get random number upto m
        this.randomXToY = function(minVal, maxVal, floatVal) {
            var randVal = minVal + (Math.random() * (maxVal - minVal));
            return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
        }

        this.loadRotatorImage = function(imgsrc) {
            img = new Image();
            img.src = imgsrc;
            this.Images.push(img);

            img = null;
        }


        this.startEffect = function() {
            switch (this.Effect) {
                case "zoom":
                    if (this.ZoomFactor > 0) {
                        if (jQuery.browser.msie)
                            this.Timer = setTimeout("imagerotators['" + this.Id + "'].zoom()", this.RotatorInterval + 4000);
                        else
                            this.Timer = setTimeout("imagerotators['" + this.Id + "'].zoom()", this.RotatorInterval > 5000 ? this.RotatorInterval - 4000 : 2000);
                    }
                    break;
                case "fade":
                    this.fade();
                    break;
                case "slide":
                    this.slider();
                    break;
            }
        }



        this.setRotator = function() {
            if (this.UseGallery) {
                for (var x = 0; x < this.Images.length; x++) {
                    jQuery("<img id='" + this.Prefix + x + "' src='" + this.Images[x].src + "' rotator='" + this.Id + "'  style='position:absolute; left:0px;top:0px;z-index:" + (this.Images.length - x) * 10 + "' />").appendTo("#" + this.Id);
                    if (this.ScaleToFit) this.resetSize(x);
                }

                for (var x in this.Images) {
                    jQuery("#" + this.Prefix + x).show();
                }
            }
            else if (this.UseNews) {
                var prefix = this.Prefix;
                this.Layer.children().each(function(index) { jQuery(this).attr("id", prefix + index) });
            }

            this.counter = 0;

            this.CurrentElement = jQuery("#" + this.Prefix + this.counter);
            this.CurrentElement.show();

            this.startEffect();
        }

        for (var x = 0; x < this.img.length; x++) {
            this.loadRotatorImage(this.img[x]);
        }

        if (jQuery.browser.msie && this.ZoomFactor > 0 && this.RotatorInterval > 5000) {
            this.RotatorInterval -= 4000;
        }

        this.getNews = function() {

        }

        /************************************zoom and fade functions*******************************/
        this.fadeOut = function(index) {
            clearTimeout(this.Timer);
            var olditem = this.currentslideritem;
            if (index != undefined)
                this.currentslideritem = index;
            else
                this.currentslideritem++;
            if (this.currentslideritem == this.Layer.children().length)
                this.currentslideritem = 0;
            if(this.currentslideritem<0)
                this.currentslideritem = this.Layer.children().length-1;

            this.Layer.children().eq(this.currentslideritem).css("z-index", "100");
            
            this.Layer.children().eq(olditem).css("z-index", "102")
            this.Layer.children().eq(this.currentslideritem).show();
            this.Layer.children().eq(olditem).fadeOut(1000);
            jQuery("#" + this.Id + "_navigation .active").removeClass("active");
            jQuery("#" + this.Id + "_navigation .navigation[index=" + this.currentslideritem + "]").addClass("active");
            this.setFadeTimer();
        }

        //        this.fadeIn = function() {
        //            this.currentslideritem++;
        //            if (this.currentslideritem == this.Layer.children().length)
        //                this.currentslideritem = 0;
        //            this.Layer.children().eq(this.currentslideritem).fadeIn(1000, function() { imagerotators[jQuery(this).parent().attr("id")].setFadeTimer(); });
        //            jQuery("#" + this.Id + "_navigation .active").removeClass("active");
        //            jQuery("#" + this.Id + "_navigation .navigation[index=" + this.currentslideritem + "]").addClass("active");
        //        }

        this.fadePlayNavigation = function() {
            nav = jQuery(this.PlayNavigation);

            nav.appendTo("#" + this.Id + "_navigation");

            jQuery("#" + this.Id + "_navigation .sliderpause").attr("rotator", this.Id);
            jQuery("#" + this.Id + "_navigation .sliderpause").click(this.fadePause);
            jQuery("#" + this.Id + "_navigation .sliderright").attr("rotator", this.Id);
            jQuery("#" + this.Id + "_navigation .sliderright").attr("direction", "1");
            jQuery("#" + this.Id + "_navigation .sliderleft").attr("rotator", this.Id);
            jQuery("#" + this.Id + "_navigation .sliderleft").attr("direction", "-1");
            jQuery("#" + this.Id + "_navigation .sliderright").click(this.fadebuttonmove);
            jQuery("#" + this.Id + "_navigation .sliderleft").click(this.fadebuttonmove);
        }

        this.fadePause = function() {

            var rotator = imagerotators[jQuery(this).attr("rotator")];
            clearTimeout(rotator.Timer);
        }


        this.fadeIndexNavigation = function() {
            var nav, s;

            for (var x = 0; x < this.Layer.children().length; x++) {
                s = this.IndexNavigation;
                s = s.replace("[#index#]", x);
                nav = jQuery(s);
                nav.attr("index", x);
                nav.attr("rotator", this.Id);
                nav.click(this.fadeNavigationClick);
                nav.addClass("navigation");
                nav.appendTo("#" + this.Id + "_navigation");
            }
            jQuery("#" + this.Id + "_navigation").children().first().addClass("active");
        }

        this.fadebuttonmove = function() {

            var rotator = imagerotators[jQuery(this).attr("rotator")];
            var continous = parseInt(jQuery(this).attr("direction"));
            rotator.fadeOut(rotator.currentslideritem+continous);
        }

        this.fadeNavigationClick = function() {
            var rotator = imagerotators[jQuery(this).attr("rotator")];
            rotator.fadeOut(parseInt(jQuery(this).attr("index")));

        }

        this.setFadeTimer = function() {
            this.Timer = setTimeout("imagerotators['" + this.Id + "'].fadeOut()", this.RotatorInterval);
        }

        this.resetPos = function(x) {
            this.image = jQuery("#" + this.Prefix + x);
            this.image.css("left", 0);
        }

        this.resetSize = function(x) {

            this.image = jQuery("#" + this.Prefix + x);
            this.img = this.Images[x];
            this.ih = parseInt(jQuery(this).css("height"), 10);
            this.iw = parseInt(jQuery("this").css("width"), 10);

            if (this.iw / this.img.width > this.ih / this.img.height) {
                this.image.attr("height", parseInt((this.iw / this.img.width) * this.img.height));
                this.image.attr("width", parseInt(this.iw));
                this.image.css("height", parseInt((this.iw / this.img.width) * this.img.height));
                this.image.css("width", parseInt(this.iw));
            }
            else {
                this.image.attr("height", this.ih);
                this.image.attr("width", parseInt((this.ih / this.img.height) * this.img.width));
                this.image.css("height", this.ih);
                this.image.css("width", parseInt((this.ih / this.img.height) * this.img.width));
            }
            this.img = null;
        }

        this.changeStackOrder = function() {
            for (var x in this.Images) {
                this.img = jQuery("#" + this.Prefix + x);
                this.img.css("z-index", parseInt(this.img.css("z-index"), 10) + 10);
                this.img = null;
            }
        }

        this.zoom = function() {
            if (this.RotatorInterval > 0) {
                if (this.ZoomFactor > 0) {
                    this.CurrentElement.animate({ height: parseFloat(this.CurrentElement.attr("height"), 10) + this.ZoomFactor, width: parseFloat(this.CurrentElement.attr("width"), 10) + this.ZoomFactor }, this.RotatorInterval, this.fadeOut);
                }
            }
        }

        this.slide = function() {
            this.CurrentElement.animate({ left: this.ZoomFactor }, this.RotatorInterval, this.fadeOut);
        }

        this.fade = function() {
            this.Layer.attr("rotatorid", this.Id);
            this.Layer.children().each(
            function(index, Element) {
                jQuery(this).css({ "z-index": index * 100 + "", position: "absolute", top: "0px", left: "0px", display: "none" });
            });
            this.Layer.children().eq(0).show();
            this.fadePlayNavigation();
            this.fadeIndexNavigation();
            this.setFadeTimer();
        }

        /************************************End zoom and fade functions*******************************/

        /************************************Slider functions*******************************/


        this.buttonmove = function() {

            var rotator = imagerotators[jQuery(this).attr("rotator")];
            rotator.sliderPause();
            var continous = parseInt(jQuery(this).attr("direction")) > 0 ? true : false;
            rotator.sliderMove(jQuery(this).attr("direction"), jQuery(this).attr("direction"));
        }

        this.sliderMoveTo = function(index, auto) {
            //auto=-1 - alltid vänster, auto=0 - automatiskt beroende på var elementet ligger i förhållande till det som skall flyttas, auto=1 alltid höger
            if (index != this.currentslideritem) {
                if (index < 0) index = this.Layer.children().length - 1;
                if (index > this.Layer.children().length - 1) index = 0;
                var dir = index > this.currentslideritem || auto != 0 ? 1 : -1;

                var olditem = this.currentslideritem;

                this.currentslideritem = index;
                var oldobj = this.Layer.children().eq(olditem);
                var currobj = this.Layer.children().eq(this.currentslideritem);

                oldobj.addClass("active");
                currobj.addClass("active");

                var movelength = this.Layer.outerWidth() > oldobj.outerWidth() ? this.Layer.outerWidth() : oldobj.outerWidth();
                var movex = auto == 1 ? Math.abs(dir * movelength) : (auto == -1 ? -(Math.abs(dir * movelength)) : dir * movelength);
                //movelength += 10;
                currobj.css({ left: (movex) + "px", top: "0px", display: "inline" });
                jQuery("#" + this.Id + " .active").each(
                function() {
                    jQuery(this).animate({
                        left: "-=" + movex
                    }, 500, function() { jQuery("#" + this.Id + " .active").removeClass("active"); });
                });
                jQuery("#" + this.Id + "_navigation .active").removeClass("active");
                jQuery("#" + this.Id + "_navigation .navigation[index=" + index + "]").addClass("active");
            }
            if (this.Timer != null)
                clearTimeout(this.Timer);
            if (this.Running) this.setTimer();
        }

        this.sliderMove = function(dir, continous) {
            continous = continous != undefined ? continous : false;
            var dir2 = dir = undefined || dir == null ? parseInt(jQuery(this).attr("direction")) : parseInt(this.currentslideritem) + parseInt(dir);
            if (dir2 < 0) dir2 = this.Layer.children().length - 1;
            if (dir2 > this.Layer.children().length - 1) dir2 = 0;
            this.sliderMoveTo(dir2, dir);
        }

        this.setTimer = function() {
            this.Timer = setTimeout("imagerotators['" + this.Id + "'].sliderMoveTo(" + (parseInt(this.currentslideritem) + 1) + ", 1)", this.RotatorInterval);
        }

        this.sliderPause = function() {
            var rotator = jQuery(this).attr("rotator") != undefined ? imagerotators[jQuery(this).attr("rotator")] : this;
            rotator.Running = false;
            if (rotator.Timer != null)
                clearTimeout(rotator.Timer);
            rotator.Timer = null;
            jQuery("#" + rotator.Id + "_navigation .sliderpause").unbind('click', rotator.sliderPause);
            jQuery(".sliderpause").addClass("sliderstart");
            jQuery("#" + rotator.Id + "_navigation .sliderstart").click(rotator.sliderStart);

        }

        this.sliderStart = function() {
            var rotator = imagerotators[jQuery(this).attr("rotator")];
            rotator.Running = true;
            jQuery("#" + rotator.Id + "_navigation .sliderpause").removeClass("sliderstart");
            jQuery(this).unbind('click', rotator.sliderStart);
            jQuery(this).click(rotator.sliderPause);
            rotator.setTimer();
        }

        this.slider = function() {
            this.Layer.children().each(
                    function(index, Element) {
                        jQuery(this).css({ "z-index": index * 100 + "", position: "absolute", top: "0px", left: "0px", display: "none" });

                    });
            this.Layer.children().eq(0).css({ top: "0px", left: "0px", display: "inline" });

            this.indexNavigation();
            this.playNavigation();
            if (this.Running && this.Layer.children().length > 0) this.sliderMoveTo(0, 1);

        }



        this.playNavigation = function() {
            nav = jQuery(this.PlayNavigation);

            nav.appendTo("#" + this.Id + "_navigation");

            jQuery("#" + this.Id + "_navigation .sliderpause").attr("rotator", this.Id);
            jQuery("#" + this.Id + "_navigation .sliderpause").click(this.sliderPause);
            jQuery("#" + this.Id + "_navigation .sliderright").attr("rotator", this.Id);
            jQuery("#" + this.Id + "_navigation .sliderright").attr("direction", "1");
            jQuery("#" + this.Id + "_navigation .sliderleft").attr("rotator", this.Id);
            jQuery("#" + this.Id + "_navigation .sliderleft").attr("direction", "-1");
            jQuery("#" + this.Id + "_navigation .sliderright").click(this.buttonmove);
            jQuery("#" + this.Id + "_navigation .sliderleft").click(this.buttonmove);
        }

        this.indexNavigation = function() {
            var nav, s;

            for (var x = 0; x < this.Layer.children().length; x++) {
                s = this.IndexNavigation;
                s = s.replace("[#index#]", x);
                nav = jQuery(s);
                nav.attr("index", x);
                nav.attr("rotator", this.Id);
                nav.click(this.navigationClick);
                nav.addClass("navigation");
                nav.appendTo("#" + this.Id + "_navigation");
            }
            jQuery("#" + this.Id + "_navigation").children().first().addClass("active");
        }

        this.navigationClick = function() {
            var rotator = imagerotators[jQuery(this).attr("rotator")];
            rotator.sliderMoveTo(jQuery(this).attr("index"), 0);

        }


        /************************************End Slider functions*******************************/

        imagerotators[this.Id] = this;
        this.setRotator();


    }


})(jQuery)
