﻿$.extend({ prefix: function (value, prefix) {
    if (value.indexOf(prefix) == 0) return value;
    else return prefix + value;
}
});

$.fn.extend({

    //opens a billboard by index
    openBillboard: function (index) {
        $container = this;
        //apply bounds and remember the index
        index = (index >= billboards.definitions.length ? 0 : index);
        //dont open something thats already open
        if (billboards.current == index) return this;
        else billboards.current = index;

        //if we're dealing with an image cycle...
        if ($.isArray(billboards.definitions[index].image)) {
            //get the current url(x)
            var tmp_sCurrentUrl = $(".QBillboardInstance:eq(" + index + ")", $container).css("background-image");
            //loop each image
            $(billboards.definitions[index].image).each(function (i, url) {
                //if its the current image
                if (tmp_sCurrentUrl.indexOf(url) > -1) {
                    //determine the next index
                    if (++i >= billboards.definitions[index].image.length) i = 0;
                    //and set it
                    $(".QBillboardInstance:eq(" + index + ")", $container).css("background-image", "url(" + $.prefix(billboards.definitions[index].image[i], "/oom.website2/upload") + ")");
                    //exit the each-loop
                    return false;
                }
            });
        }

        //stop animation and reset z-index
        $(".QBillboardInstance", $container).css("z-index", 10).stop(true, true);
        //show new target billboard and hide the rest
        $(".QBillboardInstance:eq(" + index + ")", $container).css("z-index", 100).fadeIn(1000, function () {
            $(".QBillboardInstance", $container).not(this).hide();
        });
        return this;
    },

    //initializes a billboard instance
    initializeBillboard: function (billboard, index) {

        //configure elements
        $("h1", this).html(billboard.title).css("color", billboard.titleColor);
        $("p", this).html(billboard.description).css("color", billboard.descriptionColor);
        $(".Panel", this).css({ background: billboard.panelBackground });
        $("a.ReadMore", this)
            .attr("href", typeof (billboard.url) == "number" ? "/oom.website2/?OpenRID=" + billboard.url : billboard.url)
            .attr("target", typeof (billboard.url) == "number" ? "_self" : "_blank")
            .css({ background: billboard.linkBackground, color: billboard.linkColor })
            .find(".Text").html(billboard.link);

        if (billboard.linkBackground == "White") {
            $("<style> .QBillboard" + index + " a.ReadMore:hover { color:black!important; } </style>").appendTo("head");
        }

        $("ol", this).css("background", billboard.listBackground);

        if (billboard.image != null && !$.isArray(billboard.image) && (billboard.image.toLowerCase().indexOf(".mp4") > -1 || billboard.image.toLowerCase().indexOf(".mpg") > -1)) {
            $("<div id='billboardVideo" + index + "'></div>").css({ width: 623, height: 314 }).appendTo(this);
            var tmp_oVideo = flowplayer("billboardVideo" + index, {
                src: "/oom.website2/flash/flowplayer-3.2.4.swf",
                wmode: "transparent"
            }, {
                onBeforeFullscreen: function () { return false; },
                play: null, //removes progress animation
                clip: {
                    url: $.prefix(billboard.image, "/oom.website2/upload"),
                    autoPlay: true,
                    onBeforePause: function () { return false; },
                    onBeforeFinish: function () { return false; }
                },
                plugins: { controls: null} //removes controls
            });
        } else {
            $(this).css("background-image", function () {
                if ($.isArray(billboard.image)) return "url(" + $.prefix(billboard.image[0], "/oom.website2/upload") + ")"
                else return "url(" + $.prefix(billboard.image, "/oom.website2/upload") + ")"
            });
        }

        return this;
    },

    //initializes the billboards
    initializeBillboards: function () {
        var $container = this;

        //pause when hovering over a billboard
        $container.mouseenter(function () { $container.stopBillboards(); }).mouseleave(function () { $container.runBillboards(); });

        var tmp_sCss = "";

        //create an instance of each definition
        $(billboards.definitions).each(function (index, billboard) {

            //apply default values
            billboard = $.extend({
                panelBackground: "#3E3D40",
                buttonBackground: billboard.panelBackground,
                titleColor: "white",
                descriptionColor: "white",
                linkBackground: "#211A03",
                linkColor: billboard.panelBackground,
                listBackground: "#0078B9"
            }, billboard);

            tmp_sCss += "\r\n.QBillboard" + index + " ol li:hover, .QBillboard" + index + " ol li.Active { background: " + billboard.buttonBackground + "; }"
            tmp_sCss += "\r\n.QBillboard" + index + " ol li a[href]:hover, .QBillboard" + index + " ol li.Active a { color: " + billboard.buttonColor + "; border-color:" + billboard.buttonBackground + "; }";

            //append html template
            $container.append($("<div class='QBillboardInstance QBillboard" + index + "'><div class='Panel'><h1></h1><p></p><a class='ReadMore'><span class='Text'></span><span class='Arrow'>&#10132;</span></a><ol></ol></div></div>"));
            //initialize each instance and hide every billboard except the first
            $(".QBillboardInstance:eq(" + index + ")", $container).initializeBillboard(billboard, index).each(function () { if (index > 0) $(this).hide() });
        });

        $("<style>" + tmp_sCss + "</style>").appendTo("head");

        //populate the listitems to toggle billboards
        this.find("ol").each(function (billboardIndex) {
            var $list = $(this);
            $(billboards.definitions).each(function (index, billboard) {
                $list.append(
                    $("<li>")
                        .addClass(billboardIndex == index ? "Active QBillboard" + index : "QBillboard" + index)
                        .append(
                            $("<a>")
                                .attr("href", "#")
                                .text(index + 1)
                                .click(function () { $container.openBillboard(index); return false; })));
            });
        });
        return this;
    },

    //stops the cycle
    stopBillboards: function () {
        if (billboards.timerid) {
            clearTimeout(billboards.timerid);
            billboards.timerid = -1;
        }
        return this;
    },

    //starts the cycle
    runBillboards: function () {
        var $container = this;
        if (!billboards.timerid || billboards.definitions.length <= 1) return;
        billboards.timerid = setInterval(function () {
            $container.openBillboard(billboards.current + 1);
        }, 9000);
        return this;
    }
});


$(function () {
    $(".QBillboards").initializeBillboards().runBillboards();
});

var billboards = {
    timerid: -1,
    current: 0,
    definitions: null
};

