var tfso = {};
(function (a) {
    
    //Add bind function if missing. http://goo.gl/M9rb8
    if (!Function.prototype.bind) {
      Function.prototype.bind = function (oThis) {
        if (typeof this !== "function") // closest thing possible to the ECMAScript 5 internal IsCallable function
          throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");
    
        var aArgs = Array.prototype.slice.call(arguments, 1), 
            fToBind = this, 
            fNOP = function () {},
            fBound = function () {
              return fToBind.apply(this instanceof fNOP ? this : oThis || window, aArgs.concat(Array.prototype.slice.call(arguments)));    
            };
        fNOP.prototype = this.prototype;
        fBound.prototype = new fNOP();
        return fBound;
      };
    }    
    
    a(document).ready(function () {
        a(".slideContainer").slideShow({
            fadeSpeed: 1000,
            rotateSpeed: 2500,
            fadeActive: true
        });
        a(".ccsSlideshow").slideShow({
            fadeSpeed: 800,
            rotateSpeed: 8000,
            targetTag: "a",
            fadeActive: true
        });
        a(".ccsSlideshowFront").slideShow({
            fadeSpeed: 800,
            rotateSpeed: 4000,
            targetTag: "a",
            fadeActive: true
        });
        var b = {
            trigger: a("body.lang-no #navigation li:first a, body.lang-se #navigation li:first a"),
            container: a("#header #megamenu"),
            toggle: function () {
                if (b.container.is(":visible")) {
                    b.hide()
                } else {
                    b.show()
                }
            },
            show: function () {
                b.container.show();
                b.trigger.addClass("a");
                a("body").click(b.hide)
            },
            hide: function () {
                b.container.hide();
                b.trigger.removeClass("a")
            },
            init: function () {
                b.trigger.click(function (c) {
                    c.preventDefault();
                    c.stopPropagation();
                    b.toggle()
                });
                b.container.find(".inner").click(function (c) {
                    c.stopPropagation()
                })
            }
        };
        b.init()
    });
    tfso.moduleFeatures = {
        images: {},
        init: function () {
            a("div.feature").each(function () {
                var d = a(this);
                var g = d.data("images");
                if (g.length > 0) {
                    var e = d.find("div.content");
                    var c = a('<a href="">Se skjermbilder</a>');
                    var b = a("<ul />", {
                        "class": "fancybox-pagination"
                    });
                    var loc = window.location.toString();
                    //"Language support"
                    if(loc.indexOf('/no/') > 0) {
                        c.text('Se skjermbilder')
                    } else if(loc.indexOf('/se/') > 0) {
                        c.text('Se skärmdumpar')
                    } else {
                        c.text('See screenshots')
                    }
                    for (img in g) {
                        (function (f) {
                            page = a('<li><a href="#' + ((f * 1) + 1) + '">' + ((f * 1) + 1) + "</a></li>").click(function (h) {
                                a.fancybox.pos(f);
                                h.preventDefault()
                            }.bind(this))
                        }(img));
                        b.append(page)
                    }
                    tfso.moduleFeatures.pagination = b;
                    c.click(function (h) {
                        var f = a(this).parents(".feature").data("texts");
                        a.fancybox(g.slice(0), {
                            type: "image",
                            titlePosition: "inside",
                            titleFormat: tfso.moduleFeatures.formatTitle,
                            texts: f
                        });
                        h.preventDefault()
                    });
                    e.append(c)
                }
            })
        },
        updatePagination: function () {
            var d = a("#fancybox-content"),
                c = a("#fancybox-pagination"),
                b = "Pagination here";
            if (c.length === 0) {
                a("<div />", {
                    id: "fancybox-pagination"
                }).insertBefore(d);
                c = a("#fancybox-pagination")
            }
            c.html(a(tfso.moduleFeatures.pagination))
        },
        formatTitle: function (g, f, b, c) {
            var d = c.texts,
                e = b + 1;
            titleHTML = false;
            if (typeof d[e] == "object") {
                titleHTML = "";
                if (d[e].title !== "") {
                    titleHTML += "<h2>" + d[e].title + "</h2>"
                }
                if (d[e].desc !== "") {
                    titleHTML += "<p>" + d[e].desc + "</p>"
                }
            }
            return titleHTML
        }
    }
})(jQuery);
