if (typeof(XPRIZE) == "undefined") {
    XPRIZE = {};
}

XPRIZE.tabs = function() {
    // private
    var INACTIVE = 0;
    var HOVER = -29;
    var SELECTED = -58;
    var SPEED = 250;
    var activePage = null;

    function set_backpos_y(ele, val) {
        if ($(ele).css("background-position-y")) {
            $(ele).css("background-position-y", val + "px");
        }
        else {
            var posX = $(ele).css("background-position").split(" ")[0];
            $(ele).css("background-position", posX + " " + val + "px");
        }
    }
    function set_all_inactive() {
        $(".content_tab.tab_selected").removeClass("tab_selected").each(function(i) {
            set_backpos_y(this, INACTIVE);
        });
    }
	function set_selected(tab) {
                        // NOTE: we have to set y background position of sprite manually
                        // changle previously selected to inactive
                        set_all_inactive();
                        // set this tab to selected
                        var tab = $(tab);
                        tab.addClass("tab_selected");
                        set_backpos_y(tab, SELECTED);
	}
    // public
    return {
        init: function(caller) {
                    activePage = caller;
                    // content tab functions
                    $(".content_tab").click(function(e) {
						set_selected(this);
                        // change the content
                        caller.setContent(this.id.substring(0, this.id.indexOf("_tab")));
                    });
                    $(".dropdown_tab, .clickable_open_tab").click(function(e) {
                        var tab = $(this);
                        // before closing open tabs, check if this is a dropdown tab that needs to open
                        var open_this = tab.hasClass("dropdown_tab") && !tab.hasClass("hide_open");
                        // close all open tabs 
                        var opentab = $(".dropdown_tab.hide_open");
                        opentab.each(function(i) {
                            var tabid = this.id;
                            $("#" + tabid + "_open").slideUp(SPEED, function(){
                                // show tab when done
                                opentab.removeClass("hide_open");
                                opentab.css("z-index", 3);
                            });
                        });
                        // open this one if required
                        if (open_this) {
                            // check if it has an open option
                            var id = tab.attr("id") + "_open";
                            var tab_to_open = $("#" + id);
                            if (tab_to_open.length > 0) {
                                // hide the regular tab when the dropdown tab is open
                                tab.addClass("hide_open");
                                tab.css("z-index", 1);
                                tab_to_open.slideDown(SPEED);
                            }
                        }                       
                    });
                    $(".content_tab").hover(function(e) {
                        var tab = $(this);
                        if (!tab.hasClass("tab_selected")) {
                            set_backpos_y(this, HOVER);
                        }
                    }, function(e) {
                        var tab = $(this);
                        if (!tab.hasClass("tab_selected")) {
                            set_backpos_y(this, INACTIVE);
                        }
                    });
        },
        reset: function() {
            set_all_inactive();
        },
		setSelected: function(tab) {
			set_selected(tab);
		}
    }
}();

