﻿(function ($) {

    $.fn.oxygenImgSlider = function (options, elements) {

        return this.each(function () {
            // defaults 
            var settings = {
                'width': '300px',
                'height': '200px',
                'imageStartZIndex': '1000',
                'titleStartZIndex': '2000',
                'slideInterval': '5000',
                'textSpaceToBottom': '9',
                'heading': '',
                'showPaginator': 'false',
                'classPrefix': '',
                'currentIndex': -1,
                'timer': null,
                'parentId': "#" + $(this).attr("id")
            };
            // If options exist, lets merge them
            // with our default settings
            if (options) {
                $.extend(settings, options);
            }
            $(this).height(settings.height);
            $(this).width(settings.width);


            $(this).addClass(settings.classPrefix + "ois-container");
            var items = new Array();
            var texts = new Array();
            for (var i = 0; i < elements.length; i++) {
                if (isImage(elements[i])) {
                    items[i] = $("<img />")
                        .attr("src", elements[i].src)
                        .css("z-index", parseInt(settings.imageStartZIndex) + i)
                        .css("width", settings.width)
                        .css("height", settings.height)
                        .addClass(settings.classPrefix + "ois-image")
                        .hide();
                    items[i].appendTo($(this));
                }

                texts[i] = $("<div />")
                    .addClass(settings.classPrefix + "ois-textcontainer")
                    .css("z-index", parseInt(settings.titleStartZIndex) + i)
                    .hide();

                $("<div/>").text(elements[i].title)
                    .addClass(settings.classPrefix + "ois-title")
                    .appendTo(texts[i]);
                if (elements[i].subtitle) {
                    $("<div>").text(elements[i].subtitle)
                    .addClass(settings.classPrefix + "ois-subtitle")
                    .appendTo(texts[i]);
                }
                if (elements[i].text) {
                    $("<div>").text(elements[i].text)
                    .addClass(settings.classPrefix + "ois-content")
                    .appendTo(texts[i]);
                }
                $("<div/>")
                .addClass(settings.classPrefix + "ois-spacer")
                .appendTo(texts[i]);

                var targetWindow = "";
                targetWindow = elements[i].target;

                if (elements[i].linkUrl) {
                    texts[i].data('linkUrl', elements[i].linkUrl);
                    texts[i].data('target', targetWindow);
                    texts[i].click(function () {

                        if ($(this).data('target') == "_blank") {
                            window.open($(this).data('linkUrl'), '_blank');
                        }
                        else {
                            window.open($(this).data('linkUrl'), '_self');
                        }

                    });
                    items[i].data('linkUrl', elements[i].linkUrl);
                    items[i].data('target', targetWindow);
                    items[i].click(function () {
                        if ($(this).data('target') == "_blank") {
                            window.open($(this).data('linkUrl'), '_blank');
                        }
                        else {
                            window.open($(this).data('linkUrl'), '_self');
                        }

                    });
                    texts[i].css("cursor", "pointer");
                    items[i].css("cursor", "pointer");
                }

                texts[i].appendTo($(this));

                var textHeight = "";
                if (elements[i].textboxheight != '') { textHeight = elements[i].textboxheight; }
                else { textHeight = $(texts[i]).height(); }

                var topPx = (settings.height.replace("px", "") - textHeight - settings.textSpaceToBottom) + 'px';
                $(texts[i]).css("top", topPx);
                $(texts[i]).attr("textHeight", textHeight);
                texts[i].hideMe = hideTextBlock;
                texts[i].showMe = showTextBlock;
                items[i][0].myIndex = i;
            }

            $("<div class='" + settings.classPrefix + "progressBar' />")
                .addClass(settings.classPrefix + "ois-progressbar")
                .css("bottom", settings.textSpaceToBottom + "px")
                .appendTo($(this));

            if (settings.heading != "") {
                var headingContainer = $("<div/>").addClass(settings.classPrefix + "ois-headingcontainer");
                $("<div />").text(settings.heading)
                .addClass(settings.classPrefix + "ois-heading")
                .appendTo(headingContainer);
                headingContainer.appendTo($(this));
                $("<div/>")
                    .addClass(settings.classPrefix + "ois-headingcontainer-transparent")
                    .css("height", headingContainer.height() + "px")
                    .appendTo($(this));
            }

            $("<div />")
                .addClass(settings.classPrefix + "ois-textcontainer-transparent")
                .appendTo($(this));
            if (settings.showPaginator == 'true') {
                var ul = $("<ul/>")
                    .addClass(settings.classPrefix + "ois-paginator");
                for (i = 0; i < elements.length; i++) {
                    var link = $("<a href='#'>&nbsp;</a>");
                    link.data('myIndex', i.toString());
                    link.click(function () {
                        next($(this).data('myIndex'), items, texts, elements, settings);
                        return false;
                    });
                    link.appendTo($("<li />")
                    .appendTo(ul));
                }
                ul.appendTo($(this));
            }
            setTimeout(function () { next(0, items, texts, elements, settings); });
        });
    };
    function showTextBlock(settings, nextIndex) {
        $(this).fadeIn('slow');
        var textHeight = $(this).attr("textHeight");

        var topPx = (settings.height.replace("px", "") - textHeight - settings.textSpaceToBottom) + 'px';
        $("." + settings.classPrefix + "ois-textcontainer-transparent", settings.parentId)
            .height(textHeight + 'px')
            .css("z-index", parseInt(settings.titleStartZIndex) - 1)
            .css("top", topPx);

        $("." + settings.classPrefix + "ois-paginator", settings.parentId)
            .height(textHeight + 'px')
            .css("z-index", parseInt(settings.titleStartZIndex) + parseInt(nextIndex) + 1)
            .css("top", topPx);



    }
    function hideTextBlock() {
        $(this).fadeOut(500);
    }
    function next(nextIndex, items, texts, elements, settings) {
        if (settings.timer) {
            clearTimeout(settings.timer);
        }
        $("." + settings.classPrefix + "progressBar", settings.parentId).clearQueue();
        $("." + settings.classPrefix + "progressBar", settings.parentId).css("width", "0%");
        if (items.length == nextIndex)
            nextIndex = 0;
        if (settings.currentIndex == -1) {
            items[nextIndex].show();
            texts[nextIndex].showMe(settings, nextIndex);
        }
        if (settings.currentIndex != -1) {
            items[nextIndex].fadeIn(500);
            items[settings.currentIndex].fadeOut(500);
            texts[nextIndex].showMe(settings, nextIndex);
            texts[settings.currentIndex].stop(true);
            texts[settings.currentIndex].hideMe();
        }
        fixPaginator(settings, nextIndex);

        var interval;
        if (elements[nextIndex].slideInterval)
            interval = elements[nextIndex].slideInterval;
        else
            interval = settings.slideInterval;
        $("." + settings.classPrefix + "progressBar", settings.parentId).animate({ width: "100%" }, parseInt(interval) - 1000);
        settings.currentIndex = nextIndex;
        settings.timer = setTimeout(function () { next(parseInt(nextIndex) + 1, items, texts, elements, settings); }, interval);
    }
    function fixPaginator(settings, nextIndex) {
        var paginator;
        var lastPaginator;
        if (settings.currentIndex == -1) {
            paginator = $("li", $("." + settings.classPrefix + "ois-paginator", settings.parentId))[0];
        }
        if (settings.currentIndex != -1) {
            paginator = $("li", $("." + settings.classPrefix + "ois-paginator", settings.parentId))[nextIndex];
            lastPaginator = $("li", $("." + settings.classPrefix + "ois-paginator", settings.parentId))[settings.currentIndex];
        }
        if (paginator)
            $(paginator).addClass("active");
        if (lastPaginator)
            $(lastPaginator).removeClass("active");
    }
    function isImage(item) {
        var type = item.format;

        if (type == undefined || type == "undefined" || type == "" || type == 'image')
            return true;
        return false;
    }

})(jQuery);

