/**
* Animated info box
*
* @version	1.0
* @author	Jasal Vadgama - Live Nation UK
* @require	jquery
* @license	GPL v3
**/

(function($) {
    $.fn.infobox = function(settings) {
        var config = {
            sidebar: ".sidebarInternal",
            infoHolder: ".infoContent",
            infoSlider: ".infoSlider",
            contentPrefix: "#info"
        };

        if (settings) $.extend(config, settings);

        $.extend(this, {
            init: function() {
                var info = this;

                $(config.sidebar + " ul").css({ backgroundPosition: "0 0" });
                $(config.sidebar + " li").css({ backgroundPosition: "215px 20px" });
                $(config.sidebar + " li:first").css({ backgroundPosition: "300px 20px" });

                $(config.sidebar + " li").each(function(item) {
                    $(this).click(function() {
                        // remove active class from old link
                        $(config.sidebar + " .active").animate({
                            backgroundPosition: "(215px 20px)"
                        }, { duration: 500 });
                        $(config.sidebar + " .active").removeClass("active");

                        // onclick add active class and move ul bg
                        $(this).addClass("active");
                        $(this).animate({
                            backgroundPosition: "(300px 20px)"
                        }, { duration: 500 });

                        // move ul bg
                        offset = $(this).offset().top - $(config.sidebar).offset().top;

                        $(config.sidebar + " ul").animate({
                            backgroundPosition: "(0 " + offset + "px)"
                        }, { duration: 500 });

                        info.showContent(item + 1)
                    });
                });

                // set first li to be active
                $(config.sidebar + " li:first").addClass("active");

                // move active bg off screen
                $(config.sidebar + " .active").css({
                    backgroundPosition: "300px 20px"
                });

                if ($(config.sidebar + " ul").height() < $(config.infoSlider + " div:first").height() + 10) {
                    $(config.sidebar + " ul").css({
                        height: ($(config.infoSlider + " div:first").height() + 10) + "px"
                    });
                    $(config.infoHolder).css({
                        height: $(config.infoSlider + " div:first").height() + "px"
                    });
                }
            },
            setSidebarHeight: function() {
                $(config.sidebar + " ul").css({
                    height: $(config.infoHolder).height() + 10 + "px"
                });
            },
            /*setSliderHeight: function() {
                $(config.infoSlider).css({
                    height: $(config.infoHolder).height() + "px"
                });
            },*/
            hideContent: function() {
                // set content height is less than sidebar
                $(config.infoSlider + " > div").each(function(item) {
                    if ($(this).height() < $(config.sidebar + " ul").height()) {
                        $(this).css({
                            height: $(config.sidebar + " ul").height() + 50 + "px"
                        });
                    }
                });

                $(config.infoHolder).css({
                    height: $(config.sidebar + " ul").height() + "px"
                });
            },
            showContent: function(pos) {
                var h = 0;
                for (var i = 0; i < pos; i++) {
                    h += $(config.contentPrefix + i).height();
                };

                $(config.infoSlider).animate({
                    marginTop: "-" + h + "px"
                }, { duration: 500 });

                if ($(config.sidebar + " ul").height() != $(config.contentPrefix + pos).height() + 10) {
                    $(config.sidebar + " ul").animate({
                        height: ($(config.contentPrefix + pos).height() + 10) + "px"
                    }, { duration: 500, queue: false });
                    $(config.infoHolder).animate({
                        height: ($(config.contentPrefix + pos).height()) + "px"
                    }, { duration: 500, queue: false });
                }
            },
            setStartingTab: function() {
                qs = window.location.toString().split("id=info")[1];

                if (qs != null && qs <= $(config.sidebar + " li").length && qs != 1) {
                    // remove active class from old link
                    $(config.sidebar + " .active").animate({
                        backgroundPosition: "(215px 20px)"
                    }, { duration: 500 });
                    $(config.sidebar + " .active").removeClass("active");

                    $(config.sidebar + " li").eq(qs - 1).addClass("active");
                    $(config.sidebar + " li").eq(qs - 1).animate({
                        backgroundPosition: "(300px 20px)"
                    }, { duration: 500 });

                    // move ul bg
                    offset = $(config.sidebar + " li").eq(qs - 1).offset().top - $(config.sidebar).offset().top;

                    $(config.sidebar + " ul").animate({
                        backgroundPosition: "(0 " + offset + "px)"
                    }, { duration: 500 });

                    this.showContent(qs);
                }
            }
        });

        this.hideContent();

        //this.setSliderHeight();

        this.setSidebarHeight();

        this.init();

        this.setStartingTab();

        return this;
    };
})(jQuery);