//giamix modification. After a page postback, ie7 replace href values with the complete url, instead of keeping the hash part.
(function($) {
    $.fn.extend({
        tabify: function() {
            function getHref(el) {
                hash = $(el).find('a').attr('href');

                if (hash) {
                    /*************** giamix ******************/
                    var idx = hash.indexOf('#');
                    hash = hash.substring(idx);
                    /*************** giamix ******************/
                    return hash.substring(0, hash.length - 4);
                } else
                    return false;
            }
            function setActive(el) {
                $(el).addClass('active');
                if (getHref(el))
                    $(getHref(el)).show();
                else
                    return false;
                $(el).siblings('li').each(function() {
                    $(this).removeClass('active');
                    $(getHref(this)).hide();
                });
            }

            return this.each(function() {
                var self = this;
                var me = $(self);
                var isatleastoneactive = 0;

                //if ($(this).data('tabify') != true)
                if (me.data('tabified') !== 'yes') {
                    // execute all
                    me.data('tabified', 'yes');

                    $(this).find('li>a').each(function() {
                        $(this).attr('href', $(this).attr('href') + '-tab');
                    });

                    function handleHash() {
                        if (location.hash) {
                            /*************** giamix ******************/
                            var hash = location.hash;
                            var idx = hash.indexOf('#');
                            hash = hash.substring(idx);
                            /*************** /giamix ******************/
                            setActive($(self).find('a[href$=' + hash + ']').parent());
                        }
                    }
                    if (location.hash)
                        handleHash();
                    setInterval(handleHash, 100);
                    $(this).find('li').each(function() {
                        if ($(this).hasClass('active')) {
                            $(getHref(this)).show();
                            isatleastoneactive += 1;
                        }
                        else {
                            $(getHref(this)).hide();
                        }
                    });

                    //if none of the tabs is active, activate the first one
                    if (isatleastoneactive == 0) {
                        var el = $(self).find('li:first');
                        setActive(el);
                    }
                }
            });
        }
    });
})(jQuery);
