﻿jQuery.fn.tabsLite = function(obj) {
    return this.each(function() {
        var ul = jQuery(this).find('ul:first');

        $(this).attr('class', $(this).attr('class') + ' tabsLite ui-tabs ui-widget ui-widget-content ui-corner-all');
        ul.attr('class', 'ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all');
        ul.parent().find('div:first').attr('class', 'ui-tabs-panel ui-widget-content ui-corner-bottom');

        function fixHref(href) {
            // For dynamically created HTML that contains a hash as href IE < 8 expands
            // such href to the full page url with hash and then misinterprets tab as ajax.
            // Same consideration applies for an added tab with a fragment identifier
            // since a[href=#fragment-identifier] does unexpectedly not match.
            // Thus normalize href attribute...
            return href.substring(href.indexOf('#'));
        }

        // Go through all the in-page links in the ul
        ul.find('a[href*=#]').each(function(i) {
            var link = jQuery(this);

            var idx = 0;
            if (obj != null)
                if (obj.defaultTab != null) idx = obj.defaultTab;


            // Hide all containers cept the first
            if (i != idx) {
                link.parent().attr('class', 'ui-state-default ui-corner-top');
                jQuery(fixHref(link.attr('href'))).attr('class', 'ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide').hide();
            }
            else {
                link.parent().attr('class', 'ui-state-default ui-corner-top ui-tabs-selected ui-state-active');
                link.addClass('selected');
                jQuery(fixHref(link.attr('href'))).attr('class', 'ui-tabs-panel ui-widget-content ui-corner-bottom').show();
            }



            // When clicking link
            link.click(function() {
                if (obj != null) {
                    if (obj.select != null) obj.select('click', { index: i, tab: link});
                }

                // Hide selected link's containers
                ul.find('a.selected').parent().attr('class', 'ui-state-default ui-corner-top');
                jQuery(fixHref(ul.find('a.selected').removeClass('selected').attr('href'))).attr('class', 'ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide').hide();

                // Show this one's
                link.parent().attr('class', 'ui-state-default ui-corner-top ui-tabs-selected ui-state-active');
                jQuery(fixHref(link.addClass('selected').attr('href'))).attr('class', 'ui-tabs-panel ui-widget-content ui-corner-bottom').show();

                return false;
            });

            link.hover(
					function() {
					    $(this).parent().find("a:not(.selected)").parent().attr('class', 'ui-state-default ui-corner-top ui-state-hover');
					},
					function() {
					    $(this).parent().find("a:not(.selected)").parent().attr('class', 'ui-state-default ui-corner-top');
					}
				);

        });


    });
};
