﻿(function ($) {

    $.fn.extend({
        Gr3Slideshow: function (options) {
            if (typeof (options) == "string") {
                alert(options);
            }
            else {

                var defaultOptions = {
                    transitionTime: 500,
                    animate: true,
                    pauseTime: 5000,
                    showControls: false,
                    autoSlide: 0
                }

                var options = $.extend(defaultOptions, options);

                return this.each(function () {

                    var opt = options;
                    var obj = $(this);

                    var currentPosition = 0;
                    var slideHeight = obj.height();
                    var slideWidth = obj.width();
                    var slides = $(".slide", obj);
                    var numberOfSlides = slides.length;

                    obj.wrap("<div class=\"slideshowContainer\"></div>");
                    var container = obj.parent();

                    var controlsVisible = false;

                    // parses attr attribute
                    if ($(obj).attr("attr")) {
                        // Parse settings
                        var initSettings = obj.attr("attr").split(";");
                        for (i = 0; i < initSettings.length; i++) {
                            var attributeSet = initSettings[i].split("=");
                            parseAttribute(attributeSet[0], attributeSet[1]);
                        }
                    }

                    function parseAttribute(n, v) {
                        switch (n) {
                            case "animate":
                                opt.animate = Boolean(v);
                                break;
                            case "pauseTime":
                                opt.pauseTime = Number(v);
                                break;
                            case "transitionTime":
                                opt.transitionTime = Number(v);
                                break;
                            case "autoSlide":
                                opt.autoSlide = Number(v);
                                break;
                        }
                    }

                    // Removes scrollbar
                    container.css({
                        "height": slideHeight,
                        "width": slideWidth
                    });

                    obj.css({
                        "overflow": "visible",
                        "width": slideWidth * (numberOfSlides)
                    });

                    // Wraps slides in div.slideshowInner
                    slides.css({
                        "float": "left",
                        "height": slideHeight,
                        "width": slideWidth
                    });

                    container.append("<span class=\"control leftControl\">Forrige</span>")
                    .append("<span class=\"control rightControl\">Næste</span>");


                    /*JOS*/
                    $(container).bind('nextSlide', function () {
                        nextSlide();
                    });

                    $(container).hover(
                        function () {
                            controlsVisible = true;
                            updateControls();
                        },
                        function () {
                            controlsVisible = false;
                            updateControls();
                        }
                    );

                    $(".control", container).css({
                        "height": slideHeight
                    })
                    .bind('click', function () {
                        if ($(this).hasClass("rightControl")) {
                            currentPosition += 1;
                            if (currentPosition >= numberOfSlides) {
                                currentPosition = numberOfSlides - 1;
                            }
                        }
                        else {
                            currentPosition -= 1;
                            if (currentPosition < 0) {
                                currentPosition = 0;
                            }
                        }

                        resetAutoSlide();
                        updateControls();
                        animateSlide();
                    });

                    function animateSlide() {
                        obj.animate({
                            'marginLeft': slideWidth * (-currentPosition)
                        }, opt.transitionTime);
                    }

                    function updateControls() {
                        $(".control", container).stop(true, true);
                        if (opt.showControls) {
                            if (controlsVisible == true) {
                                if (currentPosition == 0) {
                                    $(".leftControl", container).animate({
                                        "left": -50
                                    });
                                }
                                else {
                                    $(".leftControl", container).animate({
                                        "left": 0
                                    });
                                }

                                if (currentPosition == numberOfSlides - 1) {
                                    $(".rightControl", container).animate({
                                        "right": -50
                                    });
                                }
                                else {
                                    $(".rightControl", container).animate({
                                        "right": 0
                                    });
                                }
                            }
                            else {
                                $(".leftControl", container).animate({
                                    "left": -50
                                });

                                $(".rightControl", container).animate({
                                    "right": -50
                                });
                            }
                        }
                    }

                    // Function to hide controls, when autoscroll enabled
                    function hideControlsNoAnimation() {
                        $(".leftControl", container).css({
                            "left": -50
                        });

                        $(".rightControl", container).css({
                            "right": -50
                        });
                    }

                    // Move to next slide and update controls;
                    function nextSlide() {
                        currentPosition += 1;
                        if (currentPosition >= numberOfSlides) {
                            currentPosition = 0;
                        }
                        animateSlide();
                        updateControls();
                    }

                    var slideTimer;
                    var meref;

                    function startAutoSlide(time) {
                        slideTimer = setTimeout(function () {
                            meref.trigger("nextSlide");
                            startAutoSlide(time);
                        }, time);
                    };

                    function resetAutoSlide() {
                        if (slideTimer != "undefined") {
                            clearTimeout(slideTimer);
                            if (opt.autoSlide > 0) startAutoSlide(opt.autoSlide);
                        }
                    }

                    meref = $(this);
                    if (opt.autoSlide > 0) {
                        hideControlsNoAnimation();
                        startAutoSlide(opt.autoSlide);
                    }
                });
            }
        }
    });
})(jQuery);

//$(document).ready(function () {
//    $(".noauto .slideshow").each(function () {
//        $(this).Gr3Slideshow({
//            transitionTime: 300,
//            showControls: true
//        });
//    });
//});

//$(document).ready(function () {
//    $(".auto .slideshow").each(function () {
//        $(this).Gr3Slideshow({
//            transitionTime: 300,
//            showControls: true,
//            autoSlide: 5000
//        });
//    });
//});
