/**
* 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: ".sidebar_internal",
            infoHolder: ".info_content",
            infoSlider: ".info_slider",
            contentPrefix: "#info"
        };

        if (settings) $.extend(config, settings);

        $.extend(this, {
            init: function() {
                var info = this;

                $("<img class='infoPointer' src='/_Resources/img/icons/pointer_info.png' alt='' />").appendTo($(config.infoHolder));

                $(config.sidebar + " a").each(function(item) {
                    $(this).click(function() {
                        // remove active class from old link
                        $(config.sidebar + " .active").removeClass("active");

                        // onclick add active class and move ul bg
                        $(this).addClass("active");

                        // move ul bg
                        offset = $(this).offset().top - $(config.sidebar).offset().top + 3;

                        $(".infoPointer").animate({
                            top: offset + "px",
                            height: $(this).height() + 5 + "px"
                        }, { duration: 500 });

                        info.showContent(item + 1)

                        return false;
                    });
                });

                // set first li to be active
                $(config.sidebar + " a:first").addClass("active");

                offset = $(config.sidebar + " a:first").offset().top - $(config.sidebar).offset().top + 3;

                // resize pointer if needed
                $(".infoPointer").animate({
                    top: offset + "px",
                    height: $(config.sidebar + " a:first").height() + 5 + "px"
                }, { duration: 500 });

                $(config.infoSlider + " div").each(function() {
                    if ($(this).outerHeight() + 40 < $(config.sidebar).outerHeight())
                        $(this).css("height", $(config.sidebar).outerHeight() - 40 + "px");
                    else
                        $(this).css("height", $(this).outerHeight() + "px");
                });

                $(config.infoHolder).css({
                    height: $(config.infoSlider + " div:first").outerHeight() + "px"
                });
                
                $(config.sidebar).css({
                    height: $(config.infoSlider + " div:first").outerHeight() + 20 + "px"
                });
            },
            setSidebarHeight: function() {
                $(config.sidebar).css({
                    height: $(config.infoHolder).height() + "px"
                });
            },
            /*hideContent: function() {
                // set content height if 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).outerHeight();
                };

                $(config.infoSlider).animate({
                    marginTop: "-" + h + "px"
                }, { duration: 500 });

                if ($(config.sidebar).outerHeight() != $(config.contentPrefix + pos).outerHeight()) {
                    $(config.sidebar).animate({
                        height: ($(config.contentPrefix + pos).outerHeight()) + 20 + "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").removeClass("active");

                    $(config.sidebar + " li:eq(" + (qs - 1) + ")").addClass("active");

                    // move ul bg
                    offset = $(config.sidebar + " li:eq(" + (qs - 1) + ")").offset().top - $(config.sidebar).offset().top;

                    $(".infoPointer").animate({
                        top: offset + "px",
                        height: $(this).height() + 5 + "px"
                    }, { duration: 500 });

                    this.showContent(qs);
                }
            }*/
        });

        //this.hideContent();

        //this.setSidebarHeight();

        this.init();

        //this.setStartingTab();

        return this;
    };
})(jQuery);
