﻿jQuery.fn.tabs = function() {
    var pages = null;
    var tabs = null;
    var selector = "";
    this.each(function(index, el) {
        if (selector.length > 0)
            selector += ",";
        selector += "#tabpage-" + $(el).attr("data");
    });

    pages = $(selector);
    tabs = this;

    if (pages.length > 0) {
        pages.css("display", "none");
        if (window.location.hash) {
            var name = window.location.hash.substr(1);
            $("#onglet li").removeClass("selected");
            $("#tabpage-" + name).css("display", "block");
            $(".tabbutton[data=" + name + "]").addClass("selected");
        } else {
            $(".tabpage:first").css("display", "block");
            $(".tabbutton:first").addClass("selected");
        }
    }

    tabs.click(function() {
        pages.css("display", "none");
        $("#tabpage-" + $(this).attr("data")).css("display", "block");
        $("#onglet li").removeClass("selected");
        $(this).addClass("selected");
        var id = "#" + $(this).attr("data");
        if (window.location.hash)
            window.location.href = window.location.href.replace(window.location.hash, id);
        else
            window.location.href = id;
    });
};
